MCPcopy Create free account
hub / github.com/cloud-native-go/examples / Keys

Method Keys

ch04/sharding.go:90–115  ·  view source on GitHub ↗

Keys returns a list of all keys in the sharded map.

()

Source from the content-addressed store, hash-verified

88
89// Keys returns a list of all keys in the sharded map.
90func (m ShardedMap[K, V]) Keys() []K {
91 var keys []K // Declare an empty keys slice
92 var mutex sync.Mutex // Mutex for write safety to keys
93
94 var wg sync.WaitGroup // Create a wait group and add a
95 wg.Add(len(m)) // wait value for each slice
96
97 for _, shard := range m { // Run a goroutine for each slice in m
98 go func(s *Shard[K, V]) {
99 s.RLock() // Establish a read lock on s
100
101 defer wg.Done() // Release of the read lock
102 defer s.RUnlock() // Tell the WaitGroup it's done
103
104 for key, _ := range s.items { // Get the slice's keys
105 mutex.Lock()
106 keys = append(keys, key)
107 mutex.Unlock()
108 }
109 }(shard)
110 }
111
112 wg.Wait() // Block until all goroutines are done
113
114 return keys // Return combined keys slice
115}

Callers 2

TestShardingKeysFunction · 0.80
TestShardingDeleteFunction · 0.80

Calls 1

WaitMethod · 0.65

Tested by 2

TestShardingKeysFunction · 0.64
TestShardingDeleteFunction · 0.64