-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondary.go
323 lines (291 loc) · 7.08 KB
/
secondary.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package btree2d
import (
"io"
"sync/atomic"
"github.com/joeshaw/gengen/generic"
"github.com/astranet/btree-2d/lockie"
"github.com/astranet/btree-2d/util"
)
// SecondaryCmpFunc compares a and b. Return value is:
//
// < 0 if a < b
// 0 if a == b
// > 0 if a > b
//
type SecondaryCmpFunc func(key1, key2 generic.U) int
// SecondaryLayer represents the secondary layer,
// a tree holding Finalizable yet Comparable keys.
type SecondaryLayer struct {
store *SecondaryTree
offset uint64
synced *uint64 // id of the previously synced layer
lock lockie.Lockie
cmp SecondaryCmpFunc
}
// NewSecondaryLayer initializes a new secondary layer handle.
func NewSecondaryLayer(cmp SecondaryCmpFunc) SecondaryLayer {
var synced uint64
return SecondaryLayer{
synced: &synced,
store: NewSecondaryTree(cmp),
offset: uint64(util.RevOffset()),
lock: lockie.NewLockie(),
cmp: cmp,
}
}
func (l SecondaryLayer) Rev() uint64 {
return l.store.Ver() + l.offset
}
// Put adds finalizers for the key, creating the item if not exists yet.
func (l SecondaryLayer) Put(k generic.U, finalizers ...func()) (added int) {
l.lock.Lock()
l.store.Put(k, func(oldV *FinalizerList, exists bool) (newV *FinalizerList, write bool) {
if !exists || oldV == nil {
if len(finalizers) == 0 {
return nil, true
}
oldV = &FinalizerList{}
}
for i := range finalizers {
if oldV.AddFinalizer(finalizers[i]) {
added++
}
}
return oldV, true
})
l.lock.Unlock()
return
}
// ForEach runs the provided function for every element in the layer,
// if function returns true, the loop stops.
func (l SecondaryLayer) ForEach(fn func(key generic.U, val *FinalizerList) bool) {
l.lock.Lock()
e, err := l.store.SeekFirst()
l.lock.Unlock()
if err != io.EOF {
k, v, err := e.Next()
for err != io.EOF {
if stop := fn(k, v); stop {
return
}
l.lock.Lock()
k, v, err = e.Next()
l.lock.Unlock()
}
e.Close()
}
}
// Seek returns an SecondaryEnumerator positioned on a key such that k >= key.
func (l SecondaryLayer) Seek(k generic.U) (e *SecondaryEnumerator, ok bool) {
l.lock.Lock()
e, ok = l.store.Seek(k)
l.lock.Unlock()
return
}
// SeekFirst returns an SecondaryEnumerator positioned on the first key in the tree.
func (l SecondaryLayer) SeekFirst() (e *SecondaryEnumerator, err error) {
l.lock.Lock()
e, err = l.store.SeekFirst()
l.lock.Unlock()
return
}
// Delete removes the key and runs all its finalizers.
func (l SecondaryLayer) Delete(k generic.U) (ok bool) {
l.lock.Lock()
v, found := l.store.Get(k)
if found {
ok = l.store.Delete(k)
}
l.lock.Unlock()
if found && v != nil {
v.Finalize()
}
return
}
func (l SecondaryLayer) close() {
l.store.Close()
}
// Finalize locks the layer and runs finalizers of all the keys
// from this layer. Call this if you're going to drop an entire layer.
func (l SecondaryLayer) Finalize() {
l.lock.Lock()
e, err := l.store.SeekFirst()
if err != io.EOF {
_, v, err := e.Next()
for err != io.EOF {
if v != nil {
v.Finalize()
}
_, v, err = e.Next()
}
e.Close()
}
l.lock.Unlock()
}
func (prev SecondaryLayer) Sync(next SecondaryLayer, onAdd, onDel func(key generic.U)) {
if prev.store == next.store {
return
}
nextRev := next.Rev()
if prevRev := atomic.LoadUint64(prev.synced); prevRev == nextRev {
return
}
prev.lock.Lock()
prevIter, prevErr := prev.store.SeekFirst()
prev.lock.Unlock()
next.lock.Lock()
nextIter, nextErr := next.store.SeekFirst()
next.lock.Unlock()
switch {
case prevErr == io.EOF && nextErr == io.EOF:
// do nothing, both are empty
atomic.StoreUint64(prev.synced, nextRev)
return
case prevErr == io.EOF:
// previous storage is empty, everything is added
prev.addAll(next.lock, nextIter, onAdd)
nextIter.Close()
atomic.StoreUint64(prev.synced, nextRev)
return
case nextErr == io.EOF:
// next storage is empty, everything is deleted
prev.deleteAll(prevIter, onDel)
prevIter.Close()
atomic.StoreUint64(prev.synced, nextRev)
return
default:
// do sync and trigger the corresponding callbacks
prev.syncAll(next, prevIter, nextIter, onAdd, onDel)
prevIter.Close()
nextIter.Close()
atomic.StoreUint64(prev.synced, nextRev)
return
}
}
func (prev SecondaryLayer) addAll(nextLock lockie.Lockie, nextIter *SecondaryEnumerator, onAdd func(k generic.U)) {
nextLock.Lock()
k, _, err := nextIter.Next()
nextLock.Unlock()
for err != io.EOF {
prev.lock.Lock()
prev.store.Set(k, nil)
prev.lock.Unlock()
if onAdd != nil {
onAdd(k)
}
nextLock.Lock()
k, _, err = nextIter.Next()
nextLock.Unlock()
}
}
func (prev SecondaryLayer) deleteAll(prevIter *SecondaryEnumerator, onDel func(k generic.U)) {
prev.lock.Lock()
k, v, err := prevIter.Next()
prev.lock.Unlock()
for err != io.EOF {
if onDel != nil {
onDel(k) // run the callback
}
if v != nil {
v.Finalize() // emit the finalizers
}
prev.lock.Lock()
k, v, err = prevIter.Next()
prev.lock.Unlock()
}
// finally clear the store
prev.lock.Lock()
prev.store.Clear()
prev.lock.Unlock()
}
func (prev SecondaryLayer) syncAll(next SecondaryLayer, prevIter, nextIter *SecondaryEnumerator, onAdd, onDel func(k generic.U)) {
prev.lock.Lock()
prevK, prevV, prevErr := prevIter.Next()
prev.lock.Unlock()
next.lock.Lock()
nextK, _, nextErr := nextIter.Next()
next.lock.Unlock()
for {
switch {
case prevErr == io.EOF:
if nextErr == io.EOF {
return // we're done
}
// at this point prev is ended, so nextK is added
if onAdd != nil {
onAdd(nextK)
}
// set nextK into prev
prev.lock.Lock()
prev.store.Set(nextK, nil)
prev.lock.Unlock()
// move next iterator
next.lock.Lock()
nextK, _, nextErr = nextIter.Next()
next.lock.Unlock()
continue
case nextErr == io.EOF:
if prevErr == io.EOF {
return // we're done
}
// at this point next is ended, so prevK is deleted
if onDel != nil {
onDel(prevK)
}
if prevV != nil {
prevV.Finalize()
}
// delete prevK in prev
prev.lock.Lock()
prev.store.Delete(prevK)
// move prev iterator
prevK, _, prevErr = prevIter.Next()
prev.lock.Unlock()
continue
}
prevCmp := prev.cmp(prevK, nextK)
switch {
case prevCmp < 0: // prevK < nextK
// old prevK has been deleted apparently
if onDel != nil {
onDel(prevK)
}
if prevV != nil {
prevV.Finalize()
}
// delete prevK in prev
prev.lock.Lock()
prev.store.Delete(prevK)
// move prev iterator
prevK, _, prevErr = prevIter.Next()
prev.lock.Unlock()
case prevCmp > 0: // nextK < prevK
// new nextK has been inserted apparently
if onAdd != nil {
onAdd(nextK)
}
// set nextK into prev
prev.lock.Lock()
prev.store.Set(nextK, nil)
prev.lock.Unlock()
// move next iterator
next.lock.Lock()
nextK, _, nextErr = nextIter.Next()
next.lock.Unlock()
default:
// we're on the same keys, move both iterators
prev.lock.Lock()
prevK, _, prevErr = prevIter.Next()
prev.lock.Unlock()
next.lock.Lock()
nextK, _, nextErr = nextIter.Next()
next.lock.Unlock()
}
}
}
func (l SecondaryLayer) Len() int {
l.lock.Lock()
count := l.store.Len()
l.lock.Unlock()
return count
}