bolt/page.go

137 lines
3.4 KiB
Go
Raw Normal View History

2014-01-08 15:06:17 +00:00
package bolt
import (
2014-01-31 18:18:51 +00:00
"fmt"
"os"
2014-01-08 15:06:17 +00:00
"unsafe"
)
2014-01-27 15:11:54 +00:00
const pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))
2014-01-10 14:32:12 +00:00
2014-01-28 21:18:16 +00:00
const maxAllocSize = 0xFFFFFFF
2014-01-27 15:11:54 +00:00
const minKeysPerPage = 2
const maxNodesPerPage = 65535
2014-01-08 15:06:17 +00:00
2014-01-31 18:18:51 +00:00
const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))
const leafPageElementSize = int(unsafe.Sizeof(leafPageElement{}))
2014-01-08 15:06:17 +00:00
const (
2014-02-12 21:57:27 +00:00
branchPageFlag = 0x01
leafPageFlag = 0x02
metaPageFlag = 0x04
freelistPageFlag = 0x10
2014-01-13 17:35:04 +00:00
)
2014-01-09 16:07:10 +00:00
const (
bucketLeafFlag = 0x01
)
2014-01-24 23:32:18 +00:00
type pgid uint64
2014-01-09 16:07:10 +00:00
2014-01-08 15:06:17 +00:00
type page struct {
2014-01-27 15:11:54 +00:00
id pgid
flags uint16
count uint16
overflow uint32
ptr uintptr
2014-01-09 16:07:10 +00:00
}
2014-01-31 18:18:51 +00:00
// typ returns a human readable page type string used for debugging.
func (p *page) typ() string {
2014-02-12 21:57:27 +00:00
if (p.flags & branchPageFlag) != 0 {
2014-01-31 18:18:51 +00:00
return "branch"
2014-02-12 21:57:27 +00:00
} else if (p.flags & leafPageFlag) != 0 {
2014-01-31 18:18:51 +00:00
return "leaf"
2014-02-12 21:57:27 +00:00
} else if (p.flags & metaPageFlag) != 0 {
2014-01-31 18:18:51 +00:00
return "meta"
2014-02-12 21:57:27 +00:00
} else if (p.flags & freelistPageFlag) != 0 {
2014-01-31 18:18:51 +00:00
return "freelist"
}
return fmt.Sprintf("unknown<%02x>", p.flags)
}
2014-01-10 14:32:12 +00:00
// meta returns a pointer to the metadata section of the page.
2014-01-30 03:35:58 +00:00
func (p *page) meta() *meta {
return (*meta)(unsafe.Pointer(&p.ptr))
2014-01-27 15:11:54 +00:00
}
2014-01-31 18:18:51 +00:00
// leafPageElement retrieves the leaf node by index
func (p *page) leafPageElement(index uint16) *leafPageElement {
n := &((*[maxNodesPerPage]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]
return n
}
2014-01-30 03:35:58 +00:00
2014-01-31 18:18:51 +00:00
// leafPageElements retrieves a list of leaf nodes.
func (p *page) leafPageElements() []leafPageElement {
return ((*[maxNodesPerPage]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
2014-01-30 05:11:46 +00:00
}
2014-01-31 18:18:51 +00:00
// branchPageElement retrieves the branch node by index
func (p *page) branchPageElement(index uint16) *branchPageElement {
return &((*[maxNodesPerPage]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]
2014-01-30 03:35:58 +00:00
}
2014-01-30 03:50:29 +00:00
2014-01-31 18:18:51 +00:00
// branchPageElements retrieves a list of branch nodes.
func (p *page) branchPageElements() []branchPageElement {
return ((*[maxNodesPerPage]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
2014-01-30 05:11:46 +00:00
}
2014-01-31 18:18:51 +00:00
// dump writes n bytes of the page to STDERR as hex output.
func (p *page) hexdump(n int) {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:n]
fmt.Fprintf(os.Stderr, "%x\n", buf)
}
2014-01-30 23:22:02 +00:00
type pages []*page
func (s pages) Len() int { return len(s) }
func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s pages) Less(i, j int) bool { return s[i].id < s[j].id }
2014-01-31 18:18:51 +00:00
// branchPageElement represents a node on a branch page.
type branchPageElement struct {
pos uint32
ksize uint32
pgid pgid
}
// key returns a byte slice of the node key.
func (n *branchPageElement) key() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return buf[n.pos : n.pos+n.ksize]
}
// leafPageElement represents a node on a leaf page.
type leafPageElement struct {
flags uint32
pos uint32
ksize uint32
vsize uint32
}
// key returns a byte slice of the node key.
func (n *leafPageElement) key() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return buf[n.pos : n.pos+n.ksize]
}
// value returns a byte slice of the node value.
func (n *leafPageElement) value() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return buf[n.pos+n.ksize : n.pos+n.ksize+n.vsize]
}
2014-03-22 04:34:54 +00:00
// PageInfo represents human readable information about a page.
type PageInfo struct {
ID int
Type string
Count int
OverflowCount int
}
type pgids []pgid
func (s pgids) Len() int { return len(s) }
func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
2014-05-19 20:11:32 +00:00
func (s pgids) Less(i, j int) bool { return s[i] < s[j] }