-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
189 lines (171 loc) · 3.65 KB
/
node.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
package trie
import "reflect"
type Node struct {
key Key
value interface{}
edges edges
}
func (n *Node) Get(k []byte) interface{} {
for i := 0; n != nil; {
end := len(n.key)
if len(k) < end || !n.key[i:].EqualToBytes(k[i:end]) {
break
}
if len(k) == end {
return n.value
}
i = end
_, n = n.edges.get(k[i], i)
}
return nil
}
func (n *Node) GetString(k string) interface{} {
for i := 0; n != nil; {
end := len(n.key)
if len(k) < end || !n.key[i:].EqualToString(k[i:end]) {
break
}
if len(k) == end {
return n.value
}
i = end
_, n = n.edges.get(k[i], i)
}
return nil
}
func (n *Node) Delete(k []byte) *Node {
if n == nil {
return nil
}
return n.delete(&Txn{root: n}, 0, k)
}
func (n *Node) DeleteString(k string) *Node {
if n == nil {
return nil
}
return n.deleteString(&Txn{root: n}, 0, k)
}
func (n *Node) Put(k []byte, v interface{}) *Node {
if n == nil {
return &Node{key: k, value: v}
}
return n.put(&Txn{root: n}, 0, k, v, nil)
}
func (n *Node) PutString(k string, v interface{}) *Node {
if n == nil {
return &Node{key: Key(k), value: v}
}
return n.putString(&Txn{root: n}, 0, k, v, nil)
}
func (n *Node) delete(t *Txn, depth int, k []byte) *Node {
d, ok := n.key.commonBytesLen(k, depth)
if ok { // not found
return n
}
if d == len(k) { // exact match
return n.deleteValue(t)
}
es, modified := n.edges.delete(t, d, k)
if !modified {
return n
}
if t.isMutable(n) {
n.edges = es
return n
}
return t.newNode(n.key, n.value, es)
}
func (n *Node) deleteString(t *Txn, depth int, k string) *Node {
d, short := n.key.commonStringLen(k, depth)
if short { // not found
return n
}
if d == len(k) { // exact match
return n.deleteValue(t)
}
es, modified := n.edges.deleteString(t, d, k)
if !modified {
return n
}
if t.isMutable(n) {
n.edges = es
return n
}
return t.newNode(n.key, n.value, es)
}
func (n *Node) deleteValue(t *Txn) *Node {
switch len(n.edges) {
case 0:
// t.maybeFree(n)
return nil
case 1:
// t.maybeFree(n)
return n.edges[0]
}
if n.value != nil {
if !t.isMutable(n) {
return t.newNode(n.key, nil, n.edges)
}
n.value = nil
}
return n
}
// put sets the value (and merges the edges) under a given key.
func (n *Node) put(t *Txn, depth int, k []byte, v interface{}, es edges) *Node {
d, short := n.key.commonBytesLen(k, depth)
if short { // split
n, _ = split(t, d, n, t.newNode(k, v, es), false)
return n
}
if d == len(k) { // exact match
return n.set(t, v, es)
}
es, modified := n.edges.put(t, d, k, v, es)
if !modified {
return n
}
if t.isMutable(n) {
n.edges = es
return n
}
return t.newNode(n.key, n.value, es)
}
// putString is like put, but takes a string key.
func (n *Node) putString(t *Txn, depth int, k string, v interface{}, es edges) *Node {
d, short := n.key.commonStringLen(k, depth)
if short { // split
n, _ = split(t, d, n, t.newNode(Key(k), v, es), false)
return n
}
if d == len(k) { // exact match
return n.set(t, v, es)
}
es, modified := n.edges.putString(t, d, k, v, es)
if !modified {
return n
}
if t.isMutable(n) {
n.edges = es
return n
}
return t.newNode(n.key, n.value, es)
}
// set updates the value and merges in the provided edges.
func (n *Node) set(t *Txn, v interface{}, e edges) *Node {
e, side := mergeEdges(t, len(n.key), n.edges, e, false)
if side == mergeUseA {
if reflect.DeepEqual(n.value, v) {
debugf("set: nothing to change")
return n
}
debugf("set: no edges to add, but values not equal: %#v != %#v", n.value, v)
}
if t.isMutable(n) {
debugf("set: mutating")
n.value = v
n.edges = e
return n
}
debugf("set: creating a new node")
return t.newNode(n.key, v, e)
}