Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
84ec0811
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 大约 1 年
通知
21
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,发现更多精彩内容 >>
提交
84ec0811
编写于
7月 05, 2022
作者:
L
Lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
search api is ready
上级
6dd15fa3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
95 addition
and
8 deletion
+95
-8
binding/lua/xdb_searcher.lua
binding/lua/xdb_searcher.lua
+95
-8
未找到文件。
binding/lua/xdb_searcher.lua
浏览文件 @
84ec0811
...
@@ -34,7 +34,8 @@ _M.__tostring = function(self)
...
@@ -34,7 +34,8 @@ _M.__tostring = function(self)
end
end
-- --- construct functions
-- construct functions
function
newBase
(
dbPath
,
vIndex
,
cBuffer
)
function
newBase
(
dbPath
,
vIndex
,
cBuffer
)
local
obj
=
setmetatable
({},
_M
)
local
obj
=
setmetatable
({},
_M
)
if
cBuffer
~=
nil
then
if
cBuffer
~=
nil
then
...
@@ -89,19 +90,90 @@ function _M:search(ip_src)
...
@@ -89,19 +90,90 @@ function _M:search(ip_src)
-- locate the segment index based on the vector index
-- locate the segment index based on the vector index
local
il0
=
(
ip
>>
24
)
&
0xFF
local
il0
=
(
ip
>>
24
)
&
0xFF
local
il1
=
(
ip
>>
16
)
&
0xFF
local
il1
=
(
ip
>>
16
)
&
0xFF
local
idx
=
il0
*
VectorIndexCols
*
SegmentIndexSize
+
il1
*
Segment
IndexSize
local
idx
=
il0
*
VectorIndexCols
*
VectorIndexSize
+
il1
*
Vector
IndexSize
local
s_ptr
,
e_ptr
=
0
,
0
local
s_ptr
,
e_ptr
=
0
,
0
if
self
.
vector_index
~=
nil
then
if
self
.
vector_index
~=
nil
then
s_ptr
=
getLong
(
self
.
vector_index
,
idx
)
s_ptr
=
getLong
(
self
.
vector_index
,
idx
+
1
)
e_ptr
=
getLong
(
self
.
vector_index
,
idx
+
4
)
e_ptr
=
getLong
(
self
.
vector_index
,
idx
+
5
)
elseif
self
.
content_buff
~=
nil
then
elseif
self
.
content_buff
~=
nil
then
s_ptr
=
getLong
(
self
.
content_buff
,
HeaderInfoLength
+
idx
)
s_ptr
=
getLong
(
self
.
content_buff
,
HeaderInfoLength
+
idx
+
1
)
e_ptr
=
getLong
(
self
.
content_buff
,
HeaderInfoLength
+
idx
+
4
)
e_ptr
=
getLong
(
self
.
content_buff
,
HeaderInfoLength
+
idx
+
5
)
else
else
-- load from the file
-- load from the file
buff
,
err
=
self
:
read
(
HeaderInfoLength
+
idx
,
SegmentIndexSize
)
if
err
~=
nil
then
return
""
,
string.format
(
"read buffer: %s"
,
err
)
end
s_ptr
=
getLong
(
buff
,
1
)
e_ptr
=
getLong
(
buff
,
5
)
end
-- print(string.format("s_ptr: %d, e_ptr: %d", s_ptr, e_ptr))
-- binary search to get the data
local
data_ptr
,
data_len
,
p
=
0
,
0
,
0
local
sip
,
eip
,
err
,
buff
=
0
,
0
,
""
local
l
,
m
,
h
=
0
,
0
,
(
e_ptr
-
s_ptr
)
/
SegmentIndexSize
while
l
<=
h
do
m
=
(
l
+
h
)
>>
1
p
=
s_ptr
+
m
*
SegmentIndexSize
-- read the segment index
buff
,
err
=
self
:
read
(
p
,
SegmentIndexSize
)
if
err
~=
nil
then
return
""
,
string.format
(
"read segment index at %d"
,
p
)
end
sip
=
getLong
(
buff
,
1
)
if
ip
<
sip
then
h
=
m
-
1
else
eip
=
getLong
(
buff
,
5
)
if
ip
>
eip
then
l
=
m
+
1
else
data_len
=
getShort
(
buff
,
9
)
data_ptr
=
getLong
(
buff
,
11
)
break
end
end
end
end
return
""
,
"not implemented yet"
-- matching nothing interception
-- print(string.format("data_len=%d, data_ptr=%d", data_len, data_ptr))
if
data_len
==
0
then
return
""
,
nil
end
-- load and return the region data
buff
,
err
=
self
:
read
(
data_ptr
,
data_len
)
if
err
~=
nil
then
return
""
,
string.format
(
"read data at %d:%d"
,
data_ptr
,
data_len
)
end
return
buff
,
nil
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
return
string.sub
(
self
.
content_buff
,
offset
+
1
,
offset
+
1
+
length
),
nil
end
-- read from the file
local
r
=
self
.
handle
:
seek
(
"set"
,
offset
)
if
r
==
nil
then
return
nil
,
string.format
(
"seek to offset %d"
,
offset
)
end
self
.
io_count
=
self
.
io_count
+
1
local
buff
=
self
.
handle
:
read
(
length
)
if
buff
==
nil
then
return
nil
,
string.format
(
"read %d bytes"
,
length
)
end
return
buff
,
nil
end
end
function
_M
:
get_io_count
()
function
_M
:
get_io_count
()
...
@@ -130,7 +202,7 @@ function _M.load_content(dbPath)
...
@@ -130,7 +202,7 @@ function _M.load_content(dbPath)
end
end
function
_M
.
check_ip
(
ip_str
)
function
_M
.
check_ip
(
ip_str
)
local
ip
,
id
=
0
,
1
local
ip
,
id
,
v
=
0
,
1
,
0
local
offset_arr
=
{
24
,
16
,
8
,
0
}
local
offset_arr
=
{
24
,
16
,
8
,
0
}
for
p
in
string.gmatch
(
ip_str
..
"."
,
"([%d]+)%."
)
do
for
p
in
string.gmatch
(
ip_str
..
"."
,
"([%d]+)%."
)
do
-- match pattern checking
-- match pattern checking
...
@@ -169,4 +241,19 @@ function _M.now()
...
@@ -169,4 +241,19 @@ function _M.now()
end
end
-- End of util functions
-- 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
)
local
i3
=
(
string.byte
(
string.sub
(
buff
,
idx
+
2
,
idx
+
2
))
<<
16
)
local
i4
=
(
string.byte
(
string.sub
(
buff
,
idx
+
3
,
idx
+
3
))
<<
24
)
return
(
i1
|
i2
|
i3
|
i4
)
end
function
getShort
(
buff
,
idx
)
local
i1
=
(
string.byte
(
string.sub
(
buff
,
idx
,
idx
)))
local
i2
=
(
string.byte
(
string.sub
(
buff
,
idx
+
1
,
idx
+
1
))
<<
8
)
return
(
i1
|
i2
)
end
return
_M
return
_M
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录