-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcurrentPermanentMap.go
65 lines (51 loc) · 1.17 KB
/
ConcurrentPermanentMap.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
62
63
64
65
package util
import (
"sync"
)
// keys are strings
type ConcurrentPermanentMap struct {
mut sync.Mutex
m map[interface{}]interface{}
}
func (cpm *ConcurrentPermanentMap) NumItems() int {
return len(cpm.m)
}
func (cpm *ConcurrentPermanentMap) Put_Entry(key interface{}, value interface{}) {
cpm.mut.Lock()
defer cpm.mut.Unlock()
cpm.m[key] = value
}
func (cpm *ConcurrentPermanentMap) Get_Entry(key interface{}) (interface{}, bool) {
// 1. acquire lock
cpm.mut.Lock()
defer cpm.mut.Unlock()
// 2. check if it's in the map
map_item, ok := cpm.m[key]
return map_item, ok
}
func NewEmptyConcurrentPermanentMap() *ConcurrentPermanentMap {
return &ConcurrentPermanentMap{
mut: sync.Mutex{},
m: make(map[interface{}]interface{}),
}
}
type CPMItem struct {
Key interface{}
Value interface{}
}
func NewConcurrentPermanentMapFromSlice(kv_pairs []CPMItem) *ConcurrentPermanentMap {
m := make(map[interface{}]interface{}, len(kv_pairs))
for _, cpm_item := range kv_pairs {
m[cpm_item.Key] = cpm_item.Value
}
return &ConcurrentPermanentMap{
mut: sync.Mutex{},
m: m,
}
}
/*
type TTLMap [K any, V any] struct {
Data []T
queue chan[K]
Map map[K]V
}*/