Skip to content

Commit

Permalink
fix:change file will not notify to client (#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Sep 18, 2023
1 parent d4e54ff commit 6a55daa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
23 changes: 18 additions & 5 deletions common/utils/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ func (set *SyncSet[K]) ToSlice() []K {
}

func (set *SyncSet[K]) Range(fn func(val K)) {
snapshot := map[K]struct{}{}
set.lock.RLock()
defer set.lock.RUnlock()

for k := range set.container {
snapshot[k] = struct{}{}
}
set.lock.RUnlock()

for k := range snapshot {
fn(k)
}
}
Expand Down Expand Up @@ -188,9 +192,14 @@ func (s *SegmentMap[K, V]) Range(f func(k K, v V)) {
lock := s.locks[i]
solt := s.solts[i]
func() {
snapshot := map[K]V{}
lock.RLock()
defer lock.RUnlock()
for k, v := range solt {
snapshot[k] = v
}
lock.RUnlock()

for k, v := range snapshot {
f(k, v)
}
}()
Expand Down Expand Up @@ -268,10 +277,14 @@ func (s *SyncMap[K, V]) Store(key K, val V) {

// Range
func (s *SyncMap[K, V]) Range(f func(key K, val V) bool) {
snapshot := map[K]V{}
s.lock.RLock()
defer s.lock.RUnlock()

for k, v := range s.m {
snapshot[k] = v
}
s.lock.RUnlock()

for k, v := range snapshot {
_ = f(k, v)
}
}
Expand Down
36 changes: 36 additions & 0 deletions common/utils/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,40 @@ func Test_SyncSegmentMap(t *testing.T) {
break
}
}

for {
count := 0
segmentMap.Range(func(k string, v *SegmentMap[string, string]) {
v.Range(func(k string, _ string) {
v.Del(k)
})
count++
})
if count == total {
break
}
}
}

func Test_SyncMap(t *testing.T) {
syncMap := NewSyncMap[int, int]()

for i := 0; i < 10; i++ {
syncMap.Store(i, i)
}

assert.Equal(t, 10, syncMap.Len())

syncMap.Range(func(key, val int) bool {
syncMap.Delete(key)
syncMap.Store(key+100, key+100)
return true
})

assert.Equal(t, 10, syncMap.Len())

for i := 0; i < 10; i++ {
_, ok := syncMap.Load(i)
assert.False(t, ok)
}
}

0 comments on commit 6a55daa

Please sign in to comment.