some optimizations

This commit is contained in:
Lion 2022-06-29 17:49:59 +08:00
parent 6ecff85a3e
commit 68486ea679
3 changed files with 7 additions and 40 deletions

View File

@ -1,36 +0,0 @@
// 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.
// @Author Lion <chenxin619315@gmail.com>
// @Date 2022/06/16
package xdb
import (
"encoding/binary"
"fmt"
)
type Header struct {
// data []byte
Version uint16
IndexPolicy IndexPolicy
CreatedAt uint32
StartIndexPtr uint32
EndIndexPtr uint32
}
func NewHeader(input []byte) (*Header, error) {
if len(input) < 16 {
return nil, fmt.Errorf("invalid input buffer")
}
return &Header{
Version: binary.LittleEndian.Uint16(input),
IndexPolicy: IndexPolicy(binary.LittleEndian.Uint16(input[2:])),
CreatedAt: binary.LittleEndian.Uint32(input[4:]),
StartIndexPtr: binary.LittleEndian.Uint32(input[8:]),
EndIndexPtr: binary.LittleEndian.Uint32(input[12:]),
}, nil
}

View File

@ -1,5 +1,6 @@
# ip2region xdb golang 生成实现
# 程序编译
通过如下方式编译得到 xdb_maker 可执行程序:
@ -9,7 +10,8 @@ make
```
编译成功后会在当前目录生成一个 xdb_maker 的可执行文件
# 数据生成
# `xdb` 数据生成
通过 `xdb_maker gen` 命令生成 ip2region.xdb 二进制文件:
```
@ -29,7 +31,8 @@ options:
2022/06/16 16:38:48 main.go:89: Done, elapsed: 33.615278847s
```
# 数据查询
# `xdb` 数据查询
通过 `xdb_maker search` 命令来测试查询输入的 ip
```
@ -59,7 +62,7 @@ ip2region>>
# bench 测试
如果你自主生成了 xdb 文件,请确保运行如下的 `xdb_maker bench` 命令来确保生成的的 xdb 文件的正确性:
如果你自主生成了 `xdb` 文件,请确保运行如下的 `xdb_maker bench` 命令来确保生成的的 `xdb` 文件的正确性:
```
➜ golang git:(v2.0_xdb) ✗ ./xdb_maker bench
./xdb_maker bench [command options]

View File

@ -113,7 +113,7 @@ func (s *Searcher) Search(ip uint32) (string, int, error) {
}
ioCount++
var buff = make([]byte, 8)
var buff = make([]byte, VectorIndexSize)
rLen, err := s.handle.Read(buff)
if err != nil {
return "", ioCount, fmt.Errorf("read vector index at %d: %w", pos, err)