-
Notifications
You must be signed in to change notification settings - Fork 374
/
store.go
238 lines (207 loc) · 5.28 KB
/
store.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ristretto
import (
"sync"
"time"
)
type storeItem struct {
key uint64
conflict uint64
value interface{}
expiration time.Time
}
// store is the interface fulfilled by all hash map implementations in this
// file. Some hash map implementations are better suited for certain data
// distributions than others, so this allows us to abstract that out for use
// in Ristretto.
//
// Every store is safe for concurrent usage.
type store interface {
// Get returns the value associated with the key parameter.
Get(uint64, uint64) (interface{}, bool)
// Expiration returns the expiration time for this key.
Expiration(uint64) time.Time
// Set adds the key-value pair to the Map or updates the value if it's
// already present. The key-value pair is passed as a pointer to an
// item object.
Set(*item)
// Del deletes the key-value pair from the Map.
Del(uint64, uint64) (uint64, interface{})
// Update attempts to update the key with a new value and returns true if
// successful.
Update(*item) bool
// Cleanup removes items that have an expired TTL.
Cleanup(policy policy, onEvict onEvictFunc)
// Clear clears all contents of the store.
Clear()
}
// newStore returns the default store implementation.
func newStore(m *expirationMap) store {
return newShardedMap(m)
}
const numShards uint64 = 256
type shardedMap struct {
shards []*lockedMap
em *expirationMap
}
func newShardedMap(em *expirationMap) *shardedMap {
sm := &shardedMap{
shards: make([]*lockedMap, int(numShards)),
em: em,
}
for i := range sm.shards {
sm.shards[i] = newLockedMap(em)
}
return sm
}
func (sm *shardedMap) Get(key, conflict uint64) (interface{}, bool) {
return sm.shards[key%numShards].get(key, conflict)
}
func (sm *shardedMap) Expiration(key uint64) time.Time {
return sm.shards[key%numShards].Expiration(key)
}
func (sm *shardedMap) Set(i *item) {
if i == nil {
// If item is nil make this Set a no-op.
return
}
sm.shards[i.key%numShards].Set(i)
}
func (sm *shardedMap) Del(key, conflict uint64) (uint64, interface{}) {
return sm.shards[key%numShards].Del(key, conflict)
}
func (sm *shardedMap) Update(newItem *item) bool {
return sm.shards[newItem.key%numShards].Update(newItem)
}
func (sm *shardedMap) Cleanup(policy policy, onEvict onEvictFunc) {
sm.em.cleanup(sm, policy, onEvict)
}
func (sm *shardedMap) Clear() {
for i := uint64(0); i < numShards; i++ {
sm.shards[i].Clear()
}
}
type lockedMap struct {
sync.RWMutex
data map[uint64]storeItem
em *expirationMap
}
func newLockedMap(em *expirationMap) *lockedMap {
return &lockedMap{
data: make(map[uint64]storeItem),
em: em,
}
}
func (m *lockedMap) get(key, conflict uint64) (interface{}, bool) {
m.RLock()
item, ok := m.data[key]
m.RUnlock()
if !ok {
return nil, false
}
if conflict != 0 && (conflict != item.conflict) {
return nil, false
}
// Handle expired items.
if !item.expiration.IsZero() {
if time.Now().After(item.expiration) {
return nil, false
}
}
return item.value, true
}
func (m *lockedMap) Expiration(key uint64) time.Time {
m.RLock()
defer m.RUnlock()
return m.data[key].expiration
}
func (m *lockedMap) Set(i *item) {
if i == nil {
// If the item is nil make this Set a no-op.
return
}
m.Lock()
item, ok := m.data[i.key]
if ok {
m.em.update(i.key, i.conflict, item.expiration, i.expiration)
} else {
m.em.add(i.key, i.conflict, i.expiration)
m.data[i.key] = storeItem{
key: i.key,
conflict: i.conflict,
value: i.value,
expiration: i.expiration,
}
m.Unlock()
return
}
if i.conflict != 0 && (i.conflict != item.conflict) {
m.Unlock()
return
}
m.data[i.key] = storeItem{
key: i.key,
conflict: i.conflict,
value: i.value,
expiration: i.expiration,
}
m.Unlock()
}
func (m *lockedMap) Del(key, conflict uint64) (uint64, interface{}) {
m.Lock()
item, ok := m.data[key]
if !ok {
m.Unlock()
return 0, nil
}
if conflict != 0 && (conflict != item.conflict) {
m.Unlock()
return 0, nil
}
if !item.expiration.IsZero() {
m.em.del(key, item.expiration)
}
delete(m.data, key)
m.Unlock()
return item.conflict, item.value
}
func (m *lockedMap) Update(newItem *item) bool {
m.Lock()
item, ok := m.data[newItem.key]
if !ok {
m.Unlock()
return false
}
if newItem.conflict != 0 && (newItem.conflict != item.conflict) {
m.Unlock()
return false
}
m.em.update(newItem.key, newItem.conflict, item.expiration, newItem.expiration)
m.data[newItem.key] = storeItem{
key: newItem.key,
conflict: newItem.conflict,
value: newItem.value,
expiration: newItem.expiration,
}
m.Unlock()
return true
}
func (m *lockedMap) Clear() {
m.Lock()
m.data = make(map[uint64]storeItem)
m.Unlock()
}