Atlas › Test

TestMapConcurrency

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

Package
go.temporal.io/server/common/collection
Suite / test hierarchy
TestConcurrentTxMapSuite/TestMapConcurrency
Test
TestMapConcurrency
Introduced at
TestMapConcurrency Frontier kind: Test frontier
Covered ranges
12
Covered lines
46
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 39 covered LOC · 8 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
96 // PutIfNotExist records the mapping, if there is no mapping for this key already
97 // Returns true if the mapping was recorded, false otherwise
98 > func (cmap *ShardedConcurrentTxMap) PutIfNotExist(key any, value any) bool { concurrent_tx_map.go
99 > shard := cmap.getShard(key)
100 > var ok bool
101 > shard.Lock()
102 > cmap.lazyInitShard(shard)
103 > _, ok = shard.items[key]
104 > if !ok {
105 > shard.items[key] = value concurrent_tx_map.go
106 > atomic.AddInt32(&cmap.size, 1)
107 > }
108 > shard.Unlock() concurrent_tx_map.go
109 > return !ok
110 }
111
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 }