refactor: sequential range over safemap (#2316)

This commit is contained in:
Kevin Wan
2022-08-28 17:16:31 +08:00
committed by GitHub
parent 15a2802f12
commit 813625d995
2 changed files with 27 additions and 37 deletions

View File

@@ -68,6 +68,24 @@ func (m *SafeMap) Get(key interface{}) (interface{}, bool) {
return val, ok
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (m *SafeMap) Range(f func(key, val interface{}) bool) {
m.lock.RLock()
defer m.lock.RUnlock()
for k, v := range m.dirtyOld {
if !f(k, v) {
return
}
}
for k, v := range m.dirtyNew {
if !f(k, v) {
return
}
}
}
// Set sets the value into m with the given key.
func (m *SafeMap) Set(key, value interface{}) {
m.lock.Lock()
@@ -94,31 +112,3 @@ func (m *SafeMap) Size() int {
m.lock.RUnlock()
return size
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
// Range m .
func (m *SafeMap) Range(f func(key, value interface{}) bool) {
m.lock.RLock()
defer m.lock.RUnlock()
var wg sync.WaitGroup
wg.Add(2)
go func() {
for k, v := range m.dirtyOld {
if !f(k, v) {
break
}
}
wg.Done()
}()
go func() {
for k, v := range m.dirtyNew {
if !f(k, v) {
break
}
}
wg.Done()
}()
wg.Wait()
}