MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / Range

Method Range

pkg/generic_sync/map.go:318–349  ·  view source on GitHub ↗

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. Range does not necessarily correspond to any consistent snapshot of the MapOf's contents: no key will be visited more than once, but if the value for any key is stored or deleted con

(f func(key K, value V) bool)

Source from the content-addressed store, hash-verified

316// Range may be O(N) with the number of elements in the map even if f returns
317// false after a constant number of calls.
318func (m *MapOf[K, V]) Range(f func(key K, value V) bool) {
319 // We need to be able to iterate over all of the keys that were already
320 // present at the start of the call to Range.
321 // If read.amended is false, then read.m satisfies that property without
322 // requiring us to hold m.mu for a long time.
323 read, _ := m.read.Load().(readOnly[K, V])
324 if read.amended {
325 // m.dirty contains keys not in read.m. Fortunately, Range is already O(N)
326 // (assuming the caller does not break out early), so a call to Range
327 // amortizes an entire copy of the map: we can promote the dirty copy
328 // immediately!
329 m.mu.Lock()
330 read, _ = m.read.Load().(readOnly[K, V])
331 if read.amended {
332 read = readOnly[K, V]{m: m.dirty}
333 m.read.Store(read)
334 m.dirty = nil
335 m.misses = 0
336 }
337 m.mu.Unlock()
338 }
339
340 for k, e := range read.m {
341 v, ok := e.load()
342 if !ok {
343 continue
344 }
345 if !f(k, v) {
346 break
347 }
348 }
349}
350
351// Values returns a slice of the values in the map.
352func (m *MapOf[K, V]) Values() []V {

Callers 9

ValuesMethod · 0.95
ToMapMethod · 0.95
ClearMethod · 0.95
ListConfigsMethod · 0.80
GetByStatesMethod · 0.80
TestConcurrentRangeFunction · 0.80
getStoragesByPathFunction · 0.80
getReaderAtOffsetMethod · 0.80
GetObjectMethod · 0.80

Calls 4

loadMethod · 0.80
UnlockMethod · 0.65
LoadMethod · 0.45
StoreMethod · 0.45

Tested by 1

TestConcurrentRangeFunction · 0.64