-
Notifications
You must be signed in to change notification settings - Fork 94
/
session_storage.go
139 lines (119 loc) · 2.54 KB
/
session_storage.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gws
import (
"sync"
"github.com/dolthub/maphash"
"github.com/lxzan/gws/internal"
)
type SessionStorage interface {
Len() int
Load(key string) (value any, exist bool)
Delete(key string)
Store(key string, value any)
Range(f func(key string, value any) bool)
}
func newSmap() *smap { return &smap{data: make(map[string]any)} }
type smap struct {
sync.Mutex
data map[string]any
}
func (c *smap) Len() int {
c.Lock()
defer c.Unlock()
return len(c.data)
}
func (c *smap) Load(key string) (value any, exist bool) {
c.Lock()
defer c.Unlock()
value, exist = c.data[key]
return
}
func (c *smap) Delete(key string) {
c.Lock()
defer c.Unlock()
delete(c.data, key)
}
func (c *smap) Store(key string, value any) {
c.Lock()
defer c.Unlock()
c.data[key] = value
}
func (c *smap) Range(f func(key string, value any) bool) {
c.Lock()
defer c.Unlock()
for k, v := range c.data {
if !f(k, v) {
return
}
}
}
type (
ConcurrentMap[K comparable, V any] struct {
hasher maphash.Hasher[K]
sharding uint64
buckets []*bucket[K, V]
}
bucket[K comparable, V any] struct {
sync.Mutex
m map[K]V
}
)
func NewConcurrentMap[K comparable, V any](sharding uint64) *ConcurrentMap[K, V] {
sharding = internal.SelectValue(sharding == 0, 16, sharding)
sharding = internal.ToBinaryNumber(sharding)
var cm = &ConcurrentMap[K, V]{
hasher: maphash.NewHasher[K](),
sharding: sharding,
buckets: make([]*bucket[K, V], sharding),
}
for i, _ := range cm.buckets {
cm.buckets[i] = &bucket[K, V]{m: make(map[K]V)}
}
return cm
}
func (c *ConcurrentMap[K, V]) getBucket(key K) *bucket[K, V] {
var hashCode = c.hasher.Hash(key)
var index = hashCode & (c.sharding - 1)
return c.buckets[index]
}
func (c *ConcurrentMap[K, V]) Len() int {
var length = 0
for _, b := range c.buckets {
b.Lock()
length += len(b.m)
b.Unlock()
}
return length
}
func (c *ConcurrentMap[K, V]) Load(key K) (value V, exist bool) {
var b = c.getBucket(key)
b.Lock()
value, exist = b.m[key]
b.Unlock()
return
}
func (c *ConcurrentMap[K, V]) Delete(key K) {
var b = c.getBucket(key)
b.Lock()
delete(b.m, key)
b.Unlock()
}
func (c *ConcurrentMap[K, V]) Store(key K, value V) {
var b = c.getBucket(key)
b.Lock()
b.m[key] = value
b.Unlock()
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (c *ConcurrentMap[K, V]) Range(f func(key K, value V) bool) {
for _, b := range c.buckets {
b.Lock()
for k, v := range b.m {
if !f(k, v) {
b.Unlock()
return
}
}
b.Unlock()
}
}