/
btree.go
510 lines (421 loc) · 11.8 KB
/
btree.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package v2
import (
"crypto/sha256"
"fmt"
"sort"
"strconv"
"github.com/golang/protobuf/proto"
)
var EMPTY_DIR = Leaf{entries: make([]*LeafEntry, 0, 10)}
type NodeStore interface {
// serialization and deserialization of BTree nodes
GetLeaf(key *Key) *Leaf
StoreLeaf(leaf *Leaf) *Key
GetBranch(key *Key) *Branch
StoreBranch(branch *Branch) *Key
GetDirectory(key *Key) *Directory
}
type LeafEntry struct {
name string
metadata *FileMetadata
}
type Leaf struct {
entries []*LeafEntry
}
type BranchEntry struct {
isLeaf bool
child Key
lastName string
}
type Branch struct {
children []BranchEntry
}
type TreeSettings struct {
MaxBlockSize int
MinBlockSize int
}
type TreeStats struct {
leavesSplit uint32
branchesSplit uint32
branchesMerged uint32
leavesMerged uint32
valuesReplaced uint32
valuesInserted uint32
}
func CopyLeafWithMutation(leaf *Leaf, replaceIndex int, entry *LeafEntry) *Leaf {
newLeaf := &Leaf{entries: make([]*LeafEntry, len(leaf.entries))}
for i := 0; i < len(leaf.entries); i++ {
if i == replaceIndex {
newLeaf.entries[i] = entry
} else {
newLeaf.entries[i] = leaf.entries[i]
}
}
return newLeaf
}
func CopyLeafWithInsertion(leaf *Leaf, insertIndex int, entry *LeafEntry) *Leaf {
newLeaf := &Leaf{entries: make([]*LeafEntry, len(leaf.entries)+1)}
for i := 0; i < insertIndex; i++ {
newLeaf.entries[i] = leaf.entries[i]
}
newLeaf.entries[insertIndex] = entry
for i := insertIndex; i < len(leaf.entries); i++ {
newLeaf.entries[i+1] = leaf.entries[i]
}
return newLeaf
}
func CopyLeafWithRemoval(leaf *Leaf, removeIndex int) *Leaf {
newLeaf := &Leaf{entries: make([]*LeafEntry, len(leaf.entries)-1)}
for i := 0; i < removeIndex; i++ {
newLeaf.entries[i] = leaf.entries[i]
}
for i := removeIndex + 1; i < len(leaf.entries); i++ {
newLeaf.entries[i-1] = leaf.entries[i]
}
return newLeaf
}
func (leaf *Leaf) get(name string) *FileMetadata {
i := sort.Search(len(leaf.entries), func(i int) bool {
return leaf.entries[i].name >= name
})
if i < len(leaf.entries) && leaf.entries[i].name == name {
return leaf.entries[i].metadata
}
return nil
}
func (leaf *Leaf) insert(entry *LeafEntry) *Leaf {
i := sort.Search(len(leaf.entries), func(i int) bool {
return leaf.entries[i].name >= entry.name
})
// replace existing entry
var newLeaf *Leaf
if len(leaf.entries) > i && leaf.entries[i].name == entry.name {
newLeaf = CopyLeafWithMutation(leaf, i, entry)
// stats.valuesReplaced ++;
} else {
// otherwise we have an insertion
newLeaf = CopyLeafWithInsertion(leaf, i, entry)
// stats.valuesInserted ++;
}
return newLeaf
}
func (leaf *Leaf) remove(name string) *Leaf {
i := sort.Search(len(leaf.entries), func(i int) bool {
return leaf.entries[i].name >= name
})
if leaf.entries[i].name == name {
newLeaf := CopyLeafWithRemoval(leaf, i)
return newLeaf
} else {
// otherwise, no entry with that name found, so do nothing
return nil
}
}
type LeafDirService struct {
chunks ChunkService
}
type LeafDir struct {
chunks ChunkService
key *Key
}
func NewLeafDirService(chunks ChunkService) *LeafDirService {
return &LeafDirService{chunks: chunks}
}
func (s *LeafDirService) GetDirectory(key *Key) Directory {
return &LeafDir{chunks: s.chunks, key: key}
}
func UnpackLeafEntry(entry *LeafRecordEntry) *LeafEntry {
return &LeafEntry{name: entry.GetName(), metadata: entry.GetMetadata()}
}
func UnpackLeaf(data []byte) *Leaf {
dest := &LeafRecord{}
err := proto.Unmarshal(data, dest)
if err != nil {
panic(fmt.Sprintf("Could not unmarshal leaf: %s", err.Error()))
}
// convert LeafRecord to Leaf
entries := make([]*LeafEntry, 0, len(dest.GetEntries()))
for _, entry := range dest.GetEntries() {
entries = append(entries, UnpackLeafEntry(entry))
}
return &Leaf{entries: entries}
}
func PackLeaf(leaf *Leaf) []byte {
entries := make([]*LeafRecordEntry, 0, len(leaf.entries))
for _, entry := range leaf.entries {
if entry.metadata == nil {
panic("entry.metadata")
}
e := &LeafRecordEntry{Name: &entry.name, Metadata: entry.metadata}
entries = append(entries, e)
}
src := &LeafRecord{Entries: entries}
data, err := proto.Marshal(src)
if err != nil {
panic(fmt.Sprintf("Couldn't marshal metadata object: %s", err))
}
return data
}
func (d *LeafDir) readLeaf(key *Key) (*Leaf, error) {
if *key == *EMPTY_DIR_KEY {
return &EMPTY_DIR, nil
} else {
resource, err := d.chunks.Get(key)
if err != nil {
return nil, err
}
return UnpackLeaf(resource.AsBytes()), nil
}
}
func writeLeaf(chunks ChunkService, leaf *Leaf) *Key {
buffer := PackLeaf(leaf)
newLeafKey := computeContentKey(buffer)
chunks.Put(newLeafKey, NewMemResource(buffer))
return newLeafKey
}
func (d *LeafDir) writeLeaf(leaf *Leaf) *Key {
return writeLeaf(d.chunks, leaf)
}
func computeContentKey(buffer []byte) *Key {
key := Key(sha256.Sum256(buffer))
return &key
}
func (d *LeafDir) Get(name string) (*FileMetadata, error) {
leaf, err := d.readLeaf(d.key)
if err != nil {
return nil, err
}
return leaf.get(name), nil
}
// create a leaf which only contains the specified metadata and the filenames do not matter
// this is used to create a set of references which are used in the transient refs.
func CreateAnonymousRefLeaf(chunks ChunkService, metadatas []*FileMetadata) *Key {
leaf := &EMPTY_DIR
for i, meta := range metadatas {
leaf = leaf.insert(&LeafEntry{name: strconv.Itoa(i), metadata: meta})
}
return writeLeaf(chunks, leaf)
}
func (d *LeafDir) Put(name string, metadata *FileMetadata) (*Key, int64, error) {
leaf, err := d.readLeaf(d.key)
if err != nil {
return nil, 0, err
}
if metadata == nil {
panic(fmt.Sprintf(">>>> metadata = %s\n", metadata))
}
newLeaf := leaf.insert(&LeafEntry{name: name, metadata: metadata})
totalSize := newLeaf.GetTotalSize()
return d.writeLeaf(newLeaf), totalSize, nil
}
func (d *Leaf) GetTotalSize() int64 {
var totalSize int64
for _, e := range d.entries {
totalSize += e.metadata.GetTotalSize()
}
return totalSize
}
func (d *LeafDir) GetTotalSize() (int64, error) {
leaf, err := d.readLeaf(d.key)
if err != nil {
return 0, err
}
return leaf.GetTotalSize(), nil
}
func (d *LeafDir) Remove(name string) (*Key, int64, error) {
leaf, err := d.readLeaf(d.key)
if err != nil {
return nil, 0, err
}
newLeaf := leaf.remove(name)
if newLeaf == nil {
return d.key, leaf.GetTotalSize(), nil
} else {
return d.writeLeaf(newLeaf), newLeaf.GetTotalSize(), nil
}
}
type LeafIterator struct {
leafIndex int
leaf *Leaf
reachedEnd bool
}
func (it *LeafIterator) HasNext() bool {
return !it.reachedEnd
}
func (it *LeafIterator) Next() (string, *FileMetadata) {
next := it.leaf.entries[it.leafIndex]
it.leafIndex++
if it.leafIndex >= len(it.leaf.entries) {
it.reachedEnd = true
}
return next.name, next.metadata
}
func (d *LeafDir) Iterate() Iterator {
leaf, err := d.readLeaf(d.key)
if err != nil {
panic(err.Error())
}
return &LeafIterator{leafIndex: 0, leaf: leaf, reachedEnd: len(leaf.entries) == 0}
}
/*
func GetLeaf(key *Key) *Leaf {
panic();
}
func GetBranch(key *Key) *Branch {
panic();
}
func copyLeaf(leaf *Leaf) *Leaf {
panic();
}
func persistLeaf(leaf *Leaf) *Key {
panic();
}
type BTreeService interface {
// root might key of either a Branch or Leaf
Insert(root *Key, name string, metadata *FileMetadata) *Key;
Delete(root *Key, name string) *Key;
Get(root *Key, name string) *FileMetadata;
// what is the best interface to provide for walking through entries? An iterator or a callback?
// Probably an iterator because an iterator can always be transformed into a callback
Iterate(root *Key) *Iterator;
}
func NewNamespace(store *NodeStore) {
panic()
}
// test insertion
// root1 := ns.Insert(root0, name, metadata)
// it := ns.Iterator(root1)
// assert it.HasNext()
// entry := it.Next()
// assert !it.HasNext()
// root2 := ns.Insert(root1, name, metadata)
// it := ns.Iterator(root1)
// assert it.HasNext()
// entry := it.Next()
// assert it.HasNext()
// entry := it.Next()
type Iterator interface {
HasNext() bool;
Next() *LeafEntry;
}
type iterator struct {
store *NodeStore;
leafIndex int;
leaf *Leaf;
branchIndex int;
branch *Branch;
reachedEnd bool;
}
func (it *iterator) HasNext() bool{
return !it.reachedEnd;
}
func (it *iterator) Next() *LeafEntry {
next := it.leaf.entries[it.leafIndex];
it.leafIndex ++;
if it.leafIndex >= len(it.leaf.entries) {
if it.branch == nil {
it.reachedEnd = true;
} else {
it.branchIndex ++;
if it.branchIndex >= len(it.branch.children) {
it.reachedEnd = true;
} else {
it.leafIndex = 0;
entry := it.branch.children[it.branchIndex];
if !entry.isLeaf {
panic("expected leaf");
}
it.leaf = it.store.GetLeaf(entry.child);
}
}
}
return next;
}
func insertIntoLeaf(leaf *Leaf, name string, metadata *FileMetadata, stats *TreeStats, MaxBlockSize int) []Key {
i := sort.Search(len(leaf.entries), func(i int) bool {
return leaf.entries[i].name >= name;
});
// replace existing entry
var newLeaf *Leaf;
if(leaf.entries[i].name == name) {
newLeaf = CopyLeafWithMutation(leaf, i, metadata);
stats.valuesReplaced ++;
} else {
// otherwise we have an insertion
newLeaf = CopyLeafWithInsertion(leaf, i, name, metadata);
stats.valuesInserted ++;
}
newLeaves := splitLeafIfTooLarge(newLeaf, MaxBlockSize);
newLeafKeys := make([]Key, len(newLeaves));
for i, l := range(newLeaves) {
k := persistLeaf(l);
newLeafKeys[i] = k;
}
return newLeafKeys;
}
func splitLeafIfTooLarge(leaf *Leaf, maxBlockSize int) []*Leaf {
if(LeafSize(leaf) > maxBlockSize) {
// this block has gotten too big, so split it in half
leafA, leafB := SplitLeaf(leaf)
return &Leaf{leafA, leafB};
} else {
newLeafKey := persistLeaf(leaf);
return BranchEntry{isLeaf: true, key: newLeafKey}
}
}
func findLeafContaining(root *Key, name string) ([]*Branch, []int) {
branch := GetBranch(root)
i := sort.Search(len(branch.entries), func(i int) bool {
return branch.entries[i].lastName >= name;
});
}
func Insert(root *Key, name string, metadata *FileMetadata, MaxBlockSize int, stats *TreeStats) Key {
if(root.isLeaf) {
leaf := GetLeaf(root.key)
keys := insertIntoLeaf(leaf, name, metadata, stats, MaxBlockSize);
if len(keys) > 1 {
newBranch := NewBranch(keys)
return PersistBranch(newBranch)
} else {
return keys[0];
}
} else {
// we've got a branch, so first identify the path to the impacted leaf
var path [] *Branch;
var pathIndex[] int;
var i int;
var newPath[] *Branch;
// perform the insert in the leaf (todo)
// somehow find the actual leaf
path, pathIndex = findLeafContaining(root, name)
keys := insertIntoLeaf(leaf, name, metadata, stats, MaxBlockSize);
if len(keys) == 1 {
// the easy case: we get one leaf back, so we just need to update the existing entry in the
newPath[i] = CopyBranchWithMutation(path[i], pathIndex[i], newEntry);
newBranch := NewBranch(keys)
// merge branch into parent
return PersistBranch(newBranch)
} else {
// we have multiple leaves which need to merge in. This might result in a cascading split of branches
// todo: implement splitting of branches
newPath[i] = CopyBranchWithMerge(path[i], pathIndex[i], entry1, entry2);
return keys[0];
}
newEntry := Insert(path[i].children[pathIndex[i]], name, metadata, MaxBlockSize);
var newPath[] *Branch;
if(!newEntry.isLeaf) {
// if this entry is actual a branch a split occurred and we need to merge it into the parent branch
if(BranchSize(path[i]) > MaxBlockSize) {
// the merge will be too big, so we need to split
newBranch1, newBranch2 := SplitBranchAndMerge(path[i], entry1, entry2)
} else {
// the merge can safely be done by simple insertion
newPath[i] = CopyBranchWithMerge(path[i], pathIndex[i], entry1, entry2);
}
} else {
// otherwise, this entry is a leaf and we can just replace the element in the parent branch
}
return BranchEntry{isLeaf: false, key: newBranchKey}
}
}
*/