calling the latest xdb api
This commit is contained in:
parent
5fa69300ad
commit
6712ee9504
|
|
@ -18,7 +18,7 @@ function printHelp()
|
||||||
print(" --cache-policy string cache policy: file/vectorIndex/content")
|
print(" --cache-policy string cache policy: file/vectorIndex/content")
|
||||||
end
|
end
|
||||||
|
|
||||||
if #arg < 2 then
|
if #arg < 1 then
|
||||||
printHelp(arg)
|
printHelp(arg)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -59,10 +59,12 @@ if string.len(dbFile) < 2 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local version = IPv4
|
||||||
|
|
||||||
-- create the searcher based on the cache-policy
|
-- create the searcher based on the cache-policy
|
||||||
local searcher, v_index, content
|
local searcher, v_index, content
|
||||||
if cachePolicy == "file" then
|
if cachePolicy == "file" then
|
||||||
searcher, err = xdb.new_with_file_only(dbFile)
|
searcher, err = xdb.new_with_file_only(version, dbFile)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("failed to create searcher: %s", err))
|
print(string.format("failed to create searcher: %s", err))
|
||||||
return
|
return
|
||||||
|
|
@ -74,7 +76,7 @@ elseif cachePolicy == "vectorIndex" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
searcher, err = xdb.new_with_vector_index(dbFile, v_index)
|
searcher, err = xdb.new_with_vector_index(version, dbFile, v_index)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("failed to create vector index searcher: %s", err))
|
print(string.format("failed to create vector index searcher: %s", err))
|
||||||
return
|
return
|
||||||
|
|
@ -86,7 +88,7 @@ elseif cachePolicy == "content" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
searcher, err = xdb.new_with_buffer(content)
|
searcher, err = xdb.new_with_buffer(version, content)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("failed to create content buffer searcher: %s", err))
|
print(string.format("failed to create content buffer searcher: %s", err))
|
||||||
return
|
return
|
||||||
|
|
@ -114,7 +116,7 @@ while ( true ) do
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
ip_int, err = xdb.check_ip(line)
|
version, err = xdb.parse_ip(line)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("invalid ip address `%s`", line))
|
print(string.format("invalid ip address `%s`", line))
|
||||||
goto continue
|
goto continue
|
||||||
|
|
@ -123,10 +125,10 @@ while ( true ) do
|
||||||
-- do the search
|
-- do the search
|
||||||
s_time = xdb.now()
|
s_time = xdb.now()
|
||||||
region, err = searcher:search(line)
|
region, err = searcher:search(line)
|
||||||
|
c_time = xdb.now() - s_time
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("{err: %s, io_count: %d}", err, searcher:get_io_count()))
|
print(string.format("{err: %s, io_count: %d}", err, searcher:get_io_count()))
|
||||||
else
|
else
|
||||||
c_time = xdb.now() - s_time
|
|
||||||
print(string.format("{region: %s, io_count: %d, took: %dμs}", region, searcher:get_io_count(), c_time))
|
print(string.format("{region: %s, io_count: %d, took: %dμs}", region, searcher:get_io_count(), c_time))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,86 +12,103 @@ package.cpath = "./?.so" .. package.cpath
|
||||||
local xdb = require("xdb_searcher")
|
local xdb = require("xdb_searcher")
|
||||||
|
|
||||||
---- ip checking testing
|
---- ip checking testing
|
||||||
print("--- testing check_ip and long2ip ... ")
|
function test_parse_ip()
|
||||||
local ip_list = {
|
local ip_list = {
|
||||||
"1.2.3.4", "192.168.2.3", "120.24.78.129", "255.255.255.0",
|
"1.2.3.4", "192.168.2.3", "120.24.78.129", "255.255.255.0",
|
||||||
"256.7.12.9", "12.56.78.320", "32.12.45.192", "222.221.220.219",
|
"256.7.12.9", "12.56.78.320", "32.12.45.192", "222.221.220.219",
|
||||||
"192.168.1.101 ", "132.96.12.98a", "x23.12.2.12"
|
"192.168.1.101 ", "132.96.12.98a", "x23.12.2.12"
|
||||||
}
|
}
|
||||||
|
|
||||||
local s_time = xdb.now()
|
local s_time = xdb.now()
|
||||||
for _, ip_src in ipairs(ip_list) do
|
for _, ip_src in ipairs(ip_list) do
|
||||||
ip, err = xdb.check_ip(ip_src)
|
ip, err = xdb.parse_ip(ip_src)
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print(string.format("invalid ip address `%s`: %s", ip_src, err))
|
print(string.format("invalid ip address `%s`: %s", ip_src, err))
|
||||||
else
|
else
|
||||||
ip_dst = xdb.long2ip(ip)
|
-- io.write(string.format("long(%-15s)=%10d, long2ip(%-10d)=%-15s", ip_src, ip, ip, ip_dst))
|
||||||
io.write(string.format("long(%-15s)=%10d, long2ip(%-10d)=%-15s", ip_src, ip, ip, ip_dst))
|
-- if ip_src ~= ip_dst then
|
||||||
if ip_src ~= ip_dst then
|
-- print(" --[Failed]")
|
||||||
print(" --[Failed]")
|
-- else
|
||||||
else
|
-- print(" --[Ok]")
|
||||||
print(" --[Ok]")
|
-- end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---- buffer loading test
|
---- buffer loading test
|
||||||
print("\n--- testing load header ... ")
|
function test_load_header()
|
||||||
header, err = xdb.load_header("../../data/ip2region.xdb")
|
header, err = xdb.load_header("../../data/ip2region_v4.xdb")
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print("failed to load header: ", err)
|
print("failed to load header: ", err)
|
||||||
else
|
else
|
||||||
print(string.format("xdb header buffer `%s` loaded", header))
|
print(string.format("xdb header buffer `%s` loaded", header))
|
||||||
|
|
||||||
local tpl = [[
|
local tpl = [[
|
||||||
header: {
|
header: {
|
||||||
version: %d
|
version: %d
|
||||||
index_policy: %d
|
index_policy: %d
|
||||||
created_at: %d
|
created_at: %d
|
||||||
start_index_ptr: %d
|
start_index_ptr: %d
|
||||||
end_index_ptr: %d
|
end_index_ptr: %d
|
||||||
}]]
|
}]]
|
||||||
|
|
||||||
local t = header:to_table()
|
local t = header:to_table()
|
||||||
print(string.format(tpl,
|
print(string.format(tpl,
|
||||||
t["version"], t["index_policy"], t["created_at"], t["start_index_ptr"], t["end_index_ptr"])
|
t["version"], t["index_policy"], t["created_at"], t["start_index_ptr"], t["end_index_ptr"])
|
||||||
)
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
print("\n--- testing load vector index ... ")
|
function test_load_vector_index()
|
||||||
v_index, err = xdb.load_vector_index("../../data/ip2region.xdb")
|
v_index, err = xdb.load_vector_index("../../data/ip2region_v4.xdb")
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print("failed to load vector index: ", err)
|
print("failed to load vector index: ", err)
|
||||||
else
|
else
|
||||||
print(string.format("xdb vector index buffer `%s` loaded, info={name=%s, type=%d, length=%d}",
|
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()))
|
v_index, v_index:name(), v_index:type(), v_index:length()))
|
||||||
v_index:close()
|
v_index:close()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
print("\n--- testing load content buffer ... ")
|
function test_load_content()
|
||||||
c_buffer, err = xdb.load_content("../../data/ip2region.xdb")
|
c_buffer, err = xdb.load_content("../../data/ip2region_v4.xdb")
|
||||||
if err ~= nil then
|
if err ~= nil then
|
||||||
print("failed to load content: ", err)
|
print("failed to load content: ", err)
|
||||||
else
|
else
|
||||||
print(string.format("xdb content buffer `%s` loaded, info={name=%s, type=%d, length=%d}",
|
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()))
|
c_buffer, c_buffer:name(), c_buffer:type(), c_buffer:length()))
|
||||||
c_buffer:close();
|
c_buffer:close();
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
print("\n--- testing search ... ")
|
function test_search()
|
||||||
local ip_str = "1.2.3.4"
|
local ip_str = "1.2.3.4"
|
||||||
searcher, err = xdb.new_with_file_only("../../data/ip2region.xdb")
|
searcher, err = xdb.new_with_file_only(IPv4, "../../data/ip2region_v4.xdb")
|
||||||
local t_start = xdb.now()
|
local t_start = xdb.now()
|
||||||
region, err = searcher:search(ip_str)
|
region, err = searcher:search(ip_str)
|
||||||
local c_time = xdb.now() - t_start
|
local c_time = xdb.now() - t_start
|
||||||
print(string.format("search(%s): {region=%s, io_count: %d, took: %dμs, err=%s}",
|
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, err))
|
||||||
print(string.format("searcher.tostring=%s", searcher))
|
print(string.format("searcher.tostring=%s", searcher))
|
||||||
searcher:close()
|
searcher:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local func_name = arg[1]
|
||||||
|
if func_name == nil then
|
||||||
|
print("please specified the function to test\n")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
print("")
|
if (_G[func_name] == nil) then
|
||||||
print(string.format("all tests done, elapsed %d μs", xdb.now() - s_time))
|
print(string.format("undefined function `%s` to call", func_name))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local s_time = xdb.now();
|
||||||
|
print(string.format("+---calling test function %s ...", func_name))
|
||||||
|
_G[func_name]();
|
||||||
|
local cost_time = xdb.now() - s_time
|
||||||
|
print(string.format("|---done, took: %.3fμs", cost_time))
|
||||||
Loading…
Reference in New Issue