-
Notifications
You must be signed in to change notification settings - Fork 1
/
txn.go
76 lines (66 loc) · 1.17 KB
/
txn.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
package trie
type Txn struct {
root *Node
mut map[*Node]bool
}
func (t *Txn) Prealloc(n int) {
t.mut = make(map[*Node]bool, n)
}
func (t *Txn) Commit() *Node {
t.mut = nil
return t.root
}
func (t *Txn) Delete(k []byte) {
if t.root != nil {
t.root = t.root.delete(t, 0, k)
}
}
func (t *Txn) DeleteString(k string) {
if t.root != nil {
t.root = t.root.deleteString(t, 0, k)
}
}
func (t *Txn) Merge(n *Node) {
if n == nil {
return
}
if t.root == nil {
t.root = n
return
}
t.root, _ = mergeNodes(t, 0, t.root, n, false)
}
func (t *Txn) Put(k []byte, v interface{}) {
if t.root != nil {
t.root = t.root.put(t, 0, k, v, nil)
return
}
t.root = t.newNode(k, v, nil)
}
func (t *Txn) PutString(k string, v interface{}) {
if t.root != nil {
t.root = t.root.putString(t, 0, k, v, nil)
return
}
t.root = t.newNode(Key(k), v, nil)
}
func (t *Txn) isMutable(n *Node) bool {
if t == nil || t.mut == nil {
return false
}
return t.mut[n]
}
func (t *Txn) newNode(k Key, v interface{}, es edges) (n *Node) {
n = &Node{
key: k,
value: v,
edges: es,
}
if t != nil {
if t.mut == nil {
t.mut = make(map[*Node]bool)
}
t.mut[n] = true
}
return
}