archive the v1.0 - old db structure

This commit is contained in:
lion
2022-06-16 16:25:31 +08:00
parent bfd70f95dd
commit 1e2544cc57
150 changed files with 0 additions and 706936 deletions

View File

@@ -1,28 +0,0 @@
# ip2region luc c module Make file
# @author chenxin<chenxin619315@gmail.com>
# @date 2018/10/05
# @Note
# Please modify the LIBS and the LIB_DIR to fit you system
#
VER = 5.2
CC = gcc
LIBS = -I ../c/ -I /usr/include/lua$(VER)/
FFLAGS = -O2 -Wall -fPIC
SO_FILE = Ip2region.so
LIB_DIR = /usr/local/share/lua/$(VER)
all: ../c/ip2region.c ../c/ip2region.h lua_ip2region.c
$(CC) $(FFLAGS) $(LIBS) ../c/ip2region.c lua_ip2region.c -fPIC -shared -o $(SO_FILE)
install:
sudo mkdir -p $(LIB_DIR); \
sudo cp $(SO_FILE) $(LIB_DIR);\
echo "install Ip2region to $(LIB_DIR) successfully.";\
clean:
find . -name \*.so | xargs rm -f
find . -name \*.o | xargs rm -f
.PHONY: clean

View File

@@ -1,74 +0,0 @@
# Lua ip2region c module binding
### 一,如何安装
* 1, cd到ip2region/binding/lua_c/根目录
* 2, 运行如下命令
```
make
sudo make install
```
* 3, 关于Makefile你可能需要更改里面的VER(版本号)和LIB_DIR(lua的so lib目录)来适应你的系统
```shell
VER = 5.2
CC = gcc
LIBS = -I ../c/ -I /usr/include/lua$(VER)/
FFLAGS = -O2 -Wall -fPIC
SO_FILE = Ip2region.so
LIB_DIR = /usr/local/share/lua/$(VER)
```
### 二,如何测试
* 1, cd到ip2region/binding/lua_c/根目录
* 2, 运行testSearcher测试程序
```shell
lua testSearcher.lua ../../data/ip2region.db
```
* 3, 输入ip地址开始测试即可
```shell
initializing btree
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
ip2region>> 1.2.3.4
0|美国|0|华盛顿|0|0 in 0.100000 millseconds
ip2region>> 101.233.153.103
2163|中国|0|广东省|深圳市|鹏博士 in 0.045000 millseconds
ip2region>>
```
### 三,如何使用
* 1, 先参考第一步安装把Ip2region.so安装到你系统lua的lib目录下
* 2, 通过如下流程在你的lua程序中使用
```lua
-- 包含模块
local Ip2region = require "Ip2region";
-- 创建查询对象
-- 设置ip2region.db的文件地址dbFile表示ip2region.db数据库文件的地址
-- 注意new方法是通过“.”调用,而不是“:”
local ip2region = Ip2region.new("ip2region.db file path");
local data;
-- 查询,备注,尽量请使用“:”调用方法,使用“.”需要主动传递ip2region对象参数
-- 1binary查询
data = ip2region:binarySearch("101.233.153.103");
-- 2btree查询
data = ip2region:btreeSearch("101.233.153.103");
-- 3memory查询
data = ip2region:memorySearch("101.233.153.103");
-- 返回结果如下
print("city_id=", data.city_id, "region=", data.region);
```
### 四,备注
* 1c模块的Ip2region以来binding/c/的实现请保持binding/c/存在并且和lua_c模块同目录。
* 2Ip2region的c模块拥有和c模块几乎等同的性能生产建议使用lua_c模块代替纯lua模块的使用。

View File

