Atlas › Test

TestGetAndDo

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

Package
go.temporal.io/server/common/collection
Suite / test hierarchy
TestConcurrentTxMapSuite/TestGetAndDo
Test
TestGetAndDo
Introduced at
TestGetAndDo Frontier kind: Test frontier
Covered ranges
11
Covered lines
44
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 37 covered LOC · 7 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
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
125 // GetAndDo returns the value corresponding to the key, and apply fn to key value before return value
126 // return (value, value exist or not, error when evaluation fn)
127 > func (cmap *ShardedConcurrentTxMap) GetAndDo(key any, fn ActionFunc) (any, bool, error) { concurrent_tx_map.go
128 > shard := cmap.getShard(key)
129 > var value any
130 > var ok bool
131 > var err error
132 > shard.Lock()
133 > if shard.items != nil {
134 > value, ok = shard.items[key] concurrent_tx_map.go
135 > if ok {
136 > err = fn(key, value)
137 > }
138 }
139 > shard.Unlock() concurrent_tx_map.go
140 > return value, ok, err
141 }
142
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 }