diff options
| author | 2025-01-27 15:54:51 +0000 | |
|---|---|---|
| committer | 2025-01-27 15:54:51 +0000 | |
| commit | 3617e27afa181392763258240bc276f3da4b44e2 (patch) | |
| tree | 9a65f9a361fd9ddf53de7d32ca35c81aade6cb84 /vendor/github.com/puzpuzpuz/xsync/v3/map.go | |
| parent | [feature/frontend] Add login button to index page which reiterates info about... (diff) | |
| download | gotosocial-3617e27afa181392763258240bc276f3da4b44e2.tar.xz | |
bumps uptrace/bun deps to v1.2.8 (#3698)
Diffstat (limited to 'vendor/github.com/puzpuzpuz/xsync/v3/map.go')
| -rw-r--r-- | vendor/github.com/puzpuzpuz/xsync/v3/map.go | 53 |
1 files changed, 48 insertions, 5 deletions
diff --git a/vendor/github.com/puzpuzpuz/xsync/v3/map.go b/vendor/github.com/puzpuzpuz/xsync/v3/map.go index 6c5b6ebd6..a2730c3f1 100644 --- a/vendor/github.com/puzpuzpuz/xsync/v3/map.go +++ b/vendor/github.com/puzpuzpuz/xsync/v3/map.go @@ -200,6 +200,21 @@ func newMapTable(minTableLen int) *mapTable { return t } +// ToPlainMap returns a native map with a copy of xsync Map's +// contents. The copied xsync Map should not be modified while +// this call is made. If the copied Map is modified, the copying +// behavior is the same as in the Range method. +func ToPlainMap(m *Map) map[string]interface{} { + pm := make(map[string]interface{}) + if m != nil { + m.Range(func(key string, value interface{}) bool { + pm[key] = value + return true + }) + } + return pm +} + // Load returns the value stored in the map for a key, or nil if no // value is present. // The ok result indicates whether value was found in the map. @@ -279,6 +294,34 @@ func (m *Map) LoadAndStore(key string, value interface{}) (actual interface{}, l ) } +// LoadOrTryCompute returns the existing value for the key if present. +// Otherwise, it tries to compute the value using the provided function +// and, if success, returns the computed value. The loaded result is true +// if the value was loaded, false if stored. If the compute attempt was +// cancelled, a nil will be returned. +// +// This call locks a hash table bucket while the compute function +// is executed. It means that modifications on other entries in +// the bucket will be blocked until the valueFn executes. Consider +// this when the function includes long-running operations. +func (m *Map) LoadOrTryCompute( + key string, + valueFn func() (newValue interface{}, cancel bool), +) (value interface{}, loaded bool) { + return m.doCompute( + key, + func(interface{}, bool) (interface{}, bool) { + nv, c := valueFn() + if !c { + return nv, false + } + return nil, true + }, + true, + false, + ) +} + // LoadOrCompute returns the existing value for the key if present. // Otherwise, it computes the value using the provided function and // returns the computed value. The loaded result is true if the value @@ -447,11 +490,11 @@ func (m *Map) doCompute( if b.next == nil { if emptyb != nil { // Insertion into an existing bucket. - var zeroedV interface{} - newValue, del := valueFn(zeroedV, false) + var zeroV interface{} + newValue, del := valueFn(zeroV, false) if del { unlockBucket(&rootb.topHashMutex) - return zeroedV, false + return zeroV, false } // First we update the value, then the key. // This is important for atomic snapshot states. @@ -471,8 +514,8 @@ func (m *Map) doCompute( goto compute_attempt } // Insertion into a new bucket. - var zeroedV interface{} - newValue, del := valueFn(zeroedV, false) + var zeroV interface{} + newValue, del := valueFn(zeroV, false) if del { unlockBucket(&rootb.topHashMutex) return newValue, false |
