-
Notifications
You must be signed in to change notification settings - Fork 0
/
primary.go
394 lines (356 loc) · 9.41 KB
/
primary.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package btree2d
import (
"io"
"github.com/joeshaw/gengen/generic"
"github.com/zenhotels/btree-2d/lockie"
"github.com/zenhotels/btree-2d/util"
)
// PrimaryCmpFunc compares a and b. Return value is:
//
// < 0 if a < b
// 0 if a == b
// > 0 if a > b
//
type PrimaryCmpFunc func(key1, key2 generic.T) int
// PrimaryLayer represents the primary layer,
// a tree holding comparable keys pointing to secondary layers.
type PrimaryLayer struct {
store *PrimaryTree
offset uint64
synced *uint64 // id of the previously synced layer
lock lockie.Lockie
cmp1 PrimaryCmpFunc
cmp2 SecondaryCmpFunc
}
// NewPrimaryLayer initializes a new primary layer handle.
func NewPrimaryLayer(cmp1 PrimaryCmpFunc, cmp2 SecondaryCmpFunc) PrimaryLayer {
var synced uint64
return PrimaryLayer{
synced: &synced,
store: NewPrimaryTree(cmp1),
offset: uint64(util.RevOffset()),
lock: lockie.NewLockie(),
cmp1: cmp1,
cmp2: cmp2,
}
}
// Set just adds a secondary layer to the tree, overwriting the previous one.
// Note that this action would trigger the replaced layer finalizers.
func (l PrimaryLayer) Set(k generic.T, layer SecondaryLayer) {
l.lock.Lock()
l.store.Put(k, func(oldLayer SecondaryLayer, exists bool) (newLayer SecondaryLayer, write bool) {
if exists {
oldLayer.Finalize()
}
return layer, true
})
l.lock.Unlock()
}
func (l PrimaryLayer) Rev() uint64 {
return l.store.Ver() + l.offset
}
// Put adds keys and callbacks to the secondary layer, which will be created
// if not yet existing.
func (l PrimaryLayer) Put(k generic.T, k2 generic.U, finalizers ...func()) {
l.lock.Lock()
l.store.Put(k, func(oldLayer SecondaryLayer, exists bool) (newLayer SecondaryLayer, write bool) {
if !exists {
oldLayer = NewSecondaryLayer(l.cmp2)
}
oldLayer.Put(k2, finalizers...)
return oldLayer, true
})
l.lock.Unlock()
}
// Seek returns an PrimaryEnumerator positioned on a secondary layer such that k >= layer's key.
func (l PrimaryLayer) Seek(k generic.T) (e *PrimaryEnumerator, ok bool) {
l.lock.Lock()
e, ok = l.store.Seek(k)
l.lock.Unlock()
return
}
// SeekFirst returns an PrimaryEnumerator positioned on the first secondary layer in the tree.
func (l PrimaryLayer) SeekFirst() (e *PrimaryEnumerator, err error) {
l.lock.Lock()
e, err = l.store.SeekFirst()
l.lock.Unlock()
return
}
// ForEach runs the provided function for every element in the layer,
// if function returns true, the loop stops.
func (l PrimaryLayer) ForEach(fn func(key generic.T, layer SecondaryLayer) bool) {
l.lock.Lock()
e, err := l.store.SeekFirst()
l.lock.Unlock()
if err != io.EOF {
k, layer, err := e.Next()
for err != io.EOF {
if stop := fn(k, layer); stop {
return
}
l.lock.Lock()
k, layer, err = e.Next()
l.lock.Unlock()
}
e.Close()
}
}
// Drop removes the whole secondary layer associated with the key,
// invokes all the finalizers associated with elements of this secondary layer.
func (l PrimaryLayer) Drop(k generic.T) (ok bool) {
l.lock.Lock()
v, found := l.store.Get(k)
if found {
ok = l.store.Delete(k)
}
l.lock.Unlock()
if found {
v.Finalize()
v.close()
}
return
}
// Get returns the secondary layer associated with the key.
func (l PrimaryLayer) Get(k generic.T) (layer SecondaryLayer, ok bool) {
l.lock.Lock()
v, ok := l.store.Get(k)
l.lock.Unlock()
return v, ok
}
func (prev PrimaryLayer) Sync(next PrimaryLayer, onAdd, onDel func(key1 generic.T, key2 generic.U)) {
if prev.store == next.store {
return
}
// TODO(xlab): primary cannot handle changes on secondary layers.
// Disable this feature for now
//
// nextRev := next.Rev()
// if prevRev := atomic.LoadUint64(prev.synced); prevRev == nextRev {
// log.Println()
// 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
return
case prevErr == io.EOF:
// previous storage is empty, everything is added
prev.addAll(next.lock, nextIter, onAdd)
nextIter.Close()
return
case nextErr == io.EOF:
// next storage is empty, everything is deleted
prev.deleteAll(prevIter, onDel)
prevIter.Close()
return
default:
// do sync and trigger the corresponding callbacks
prev.syncAll(next, prevIter, nextIter, onAdd, onDel)
prevIter.Close()
nextIter.Close()
return
}
}
func (prev PrimaryLayer) addAll(nextLock lockie.Lockie, nextIter *PrimaryEnumerator,
onAdd func(key1 generic.T, key2 generic.U)) {
nextLock.Lock()
nextK, nextLayer, err := nextIter.Next()
nextLock.Unlock()
for err != io.EOF {
if nextLayer.Len() > 0 {
// create a new layer to set into prev
newLayer := NewSecondaryLayer(prev.cmp2)
// fills layer while calling the onAdd callback
if onAdd != nil {
newLayer.Sync(nextLayer, func(k2 generic.U) {
onAdd(nextK, k2)
}, nil)
} else {
newLayer.Sync(nextLayer, nil, nil)
}
// set the new layer into prev
prev.lock.Lock()
prev.store.Set(nextK, newLayer)
prev.lock.Unlock()
}
// advance next iter
nextLock.Lock()
nextK, nextLayer, err = nextIter.Next()
nextLock.Unlock()
}
}
func (prev PrimaryLayer) deleteAll(prevIter *PrimaryEnumerator,
onDel func(key1 generic.T, key2 generic.U)) {
prev.lock.Lock()
prevK, prevLayer, err := prevIter.Next()
prev.lock.Unlock()
for err != io.EOF {
// nukes the prevLayer yet calling the onDel callback
if onDel != nil {
prevLayer.Sync(NewSecondaryLayer(prev.cmp2), nil, func(k2 generic.U) {
onDel(prevK, k2)
})
} else {
prevLayer.Sync(NewSecondaryLayer(prev.cmp2), nil, nil)
}
// advance next iter
prev.lock.Lock()
prevK, prevLayer, err = prevIter.Next()
prev.lock.Unlock()
}
// finally clear the store
prev.lock.Lock()
prev.store.Clear()
prev.lock.Unlock()
}
func (prev PrimaryLayer) syncAll(next PrimaryLayer, prevIter, nextIter *PrimaryEnumerator,
onAdd, onDel func(k1 generic.T, k2 generic.U)) {
prev.lock.Lock()
prevK, prevLayer, prevErr := prevIter.Next()
prev.lock.Unlock()
next.lock.Lock()
nextK, nextLayer, 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 nextLayer.Len() > 0 {
// create a new layer to set into prev
newLayer := NewSecondaryLayer(prev.cmp2)
// fills layer while calling the onAdd callback
if onAdd != nil {
newLayer.Sync(nextLayer, func(k2 generic.U) {
onAdd(nextK, k2)
}, nil)
} else {
newLayer.Sync(nextLayer, nil, nil)
}
// set the new layer into prev
prev.lock.Lock()
prev.store.Set(nextK, newLayer)
prev.lock.Unlock()
}
// move next iterator
next.lock.Lock()
nextK, nextLayer, 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 {
prevLayer.ForEach(func(k2 generic.U, v2 *FinalizerList) bool {
if onDel != nil {
onDel(prevK, k2)
}
if v2 != nil {
v2.Finalize()
}
return false
})
} else {
prevLayer.Finalize()
}
// delete prevK in prev
prev.lock.Lock()
prev.store.Delete(prevK)
prevLayer.close()
// move prev iterator
prevK, prevLayer, prevErr = prevIter.Next()
prev.lock.Unlock()
continue
}
prevCmp := prev.cmp1(prevK, nextK)
switch {
case prevCmp < 0: // prevK < nextK
// old prevK has been deleted apparently
if onDel != nil {
prevLayer.ForEach(func(k2 generic.U, v2 *FinalizerList) bool {
if onDel != nil {
onDel(prevK, k2)
}
if v2 != nil {
v2.Finalize()
}
return false
})
} else {
prevLayer.Finalize()
}
// delete prevK in prev
prev.lock.Lock()
prev.store.Delete(prevK)
prevLayer.close()
// move prev iterator
prevK, prevLayer, prevErr = prevIter.Next()
prev.lock.Unlock()
case prevCmp > 0: // nextK < prevK
// new nextK has been inserted apparently
if nextLayer.Len() > 0 {
// create a new layer to set into prev
newLayer := NewSecondaryLayer(prev.cmp2)
// fills layer while calling the onAdd callback
if onAdd != nil {
newLayer.Sync(nextLayer, func(k2 generic.U) {
onAdd(nextK, k2)
}, nil)
} else {
newLayer.Sync(nextLayer, nil, nil)
}
// set the new layer into prev
prev.lock.Lock()
prev.store.Set(nextK, newLayer)
prev.lock.Unlock()
}
// move next iterator
next.lock.Lock()
nextK, nextLayer, nextErr = nextIter.Next()
next.lock.Unlock()
default:
// we're on the same keys, sync the layers
switch {
case onAdd != nil && onDel != nil:
prevLayer.Sync(nextLayer, func(k2 generic.U) {
onAdd(nextK, k2)
}, func(k2 generic.U) {
onDel(prevK, k2)
})
case onAdd != nil:
prevLayer.Sync(nextLayer, func(k2 generic.U) {
onAdd(nextK, k2)
}, nil)
case onDel != nil:
prevLayer.Sync(nextLayer, nil, func(k2 generic.U) {
onDel(prevK, k2)
})
default: // no callbacks
prevLayer.Sync(nextLayer, nil, nil)
}
// move both iterators
prev.lock.Lock()
prevK, prevLayer, prevErr = prevIter.Next()
prev.lock.Unlock()
next.lock.Lock()
nextK, nextLayer, nextErr = nextIter.Next()
next.lock.Unlock()
}
}
}
func (l PrimaryLayer) Len() int {
l.lock.Lock()
count := l.store.Len()
l.lock.Unlock()
return count
}