Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
f1a097ca
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 11 个月
通知
19
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,体验更适合开发者的 AI 搜索 >>
提交
f1a097ca
编写于
7月 02, 2022
作者:
L
Lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add docs for compile/install/api and tests
上级
9e8f672b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
156 addition
and
1 deletion
+156
-1
binding/lua_c/ReadMe.md
binding/lua_c/ReadMe.md
+156
-1
未找到文件。
binding/lua_c/ReadMe.md
浏览文件 @
f1a097ca
# ip2region lua c 扩展查询客户端实现
# ip2region xdb lua c 扩展查询客户端实现
# 编译安装
通过如下方式来编译安装该扩展:
```
bash
# cd 到 lua_c binding 的根目录
make
sudo
make
install
```
备注:Makefile 里面的
`LuaVersion`
变量代表你本地环境的 lua 的版本,默认为 5.3,如果你的版本不是 5.3,记得先修改为和你本地 lua 版本一致的版本号。
# 使用方式
### 完全基于文件的查询
```
lua
local
xdb
=
require
(
"xdb_searcher"
)
-- 1、从 db_path 创建基于文件的 xdb 查询对象
local
db_path
=
"ip2region.xdb file path"
local
searcher
,
err
=
xdb
.
new_with_file_only
(
db_path
)
if
err
~=
nil
then
print
(
string.format
(
"failed to create searcher: %s"
,
err
))
return
end
-- 2、调用查询 API 进行查询
local
ip_str
=
"1.2.3.4"
local
s_time
=
xdb
.
now
()
region
,
err
=
searcher
:
search
(
ip_str
)
if
err
~=
nil
then
print
(
string.format
(
"failed to search(%s): %s"
,
ip_str
,
err
))
return
end
-- 备注:并发使用,每个协程需要创建单独的 xdb 查询对象
print
(
string.format
(
"{region: %s, took: %.5f μs}"
,
region
,
xdb
.
now
()
-
s_time
))
```
### 缓存 `VectorIndex` 索引
如果你的
`lua`
母环境支持,可以预先加载 vectorIndex 缓存,然后做成全局变量,每次创建 Searcher 的时候使用全局的 vectorIndex,可以减少一次固定的 IO 操作从而加速查询,减少 io 压力。
```
lua
local
xdb
=
require
(
"xdb_searcher"
)
local
db_path
=
"ip2region.xdb file path"
-- 1、从指定的 db_path 加载 VectorIndex 缓存,把下述的 v_index 对象做成全局变量。
-- vectorIndex 加载一次即可,建议在服务启动的时候加载为全局对象。
v_index
,
err
=
xdb
.
load_vector_index
(
db_path
)
if
err
~=
nil
then
print
(
string.format
(
"failed to load vector index from '%s'"
,
db_path
))
return
end
-- 2、使用全局的 v_index 创建带 VectorIndex 缓存的查询对象。
searcher
,
err
=
xdb
.
new_with_vector_index
(
db_path
,
v_index
)
if
err
~=
nil
then
print
(
string.format
(
"failed to create vector index searcher: %s"
,
err
))
return
end
-- 3、调用查询 API
local
ip_str
=
"1.2.3.4"
local
s_time
=
xdb
.
now
()
region
,
err
=
searcher
:
search
(
ip_str
)
if
err
~=
nil
then
print
(
string.format
(
"failed to search(%s): %s"
,
ip_str
,
err
))
return
end
-- 备注:并发使用,每个协程需要创建单独的 xdb 查询对象,但是共享全局的 v_index 对象
print
(
string.format
(
"{region: %s, took: %.5f μs}"
,
region
,
xdb
.
now
()
-
s_time
))
```
### 缓存整个 `xdb` 数据
如果你的
`lua`
母环境支持,可以预先加载整个 xdb 的数据到内存,这样可以实现完全基于内存的查询,类似之前的 memory search 查询。
```
lua
local
xdb
=
require
(
"xdb_searcher"
)
local
db_path
=
"ip2region.xdb file path"
-- 1、从指定的 db_path 加载整个 xdb 到内存。
-- xdb内容加载一次即可,建议在服务启动的时候加载为全局对象。
content
=
xdb
.
load_content
(
db_path
)
if
content
==
nil
then
print
(
string.format
(
"failed to load xdb content from '%s'"
,
db_path
))
return
end
-- 2、使用全局的 content 创建带完全基于内存的查询对象。
searcher
,
err
=
xdb
.
new_with_buffer
(
content
)
if
err
~=
nil
then
print
(
string.format
(
"failed to create content buffer searcher: %s"
,
err
))
return
end
-- 3、调用查询 API
local
ip_str
=
"1.2.3.4"
local
s_time
=
xdb
.
now
()
region
,
err
=
searcher
:
search
(
ip_str
)
if
err
~=
nil
then
print
(
string.format
(
"failed to search(%s): %s"
,
ip_str
,
err
))
return
end
-- 备注:并发使用,用 xdb 整个缓存创建的查询对象可以安全的用于并发。
-- 建议在服务启动的时候创建好全局的 searcher 对象,然后全局并发使用。
print
(
string.format
(
"{region: %s, took: %.5f μs}"
,
region
,
xdb
.
now
()
-
s_time
))
```
# 查询测试
通过
`search_test.lua`
脚本来进行查询测试:
```
bash
➜ lua_c git:
(
lua_binding
)
✗ lua ./search_test.lua
lua search_test.lua
[
command
options]
options:
--db
string ip2region binary xdb file path
--cache-policy
string cache policy: file/vectorIndex/content
```
例如:使用默认的 data/ip2region.xdb 进行查询测试:
```
bash
➜ lua_c git:
(
lua_binding
)
✗ lua ./search_test.lua
--db
=
../../data/ip2region.xdb
--cache-policy
=
vectorIndex
ip2region xdb searcher
test
program, cachePolicy: vectorIndex
type
'quit'
to
exit
ip2region>> 1.2.3.4
{
region: 美国|0|华盛顿|0|谷歌, io_count: 7, took: 15μs
}
ip2region>>
```
输入 ip 即可进行查询测试。也可以分别设置
`cache-policy`
为 file/vectorIndex/content 来测试三种不同缓存实现的效率。
# bench 测试
通过
`bench_test.lua`
脚本来进行自动 bench 测试,一方面确保
`xdb`
文件没有错误,另一方面通过大量的查询测试平均查询性能:
```
bash
➜ lua_c git:
(
lua_binding
)
✗ lua ./bench_test.lua
lua bench_test.lua
[
command
options]
options:
--db
string ip2region binary xdb file path
--src
string
source
ip text file path
--cache-policy
string cache policy: file/vectorIndex/content
```
例如:通过默认的 data/ip2region.xdb 和 data/ip.merge.txt 来进行 bench 测试:
```
bash
➜ lua_c git:
(
lua_binding
)
✗ lua ./bench_test.lua
--db
=
../../data/ip2region.xdb
--src
=
../../data/ip.merge.txt
--cache-policy
=
vectorIndex
Bench finished,
{
cachePolicy: vectorIndex, total: 3417955, took: 5.865 s, cost: 1.399 μs/op
}
```
可以通过设置
`cache-policy`
参数来分别测试 file/vectorIndex/content 三种不同的缓存实现的的性能。
@Note:请注意 bench 使用的 src 文件需要是生成对应的 xdb 文件的相同的源文件。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录