util test passed
This commit is contained in:
parent
1aed3ecc46
commit
e980386c1f
|
|
@ -42,7 +42,7 @@ header, err = xdb.load_header("../../data/ip2region.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("xdb header buffer loaded")
|
||||||
|
|
||||||
local tpl = [[
|
local tpl = [[
|
||||||
header: {
|
header: {
|
||||||
|
|
@ -53,9 +53,9 @@ header: {
|
||||||
end_index_ptr: %d
|
end_index_ptr: %d
|
||||||
}]]
|
}]]
|
||||||
|
|
||||||
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"])
|
header["version"], header["index_policy"],
|
||||||
|
header["created_at"], header["start_index_ptr"], header["end_index_ptr"])
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -65,9 +65,7 @@ v_index, err = xdb.load_vector_index("../../data/ip2region.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("xdb vector index buffer loaded")
|
||||||
v_index, v_index:name(), v_index:type(), v_index:length()))
|
|
||||||
v_index:close()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -76,9 +74,7 @@ c_buffer, err = xdb.load_content("../../data/ip2region.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("xdb content buffer loaded")
|
||||||
c_buffer, c_buffer:name(), c_buffer:type(), c_buffer:length()))
|
|
||||||
c_buffer:close();
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ local VectorIndexRows = 256
|
||||||
local VectorIndexCols = 256
|
local VectorIndexCols = 256
|
||||||
local VectorIndexSize = 8
|
local VectorIndexSize = 8
|
||||||
local SegmentIndexSize = 14
|
local SegmentIndexSize = 14
|
||||||
|
local VectorIndexLength = 524288
|
||||||
|
|
||||||
local _M = {
|
local _M = {
|
||||||
-- xdb file handle
|
-- xdb file handle
|
||||||
handle = nil,
|
handle = nil,
|
||||||
|
|
@ -30,7 +32,7 @@ local _M = {
|
||||||
-- index and to string attribute set
|
-- index and to string attribute set
|
||||||
_M.__index = _M
|
_M.__index = _M
|
||||||
_M.__tostring = function(self)
|
_M.__tostring = function(self)
|
||||||
return "xdb searcher object"
|
return "xdb searcher object (lua)"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -68,8 +70,8 @@ end
|
||||||
|
|
||||||
-- End of constructors
|
-- End of constructors
|
||||||
|
|
||||||
-- object api impl
|
-- object api impl, must call via ':'
|
||||||
-- must call via ':'
|
|
||||||
function _M:search(ip_src)
|
function _M:search(ip_src)
|
||||||
-- check and convert string ip to long ip
|
-- check and convert string ip to long ip
|
||||||
local t, ip = type(ip_src), 0
|
local t, ip = type(ip_src), 0
|
||||||
|
|
@ -155,6 +157,7 @@ function _M:search(ip_src)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- read specified bytes from the specified index
|
-- read specified bytes from the specified index
|
||||||
|
|
||||||
function _M:read(offset, length)
|
function _M:read(offset, length)
|
||||||
-- check the in-memory buffer first
|
-- check the in-memory buffer first
|
||||||
if self.content_buff ~= nil then
|
if self.content_buff ~= nil then
|
||||||
|
|
@ -185,20 +188,76 @@ function _M:close()
|
||||||
self.handle:close()
|
self.handle:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- End of search api
|
-- End of search api
|
||||||
|
|
||||||
|
|
||||||
-- static util functions
|
-- static util functions
|
||||||
|
|
||||||
function _M.load_header(dbPath)
|
function _M.load_header(dbPath)
|
||||||
return nil, "not implemented yet"
|
local handle = io.open(dbPath, "r")
|
||||||
|
if handle == nil then
|
||||||
|
return nil, string.format("failed to open xdb file `%s`", dbPath)
|
||||||
|
end
|
||||||
|
|
||||||
|
local r = handle:seek("set", 0)
|
||||||
|
if r == nil then
|
||||||
|
handle:close()
|
||||||
|
return nil, "failed to seek to 0"
|
||||||
|
end
|
||||||
|
|
||||||
|
local c = handle:read(HeaderInfoLength)
|
||||||
|
if c == nil then
|
||||||
|
handle:close()
|
||||||
|
return nil, string.format("failed to read %d bytes", HeaderInfoLength)
|
||||||
|
end
|
||||||
|
|
||||||
|
handle:close()
|
||||||
|
return {
|
||||||
|
["version"] = getShort(c, 1),
|
||||||
|
["index_policy"] = getShort(c, 3),
|
||||||
|
["created_at"] = getLong(c, 5),
|
||||||
|
["start_index_ptr"] = getLong(c, 9),
|
||||||
|
["end_index_ptr"] = getLong(c, 13),
|
||||||
|
["raw_data"] = c
|
||||||
|
}, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.load_vector_index(dbPath)
|
function _M.load_vector_index(dbPath)
|
||||||
return nil, "not implemented yet"
|
local handle = io.open(dbPath, "r")
|
||||||
|
if handle == nil then
|
||||||
|
return nil, string.format("failed to open xdb file `%s`", dbPath)
|
||||||
|
end
|
||||||
|
|
||||||
|
local r = handle:seek("set", HeaderInfoLength)
|
||||||
|
if r == nil then
|
||||||
|
handle:close()
|
||||||
|
return nil, string.format("failed to seek to %d", HeaderInfoLength)
|
||||||
|
end
|
||||||
|
|
||||||
|
local c = handle:read(VectorIndexLength)
|
||||||
|
if c == nil then
|
||||||
|
handle:close()
|
||||||
|
return nil, string.format("failed to read %d bytes", VectorIndexLength)
|
||||||
|
end
|
||||||
|
|
||||||
|
handle:close()
|
||||||
|
return c, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.load_content(dbPath)
|
function _M.load_content(dbPath)
|
||||||
return nil, "not implemented yet"
|
local handle = io.open(dbPath, "r")
|
||||||
|
if handle == nil then
|
||||||
|
return nil, string.format("failed to open xdb file `%s`", dbPath)
|
||||||
|
end
|
||||||
|
|
||||||
|
local c = handle:read("*a")
|
||||||
|
if c == nil then
|
||||||
|
return nil, string.format("failed to read xdb content")
|
||||||
|
end
|
||||||
|
|
||||||
|
handle:close()
|
||||||
|
return c, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.check_ip(ip_str)
|
function _M.check_ip(ip_str)
|
||||||
|
|
@ -239,9 +298,11 @@ end
|
||||||
function _M.now()
|
function _M.now()
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
-- End of util functions
|
-- End of util functions
|
||||||
|
|
||||||
--internal function to get a integer from a binary string
|
--internal function to get a integer from a binary string
|
||||||
|
|
||||||
function getLong(buff, idx)
|
function getLong(buff, idx)
|
||||||
local i1 = (string.byte(string.sub(buff, idx, idx)))
|
local i1 = (string.byte(string.sub(buff, idx, idx)))
|
||||||
local i2 = (string.byte(string.sub(buff, idx+1, idx+1)) << 8)
|
local i2 = (string.byte(string.sub(buff, idx+1, idx+1)) << 8)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue