blob: bca9615f0fd649b4badac333ef07b47895f302f9 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001package bbolt
2
3import (
4 "fmt"
5 "os"
6 "sort"
7 "unsafe"
8)
9
10const pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))
11
12const minKeysPerPage = 2
13
14const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))
15const leafPageElementSize = int(unsafe.Sizeof(leafPageElement{}))
16
17const (
18 branchPageFlag = 0x01
19 leafPageFlag = 0x02
20 metaPageFlag = 0x04
21 freelistPageFlag = 0x10
22)
23
24const (
25 bucketLeafFlag = 0x01
26)
27
28type pgid uint64
29
30type page struct {
31 id pgid
32 flags uint16
33 count uint16
34 overflow uint32
35 ptr uintptr
36}
37
38// typ returns a human readable page type string used for debugging.
39func (p *page) typ() string {
40 if (p.flags & branchPageFlag) != 0 {
41 return "branch"
42 } else if (p.flags & leafPageFlag) != 0 {
43 return "leaf"
44 } else if (p.flags & metaPageFlag) != 0 {
45 return "meta"
46 } else if (p.flags & freelistPageFlag) != 0 {
47 return "freelist"
48 }
49 return fmt.Sprintf("unknown<%02x>", p.flags)
50}
51
52// meta returns a pointer to the metadata section of the page.
53func (p *page) meta() *meta {
54 return (*meta)(unsafe.Pointer(&p.ptr))
55}
56
57// leafPageElement retrieves the leaf node by index
58func (p *page) leafPageElement(index uint16) *leafPageElement {
59 n := &((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]
60 return n
61}
62
63// leafPageElements retrieves a list of leaf nodes.
64func (p *page) leafPageElements() []leafPageElement {
65 if p.count == 0 {
66 return nil
67 }
68 return ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
69}
70
71// branchPageElement retrieves the branch node by index
72func (p *page) branchPageElement(index uint16) *branchPageElement {
73 return &((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]
74}
75
76// branchPageElements retrieves a list of branch nodes.
77func (p *page) branchPageElements() []branchPageElement {
78 if p.count == 0 {
79 return nil
80 }
81 return ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
82}
83
84// dump writes n bytes of the page to STDERR as hex output.
85func (p *page) hexdump(n int) {
86 buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:n]
87 fmt.Fprintf(os.Stderr, "%x\n", buf)
88}
89
90type pages []*page
91
92func (s pages) Len() int { return len(s) }
93func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
94func (s pages) Less(i, j int) bool { return s[i].id < s[j].id }
95
96// branchPageElement represents a node on a branch page.
97type branchPageElement struct {
98 pos uint32
99 ksize uint32
100 pgid pgid
101}
102
103// key returns a byte slice of the node key.
104func (n *branchPageElement) key() []byte {
105 buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
106 return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]
107}
108
109// leafPageElement represents a node on a leaf page.
110type leafPageElement struct {
111 flags uint32
112 pos uint32
113 ksize uint32
114 vsize uint32
115}
116
117// key returns a byte slice of the node key.
118func (n *leafPageElement) key() []byte {
119 buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
120 return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize:n.ksize]
121}
122
123// value returns a byte slice of the node value.
124func (n *leafPageElement) value() []byte {
125 buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
126 return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize:n.vsize]
127}
128
129// PageInfo represents human readable information about a page.
130type PageInfo struct {
131 ID int
132 Type string
133 Count int
134 OverflowCount int
135}
136
137type pgids []pgid
138
139func (s pgids) Len() int { return len(s) }
140func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
141func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
142
143// merge returns the sorted union of a and b.
144func (a pgids) merge(b pgids) pgids {
145 // Return the opposite slice if one is nil.
146 if len(a) == 0 {
147 return b
148 }
149 if len(b) == 0 {
150 return a
151 }
152 merged := make(pgids, len(a)+len(b))
153 mergepgids(merged, a, b)
154 return merged
155}
156
157// mergepgids copies the sorted union of a and b into dst.
158// If dst is too small, it panics.
159func mergepgids(dst, a, b pgids) {
160 if len(dst) < len(a)+len(b) {
161 panic(fmt.Errorf("mergepgids bad len %d < %d + %d", len(dst), len(a), len(b)))
162 }
163 // Copy in the opposite slice if one is nil.
164 if len(a) == 0 {
165 copy(dst, b)
166 return
167 }
168 if len(b) == 0 {
169 copy(dst, a)
170 return
171 }
172
173 // Merged will hold all elements from both lists.
174 merged := dst[:0]
175
176 // Assign lead to the slice with a lower starting value, follow to the higher value.
177 lead, follow := a, b
178 if b[0] < a[0] {
179 lead, follow = b, a
180 }
181
182 // Continue while there are elements in the lead.
183 for len(lead) > 0 {
184 // Merge largest prefix of lead that is ahead of follow[0].
185 n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })
186 merged = append(merged, lead[:n]...)
187 if n >= len(lead) {
188 break
189 }
190
191 // Swap lead and follow.
192 lead, follow = follow, lead[n:]
193 }
194
195 // Append what's left in follow.
196 _ = append(merged, follow...)
197}