-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_leaf.go
202 lines (162 loc) · 3.52 KB
/
node_leaf.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package adaptive
import (
"bytes"
"sync/atomic"
)
type NodeLeaf[T any] struct {
id uint64
value T
key []byte
mutateCh atomic.Pointer[chan struct{}]
}
func (n *NodeLeaf[T]) getId() uint64 {
return n.id
}
func (n *NodeLeaf[T]) setId(id uint64) {
n.id = id
}
func (n *NodeLeaf[T]) getPartialLen() uint32 {
// no-op
return 0
}
func (n *NodeLeaf[T]) setPartialLen(partialLen uint32) {
// no-op
}
func (n *NodeLeaf[T]) getArtNodeType() nodeType {
return leafType
}
func (n *NodeLeaf[T]) getNumChildren() uint8 {
return 0
}
func (n *NodeLeaf[T]) setNumChildren(numChildren uint8) {
// no-op
}
func (n *NodeLeaf[T]) isLeaf() bool {
return true
}
func (n *NodeLeaf[T]) getValue() T {
return n.value
}
func (n *NodeLeaf[T]) setValue(value T) {
n.value = value
}
func (n *NodeLeaf[T]) getKeyLen() uint32 {
return uint32(len(n.key))
}
func (n *NodeLeaf[T]) setKeyLen(keyLen uint32) {
// no-op
}
func (n *NodeLeaf[T]) getKey() []byte {
return n.key
}
func (n *NodeLeaf[T]) setKey(key []byte) {
n.key = key
}
func (n *NodeLeaf[T]) getPartial() []byte {
//no-op
return []byte{}
}
func (n *NodeLeaf[T]) setPartial(partial []byte) {
// no-op
}
func (l *NodeLeaf[T]) prefixContainsMatch(key []byte) bool {
if len(key) == 0 || len(l.key) == 0 {
return false
}
if key == nil {
return false
}
return bytes.HasPrefix(getKey(key), getKey(l.key))
}
func (n *NodeLeaf[T]) Iterator() *Iterator[T] {
nodeT := Node[T](n)
return &Iterator[T]{
node: nodeT,
}
}
func (n *NodeLeaf[T]) PathIterator(path []byte) *PathIterator[T] {
nodeT := Node[T](n)
return &PathIterator[T]{node: &nodeT,
path: getTreeKey(path),
stack: []Node[T]{nodeT},
}
}
func (n *NodeLeaf[T]) matchPrefix(prefix []byte) bool {
if len(n.key) == 0 {
return false
}
return bytes.HasPrefix(n.key, prefix)
}
func (n *NodeLeaf[T]) getChild(index int) Node[T] {
return nil
}
func (n *NodeLeaf[T]) clone(keepWatch bool) Node[T] {
newNode := &NodeLeaf[T]{
key: make([]byte, len(n.getKey())),
value: n.getValue(),
}
if keepWatch {
newNode.setMutateCh(n.getMutateCh())
}
newNode.setId(n.getId())
copy(newNode.key[:], n.key[:])
return newNode
}
func (n *NodeLeaf[T]) setChild(int, Node[T]) {
return
}
func (n *NodeLeaf[T]) getKeyAtIdx(idx int) byte {
// no op
return 0
}
func (n *NodeLeaf[T]) setKeyAtIdx(idx int, key byte) {
}
func (n *NodeLeaf[T]) getChildren() []Node[T] {
return nil
}
func (n *NodeLeaf[T]) getKeys() []byte {
return nil
}
func (n *NodeLeaf[T]) getMutateCh() chan struct{} {
// This must be lock free but we should ensure that concurrent callers will
// end up with the same chan
// Fast path if there is already a chan
ch := n.mutateCh.Load()
if ch != nil {
return *ch
}
// No chan yet, create one
newCh := make(chan struct{})
swapped := n.mutateCh.CompareAndSwap(nil, &newCh)
if swapped {
return newCh
}
// We raced with another reader and they won so return the chan they created instead.
return *n.mutateCh.Load()
}
func (n *NodeLeaf[T]) getLowerBoundCh(c byte) int {
return -1
}
func (n *NodeLeaf[T]) ReverseIterator() *ReverseIterator[T] {
return &ReverseIterator[T]{
i: &Iterator[T]{
node: n,
},
}
}
func (n *NodeLeaf[T]) setMutateCh(ch chan struct{}) {
n.mutateCh.Store(&ch)
}
func (n *NodeLeaf[T]) getNodeLeaf() *NodeLeaf[T] {
return nil
}
func (n *NodeLeaf[T]) setNodeLeaf(nl *NodeLeaf[T]) {
// no op
}
func (n *NodeLeaf[T]) LowerBoundIterator() *LowerBoundIterator[T] {
return &LowerBoundIterator[T]{
node: n,
}
}