MCPcopy Create free account
hub / github.com/aceld/zinx / GetOrSetFunc

Method GetOrSetFunc

zutils/shard_lock_map.go:190–195  ·  view source on GitHub ↗

GetOrSetFunc gets the value for the given key, or sets it using the provided function if it doesn't exist. The function f is called outside the lock to avoid deadlocks. Returns the value and whether it was set (true) or already existed (false).

(key string, f func(key string) interface{})

Source from the content-addressed store, hash-verified

188// The function f is called outside the lock to avoid deadlocks.
189// Returns the value and whether it was set (true) or already existed (false).
190func (slm ShardLockMaps) GetOrSetFunc(key string, f func(key string) interface{}) (interface{}, bool) {
191 if v, ok := slm.Get(key); ok {
192 return v, false
193 }
194 return slm.doSetWithLockCheck(key, f(key))
195}
196
197// GetOrSetFuncLock gets the value for the given key, or sets it using the provided function if it doesn't exist.
198// The function f is called inside the lock for atomic operations.

Callers 1

TestGetOrSetFuncFunction · 0.95

Calls 2

GetMethod · 0.95
doSetWithLockCheckMethod · 0.95

Tested by 1

TestGetOrSetFuncFunction · 0.76