From ba4b91ba5eee75ae4e871d6836e69ab2ab687775 Mon Sep 17 00:00:00 2001 From: Lisandro Pin Date: Thu, 5 Jun 2025 15:30:39 +0200 Subject: [PATCH] Clean up type for `CompactMap` chunk IDs. --- weed/storage/needle_map/compact_map.go | 26 ++++++++++++--------- weed/storage/needle_map/compact_map_test.go | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/weed/storage/needle_map/compact_map.go b/weed/storage/needle_map/compact_map.go index e5baff1e2..d6c9fe296 100644 --- a/weed/storage/needle_map/compact_map.go +++ b/weed/storage/needle_map/compact_map.go @@ -3,6 +3,7 @@ package needle_map import ( "fmt" "math" + "slices" "sort" "sync" @@ -22,9 +23,10 @@ type CompactNeedleValue struct { size types.Size } +type Chunk uint64 type CompactMapSegment struct { list []CompactNeedleValue - chunk int + chunk Chunk firstKey CompactKey lastKey CompactKey } @@ -32,10 +34,10 @@ type CompactMapSegment struct { type CompactMap struct { sync.RWMutex - segments map[int]*CompactMapSegment + segments map[Chunk]*CompactMapSegment } -func (ck CompactKey) Key(chunk int) types.NeedleId { +func (ck CompactKey) Key(chunk Chunk) types.NeedleId { return (types.NeedleId(SegmentChunkSize) * types.NeedleId(chunk)) + types.NeedleId(ck) } @@ -49,16 +51,15 @@ func (co CompactOffset) Offset() types.Offset { return types.BytesToOffset(co[:]) } -func (cnv CompactNeedleValue) NeedleValue(chunk int) NeedleValue { - key := cnv.key.Key(chunk) +func (cnv CompactNeedleValue) NeedleValue(chunk Chunk) NeedleValue { return NeedleValue{ - Key: key, + Key: cnv.key.Key(chunk), Offset: cnv.offset.Offset(), Size: cnv.size, } } -func newCompactMapSegment(chunk int) *CompactMapSegment { +func newCompactMapSegment(chunk Chunk) *CompactMapSegment { return &CompactMapSegment{ list: []CompactNeedleValue{}, chunk: chunk, @@ -176,7 +177,7 @@ func (cs *CompactMapSegment) delete(key types.NeedleId) types.Size { func NewCompactMap() *CompactMap { return &CompactMap{ - segments: map[int]*CompactMapSegment{}, + segments: map[Chunk]*CompactMapSegment{}, } } @@ -197,6 +198,9 @@ func (cm *CompactMap) Cap() int { } func (cm *CompactMap) String() string { + if cm.Len() == 0 { + return "empty" + } return fmt.Sprintf( "%d/%d elements on %d segments, %.02f%% efficiency", cm.Len(), cm.Cap(), len(cm.segments), @@ -204,7 +208,7 @@ func (cm *CompactMap) String() string { } func (cm *CompactMap) segmentForKey(key types.NeedleId) *CompactMapSegment { - chunk := int(key / SegmentChunkSize) + chunk := Chunk(key / SegmentChunkSize) if cs, ok := cm.segments[chunk]; ok { return cs } @@ -251,11 +255,11 @@ func (cm *CompactMap) AscendingVisit(visit func(NeedleValue) error) error { cm.RLock() defer cm.RUnlock() - chunks := []int{} + chunks := []Chunk{} for c := range cm.segments { chunks = append(chunks, c) } - sort.Ints(chunks) + slices.Sort(chunks) for _, c := range chunks { cs := cm.segments[c] diff --git a/weed/storage/needle_map/compact_map_test.go b/weed/storage/needle_map/compact_map_test.go index 969808531..ae3a43353 100644 --- a/weed/storage/needle_map/compact_map_test.go +++ b/weed/storage/needle_map/compact_map_test.go @@ -381,7 +381,7 @@ func TestSegmentForKey(t *testing.T) { } wantMap := &CompactMap{ - segments: map[int]*CompactMapSegment{ + segments: map[Chunk]*CompactMapSegment{ 0: &CompactMapSegment{ list: []CompactNeedleValue{}, chunk: 0, @@ -438,7 +438,7 @@ func TestAscendingVisit(t *testing.T) { } } -func TestOrdering(t *testing.T) { +func TestRandomInsert(t *testing.T) { count := 8 * SegmentChunkSize keys := []types.NeedleId{} for i := 0; i < count; i++ {