@@ -1,232 +0,0 @@
/**
* Ip2region lua c binding
*
* @author chenxin<chenxin619315@gmail.com>
* @date 2018/10/04
*/
#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include "../c/ip2region.h"
#define L_METATABLE_NAME "Ip2region_MT"
/** create a new ip2region object with a specified dbFile */
static int lua_ip2region_new(lua_State *L)
{
ip2region_entry *self;
const char *dbFile;
/* Check the arguments are valid */
dbFile = luaL_checkstring(L, 1);
if ( dbFile == NULL ) {
luaL_error(L, "dbFile cannot be empty");
}
/* Create the user data and push it onto the stack */
self = (ip2region_entry *) lua_newuserdata(L, sizeof(ip2region_entry));
/* Push the metatable onto the stack */
luaL_getmetatable(L, L_METATABLE_NAME);
/* Set the metatable on the userdata */
lua_setmetatable(L, -2);
/* Initialize the entry */
ip2region_create(self, dbFile);
return 1;
}
/** destroy the specified ip2region instance */
static int lua_ip2region_destroy(lua_State *L)
{
ip2region_entry *self;
self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
ip2region_destroy(self);
return 0;
}
#define get_search_args(L, entry, ip) \
do { \
luaL_argcheck(L, lua_gettop(L) == 2, 2, "Object and ip address needed"); \
entry = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); \
ip = luaL_checkstring(L, 2); \
} while (0); \
#define set_search_result(L, data) \
do { \
lua_newtable(L); \
lua_pushinteger(L, data.city_id); \
lua_setfield(L, -2, "city_id"); \
lua_pushfstring(L, "%s", data.region); \
lua_setfield(L, -2, "region"); \
} while (0); \
/** ip2region_memory_search wrapper */
static int lua_ip2region_memory_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
// luaL_argcheck(L, lua_gettop(L) == 2, 2, "object and ip address needed");
// self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
// addr = luaL_checkstring(L, 2);
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* do the memory search */
if ( ip2region_memory_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
// lua_newtable(L);
// lua_pushinteger(L, data.city_id);
// lua_setfield(L, -2, "city_id");
// lua_pushfstring(L, "%s", data.region);
// lua_setfield(L, -2, "region");
set_search_result(L, data);
return 1;
}
/** ip2region_binary_search wrapper */
static int lua_ip2region_binary_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* Do the binary search */
if ( ip2region_binary_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
/* Set the return value */
set_search_result(L, data);
return 1;
}
/** ip2region_btree_search wrapper */
static int lua_ip2region_btree_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* Do the btree search */
if ( ip2region_btree_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
/* Set the return value */
set_search_result(L, data);
return 1;
}
/** ip2long wrapper */
static int lua_ip2long(lua_State *L)
{
int argc;
const char *addr;
uint_t ipval;
argc = lua_gettop(L);
if ( argc == 1 ) {
addr = luaL_checkstring(L, 1);
} else {
luaL_checkudata(L, 1, L_METATABLE_NAME);
addr = luaL_checkstring(L, 2);
}
if ( (ipval = ip2long(addr)) == 0 ) {
lua_pushnil(L);
return 1;
}
lua_pushinteger(L, (ipval & 0x7FFFFFFF));
return 1;
}
static int lua_ip2region_tostring(lua_State *L)
{
ip2region_entry *self;
self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
/* Push the string to return to lua */
lua_pushfstring(L,
"dbFile=%s, headerLen=%d, fristIndexPtr=%d, lastIndexPtr=%d, totalBlocks=%d",
self->dbFile, self->headerLen, self->firstIndexPtr,
self->lastIndexPtr, self->totalBlocks
);
return 1;
}
/** module method array */
static const struct luaL_Reg ip2region_methods[] = {
// { "new", lua_ip2region_new },
// { "ip2long", lua_ip2long },
{ "memorySearch", lua_ip2region_memory_search },
{ "binarySearch", lua_ip2region_binary_search },
{ "btreeSearch", lua_ip2region_btree_search },
{ "close", lua_ip2region_destroy },
{ "__gc", lua_ip2region_destroy },
{ "__tostring", lua_ip2region_tostring },
{ NULL, NULL },
};
/** module function array */
static const struct luaL_Reg ip2region_functions[] = {
{ "new", lua_ip2region_new },
{ "ip2long", lua_ip2long },
{ NULL, NULL }
};
/** module open function interface */
int luaopen_Ip2region(lua_State *L)
{
/* Create a metatable and push it onto the stack */
luaL_newmetatable(L, L_METATABLE_NAME);
/* Duplicate the metatable on the stack */
lua_pushvalue(L, -1);
/* Pop the first metatable off the stack
* and assign it to the __index of the second one.
* so we set the metatable to the table itself.
*/
lua_setfield(L, -2, "__index");
/* Set the methods fo the metatable that could and should be
* access via object:func in lua block
*/
luaL_setfuncs(L, ip2region_methods, 0);
luaL_setfuncs(L, ip2region_functions, 0);
/* Finally register the object.func functions
* into the table witch at the top of the stack */
// luaL_newlib(L, ip2region_functions);
return 1;
}

View File

@@ -1,86 +0,0 @@
--[[
ip2region lua client test script
@author chenxin<chenxin619315@gmail.com>
]]--
-- check the command line arguments
if ( not arg[1] ) then
print([[
Usage: lua testSearcher.lua [ip2region db file] [algorithm]
+-Optional Algorithm: binary, b-tree, memory]]);
os.exit();
end
local Ip2region = require "Ip2region";
-- local cjson = require "cjson";
-- local socket = require "socket";
-- check and parse the dbFile and the method algorithm
-- Create a new ip2region object by the new interface
local ip2region = Ip2region.new(arg[1]);
-- reset the dbFile by the follow two ways:
-- ip2region.dbFile = arg[1];
-- ip2region:setDbFile(arg[1]);
local algorithm = "btree";
if ( arg[2] ~= nil ) then
local arg_2 = string.lower(arg[2]);
if ( arg_2 ~= "binary" and arg_2 ~= "memory" ) then
algorithm = "binary";
elseif ( arg_2 == "memory" ) then
algorithm = "memory";
end
end
-- local data = searcher:memorySearch("120.79.17.142");
-- local data = searcher:binarySearch("120.79.17.142");
-- local data = searcher:btreeSearch("120.79.17.142");
print("initializing " .. algorithm ..[[
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+]]
);
while ( true ) do
io.write("ip2region>> ");
io.input(io.stdin);
local line = io.read();
if ( line == nil ) then
-- do nothing
break;
elseif ( line == "quit" ) then
break;
elseif ( Ip2region.ip2long(line) == nil ) then
print("Invalid ip address=", line);
else
local data;
local s_time = os.clock();
if ( algorithm == "btree" ) then
data = ip2region:btreeSearch(line);
elseif ( algorithm == "binary" ) then
data = ip2region:binarySearch(line);
elseif ( algorithm == "memory" ) then
data = ip2region:memorySearch(line);
end
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
if ( data == nil ) then
io.write("Failed for ip=", line, " is it a valid ip address ?");
else
io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time));
end
end
end
-- close the object
-- Also the lua gc will invoke it automatically
-- print(ip2region);
ip2region:close();