Skip to content

Commit

Permalink
Map: Add .StoreMultiple and .StoreComplete methods
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Shumaker <[email protected]>
  • Loading branch information
LukeShu committed Sep 28, 2022
1 parent e6a216e commit ac2eba0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down

0 comments on commit ac2eba0

Please sign in to comment.