-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard.go
61 lines (53 loc) · 1.48 KB
/
shard.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"errors"
"sync"
)
/*
The sync.Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
*/
type shard struct {
s sync.Map
muxS sync.Mutex
}
func NewShard() *shard {
return &shard{
s: sync.Map{},
muxS: sync.Mutex{},
}
}
// Check if a key exists in the shard
func (s *shard) isPresent(key string) bool {
var found bool
s.s.Range(func(k, v any) bool {
if k == key {
found = true
return false // Stop iterating
}
return true // Continue iterating
})
return found
}
func (s *shard) loadValue(key string) (any) {
value, _ := s.s.Load(key)
// returns value if present, nil if not
return value
}
func (s *shard) storeValue(key string, value any) (any, error) {
actual, ok := s.s.LoadOrStore(key, value)
// if key-value pair already exists. Load the value associated with key.
if ok {
return actual, errors.New("value already exists for this key")
}
// Otherwise, we store the new value-pair and returns the value.
return actual, nil
}
func (s *shard) deleteValue(key string) error {
_ , ok := s.s.LoadAndDelete(key)
// If the key doesn't exist
if !ok {
return errors.New("key not found")
}
// Otherwise
return nil
}