Keys returns a list of all keys in the sharded map.
()
| 88 | |
| 89 | // Keys returns a list of all keys in the sharded map. |
| 90 | func (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 | } |