-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
168 lines (147 loc) · 3.22 KB
/
iterator.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
package art
import (
"bytes"
)
type checkpoint[T any] struct {
node *inner[T]
parentLock *olock
parentVersion uint64
pointer *byte
prev *checkpoint[T]
}
// iterator will scan the tree in lexicographic order.
type iterator[T any] struct {
tree *Tree[T]
stack *checkpoint[T]
closed bool
cursor, terminate []byte
reverse bool
key []byte
value T
}
func (i *iterator[T]) Reverse() *iterator[T] {
i.cursor, i.terminate = i.terminate, i.cursor
i.reverse = true
return i
}
// Next will iterate over all leaf nodes between specified prefixes
func (i *iterator[T]) Next() bool {
if i.closed {
return false
}
if i.stack == nil {
// initialize iterator
if exit, next := i.init(); exit {
return next
}
}
return i.iterate()
}
func (i *iterator[T]) Value() T {
return i.value
}
func (i *iterator[T]) Key() Key {
return i.key
}
func (i *iterator[T]) inRange(key []byte) bool {
if !i.reverse {
return bytes.Compare(key, i.cursor) > 0 && (len(i.terminate) == 0 || bytes.Compare(key, i.terminate) <= 0)
}
return (bytes.Compare(key, i.cursor) < 0 || len(i.cursor) == 0) && (len(i.terminate) == 0 || bytes.Compare(key, i.terminate) >= 0)
}
func (i *iterator[T]) init() (bool, bool) {
for {
version, _ := i.tree.lock.RLock()
root := i.tree.root
if root == nil {
if i.tree.lock.RUnlock(version, nil) {
continue
}
i.closed = true
return true, false
}
l, isLeaf := root.(*leaf[T])
if isLeaf {
if i.tree.lock.RUnlock(version, nil) {
continue
}
i.closed = true
if i.inRange(l.key) {
i.key = l.key
i.value = l.value
return true, true
}
return true, false
}
i.stack = &checkpoint[T]{
node: root.(*inner[T]),
parentLock: &i.tree.lock,
parentVersion: version,
}
return false, false
}
}
func (i *iterator[T]) next(n *inner[T], pointer *byte) (byte, node[T]) {
if !i.reverse {
return n.node.next(pointer)
}
return n.node.prev(pointer)
}
func (i *iterator[T]) iterate() bool {
for i.stack != nil {
more, restart := i.tryAdvance()
if more {
return more
} else if restart {
i.stack = i.stack.prev
if i.stack == nil {
// checkpoint is root
i.stack = nil
if exit, next := i.init(); exit {
return next
}
}
}
}
i.closed = true
return false
}
func (i *iterator[T]) tryAdvance() (bool, bool) {
for {
tail := i.stack
version, obsolete := tail.node.lock.RLock()
if obsolete || tail.parentLock.Check(tail.parentVersion) {
_ = tail.parentLock.RUnlock(version, nil)
return false, true
}
pointer, child := i.next(tail.node, tail.pointer)
if child == nil {
if tail.node.lock.RUnlock(version, nil) {
continue
}
_ = tail.parentLock.RUnlock(version, nil)
// inner node is exhausted, move one level up the stack
i.stack = tail.prev
return false, false
}
// advance pointer
tail.pointer = &pointer
l, isLeaf := child.(*leaf[T])
if isLeaf {
if i.inRange(l.key) {
i.key = l.key
i.value = l.value
i.cursor = l.key
return true, false
}
return false, false
}
i.stack = &checkpoint[T]{
node: child.(*inner[T]),
prev: tail,
parentLock: &tail.node.lock,
parentVersion: version,
}
return false, false
}
}