-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtree.go
1369 lines (1295 loc) · 43.6 KB
/
tree.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package radix
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/zond/god/common"
"github.com/zond/god/murmur"
"github.com/zond/god/persistence"
"math/big"
"sync/atomic"
)
// NaiveTimer is a Timer that just provides the current system time.
type NaiveTimer struct{}
var faketime int64
func (self NaiveTimer) ContinuousTime() int64 {
return atomic.AddInt64(&faketime, 1)
}
// TreeIterators iterate over trees, and see the key, value and timestamp of what they iterate over.
// If they return false, the iteration will end.
type TreeIterator func(key, value []byte, timestamp int64) (cont bool)
// TreeIndexIterators iterate over trees, and see the key, value, timestamp and index of what they iterate over.
// If they return false, the iteration will end.
type TreeIndexIterator func(key, value []byte, timestamp int64, index int) (cont bool)
func cmps(mininc, maxinc bool) (mincmp, maxcmp int) {
if mininc {
mincmp = -1
}
if maxinc {
maxcmp = 1
}
return
}
func escapeBytes(b []byte) (result []byte) {
result = make([]byte, 0, len(b))
for _, c := range b {
if c == 0 {
result = append(result, 0, 0)
} else {
result = append(result, c)
}
}
return
}
func incrementBytes(b []byte) []byte {
return new(big.Int).Add(new(big.Int).SetBytes(b), big.NewInt(1)).Bytes()
}
func newMirrorIterator(min, max []byte, mininc, maxinc bool, f TreeIterator) TreeIterator {
return func(key, value []byte, timestamp int64) bool {
gt := 0
if mininc {
gt = -1
}
lt := 0
if maxinc {
lt = 1
}
k := key[:len(key)-len(escapeBytes(value))-1]
if (min == nil || bytes.Compare(k, min) > gt) && (max == nil || bytes.Compare(k, max) < lt) {
return f(k, value, timestamp)
}
return true
}
}
func newMirrorIndexIterator(f TreeIndexIterator) TreeIndexIterator {
return func(key, value []byte, timestamp int64, index int) bool {
return f(key[:len(key)-len(escapeBytes(value))-1], value, timestamp, index)
}
}
func newNodeIterator(f TreeIterator) nodeIterator {
return func(key, bValue []byte, tValue *Tree, use int, timestamp int64) (cont bool) {
return f(key, bValue, timestamp)
}
}
func newNodeIndexIterator(f TreeIndexIterator) nodeIndexIterator {
return func(key, bValue []byte, tValue *Tree, use int, timestamp int64, index int) (cont bool) {
return f(key, bValue, timestamp, index)
}
}
// Tree is a merkle tree inside a radix tree, where each node can contain a separate sub tree.
//
// Any method called Sub* does the same to a sub tree as the same method without Sub does to the main tree.
//
// A Tree can be mirrored, which means that it contains another Tree where the keys are the values of the master Tree, and the values are the keys of the master Tree.
//
// A Tree is configured to be mirrored or not by using AddConfiguration or SubAddConfiguration (for a sub tree) setting 'mirrored' to 'yes'.
type Tree struct {
lock *common.TimeLock
timer Timer
logger *persistence.Logger
root *node
mirror *Tree
configuration map[string]string
configurationTimestamp int64
dataTimestamp int64
}
func NewTree() *Tree {
return NewTreeTimer(NaiveTimer{})
}
func NewTreeTimer(timer Timer) (result *Tree) {
result = &Tree{
lock: common.NewTimeLock(),
timer: timer,
configuration: make(map[string]string),
}
result.root, _, _, _, _ = result.root.insert(nil, newNode(nil, nil, nil, 0, true, 0), result.timer.ContinuousTime())
result.dataTimestamp = timer.ContinuousTime()
return
}
func (self *Tree) Load() float64 {
return self.lock.Load()
}
func (self *Tree) deepEqual(o *Tree) bool {
return self.Describe() == o.Describe()
}
func (self *Tree) conf() (result map[string]string, ts int64) {
result = make(map[string]string)
for k, v := range self.configuration {
result[k] = v
}
return result, self.configurationTimestamp
}
// Configuration returns the current configuration and its timestamp.
func (self *Tree) Configuration() (map[string]string, int64) {
self.lock.RLock()
defer self.lock.RUnlock()
return self.conf()
}
func (self *Tree) mirrorClear(timestamp int64) {
if self.mirror != nil {
self.mirror.Clear(timestamp)
}
}
func (self *Tree) mirrorPut(key, value []byte, timestamp int64) {
if self.mirror != nil {
escapedKey := escapeBytes(key)
newKey := make([]byte, len(escapedKey)+len(value)+1)
copy(newKey, value)
copy(newKey[len(value)+1:], escapedKey)
self.mirror.Put(newKey, key, timestamp)
}
}
func (self *Tree) mirrorFakeDel(key, value []byte, timestamp int64) {
if self.mirror != nil {
escapedKey := escapeBytes(key)
newKey := make([]byte, len(escapedKey)+len(value)+1)
copy(newKey, value)
copy(newKey[len(value)+1:], escapedKey)
self.mirror.FakeDel(newKey, timestamp)
}
}
func (self *Tree) mirrorDel(key, value []byte) {
if self.mirror != nil {
escapedKey := escapeBytes(key)
newKey := make([]byte, len(escapedKey)+len(value)+1)
copy(newKey, value)
copy(newKey[len(value)+1:], escapedKey)
self.mirror.Del(newKey)
}
}
func (self *Tree) startMirroring() {
self.mirror = NewTreeTimer(self.timer)
self.root.each(nil, byteValue, func(key, byteValue []byte, treeValue *Tree, use int, timestamp int64) bool {
self.mirrorPut(key, byteValue, timestamp)
return true
})
}
func (self *Tree) configure(conf map[string]string, ts int64) {
if conf[mirrored] == yes && self.configuration[mirrored] != yes {
self.startMirroring()
} else if conf[mirrored] != yes && self.configuration[mirrored] == yes {
self.mirror = nil
}
self.configuration = conf
self.configurationTimestamp = ts
self.log(persistence.Op{
Configuration: conf,
Timestamp: ts,
})
}
// Configure will set a new configuration and timestamp to this tree.
// If the configuration has mirrored=yes this tree will start mirroring all its keys and values in a mirror Tree.
func (self *Tree) Configure(conf map[string]string, ts int64) {
self.lock.Lock()
defer self.lock.Unlock()
self.configure(conf, ts)
}
// AddConfiguration will set the key and value in the configuration of this tree, and set the provided timestamp as the
// new configuration timestamp.
func (self *Tree) AddConfiguration(ts int64, key, value string) bool {
self.lock.Lock()
defer self.lock.Unlock()
oldConf, _ := self.conf()
if oldConf[key] != value {
oldConf[key] = value
self.configure(oldConf, ts)
return true
}
return false
}
// Log will make this Tree start logging using a new persistence.Logger.
func (self *Tree) Log(dir string) *Tree {
self.logger = persistence.NewLogger(dir)
<-self.logger.Record()
return self
}
// Restore will temporarily stop the Logger of this Tree, make it replay all operations
// to allow us to restore the state logged in that directory, and then start recording again.
func (self *Tree) Restore() *Tree {
self.logger.Stop()
self.logger.Play(func(op persistence.Op) {
if op.Configuration != nil {
if op.Key == nil {
self.Configure(op.Configuration, op.Timestamp)
} else {
self.SubConfigure(op.Key, op.Configuration, op.Timestamp)
}
} else if op.Put {
if op.SubKey == nil {
self.Put(op.Key, op.Value, op.Timestamp)
} else {
self.SubPut(op.Key, op.SubKey, op.Value, op.Timestamp)
}
} else {
if op.SubKey == nil {
if op.Clear {
if op.Timestamp > 0 {
self.SubClear(op.Key, op.Timestamp)
} else {
self.SubKill(op.Key)
}
} else {
self.Del(op.Key)
}
} else {
self.SubDel(op.Key, op.SubKey)
}
}
})
<-self.logger.Record()
return self
}
func (self *Tree) log(op persistence.Op) {
if self.logger != nil && self.logger.Recording() {
self.logger.Dump(op)
}
}
func (self *Tree) newTreeWith(key []Nibble, byteValue []byte, timestamp int64) (result *Tree) {
result = NewTreeTimer(self.timer)
result.PutTimestamp(key, byteValue, true, 0, timestamp)
return
}
// Each will iterate over the entire tree using f.
func (self *Tree) Each(f TreeIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.each(nil, byteValue, newNodeIterator(f))
}
// ReverseEach will iterate over the entire tree in reverse order using f.
func (self *Tree) ReverseEach(f TreeIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.reverseEach(nil, byteValue, newNodeIterator(f))
}
// MirrorEachBetween will iterate between min and max in the mirror Tree using f.
func (self *Tree) MirrorEachBetween(min, max []byte, mininc, maxinc bool, f TreeIterator) {
if self == nil || self.mirror == nil {
return
}
if maxinc && max != nil {
maxinc = false
max = incrementBytes(max)
}
self.mirror.EachBetween(min, max, mininc, maxinc, newMirrorIterator(min, max, mininc, maxinc, f))
}
// EachBetween will iterate between min and max using f.
func (self *Tree) EachBetween(min, max []byte, mininc, maxinc bool, f TreeIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
mincmp, maxcmp := cmps(mininc, maxinc)
self.root.eachBetween(nil, Rip(min), Rip(max), mincmp, maxcmp, byteValue, newNodeIterator(f))
}
// MirrorReverseEachBetween will iterate between min and max in the mirror Tree, in reverse order, using f.
func (self *Tree) MirrorReverseEachBetween(min, max []byte, mininc, maxinc bool, f TreeIterator) {
if self == nil || self.mirror == nil {
return
}
if maxinc && max != nil {
maxinc = false
max = incrementBytes(max)
}
self.mirror.ReverseEachBetween(min, max, mininc, maxinc, newMirrorIterator(min, max, mininc, maxinc, f))
}
// ReverseEachBetween will iterate between min and max in reverse order using f.
func (self *Tree) ReverseEachBetween(min, max []byte, mininc, maxinc bool, f TreeIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
mincmp, maxcmp := cmps(mininc, maxinc)
self.root.reverseEachBetween(nil, Rip(min), Rip(max), mincmp, maxcmp, byteValue, newNodeIterator(f))
}
// MirrorIndexOf will return the index of (or the index it would have if it existed) key in the mirror Tree.
func (self *Tree) MirrorIndexOf(key []byte) (index int, existed bool) {
if self == nil || self.mirror == nil {
return
}
var value []byte
self.MirrorEachBetween(key, nil, true, false, func(k, v []byte, ts int64) bool {
value, existed = v, bytes.Compare(key, k[:len(key)]) == 0
return false
})
newKey := key
if existed {
escapedValue := escapeBytes(value)
newKey = make([]byte, len(escapedValue)+len(key)+1)
copy(newKey, key)
copy(newKey[len(key)+1:], escapedValue)
}
index, _ = self.mirror.IndexOf(newKey)
return
}
// IndexOf will return the index of (or the index it would have if it existed) key.
func (self *Tree) IndexOf(key []byte) (index int, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
index, ex := self.root.indexOf(0, Rip(key), byteValue, true)
existed = ex&byteValue != 0
return
}
// MirrorReverseIndexOf will return the index from the end (or the index it would have if it existed) key in the mirror Tree.
func (self *Tree) MirrorReverseIndexOf(key []byte) (index int, existed bool) {
if self == nil || self.mirror == nil {
return
}
var value []byte
self.MirrorEachBetween(key, nil, true, false, func(k, v []byte, ts int64) bool {
value, existed = v, bytes.Compare(key, k[:len(key)]) == 0
return false
})
newKey := key
if existed {
escapedValue := escapeBytes(value)
newKey = make([]byte, len(escapedValue)+len(key)+1)
copy(newKey, key)
copy(newKey[len(key)+1:], escapedValue)
}
index, _ = self.mirror.ReverseIndexOf(newKey)
return
}
// ReverseIndexOf will return the index from the end (or the index it would have if it existed) key.
func (self *Tree) ReverseIndexOf(key []byte) (index int, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
index, ex := self.root.indexOf(0, Rip(key), byteValue, false)
existed = ex&byteValue != 0
return
}
// MirrorEachBetweenIndex will iterate between the min'th and the max'th entry in the mirror Tree using f.
func (self *Tree) MirrorEachBetweenIndex(min, max *int, f TreeIndexIterator) {
if self == nil || self.mirror == nil {
return
}
self.mirror.EachBetweenIndex(min, max, newMirrorIndexIterator(f))
}
// EachBetweenIndex will iterate between the min'th and the max'th entry using f.
func (self *Tree) EachBetweenIndex(min, max *int, f TreeIndexIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.eachBetweenIndex(nil, 0, min, max, byteValue, newNodeIndexIterator(f))
}
// MirrorReverseEachBetweenIndex will iterate between the min'th and the max'th entry of the mirror Tree, in reverse order, using f.
func (self *Tree) MirrorReverseEachBetweenIndex(min, max *int, f TreeIndexIterator) {
if self == nil || self.mirror == nil {
return
}
self.mirror.ReverseEachBetweenIndex(min, max, newMirrorIndexIterator(f))
}
// ReverseEachBetweenIndex will iterate between the min'th and the max'th entry in reverse order using f.
func (self *Tree) ReverseEachBetweenIndex(min, max *int, f TreeIndexIterator) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.reverseEachBetweenIndex(nil, 0, min, max, byteValue, newNodeIndexIterator(f))
}
func (self *Tree) DataTimestamp() int64 {
if self == nil {
return 0
}
self.lock.RLock()
defer self.lock.RUnlock()
return self.dataTimestamp
}
// Hash returns the merkle hash of this Tree.
func (self *Tree) Hash() []byte {
if self == nil {
return nil
}
self.lock.RLock()
defer self.lock.RUnlock()
hash := murmur.NewString(fmt.Sprint(self.configuration))
hash.MustWrite(self.root.hash)
return hash.Get()
}
// ToMap will return a dubious map representation of this Tree, where each byte slice key is converted to a string.
func (self *Tree) ToMap() (result map[string][]byte) {
if self == nil {
return
}
result = make(map[string][]byte)
self.Each(func(key []byte, value []byte, timestamp int64) bool {
result[hex.EncodeToString(key)] = value
return true
})
return
}
// String returns a humanly readable string representation of this Tree.
func (self *Tree) String() string {
if self == nil {
return ""
}
return fmt.Sprint(self.ToMap())
}
func (self *Tree) sizeBetween(min, max []byte, mininc, maxinc bool, use int) int {
if self == nil {
return 0
}
self.lock.RLock()
defer self.lock.RUnlock()
mincmp, maxcmp := cmps(mininc, maxinc)
return self.root.sizeBetween(nil, Rip(min), Rip(max), mincmp, maxcmp, use)
}
// RealSizeBetween returns the real, as in 'including tombstones and sub trees', size of this Tree between min anx max.
func (self *Tree) RealSizeBetween(min, max []byte, mininc, maxinc bool) int {
return self.sizeBetween(min, max, mininc, maxinc, 0)
}
// MirrorSizeBetween returns the virtual, as in 'not including tombstones and sub trees', size of the mirror Tree between min and max.
func (self *Tree) MirrorSizeBetween(min, max []byte, mininc, maxinc bool) (i int) {
if self == nil || self.mirror == nil {
return
}
if !mininc && min != nil {
mininc = true
min = incrementBytes(min)
}
if maxinc && max != nil {
maxinc = false
max = incrementBytes(max)
}
return self.mirror.SizeBetween(min, max, mininc, maxinc)
}
// SizeBetween returns the virtual, as in 'not including tombstones and sub trees', size of this Tree between min and max.
func (self *Tree) SizeBetween(min, max []byte, mininc, maxinc bool) int {
return self.sizeBetween(min, max, mininc, maxinc, byteValue|treeValue)
}
// RealSize returns the real, as in 'including tombstones and sub trees', size of this Tree.
func (self *Tree) RealSize() int {
if self == nil {
return 0
}
self.lock.RLock()
defer self.lock.RUnlock()
return self.root.realSize
}
// Size returns the virtual, as in 'not including tombstones and sub trees', size of this Tree.
func (self *Tree) Size() int {
if self == nil {
return 0
}
self.lock.RLock()
defer self.lock.RUnlock()
return self.root.byteSize + self.root.treeSize
}
func (self *Tree) describeIndented(first, indent int) string {
if self == nil {
return ""
}
indentation := &bytes.Buffer{}
for i := 0; i < first; i++ {
fmt.Fprint(indentation, " ")
}
buffer := bytes.NewBufferString(fmt.Sprintf("%v<Radix size:%v hash:%v>\n", indentation, self.Size(), hex.EncodeToString(self.Hash())))
self.root.describe(indent+2, buffer)
if self.mirror != nil {
for i := 0; i < indent+first; i++ {
fmt.Fprint(buffer, " ")
}
fmt.Fprint(buffer, "<mirror>\n")
self.mirror.root.describe(indent+2, buffer)
}
return string(buffer.Bytes())
}
// Describe will return a complete and humanly readable (albeit slightly convoluted) description of this Tree.
func (self *Tree) Describe() string {
if self == nil {
return ""
}
self.lock.RLock()
defer self.lock.RUnlock()
return self.describeIndented(0, 0)
}
// FakeDel will insert a tombstone at key with timestamp in this Tree.
func (self *Tree) FakeDel(key []byte, timestamp int64) (oldBytes []byte, oldTree *Tree, existed bool) {
self.lock.Lock()
defer self.lock.Unlock()
var ex int
self.root, oldBytes, oldTree, _, ex = self.root.fakeDel(nil, Rip(key), byteValue, timestamp, self.timer.ContinuousTime())
existed = ex&byteValue != 0
if existed {
self.mirrorFakeDel(key, oldBytes, timestamp)
self.log(persistence.Op{
Key: key,
})
}
return
}
func (self *Tree) put(key []Nibble, byteValue []byte, treeValue *Tree, use int, timestamp int64) (oldBytes []byte, oldTree *Tree, existed int) {
self.dataTimestamp = timestamp
self.root, oldBytes, oldTree, _, existed = self.root.insert(nil, newNode(key, byteValue, treeValue, timestamp, false, use), self.timer.ContinuousTime())
return
}
// Put will put key and value with timestamp in this Tree.
func (self *Tree) Put(key []byte, bValue []byte, timestamp int64) (oldBytes []byte, existed bool) {
self.lock.Lock()
defer self.lock.Unlock()
oldBytes, _, ex := self.put(Rip(key), bValue, nil, byteValue, timestamp)
existed = ex*byteValue != 0
if existed {
self.mirrorDel(key, oldBytes)
}
self.mirrorPut(key, bValue, timestamp)
self.log(persistence.Op{
Key: key,
Value: bValue,
Timestamp: timestamp,
Put: true,
})
return
}
// Get will return the value and timestamp at key.
func (self *Tree) Get(key []byte) (bValue []byte, timestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
bValue, _, timestamp, ex := self.root.get(Rip(key))
existed = ex&byteValue != 0
return
}
// PrevMarker returns the previous key of tombstone or real value before key.
func (self *Tree) PrevMarker(key []byte) (prevKey []byte, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.reverseEachBetween(nil, nil, Rip(key), 0, 0, 0, func(k, b []byte, t *Tree, u int, v int64) bool {
prevKey, existed = k, true
return false
})
return
}
// NextMarker returns the next key of tombstone or real value after key.
func (self *Tree) NextMarker(key []byte) (nextKey []byte, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.eachBetween(nil, Rip(key), nil, 0, 0, 0, func(k, b []byte, t *Tree, u int, v int64) bool {
nextKey, existed = k, true
return false
})
return
}
// MirrorPrev returns the previous key, value and timestamp before key in the mirror Tree.
func (self *Tree) MirrorPrev(key []byte) (prevKey, prevValue []byte, prevTimestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
prevKey, prevValue, prevTimestamp, existed = self.mirror.Prev(key)
prevKey = prevKey[:len(prevKey)-len(escapeBytes(prevValue))-1]
return
}
// MirrorNext returns the next key, value and timestamp after key in the mirror Tree.
func (self *Tree) MirrorNext(key []byte) (nextKey, nextValue []byte, nextTimestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
nextKey, nextValue, nextTimestamp, existed = self.mirror.Next(key)
nextKey = nextKey[:len(nextKey)-len(escapeBytes(nextValue))-1]
return
}
// Prev will return the previous key, value and timestamp before key in this Tree.
func (self *Tree) Prev(key []byte) (prevKey, prevValue []byte, prevTimestamp int64, existed bool) {
self.ReverseEachBetween(nil, key, false, false, func(k, v []byte, timestamp int64) bool {
prevKey, prevValue, prevTimestamp, existed = k, v, timestamp, true
return false
})
return
}
// Next will return the next key, value and timestamp after key in this Tree.
func (self *Tree) Next(key []byte) (nextKey, nextValue []byte, nextTimestamp int64, existed bool) {
self.EachBetween(key, nil, false, false, func(k, v []byte, timestamp int64) bool {
nextKey, nextValue, nextTimestamp, existed = k, v, timestamp, true
return false
})
return
}
// NextMarkerIndex will return the next key of tombstone or real value after the given index in this Tree.
func (self *Tree) NextMarkerIndex(index int) (key []byte, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.eachBetweenIndex(nil, 0, &index, nil, 0, func(k, b []byte, t *Tree, u int, v int64, i int) bool {
key, existed = k, true
return false
})
return
}
// PrevMarkerIndex will return the previous key of tombstone or real value before the given index in this Tree.
func (self *Tree) PrevMarkerIndex(index int) (key []byte, existed bool) {
if self == nil {
return
}
self.lock.RLock()
defer self.lock.RUnlock()
self.root.reverseEachBetweenIndex(nil, 0, nil, &index, 0, func(k, b []byte, t *Tree, u int, v int64, i int) bool {
key, existed = k, true
return false
})
return
}
// MirrorNextIndex will return the next key, value, timestamp and index after index in the mirror Tree.
func (self *Tree) MirrorNextIndex(index int) (key, value []byte, timestamp int64, ind int, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, value, timestamp, ind, existed = self.mirror.NextIndex(index)
if existed {
key = key[:len(key)-len(escapeBytes(value))-1]
}
return
}
// MirrorPrevIndex will return the previous key, value, timestamp and index before index in the mirror Tree.
func (self *Tree) MirrorPrevIndex(index int) (key, value []byte, timestamp int64, ind int, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, value, timestamp, ind, existed = self.mirror.PrevIndex(index)
if existed {
key = key[:len(key)-len(escapeBytes(value))-1]
}
return
}
// NextIndex will return the next key, value, timestamp and index after index in this Tree.
func (self *Tree) NextIndex(index int) (key, value []byte, timestamp int64, ind int, existed bool) {
n := index + 1
self.EachBetweenIndex(&n, &n, func(k, v []byte, t int64, i int) bool {
key, value, timestamp, ind, existed = k, v, t, i, true
return false
})
return
}
// PrevIndex will return the previous key, value, timestamp and index before index in this Tree.
func (self *Tree) PrevIndex(index int) (key, value []byte, timestamp int64, ind int, existed bool) {
p := index - 1
self.EachBetweenIndex(&p, &p, func(k, v []byte, t int64, i int) bool {
key, value, timestamp, ind, existed = k, v, t, i, true
return false
})
return
}
// MirrorFirst will return the first key, value and timestamp of the mirror Tree.
func (self *Tree) MirrorFirst() (key, byteValue []byte, timestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, byteValue, timestamp, existed = self.mirror.First()
key = key[:len(key)-len(escapeBytes(byteValue))-1]
return
}
// MirrorLast will return the last key, value and timestamp of the mirror Tree.
func (self *Tree) MirrorLast() (key, byteValue []byte, timestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, byteValue, timestamp, existed = self.mirror.Last()
key = key[:len(key)-len(escapeBytes(byteValue))-1]
return
}
// First returns the first key, value and timestamp in this Tree.
func (self *Tree) First() (key, byteValue []byte, timestamp int64, existed bool) {
self.Each(func(k []byte, b []byte, ver int64) bool {
key, byteValue, timestamp, existed = k, b, ver, true
return false
})
return
}
// Last returns the last key, value and timestamp in this Tree.
func (self *Tree) Last() (key, byteValue []byte, timestamp int64, existed bool) {
self.ReverseEach(func(k []byte, b []byte, ver int64) bool {
key, byteValue, timestamp, existed = k, b, ver, true
return false
})
return
}
// MirrorIndex returns the key, value and timestamp at index in the mirror Tree.
func (self *Tree) MirrorIndex(n int) (key, byteValue []byte, timestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, byteValue, timestamp, existed = self.mirror.Index(n)
key = key[:len(key)-len(escapeBytes(byteValue))-1]
return
}
// MirrorReverseIndex returns the key, value and timestamp at index from the end in the mirror Tree.
func (self *Tree) MirrorReverseIndex(n int) (key, byteValue []byte, timestamp int64, existed bool) {
if self == nil || self.mirror == nil {
return
}
key, byteValue, timestamp, existed = self.mirror.Index(n)
key = key[:len(key)-len(escapeBytes(byteValue))-1]
return
}
// Index returns the key, value and timestamp at index in this Tree.
func (self *Tree) Index(n int) (key []byte, byteValue []byte, timestamp int64, existed bool) {
self.EachBetweenIndex(&n, &n, func(k []byte, b []byte, ver int64, index int) bool {
key, byteValue, timestamp, existed = k, b, ver, true
return false
})
return
}
// ReverseIndex returns the key, value and timestamp at index from the end in this Tree.
func (self *Tree) ReverseIndex(n int) (key []byte, byteValue []byte, timestamp int64, existed bool) {
self.ReverseEachBetweenIndex(&n, &n, func(k []byte, b []byte, ver int64, index int) bool {
key, byteValue, timestamp, existed = k, b, ver, true
return false
})
return
}
// Clear will remove all content of this Tree (including tombstones and sub trees) and any mirror Tree, replace them all with one giant tombstone,
// and clear any persistence.Logger assigned to this Tree.
func (self *Tree) Clear(timestamp int64) {
self.lock.Lock()
defer self.lock.Unlock()
self.dataTimestamp, self.root = timestamp, nil
self.root, _, _, _, _ = self.root.insert(nil, newNode(nil, nil, nil, 0, true, 0), self.timer.ContinuousTime())
self.mirrorClear(timestamp)
if self.logger != nil {
self.logger.Clear()
}
}
func (self *Tree) del(key []Nibble, use int) (oldBytes []byte, existed bool) {
var ex int
self.root, oldBytes, _, _, ex = self.root.del(nil, key, use, self.timer.ContinuousTime())
existed = ex&byteValue != 0
return
}
// Del will remove key from this Tree without keeping a tombstone.
func (self *Tree) Del(key []byte) (oldBytes []byte, existed bool) {
self.lock.Lock()
defer self.lock.Unlock()
oldBytes, existed = self.del(Rip(key), byteValue)
if existed {
self.mirrorDel(key, oldBytes)
self.log(persistence.Op{
Key: key,
})
}
return
}
func (self *Tree) SubMirrorReverseIndexOf(key, subKey []byte) (index int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
index, existed = subTree.MirrorReverseIndexOf(subKey)
}
return
}
func (self *Tree) SubMirrorIndexOf(key, subKey []byte) (index int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
index, existed = subTree.MirrorIndexOf(subKey)
}
return
}
func (self *Tree) SubReverseIndexOf(key, subKey []byte) (index int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
index, existed = subTree.ReverseIndexOf(subKey)
}
return
}
func (self *Tree) SubIndexOf(key, subKey []byte) (index int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
index, existed = subTree.IndexOf(subKey)
}
return
}
func (self *Tree) SubMirrorPrevIndex(key []byte, index int) (foundKey, foundValue []byte, foundTimestamp int64, foundIndex int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
foundKey, foundValue, foundTimestamp, foundIndex, existed = subTree.MirrorPrevIndex(index)
}
return
}
func (self *Tree) SubMirrorNextIndex(key []byte, index int) (foundKey, foundValue []byte, foundTimestamp int64, foundIndex int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
foundKey, foundValue, foundTimestamp, foundIndex, existed = subTree.MirrorNextIndex(index)
}
return
}
func (self *Tree) SubPrevIndex(key []byte, index int) (foundKey, foundValue []byte, foundTimestamp int64, foundIndex int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
foundKey, foundValue, foundTimestamp, foundIndex, existed = subTree.PrevIndex(index)
}
return
}
func (self *Tree) SubNextIndex(key []byte, index int) (foundKey, foundValue []byte, foundTimestamp int64, foundIndex int, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
foundKey, foundValue, foundTimestamp, foundIndex, existed = subTree.NextIndex(index)
}
return
}
func (self *Tree) SubMirrorFirst(key []byte) (firstKey []byte, firstBytes []byte, firstTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
firstKey, firstBytes, firstTimestamp, existed = subTree.MirrorFirst()
}
return
}
func (self *Tree) SubMirrorLast(key []byte) (lastKey []byte, lastBytes []byte, lastTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
lastKey, lastBytes, lastTimestamp, existed = subTree.MirrorLast()
}
return
}
func (self *Tree) SubFirst(key []byte) (firstKey []byte, firstBytes []byte, firstTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
firstKey, firstBytes, firstTimestamp, existed = subTree.First()
}
return
}
func (self *Tree) SubLast(key []byte) (lastKey []byte, lastBytes []byte, lastTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
lastKey, lastBytes, lastTimestamp, existed = subTree.Last()
}
return
}
func (self *Tree) SubMirrorPrev(key, subKey []byte) (prevKey, prevValue []byte, prevTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
prevKey, prevValue, prevTimestamp, existed = subTree.MirrorPrev(subKey)
}
return
}
func (self *Tree) SubMirrorNext(key, subKey []byte) (nextKey, nextValue []byte, nextTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
nextKey, nextValue, nextTimestamp, existed = subTree.MirrorNext(subKey)
}
return
}
func (self *Tree) SubPrev(key, subKey []byte) (prevKey, prevValue []byte, prevTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
prevKey, prevValue, prevTimestamp, existed = subTree.Prev(subKey)
}
return
}
func (self *Tree) SubNext(key, subKey []byte) (nextKey, nextValue []byte, nextTimestamp int64, existed bool) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
nextKey, nextValue, nextTimestamp, existed = subTree.Next(subKey)
}
return
}
func (self *Tree) SubSize(key []byte) (result int) {
self.lock.RLock()
defer self.lock.RUnlock()
if _, subTree, _, ex := self.root.get(Rip(key)); ex&treeValue != 0 && subTree != nil {
result = subTree.Size()
}
return
}
func (self *Tree) SubMirrorSizeBetween(key, min, max []byte, mininc, maxinc bool) (result int) {