Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
f25ef646
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 大约 1 年
通知
20
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Ip2region
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f25ef646
编写于
7月 06, 2022
作者:
L
Lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
util test passed
上级
013021c3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
15 deletion
+72
-15
binding/lua/util_test.lua
binding/lua/util_test.lua
+5
-9
binding/lua/xdb_searcher.lua
binding/lua/xdb_searcher.lua
+67
-6
未找到文件。
binding/lua/util_test.lua
浏览文件 @
f25ef646
...
...
@@ -42,7 +42,7 @@ header, err = xdb.load_header("../../data/ip2region.xdb")
if
err
~=
nil
then
print
(
"failed to load header: "
,
err
)
else
print
(
string.format
(
"xdb header buffer `%s` loaded"
,
header
)
)
print
(
"xdb header buffer loaded"
)
local
tpl
=
[[
header: {
...
...
@@ -53,9 +53,9 @@ header: {
end_index_ptr: %d
}]]
local
t
=
header
:
to_table
()
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
...
...
@@ -65,9 +65,7 @@ v_index, err = xdb.load_vector_index("../../data/ip2region.xdb")
if
err
~=
nil
then
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
()))
v_index
:
close
()
print
(
"xdb vector index buffer loaded"
)
end
...
...
@@ -76,9 +74,7 @@ c_buffer, err = xdb.load_content("../../data/ip2region.xdb")
if
err
~=
nil
then
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
()))
c_buffer
:
close
();
print
(
"xdb content buffer loaded"
)
end
...
...
binding/lua/xdb_searcher.lua
浏览文件 @
f25ef646
...
...
@@ -12,6 +12,8 @@ local VectorIndexRows = 256
local
VectorIndexCols
=
256
local
VectorIndexSize
=
8
local
SegmentIndexSize
=
14
local
VectorIndexLength
=
524288
local
_M
=
{
-- xdb file handle
handle
=
nil
,
...
...
@@ -30,7 +32,7 @@ local _M = {
-- index and to string attribute set
_M
.
__index
=
_M
_M
.
__tostring
=
function
(
self
)
return
"xdb searcher object"
return
"xdb searcher object
(lua)
"
end
...
...
@@ -68,8 +70,8 @@ end
-- End of constructors
-- object api impl
-- must call via ':'
-- object api impl
, must call via ':'
function
_M
:
search
(
ip_src
)
-- check and convert string ip to long ip
local
t
,
ip
=
type
(
ip_src
),
0
...
...
@@ -155,6 +157,7 @@ function _M:search(ip_src)
end
-- read specified bytes from the specified index
function
_M
:
read
(
offset
,
length
)
-- check the in-memory buffer first
if
self
.
content_buff
~=
nil
then
...
...
@@ -185,20 +188,76 @@ function _M:close()
self
.
handle
:
close
()
end
end
-- End of search api
-- static util functions
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
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
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
function
_M
.
check_ip
(
ip_str
)
...
...
@@ -239,9 +298,11 @@ end
function
_M
.
now
()
return
0
end
-- End of util functions
--internal function to get a integer from a binary string
function
getLong
(
buff
,
idx
)
local
i1
=
(
string.byte
(
string.sub
(
buff
,
idx
,
idx
)))
local
i2
=
(
string.byte
(
string.sub
(
buff
,
idx
+
1
,
idx
+
1
))
<<
8
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录