aboutsummaryrefslogtreecommitdiff
path: root/branch.go
blob: c9fc7cad416c369c720d4938e7bd70188cbc3b2a (plain) (blame)
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
package bolt

import (
	"bytes"
	"unsafe"
)

// branch represents a temporary in-memory branch page.
type branch struct {
	parent *branch
	items branchItems
}

// put adds a new node or replaces an existing node.
func (b *branch) put(id pgid, newid pgid, key []byte, replace bool) {
	var index int
	for ; index < len(b.items); index++ {
		if b.items[index].pgid == id {
			break
		}
	}

	if !replace {
		index++
		b.items = append(b.items, branchItem{})
		if index < len(b.items) {
			copy(b.items[index+1:], b.items[index:])
		}
	}

	b.items[index].pgid = newid
	b.items[index].key = key
}

// read initializes the item data from an on-disk page.
func (b *branch) read(page *page) {
	ncount := int(page.count)
	b.items = make(branchItems, ncount)
	bnodes := (*[maxNodesPerPage]bnode)(unsafe.Pointer(&page.ptr))
	for i := 0; i < ncount; i++ {
		bnode := &bnodes[i]
		item := &b.items[i]
		item.key = bnode.key()
	}
}


type branchItems []branchItem

type branchItem struct {
	pgid pgid
	key   []byte
}

func (s branchItems) Len() int           { return len(s) }
func (s branchItems) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s branchItems) Less(i, j int) bool { return bytes.Compare(s[i].key, s[j].key) == -1 }