-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
47 lines (40 loc) · 909 Bytes
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package shrinkmap
// BatchOperations provides batch operation capabilities
type BatchOperations[K comparable, V any] struct {
Operations []BatchOperation[K, V]
}
type BatchOperation[K comparable, V any] struct {
Type BatchOpType
Key K
Value V
}
type BatchOpType int
const (
BatchSet BatchOpType = iota
BatchDelete
)
// ApplyBatch applies multiple operations atomically
func (sm *ShrinkableMap[K, V]) ApplyBatch(batch BatchOperations[K, V]) error {
sm.mu.Lock()
defer sm.mu.Unlock()
for _, op := range batch.Operations {
switch op.Type {
case BatchSet:
_, exists := sm.data[op.Key]
sm.data[op.Key] = op.Value
if !exists {
sm.itemCount.Add(1)
sm.updateMetrics(1)
}
case BatchDelete:
if _, exists := sm.data[op.Key]; exists {
delete(sm.data, op.Key)
sm.deletedCount.Add(1)
}
}
}
if sm.config.AutoShrinkEnabled {
go sm.TryShrink()
}
return nil
}