Atlas › Test

TestGetAfterPut

Exact test identity: go.temporal.io/server/common/collection/TestConcurrentTxMapSuite/TestGetAfterPut

Package
go.temporal.io/server/common/collection
Suite / test hierarchy
TestConcurrentTxMapSuite/TestGetAfterPut
Test
TestGetAfterPut
Introduced at
concurrent_tx_map.go ×6 Frontier kind: Joint frontier
Covered ranges
17
Covered lines
76
Covered files
2

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/collection/concurrent_tx_map.go 69 covered LOC · 13 ranges

Open complete file

55 //
56 // The hash function to use for sharding
57 > func NewShardedConcurrentTxMap(initialCap int, hashfn HashFunc) ConcurrentTxMap { concurrent_tx_map.go
58 > cmap := new(ShardedConcurrentTxMap)
59 > cmap.hashfn = hashfn
60 > cmap.initialCap = max(nShards, initialCap/nShards)
61 > return cmap
62 > }
63
64 // Get returns the value corresponding to the key, if it exist
65 > func (cmap *ShardedConcurrentTxMap) Get(key any) (any, bool) { concurrent_tx_map.go
66 > shard := cmap.getShard(key)
67 > var ok bool
68 > var value any
69 > shard.RLock()
70 > if shard.items != nil {
71 > value, ok = shard.items[key]
72 > }
73 > shard.RUnlock()
74 > return value, ok
75 }
76
82
83 // Put records the given key value mapping. Overwrites previous values
84 > func (cmap *ShardedConcurrentTxMap) Put(key any, value any) { concurrent_tx_map.go
85 > shard := cmap.getShard(key)
86 > shard.Lock()
87 > cmap.lazyInitShard(shard)
88 > _, ok := shard.items[key]
89 > if !ok {
90 > atomic.AddInt32(&cmap.size, 1)
91 > }
92 > shard.items[key] = value
93 > shard.Unlock()
94 }
95
111
112 // Remove deletes the given key from the map
113 > func (cmap *ShardedConcurrentTxMap) Remove(key any) { concurrent_tx_map.go
114 > shard := cmap.getShard(key)
115 > shard.Lock()
116 > cmap.lazyInitShard(shard)
117 > _, ok := shard.items[key]
118 > if ok {
119 > delete(shard.items, key)
120 > atomic.AddInt32(&cmap.size, -1)
121 > }
122 > shard.Unlock()
123 }
124
178
179 // Close closes the iterator
180 > func (it *mapIteratorImpl) Close() { concurrent_tx_map.go
181 > close(it.stopCh)
182 > }
183
184 // Entries returns a channel of map entries
185 > func (it *mapIteratorImpl) Entries() <-chan *MapEntry { concurrent_tx_map.go
186 > return it.dataCh
187 > }
188
189 // Iter returns an iterator to the map. This map
190 // does not use re-entrant locks, so access or modification
191 // to the map during iteration can cause a dead lock.
192 > func (cmap *ShardedConcurrentTxMap) Iter() MapIterator { concurrent_tx_map.go
193 >
194 > iterator := new(mapIteratorImpl)
195 > iterator.dataCh = make(chan *MapEntry, 8)
196 > iterator.stopCh = make(chan struct{})
197 >
198 > go func(iterator *mapIteratorImpl) {
199 > for i := range nShards {
200 > cmap.shards[i].RLock()
201 > for k, v := range cmap.shards[i].items {
202 > entry := &MapEntry{Key: k, Value: v}
203 > select {
204 > case iterator.dataCh <- entry:
205 case <-iterator.stopCh:
206 cmap.shards[i].RUnlock()
209 }
210 }
211 > cmap.shards[i].RUnlock() concurrent_tx_map.go
212 }
213 > close(iterator.dataCh) concurrent_tx_map.go
214 }(iterator)
215
216 > return iterator concurrent_tx_map.go
217 }
218
219 // Len returns the number of items in the map
220 > func (cmap *ShardedConcurrentTxMap) Len() int { concurrent_tx_map.go
221 > return int(atomic.LoadInt32(&cmap.size))
222 > }
223
224 > func (cmap *ShardedConcurrentTxMap) getShard(key any) *mapShard { concurrent_tx_map.go
225 > shardIdx := cmap.hashfn(key) % nShards
226 > return &cmap.shards[shardIdx]
227 > }
228
229 > func (cmap *ShardedConcurrentTxMap) lazyInitShard(shard *mapShard) { concurrent_tx_map.go
230 > if shard.items == nil {
231 > shard.items = make(map[any]any, cmap.initialCap)
232 > }
233 }
go.temporal.io/server/common/collection/util.go 7 covered LOC · 4 ranges

Open complete file

9 // if the uuid is malformed, then the hash function always
10 // returns 0 as the hash value
11 > func UUIDHashCode(input any) uint32 { util.go
12 > key, ok := input.(string)
13 > if !ok {
14 return 0
15 }
16 > if len(key) != UUIDStringLength { util.go
17 return 0
18 }
19 // Use the first 4 bytes of the uuid as the hash
20 > b, err := hex.DecodeString(key[:8]) util.go
21 > if err != nil {
22 return 0
23 }
24 > return binary.BigEndian.Uint32(b) util.go
25 }