解决并发访问时的数据竞争问题
This commit is contained in:
parent
4996c0ff6a
commit
99119940a8
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -77,7 +78,7 @@ type Searcher struct {
|
||||||
|
|
||||||
// header info
|
// header info
|
||||||
header *Header
|
header *Header
|
||||||
ioCount int
|
ioCount int32
|
||||||
|
|
||||||
// use it only when this feature enabled.
|
// use it only when this feature enabled.
|
||||||
// Preload the vector index will reduce the number of IO operations
|
// 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
|
// GetIOCount return the global io count for the last search
|
||||||
func (s *Searcher) GetIOCount() int {
|
func (s *Searcher) GetIOCount() int {
|
||||||
return s.ioCount
|
return int(atomic.LoadInt32(&s.ioCount))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchByStr find the region for the specified ip string
|
// 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
|
// 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) {
|
||||||
// reset the global ioCount
|
// reset the global ioCount
|
||||||
s.ioCount = 0
|
atomic.StoreInt32(&s.ioCount, 0)
|
||||||
|
|
||||||
// locate the segment index block based on the vector index
|
// locate the segment index block based on the vector index
|
||||||
var il0 = (ip >> 24) & 0xFF
|
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)
|
return fmt.Errorf("seek to %d: %w", offset, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.ioCount++
|
atomic.AddInt32(&s.ioCount, 1)
|
||||||
rLen, err := s.handle.Read(buff)
|
rLen, err := s.handle.Read(buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("handle read: %w", err)
|
return fmt.Errorf("handle read: %w", err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue