Returns a array of channels that contains elements in each shard, which likely takes a snapshot of `m`. It returns once the size of each buffered channel is determined, before all the channels are populated using goroutines. 返回值是SHARD_COUNT个缓冲channel
(m ConcurrentHashMap)
| 214 | // before all the channels are populated using goroutines. |
| 215 | // 返回值是SHARD_COUNT个缓冲channel |
| 216 | func snapshot(m ConcurrentHashMap) (chans []chan Tuple) { |
| 217 | chans = make([]chan Tuple, SHARD_COUNT) |
| 218 | wg := sync.WaitGroup{} |
| 219 | wg.Add(SHARD_COUNT) |
| 220 | // Foreach shard. |
| 221 | for index, shard := range m.ThreadSafeHashMap { |
| 222 | go func(index int, shard *ConcurrentMapShared) { |
| 223 | // Foreach key, value pair. |
| 224 | shard.RLock() |
| 225 | chans[index] = make(chan Tuple, len(shard.items)) |
| 226 | wg.Done() |
| 227 | for key, val := range shard.items { |
| 228 | chans[index] <- Tuple{key, val} |
| 229 | } |
| 230 | shard.RUnlock() |
| 231 | close(chans[index]) |
| 232 | }(index, shard) |
| 233 | } |
| 234 | wg.Wait() |
| 235 | return chans |
| 236 | } |
| 237 | |
| 238 | // fanIn reads elements from channels `chans` into channel `out` |
| 239 | func fanIn(chans []chan Tuple, out chan Tuple) { |
no outgoing calls
no test coverage detected