lua 5.1/5.2/5.3/5.4 supports
This commit is contained in:
parent
dc64c27abd
commit
4b7172766c
|
|
@ -1,4 +1,4 @@
|
|||
LuaVersion = 5.4
|
||||
LuaVersion ?= 5.4
|
||||
LIB_DIR = /usr/local/share/lua/$(LuaVersion)
|
||||
|
||||
all: ../c/xdb_api.h ../c/xdb_util.c ../c/xdb_searcher.c xdb_searcher.c
|
||||
|
|
|
|||
|
|
@ -1,15 +1,26 @@
|
|||
# ip2region xdb lua c 扩展查询客户端实现
|
||||
|
||||
# 版本兼容
|
||||
该实现兼容 lua `5.1`,`5.2`,`5.3`, `5.4`
|
||||
|
||||
# 编译安装
|
||||
|
||||
通过如下方式来编译安装该扩展:
|
||||
### 默认编译
|
||||
通过如下方式来编译安装默认的 `Lua5.4` 版本的扩展:
|
||||
```bash
|
||||
# cd 到 lua_c binding 的根目录
|
||||
make
|
||||
sudo make install
|
||||
```
|
||||
|
||||
备注:Makefile 里面的 `LuaVersion` 变量代表你本地环境的 lua 的版本,默认为 5.4,如果你的版本不是 5.4,记得先修改为和你本地 lua 版本一致的版本号。
|
||||
### 指定 Lua 版本
|
||||
通过如下的 `LuaVersion` 参数指定 Lua 版本编译,例如:`5.1` / `5.2` / `5.3` / `5.4`
|
||||
```bash
|
||||
# cd 到 lua_c binding 的根目录
|
||||
# 例如,编译 5.1 版本兼容的扩展
|
||||
make LuaVersion=5.1
|
||||
sudo make install
|
||||
```
|
||||
|
||||
|
||||
# 使用方式
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
-- @Date 2022/06/30
|
||||
|
||||
-- set the package to load the current xdb_searcher.so
|
||||
package.path = "./?.lua" .. package.path
|
||||
package.cpath = "./?.so" .. package.cpath
|
||||
package.path = "./?.lua;" .. package.path
|
||||
package.cpath = "./?.so;" .. package.cpath
|
||||
local xdb = require("xdb_searcher")
|
||||
|
||||
function printHelp()
|
||||
|
|
@ -29,13 +29,10 @@ local dbFile, srcFile = "", ""
|
|||
local cachePolicy = "vectorIndex"
|
||||
for _, r in ipairs(arg) do
|
||||
if string.len(r) < 5 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if string.sub(r, 1, 2) ~= "--" then
|
||||
goto continue
|
||||
end
|
||||
|
||||
-- continue and do nothing here
|
||||
elseif string.sub(r, 1, 2) ~= "--" then
|
||||
-- continue and do nothing here
|
||||
else
|
||||
for k, v in string.gmatch(string.sub(r, 3), "([^=]+)=([^%s]+)") do
|
||||
if k == "db" then
|
||||
dbFile = v
|
||||
|
|
@ -51,9 +48,7 @@ for _, r in ipairs(arg) do
|
|||
-- break the match iterate
|
||||
break
|
||||
end
|
||||
|
||||
-- continue this loop
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
-- print(string.format("dbFile=%s, srcFile=%s, cachePolicy=%s", dbFile, srcFile, cachePolicy))
|
||||
|
|
@ -126,15 +121,13 @@ if handle == nil then
|
|||
end
|
||||
|
||||
local lines = handle:lines()
|
||||
local sip_str, eip_str, s_region, region = "", "", "", ""
|
||||
local sip, mip, eip, err = 0, 0, 0, 0
|
||||
local sip_str, eip_str, s_region, region, err = "", "", "", "", 0
|
||||
local count, t_time, c_time = 0, 0, 0
|
||||
local s_time = xdb.now()
|
||||
for l in lines do
|
||||
if string.len(l) < 1 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
-- continue and do nothing here
|
||||
else
|
||||
for v1, v2, v3 in string.gmatch(l, "([^|]+)|([^|]+)|([^\n]+)") do
|
||||
-- print(sip_str, eip_str, region)
|
||||
sip_str = v1
|
||||
|
|
@ -143,6 +136,7 @@ for l in lines do
|
|||
break
|
||||
end
|
||||
|
||||
t_time = xdb.now()
|
||||
sip_bytes, err = xdb.parse_ip(sip_str)
|
||||
if err ~= nil then
|
||||
print(string.format("invalid start ip `%s`", sip_str))
|
||||
|
|
@ -161,9 +155,7 @@ for l in lines do
|
|||
end
|
||||
|
||||
for _, ip_bytes in ipairs({sip_bytes, eip_bytes}) do
|
||||
t_time = xdb.now()
|
||||
region, err = searcher:search(ip_bytes)
|
||||
c_time = c_time + xdb.now() - t_time
|
||||
if err ~= nil then
|
||||
print(string.format("failed to search ip `%s`", xdb.ip_to_string(ip_bytes)))
|
||||
return
|
||||
|
|
@ -178,7 +170,9 @@ for l in lines do
|
|||
count = count + 1
|
||||
end
|
||||
|
||||
::continue::
|
||||
-- increase the time costs
|
||||
c_time = c_time + xdb.now() - t_time
|
||||
end
|
||||
end
|
||||
|
||||
-- resource cleanup
|
||||
|
|
@ -190,7 +184,7 @@ if content ~= nil then
|
|||
content:close()
|
||||
end
|
||||
|
||||
xdb.cleanup();
|
||||
xdb.cleanup()
|
||||
|
||||
-- print the stats
|
||||
local avg_costs = 0
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
-- @Date 2022/06/30
|
||||
|
||||
-- set the package to load the current xdb_searcher.so
|
||||
package.path = "./?.lua" .. package.path
|
||||
package.cpath = "./?.so" .. package.cpath
|
||||
package.path = "./?.lua;" .. package.path
|
||||
package.cpath = "./?.so;" .. package.cpath
|
||||
local xdb = require("xdb_searcher")
|
||||
|
||||
function printHelp()
|
||||
|
|
@ -28,13 +28,10 @@ local dbFile = ""
|
|||
local cachePolicy = "vectorIndex"
|
||||
for _, r in ipairs(arg) do
|
||||
if string.len(r) < 5 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if string.sub(r, 1, 2) ~= "--" then
|
||||
goto continue
|
||||
end
|
||||
|
||||
-- continue and do nothing here
|
||||
elseif string.sub(r, 1, 2) ~= "--" then
|
||||
-- continue and do nothing here
|
||||
else
|
||||
for k, v in string.gmatch(string.sub(r, 3), "([^=]+)=([^%s]+)") do
|
||||
if k == "db" then
|
||||
dbFile = v
|
||||
|
|
@ -48,9 +45,7 @@ for _, r in ipairs(arg) do
|
|||
-- break the match iterate
|
||||
break
|
||||
end
|
||||
|
||||
-- continue this loop
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
-- print(string.format("dbFile=%s, cachePolicy=%s", dbFile, cachePolicy))
|
||||
|
|
@ -121,7 +116,7 @@ ip2region xdb searcher test program
|
|||
source xdb: %s (%s, %s)
|
||||
type 'quit' to exit]], dbFile, xdb.version_info(version).name, cachePolicy))
|
||||
local region, err = "", nil
|
||||
local ip_int, s_time, c_time = 0, 0, 0
|
||||
local s_time, c_time = 0, 0
|
||||
while ( true ) do
|
||||
io.write("ip2region>> ");
|
||||
io.input(io.stdin);
|
||||
|
|
@ -137,18 +132,15 @@ while ( true ) do
|
|||
-- empty string ignore
|
||||
line = line:gsub("^%s*(.-)%s*$", "%1")
|
||||
if string.len(line) < 1 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
-- continue and do nothing here
|
||||
else
|
||||
s_time = xdb.now()
|
||||
ip_bytes, err = xdb.parse_ip(line)
|
||||
-- print(string.format("parse(%s): %s, err: %s", line, xdb.ip_to_string(ip_bytes), err))
|
||||
if err ~= nil then
|
||||
print(string.format("invalid ip address `%s`", line))
|
||||
goto continue
|
||||
end
|
||||
|
||||
else
|
||||
-- do the search
|
||||
s_time = xdb.now()
|
||||
region, err = searcher:search(ip_bytes)
|
||||
c_time = xdb.now() - s_time
|
||||
if err ~= nil then
|
||||
|
|
@ -156,8 +148,8 @@ while ( true ) do
|
|||
else
|
||||
print(string.format("{region: %s, io_count: %d, took: %dμs}", region, searcher:get_io_count(), c_time))
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- resource cleanup
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@
|
|||
-- @Date 2022/06/30
|
||||
|
||||
-- set the package to load the current xdb_searcher.so
|
||||
package.path = "./?.lua" .. package.path
|
||||
package.cpath = "./?.so" .. package.cpath
|
||||
package.path = "./?.lua;" .. package.path
|
||||
package.cpath = "./?.so;" .. package.cpath
|
||||
local xdb = require("xdb_searcher")
|
||||
|
||||
---- ip checking testing
|
||||
|
|
@ -25,7 +25,7 @@ function test_parse_ip()
|
|||
print(string.format("invalid ip address `%s`: %s", ip_src, err))
|
||||
else
|
||||
local ip_string = xdb.ip_to_string(ip_bytes);
|
||||
print(string.format("parse_ip(%s)->%s ? %s", ip_src, ip_string, ip_src==ip_string))
|
||||
print(string.format("parse_ip(%s)->%s ? %s", ip_src, ip_string, tostring(ip_src==ip_string)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -44,7 +44,7 @@ function test_load_header()
|
|||
if err ~= nil then
|
||||
print("failed to load header: ", err)
|
||||
else
|
||||
print(string.format("xdb header buffer `%s` loaded", header))
|
||||
print(string.format("xdb header buffer `%s` loaded", tostring(header)))
|
||||
|
||||
local tpl = [[
|
||||
header: {
|
||||
|
|
@ -79,7 +79,7 @@ function test_load_vector_index()
|
|||
print("failed to load vector index: ", err)
|
||||
else
|
||||
print(string.format("xdb vector index buffer `%s` loaded, info={name=%s, type=%d, length=%d}",
|
||||
v_index, v_index:name(), v_index:type(), v_index:length()))
|
||||
tostring(v_index), v_index:name(), v_index:type(), v_index:length()))
|
||||
v_index:close()
|
||||
end
|
||||
end
|
||||
|
|
@ -91,7 +91,7 @@ function test_load_content()
|
|||
print("failed to load content: ", err)
|
||||
else
|
||||
print(string.format("xdb content buffer `%s` loaded, info={name=%s, type=%d, length=%d}",
|
||||
c_buffer, c_buffer:name(), c_buffer:type(), c_buffer:length()))
|
||||
tostring(c_buffer), c_buffer:name(), c_buffer:type(), c_buffer:length()))
|
||||
c_buffer:close();
|
||||
end
|
||||
end
|
||||
|
|
@ -101,23 +101,23 @@ function test_search()
|
|||
-- ipv4
|
||||
local ip_str = "1.2.3.4"
|
||||
searcher, err = xdb.new_with_file_only(xdb.IPv4, "../../data/ip2region_v4.xdb")
|
||||
print(string.format("searcher.tostring=%s", searcher))
|
||||
print(string.format("searcher.tostring=%s", tostring(searcher)))
|
||||
local t_start = xdb.now()
|
||||
region, err = searcher:search(ip_str)
|
||||
local c_time = xdb.now() - t_start
|
||||
print(string.format("search(%s): {region=%s, io_count: %d, took: %dμs, err=%s}",
|
||||
ip_str, region, searcher:get_io_count(), c_time, err))
|
||||
ip_str, region, searcher:get_io_count(), c_time, tostring(err)))
|
||||
searcher:close()
|
||||
|
||||
-- IPv6
|
||||
ip_str = "240e:3b7:3276:33b0:958f:f34c:d04f:f6a"
|
||||
searcher, err = xdb.new_with_file_only(xdb.IPv6, "../../data/ip2region_v6.xdb")
|
||||
print(string.format("searcher.tostring=%s", searcher))
|
||||
print(string.format("searcher.tostring=%s", tostring(searcher)))
|
||||
t_start = xdb.now()
|
||||
region, err = searcher:search(ip_str)
|
||||
c_time = xdb.now() - t_start
|
||||
print(string.format("search(%s): {region=%s, io_count: %d, took: %dμs, err=%s}",
|
||||
ip_str, region, searcher:get_io_count(), c_time, err))
|
||||
ip_str, region, searcher:get_io_count(), c_time, tostring(err)))
|
||||
searcher:close()
|
||||
end
|
||||
|
||||
|
|
@ -134,7 +134,7 @@ end
|
|||
|
||||
local s_time = xdb.now();
|
||||
print(string.format("+---calling test function %s ...", func_name))
|
||||
_G[func_name]();
|
||||
_G[func_name]()
|
||||
local cost_time = xdb.now() - s_time
|
||||
xdb.cleanup();
|
||||
print(string.format("|---done, took: %.3fμs", cost_time))
|
||||
|
|
@ -735,14 +735,27 @@ int luaopen_xdb_searcher(lua_State *L)
|
|||
luaL_newmetatable(L, XDB_BUFFER_METATABLE_NAME);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -2, "__index");
|
||||
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
|
||||
// lua 5.1
|
||||
luaL_register(L, NULL, xdb_buffer_methods);
|
||||
#elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
|
||||
// lua version 5.2, 5.3, 5.4 ...
|
||||
luaL_setfuncs(L, xdb_buffer_methods, 0);
|
||||
#endif
|
||||
|
||||
// create a metatable for xdb searcher object
|
||||
luaL_newmetatable(L, XDB_METATABLE_NAME);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setfield(L, -2, "__index");
|
||||
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
|
||||
// lua 5.1
|
||||
luaL_register(L, NULL, xdb_searcher_methods);
|
||||
luaL_register(L, NULL, xdb_searcher_functions);
|
||||
#elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
|
||||
// lua version 5.2, 5.3, 5.4 ...
|
||||
luaL_setfuncs(L, xdb_searcher_methods, 0);
|
||||
luaL_setfuncs(L, xdb_searcher_functions, 0);
|
||||
#endif
|
||||
|
||||
// register the constants attributes
|
||||
lua_pushinteger(L, xdb_ipv4_id);
|
||||
|
|
|
|||
Loading…
Reference in New Issue