rename pacakge to xdb and add SearchByStr impl

This commit is contained in:
lion 2022-06-17 14:45:16 +08:00
parent 985dabd51c
commit 0b36cb947b
6 changed files with 63 additions and 21 deletions

View File

@ -2,6 +2,44 @@
# 使用方式 # 使用方式
### package 获取
```bash
go get github.com/lionsoul2014/ip2region/binding/golang
```
### API 使用
```golang
import (
"fmt"
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
"time"
)
func main() {
var dbPath = "ip2region.xdb file path"
searcher, err := xdb.New(dbPath)
if err != nil {
fmt.Printf("failed to create searcher: %s\n", err.Error())
return
}
defer searcher.Close()
// do the search
var ip = "1.2.3.4"
var tStart = time.Now()
region, err := searcher.SearchByStr(ip)
if err != nil {
fmt.Printf("failed to SearchIP(%s): %s\n", ip, err)
return
}
fmt.Printf("{region: %s, took: %s}\n", region, time.Since(tStart))
}
```
# 编译测试程序
# 查询测试 # 查询测试
# bench 测试 # bench 测试

View File

@ -3,7 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/lionsoul2014/ip2region/binding/golang/ip2region" "github.com/lionsoul2014/ip2region/binding/golang/xdb"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"log" "log"
"os" "os"
@ -57,7 +57,7 @@ func testSearch() {
return return
} }
searcher, err := ip2region.New(dbPath) searcher, err := xdb.New(dbPath)
if err != nil { if err != nil {
log.Fatalf("failed to create searcher: %s", err.Error()) log.Fatalf("failed to create searcher: %s", err.Error())
} }
@ -84,14 +84,8 @@ func testSearch() {
break break
} }
ip, err := ip2region.CheckIP(line)
if err != nil {
fmt.Printf("invalid ip address `%s`\n", line)
continue
}
tStart := time.Now() tStart := time.Now()
region, err := searcher.Search(ip) region, err := searcher.SearchByStr(line)
if err != nil { if err != nil {
fmt.Printf("\x1b[0;31merr:%s\x1b[0m\n", err.Error()) fmt.Printf("\x1b[0;31merr:%s\x1b[0m\n", err.Error())
} else { } else {
@ -141,7 +135,7 @@ func testBench() {
return return
} }
searcher, err := ip2region.New(dbPath) searcher, err := xdb.New(dbPath)
defer func() { defer func() {
searcher.Close() searcher.Close()
}() }()
@ -163,13 +157,13 @@ func testBench() {
return return
} }
sip, err := ip2region.CheckIP(ps[0]) sip, err := xdb.CheckIP(ps[0])
if err != nil { if err != nil {
fmt.Printf("check start ip `%s`: %s\n", ps[0], err) fmt.Printf("check start ip `%s`: %s\n", ps[0], err)
return return
} }
eip, err := ip2region.CheckIP(ps[1]) eip, err := xdb.CheckIP(ps[1])
if err != nil { if err != nil {
fmt.Printf("check end ip `%s`: %s\n", ps[1], err) fmt.Printf("check end ip `%s`: %s\n", ps[1], err)
return return
@ -180,17 +174,17 @@ func testBench() {
return return
} }
mip := ip2region.MidIP(sip, eip) mip := xdb.MidIP(sip, eip)
for _, ip := range []uint32{sip, ip2region.MidIP(sip, mip), mip, ip2region.MidIP(mip, eip), eip} { for _, ip := range []uint32{sip, xdb.MidIP(sip, mip), mip, xdb.MidIP(mip, eip), eip} {
region, err := searcher.Search(ip) region, err := searcher.Search(ip)
if err != nil { if err != nil {
fmt.Printf("failed to search ip '%s': %s\n", ip2region.Long2IP(ip), err) fmt.Printf("failed to search ip '%s': %s\n", xdb.Long2IP(ip), err)
return return
} }
// check the region info // check the region info
if region != ps[2] { if region != ps[2] {
fmt.Printf("failed Search(%s) with (%s != %s)\n", ip2region.Long2IP(ip), region, ps[2]) fmt.Printf("failed Search(%s) with (%s != %s)\n", xdb.Long2IP(ip), region, ps[2])
return return
} }

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a Apache2.0-style // Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package ip2region package xdb
import ( import (
"encoding/binary" "encoding/binary"

View File

@ -6,7 +6,7 @@
// ip2region database v2.0 searcher. // ip2region database v2.0 searcher.
// @Note this is a Not thread safe implementation. // @Note this is a Not thread safe implementation.
package ip2region package xdb
import ( import (
"encoding/binary" "encoding/binary"
@ -100,7 +100,17 @@ func (s *Searcher) ClearVectorIndex() {
s.vectorIndex = nil s.vectorIndex = nil
} }
// Search find the region for the specified ip address // SearchByStr find the region for the specified ip string
func (s *Searcher) SearchByStr(str string) (string, error) {
ip, err := CheckIP(str)
if err != nil {
return "", err
}
return s.Search(ip)
}
// Search find the region for the specified long ip
func (s *Searcher) Search(ip uint32) (string, error) { func (s *Searcher) Search(ip uint32) (string, error) {
// locate the segment index block based on the vector index // locate the segment index block based on the vector index
var vIndex *VectorIndexBlock var vIndex *VectorIndexBlock

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a Apache2.0-style // Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package ip2region package xdb
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package ip2region package xdb
import ( import (
"encoding/binary" "encoding/binary"