Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
1d4d3f92
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 搜索 >>
提交
1d4d3f92
编写于
6月 18, 2022
作者:
L
Lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add vector index and xdb content load util func
上级
79b3dd2a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
113 addition
and
46 deletion
+113
-46
binding/golang/xdb/searcher.go
binding/golang/xdb/searcher.go
+0
-46
binding/golang/xdb/util.go
binding/golang/xdb/util.go
+84
-0
binding/golang/xdb/util_test.go
binding/golang/xdb/util_test.go
+29
-0
未找到文件。
binding/golang/xdb/searcher.go
浏览文件 @
1d4d3f92
...
...
@@ -54,52 +54,6 @@ func (s *Searcher) Close() {
}
}
// LoadVectorIndex load and cache the vector index for search speedup.
// this will take up VectorIndexRows x VectorIndexCols x VectorIndexSize bytes memory.
func
(
s
*
Searcher
)
LoadVectorIndex
()
error
{
// loaded already
if
s
.
vectorIndex
!=
nil
{
return
nil
}
// load all the vector index block
_
,
err
:=
s
.
handle
.
Seek
(
HeaderInfoLength
,
0
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"seek to vector index: %w"
,
err
)
}
var
buff
=
make
([]
byte
,
VectorIndexRows
*
VectorIndexCols
*
VectorIndexSize
)
rLen
,
err
:=
s
.
handle
.
Read
(
buff
)
if
err
!=
nil
{
return
err
}
if
rLen
!=
len
(
buff
)
{
return
fmt
.
Errorf
(
"incomplete read: readed bytes should be %d"
,
len
(
buff
))
}
// decode the vector index blocks
var
vectorIndex
=
make
([][]
*
VectorIndexBlock
,
VectorIndexRows
)
for
r
:=
0
;
r
<
VectorIndexRows
;
r
++
{
vectorIndex
[
r
]
=
make
([]
*
VectorIndexBlock
,
VectorIndexCols
)
for
c
:=
0
;
c
<
VectorIndexCols
;
c
++
{
offset
:=
r
*
VectorIndexCols
*
VectorIndexSize
+
c
*
VectorIndexSize
vectorIndex
[
r
][
c
],
err
=
VectorIndexBlockDecode
(
buff
[
offset
:
])
if
err
!=
nil
{
return
fmt
.
Errorf
(
"decode vector index at [%d][%d]: %w"
,
r
,
c
,
err
)
}
}
}
s
.
vectorIndex
=
vectorIndex
return
nil
}
// ClearVectorIndex clear preloaded vector index cache
func
(
s
*
Searcher
)
ClearVectorIndex
()
{
s
.
vectorIndex
=
nil
}
// SearchByStr find the region for the specified ip string
func
(
s
*
Searcher
)
SearchByStr
(
str
string
)
(
string
,
error
)
{
ip
,
err
:=
CheckIP
(
str
)
...
...
binding/golang/xdb/util.go
浏览文件 @
1d4d3f92
...
...
@@ -3,6 +3,7 @@ package xdb
import
(
"encoding/binary"
"fmt"
"os"
"strconv"
"strings"
)
...
...
@@ -43,3 +44,86 @@ func Long2IP(ip uint32) string {
func
MidIP
(
sip
uint32
,
eip
uint32
)
uint32
{
return
uint32
((
uint64
(
sip
)
+
uint64
(
eip
))
>>
1
)
}
// LoadVectorIndex util function to load the vector index from the specified file handle
func
LoadVectorIndex
(
handle
*
os
.
File
)
([][]
*
VectorIndexBlock
,
error
)
{
// load all the vector index block
_
,
err
:=
handle
.
Seek
(
HeaderInfoLength
,
0
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"seek to vector index: %w"
,
err
)
}
var
buff
=
make
([]
byte
,
VectorIndexRows
*
VectorIndexCols
*
VectorIndexSize
)
rLen
,
err
:=
handle
.
Read
(
buff
)
if
err
!=
nil
{
return
nil
,
err
}
if
rLen
!=
len
(
buff
)
{
return
nil
,
fmt
.
Errorf
(
"incomplete read: readed bytes should be %d"
,
len
(
buff
))
}
// decode the vector index blocks
var
vectorIndex
=
make
([][]
*
VectorIndexBlock
,
VectorIndexRows
)
for
r
:=
0
;
r
<
VectorIndexRows
;
r
++
{
vectorIndex
[
r
]
=
make
([]
*
VectorIndexBlock
,
VectorIndexCols
)
for
c
:=
0
;
c
<
VectorIndexCols
;
c
++
{
offset
:=
r
*
VectorIndexCols
*
VectorIndexSize
+
c
*
VectorIndexSize
vectorIndex
[
r
][
c
],
err
=
VectorIndexBlockDecode
(
buff
[
offset
:
])
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"decode vector index at [%d][%d]: %w"
,
r
,
c
,
err
)
}
}
}
return
vectorIndex
,
nil
}
// LoadVectorIndexFromFile load vector index from a specified file path
func
LoadVectorIndexFromFile
(
dbFile
string
)
([][]
*
VectorIndexBlock
,
error
)
{
handle
,
err
:=
os
.
OpenFile
(
dbFile
,
os
.
O_RDONLY
,
0600
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"open xdb file `%s`: %w"
,
dbFile
,
err
)
}
return
LoadVectorIndex
(
handle
)
}
// LoadContent load the whole xdb content from the specified file handle
func
LoadContent
(
handle
*
os
.
File
)
([]
byte
,
error
)
{
// get file size
fi
,
err
:=
handle
.
Stat
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"stat: %w"
,
err
)
}
size
:=
fi
.
Size
()
// seek to the head of the file
_
,
err
=
handle
.
Seek
(
0
,
0
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"seek to get xdb file length: %w"
,
err
)
}
var
buff
=
make
([]
byte
,
size
)
rLen
,
err
:=
handle
.
Read
(
buff
)
if
err
!=
nil
{
return
nil
,
err
}
if
rLen
!=
len
(
buff
)
{
return
nil
,
fmt
.
Errorf
(
"incomplete read: readed bytes should be %d"
,
len
(
buff
))
}
return
buff
,
nil
}
// LoadContentFromFile load the whole xdb content from the specified db file path
func
LoadContentFromFile
(
dbFile
string
)
([]
byte
,
error
)
{
handle
,
err
:=
os
.
OpenFile
(
dbFile
,
os
.
O_RDONLY
,
0600
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"open xdb file `%s`: %w"
,
dbFile
,
err
)
}
return
LoadContent
(
handle
)
}
binding/golang/xdb/util_test.go
0 → 100644
浏览文件 @
1d4d3f92
// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
package
xdb
import
(
"fmt"
"testing"
)
func
TestLoadVectorIndex
(
t
*
testing
.
T
)
{
vIndex
,
err
:=
LoadVectorIndexFromFile
(
"../../../data/ip2region.xdb"
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to load vector index: %s
\n
"
,
err
)
return
}
fmt
.
Printf
(
"vIndex length: %d
\n
"
,
len
(
vIndex
))
}
func
TestLoadContent
(
t
*
testing
.
T
)
{
buff
,
err
:=
LoadContentFromFile
(
"../../../data/ip2region.xdb"
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to load xdb content: %s
\n
"
,
err
)
}
fmt
.
Printf
(
"buff length: %d
\n
"
,
len
(
buff
))
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录