-
Notifications
You must be signed in to change notification settings - Fork 8
/
file.go
259 lines (229 loc) · 5.76 KB
/
file.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package gannoy
import (
"bytes"
"encoding/binary"
"math"
"os"
"syscall"
)
type File struct {
tree int
dim int
K int
file *os.File
filename string
appendFile *os.File
createChan chan createArgs
locker Locker
nodeSize int64
offsetOfV int64
}
func newFile(filename string, tree, dim, K int) *File {
_, err := os.Stat(filename)
if err != nil {
f, _ := os.Create(filename)
f.Close()
}
file, _ := os.OpenFile(filename, os.O_RDWR, 0)
appendFile, _ := os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0)
f := &File{
tree: tree,
dim: dim,
K: K,
file: file,
filename: filename,
appendFile: appendFile,
createChan: make(chan createArgs, 1),
locker: newLocker(),
nodeSize: int64(1 + // free
4 + // nDescendants
4 + // key
4*tree + // parents
4*2 + // children
8*dim), // v
offsetOfV: int64(1 + // free
4 + // nDescendants
4 + // key
4*tree + // parents
4*2), // children
}
go f.creator()
return f
}
func (f *File) Create(n Node) (int, error) {
args := createArgs{node: n, result: make(chan createResult)}
f.createChan <- args
result := <-args.result
return result.id, result.err
}
func (f *File) create(n Node) (int, error) {
id := f.nodeCount()
_, err := f.appendFile.Write(f.nodeToBytes(n))
return id, err
}
func (f *File) Find(id int) (Node, error) {
node := Node{}
node.id = id
node.storage = f
offset := f.offset(id)
err := f.locker.ReadLock(f.file.Fd(), offset, f.nodeSize)
if err != nil {
return node, err
}
defer f.locker.UnLock(f.file.Fd(), offset, f.nodeSize)
b := make([]byte, f.nodeSize)
_, err = syscall.Pread(int(f.file.Fd()), b, offset)
if err != nil {
return node, err
}
node.free = b[0] != 0
node.nDescendants = int(int32(binary.BigEndian.Uint32(b[1:5])))
node.key = int(int32(binary.BigEndian.Uint32(b[5:9])))
node.parents = make([]int, f.tree)
for i := 0; i < f.tree; i++ {
node.parents[i] = int(int32(binary.BigEndian.Uint32(b[9+i*4 : 9+i*4+4])))
}
if node.nDescendants == 1 {
// leaf node
node.children = []int{0, 0} // skip children
node.v = bytesToFloat64s(b[f.offsetOfV:])
} else if node.nDescendants <= f.K {
// bucket node
node.children = make([]int, node.nDescendants)
offsetOfChildren := int(f.offsetOfV - (4 * 2))
for i := 0; i < node.nDescendants; i++ {
node.children[i] = int(int32(binary.BigEndian.Uint32(b[offsetOfChildren+i*4 : offsetOfChildren+i*4+4])))
}
} else {
// other node
node.children = make([]int, 2)
offsetOfChildren := int(f.offsetOfV - (4 * 2))
for i := 0; i < 2; i++ {
node.children[i] = int(int32(binary.BigEndian.Uint32(b[offsetOfChildren+i*4 : offsetOfChildren+i*4+4])))
}
node.v = bytesToFloat64s(b[f.offsetOfV:])
}
return node, nil
}
func (f *File) Update(n Node) error {
bytes := f.nodeToBytes(n)
offset := f.offset(n.id)
file, _ := os.OpenFile(f.filename, os.O_RDWR, 0)
defer file.Close()
err := f.locker.WriteLock(file.Fd(), offset, f.nodeSize)
if err != nil {
return err
}
defer f.locker.UnLock(file.Fd(), offset, f.nodeSize)
_, err = syscall.Pwrite(int(file.Fd()), bytes, offset)
return err
}
func (f *File) UpdateParent(id, rootIndex, parent int) error {
offset := f.offset(id) +
int64(1+ // free
4+ // nDescendants
4+ // key
4*rootIndex) // parents
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, int32(parent))
file, _ := os.OpenFile(f.filename, os.O_RDWR, 0)
defer file.Close()
err := f.locker.WriteLock(file.Fd(), offset, 4)
if err != nil {
return err
}
defer f.locker.UnLock(file.Fd(), offset, 4)
_, err = syscall.Pwrite(int(file.Fd()), buf.Bytes(), offset)
return err
}
func (f *File) Delete(n Node) error {
n.free = true
return f.Update(n)
}
func (f *File) Iterate(c chan Node) {
count := f.nodeCount()
// TODO: Use goroutine
for i := 0; i < count; i++ {
n, err := f.Find(i)
if err != nil {
close(c)
}
c <- n
}
close(c)
}
func (f File) offset(id int) int64 {
return (int64(id) * f.nodeSize)
}
func (f File) nodeCount() int {
stat, _ := f.file.Stat()
size := stat.Size()
return int(size / f.nodeSize)
}
func (f File) nodeToBytes(node Node) []byte {
bytes := make([]byte, f.nodeSize)
// 1bytes free
if node.free {
bytes[0] = 1
} else {
bytes[0] = 0
}
// 4bytes nDescendants
binary.BigEndian.PutUint32(bytes[1:5], uint32(node.nDescendants))
// 4bytes key
binary.BigEndian.PutUint32(bytes[5:9], uint32(node.key))
// 4bytes parents
for i := 0; i < f.tree; i++ {
binary.BigEndian.PutUint32(bytes[9+i*4:9+i*4+4], uint32(node.parents[i]))
}
if node.isBucket() {
// 4bytes children in K
offsetOfChildren := int(f.offsetOfV - (4 * 2))
for i, child := range node.children {
binary.BigEndian.PutUint32(bytes[offsetOfChildren+i*4:offsetOfChildren+i*4+4], uint32(child))
}
// padding by zero (nothing to do)
} else {
offsetOfV := int(f.offsetOfV)
offsetOfChildren := int(f.offsetOfV - (4 * 2))
// 4bytes 2 children
for i, child := range node.children {
binary.BigEndian.PutUint32(bytes[offsetOfChildren+i*4:offsetOfChildren+i*4+4], uint32(child))
}
// 8bytes v in f
for i, v := range node.v {
binary.BigEndian.PutUint64(bytes[offsetOfV+i*8:offsetOfV+i*8+8], math.Float64bits(v))
}
}
return bytes
}
type createArgs struct {
node Node
result chan createResult
}
type createResult struct {
id int
err error
}
func (f *File) creator() {
for args := range f.createChan {
id, err := f.create(args.node)
args.result <- createResult{
id: id,
err: err,
}
}
}
func (f File) size() int64 {
info, _ := f.file.Stat()
return info.Size()
}
func bytesToFloat64s(bytes []byte) []float64 {
size := len(bytes) / 8
floats := make([]float64, size)
for i := 0; i < size; i++ {
floats[i] = math.Float64frombits(binary.BigEndian.Uint64(bytes[0:8]))
bytes = bytes[8:]
}
return floats
}