From e6a216e71bf282e345181d7d7b50abeea35aa46d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Sep 2022 14:03:03 -0600 Subject: [PATCH 1/2] Map.Store: Fix repeated words in the doc comment Signed-off-by: Luke Shumaker --- map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/map.go b/map.go index 7ea09d4..c908a41 100644 --- a/map.go +++ b/map.go @@ -111,7 +111,7 @@ func (tm *Map[K, V]) Load(key K) (value V, ok bool) { return DeepCopy(ret), true } -// Store sets a key sets the value for a key. This panics if .Close() has already been called. +// Store sets the value for a key. This panics if .Close() has already been called. func (tm *Map[K, V]) Store(key K, val V) { tm.lock.Lock() defer tm.lock.Unlock() From ac2eba0a697d0f905abf52c132e7440719f31821 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Sep 2022 14:07:11 -0600 Subject: [PATCH 2/2] Map: Add .StoreMultiple and .StoreComplete methods Signed-off-by: Luke Shumaker --- map.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/map.go b/map.go index c908a41..fae3618 100644 --- a/map.go +++ b/map.go @@ -119,6 +119,44 @@ func (tm *Map[K, V]) Store(key K, val V) { tm.unlockedStore(key, val) } +// StoreMultiple sets multiple entries in the map "at once". +// +// This is particularly useful when first initializing the Map, so that a half-initialized state +// cannot be observed. The ordering of the .Updates in the resulting Snapshot is undefined. This +// panics if .Close() has already been called. +// +// Use StoreComplete if you wish for values not in the argument to be deleted. +func (tm *Map[K, V]) StoreMultiple(kvs map[K]V) { + tm.lock.Lock() + defer tm.lock.Unlock() + + for k, v := range kvs { + tm.unlockedStore(k, v) + } +} + +// StoreComplete completely replaces the map "at once"; storing or deleting entries from the map as +// necessary to have it match the argument. +// +// This is particularly useful when first initializing the Map, so that a half-initialized state +// cannot be observed. The ordering of the .Updates in the resulting Snapshot is undefined. This +// panics if .Close() has already been called. +// +// Use StoreMultiple if you do not wish for values not n the argument to be deleted. +func (tm *Map[K, V]) StoreComplete(kvs map[K]V) { + tm.lock.Lock() + defer tm.lock.Unlock() + + for k, v := range kvs { + tm.unlockedStore(k, v) + } + for k := range tm.value { + if _, keep := kvs[k]; !keep { + tm.unlockedDelete(k) + } + } +} + // LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns // the given value. The 'loaded' result is true if the value was loaded, false if stored. //