forked from steveyen/gkvlite
-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.go
194 lines (175 loc) · 4.33 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
190
191
192
193
194
package gkvlite
import (
"encoding/binary"
"fmt"
"sync"
"sync/atomic"
)
// A persistable node.
type node struct {
numNodes, numBytes uint64
item itemLoc
left, right nodeLoc
next *node // For free-list tracking.
}
var nodeLocGL = sync.RWMutex{}
// A persistable node and its persistence location.
type nodeLoc struct {
loc *ploc // *ploc - can be nil if node is dirty (not yet persisted).
node *node // *node - can be nil if node is not fetched into memory yet.
next *nodeLoc // For free-list tracking.
}
var empty_nodeLoc = &nodeLoc{} // Sentinel.
func (nloc *nodeLoc) Loc() *ploc {
nodeLocGL.RLock()
defer nodeLocGL.RUnlock()
return nloc.loc
}
func (nloc *nodeLoc) setLoc(n *ploc) {
nodeLocGL.Lock()
defer nodeLocGL.Unlock()
nloc.loc = n
}
func (nloc *nodeLoc) Node() *node {
nodeLocGL.RLock()
defer nodeLocGL.RUnlock()
return nloc.node
}
func (nloc *nodeLoc) setNode(n *node) {
nodeLocGL.Lock()
defer nodeLocGL.Unlock()
nloc.node = n
}
func (nloc *nodeLoc) LocNode() (*ploc, *node) {
nodeLocGL.RLock()
defer nodeLocGL.RUnlock()
return nloc.loc, nloc.node
}
func (nloc *nodeLoc) Copy(src *nodeLoc) *nodeLoc {
if src == nil {
return nloc.Copy(empty_nodeLoc)
}
nodeLocGL.Lock()
defer nodeLocGL.Unlock()
// NOTE: This trick only works because of the global lock. No reason to lock
// src independently of nlock.
nloc.loc = src.loc
nloc.node = src.node
return nloc
}
func (nloc *nodeLoc) isEmpty() bool {
nodeLocGL.RLock()
defer nodeLocGL.RUnlock()
return nloc == nil || (nloc.loc.isEmpty() && nloc.node == nil)
}
func (nloc *nodeLoc) write(o *Store) error {
loc, node := nloc.LocNode()
if nloc != nil && loc.isEmpty() {
if node == nil {
return nil
}
offset := atomic.LoadInt64(&o.size)
length := ploc_length + ploc_length + ploc_length + 8 + 8
b := make([]byte, length)
pos := 0
pos = node.item.Loc().write(b, pos)
pos = node.left.Loc().write(b, pos)
pos = node.right.Loc().write(b, pos)
binary.BigEndian.PutUint64(b[pos:pos+8], node.numNodes)
pos += 8
binary.BigEndian.PutUint64(b[pos:pos+8], node.numBytes)
pos += 8
if pos != length {
return fmt.Errorf("nodeLoc.write() pos: %v didn't match length: %v",
pos, length)
}
if _, err := o.file.WriteAt(b, offset); err != nil {
return err
}
atomic.StoreInt64(&o.size, offset+int64(length))
nloc.setLoc(&ploc{Offset: offset, Length: uint32(length)})
}
return nil
}
func (nloc *nodeLoc) read(o *Store) (n *node, err error) {
if nloc == nil {
return nil, nil
}
loc, n := nloc.LocNode()
if n != nil {
return n, nil
}
if loc.isEmpty() {
return nil, nil
}
if loc.Length != uint32(ploc_length+ploc_length+ploc_length+8+8) {
return nil, fmt.Errorf("unexpected node loc.Length: %v != %v",
loc.Length, ploc_length+ploc_length+ploc_length+8+8)
}
b := make([]byte, loc.Length)
if _, err := o.file.ReadAt(b, loc.Offset); err != nil {
return nil, err
}
pos := 0
atomic.AddUint64(&o.nodeAllocs, 1)
n = &node{}
var p *ploc
p = &ploc{}
p, pos = p.read(b, pos)
n.item.loc = p
p = &ploc{}
p, pos = p.read(b, pos)
n.left.loc = p
p = &ploc{}
p, pos = p.read(b, pos)
n.right.loc = p
n.numNodes = binary.BigEndian.Uint64(b[pos : pos+8])
pos += 8
n.numBytes = binary.BigEndian.Uint64(b[pos : pos+8])
pos += 8
if pos != len(b) {
return nil, fmt.Errorf("nodeLoc.read() pos: %v didn't match length: %v",
pos, len(b))
}
nloc.setNode(n)
return n, nil
}
func numInfo(o *Store, left *nodeLoc, right *nodeLoc) (
leftNum uint64, leftBytes uint64, rightNum uint64, rightBytes uint64, err error) {
leftNode, err := left.read(o)
if err != nil {
return 0, 0, 0, 0, err
}
rightNode, err := right.read(o)
if err != nil {
return 0, 0, 0, 0, err
}
if !left.isEmpty() && leftNode != nil {
leftNum = leftNode.numNodes
leftBytes = leftNode.numBytes
}
if !right.isEmpty() && rightNode != nil {
rightNum = rightNode.numNodes
rightBytes = rightNode.numBytes
}
return leftNum, leftBytes, rightNum, rightBytes, nil
}
func dump(o *Store, n *nodeLoc, level int) {
if n.isEmpty() {
return
}
nNode, _ := n.read(o)
dump(o, &nNode.left, level+1)
dumpIndent(level)
k := "<evicted>"
if nNode.item.Item() != nil {
k = string(nNode.item.Item().Key)
}
fmt.Printf("%p - %v\n", nNode, k)
dump(o, &nNode.right, level+1)
}
func dumpIndent(level int) {
for i := 0; i < level; i++ {
fmt.Print(" ")
}
}