Commit bdf8fe34 authored by protolambda's avatar protolambda Committed by GitHub

op-service: add Delete to RWMap (#13407)

parent 39877f60
...@@ -41,6 +41,12 @@ func (m *RWMap[K, V]) Len() int { ...@@ -41,6 +41,12 @@ func (m *RWMap[K, V]) Len() int {
return len(m.inner) return len(m.inner)
} }
func (m *RWMap[K, V]) Delete(key K) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.inner, key)
}
// Range calls f sequentially for each key and value present in the map. // Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration. // If f returns false, range stops the iteration.
func (m *RWMap[K, V]) Range(f func(key K, value V) bool) { func (m *RWMap[K, V]) Range(f func(key K, value V) bool) {
......
...@@ -49,4 +49,15 @@ func TestRWMap(t *testing.T) { ...@@ -49,4 +49,15 @@ func TestRWMap(t *testing.T) {
return false return false
}) })
require.Len(t, got, 1, "stop early") require.Len(t, got, 1, "stop early")
// remove a value
require.True(t, m.Has(10))
m.Delete(10)
require.False(t, m.Has(10))
// and add it back, sanity check
m.Set(10, 123)
require.True(t, m.Has(10))
// remove a non-existent value
m.Delete(132983213)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment