From 5b10989ac21087bf6c319f719b85af5b7522293f Mon Sep 17 00:00:00 2001 From: lionsoul2014 Date: Wed, 29 Apr 2026 13:44:14 +0800 Subject: [PATCH] Thread-safe impl for CacheRegion --- maker/golang/xdb/editor.go | 6 +++--- maker/golang/xdb/region.go | 35 ++++++++++++++++++++++------------- maker/golang/xdb/segment.go | 2 +- maker/golang/xdb/util.go | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/maker/golang/xdb/editor.go b/maker/golang/xdb/editor.go index 7b15ee3..4ffbbb2 100644 --- a/maker/golang/xdb/editor.go +++ b/maker/golang/xdb/editor.go @@ -108,7 +108,7 @@ func (e *Editor) loadSegments() error { e.segments.PushBack(&Segment{ StartIP: e.verison.Min, EndIP: IPSubOne(seg.StartIP), - Region: REmpty(), + Region: EmptyRegion, }) } } else if err := seg.RightBehind(last); err == nil { @@ -121,7 +121,7 @@ func (e *Editor) loadSegments() error { e.segments.PushBack(&Segment{ StartIP: IPAddOne(last.EndIP), EndIP: IPSubOne(seg.StartIP), - Region: REmpty(), + Region: EmptyRegion, }) } @@ -138,7 +138,7 @@ func (e *Editor) loadSegments() error { e.segments.PushBack(&Segment{ StartIP: IPAddOne(back.Value.(*Segment).EndIP), EndIP: e.verison.Max, - Region: REmpty(), + Region: EmptyRegion, }) } } diff --git a/maker/golang/xdb/region.go b/maker/golang/xdb/region.go index 874a26f..e011ea4 100644 --- a/maker/golang/xdb/region.go +++ b/maker/golang/xdb/region.go @@ -3,31 +3,32 @@ package xdb import ( "fmt" "strings" + "sync" ) // region manager with: // 1, content cache. // 2, util functions +// global cache map +var rcLock sync.Mutex +var regionCache = map[string]*Region{} + type Region struct { Str string // region string fields []string // region fields } -// global cache map -var regionCache = map[string]*Region{} +var EmptyRegion = CacheRegion("") -func RNew(str string) *Region { - return NewRegion(str) -} - -func REmpty() *Region { - return NewRegion("") -} - -func NewRegion(str string) *Region { +// Create or get the region from the global cache. +// And it is a thread-safe implementation. +func CacheRegion(str string) *Region { // check the cache and return it directly // if there is a cache available + rcLock.Lock() + defer rcLock.Unlock() + region, ok := regionCache[str] if ok { return region @@ -43,6 +44,14 @@ func NewRegion(str string) *Region { return region } +// Create a new region without checking cache info +func NewRegion(str string) *Region { + return &Region{ + Str: str, + fields: nil, + } +} + func (r *Region) Fields() []string { if r.fields == nil { r.fields = strings.Split(r.Str, "|") @@ -51,7 +60,7 @@ func (r *Region) Fields() []string { return r.fields } -func (r *Region) JoinBy(sep string) string { +func (r *Region) Join(sep string) string { if sep == "|" { return r.Str } @@ -78,7 +87,7 @@ func (r *Region) Filtering(fields []int) (*Region, error) { sb = append(sb, fs[idx]) } - new := RNew(strings.Join(sb, "|")) + new := CacheRegion(strings.Join(sb, "|")) if new.fields == nil { new.fields = sb } diff --git a/maker/golang/xdb/segment.go b/maker/golang/xdb/segment.go index e123035..179cc88 100644 --- a/maker/golang/xdb/segment.go +++ b/maker/golang/xdb/segment.go @@ -38,7 +38,7 @@ func SegmentFrom(seg string) (*Segment, error) { return &Segment{ StartIP: sip, EndIP: eip, - Region: RNew(ps[2]), + Region: CacheRegion(ps[2]), }, nil } diff --git a/maker/golang/xdb/util.go b/maker/golang/xdb/util.go index 9c3eaf4..6e8fb58 100644 --- a/maker/golang/xdb/util.go +++ b/maker/golang/xdb/util.go @@ -200,7 +200,7 @@ func IterateSegments(handle *os.File, autoMerge bool, before func(l string), fil var seg = &Segment{ StartIP: sip, EndIP: eip, - Region: RNew(region), + Region: CacheRegion(region), } // check and automatic merging the Consecutive Segments, which means: