From 99119940a8f236bd24c6900c72913595182de894 Mon Sep 17 00:00:00 2001 From: king Date: Fri, 5 Sep 2025 11:49:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=B9=B6=E5=8F=91=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E6=97=B6=E7=9A=84=E6=95=B0=E6=8D=AE=E7=AB=9E=E4=BA=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/golang/xdb/searcher.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/binding/golang/xdb/searcher.go b/binding/golang/xdb/searcher.go index b5788e7..5111861 100644 --- a/binding/golang/xdb/searcher.go +++ b/binding/golang/xdb/searcher.go @@ -15,6 +15,7 @@ import ( "encoding/binary" "fmt" "os" + "sync/atomic" ) const ( @@ -77,7 +78,7 @@ type Searcher struct { // header info header *Header - ioCount int + ioCount int32 // use it only when this feature enabled. // Preload the vector index will reduce the number of IO operations @@ -135,7 +136,7 @@ func (s *Searcher) Close() { // GetIOCount return the global io count for the last search func (s *Searcher) GetIOCount() int { - return s.ioCount + return int(atomic.LoadInt32(&s.ioCount)) } // SearchByStr find the region for the specified ip string @@ -151,7 +152,7 @@ func (s *Searcher) SearchByStr(str string) (string, error) { // Search find the region for the specified long ip func (s *Searcher) Search(ip uint32) (string, error) { // reset the global ioCount - s.ioCount = 0 + atomic.StoreInt32(&s.ioCount, 0) // locate the segment index block based on the vector index var il0 = (ip >> 24) & 0xFF @@ -236,7 +237,7 @@ func (s *Searcher) read(offset int64, buff []byte) error { return fmt.Errorf("seek to %d: %w", offset, err) } - s.ioCount++ + atomic.AddInt32(&s.ioCount, 1) rLen, err := s.handle.Read(buff) if err != nil { return fmt.Errorf("handle read: %w", err)