diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-01-31 13:18:51 -0500 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-01 12:30:37 -0500 |
commit | 1a17a2cf1ee8b509dd00b7f29a01c13108acb2cc (patch) | |
tree | d6aa197560053444d76dce1bd198a584c051c7b9 /page.go | |
parent | Merge pull request #4 from benbjohnson/api (diff) | |
download | dedo-1a17a2cf1ee8b509dd00b7f29a01c13108acb2cc.tar.gz dedo-1a17a2cf1ee8b509dd00b7f29a01c13108acb2cc.tar.xz |
Add RWTransaction.Put().
Diffstat (limited to 'page.go')
-rw-r--r-- | page.go | 85 |
1 files changed, 73 insertions, 12 deletions
@@ -1,6 +1,8 @@ package bolt import ( + "fmt" + "os" "unsafe" ) @@ -10,6 +12,9 @@ const maxAllocSize = 0xFFFFFFF const minKeysPerPage = 2 const maxNodesPerPage = 65535 +const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{})) +const leafPageElementSize = int(unsafe.Sizeof(leafPageElement{})) + const ( p_branch = 0x01 p_leaf = 0x02 @@ -28,29 +33,46 @@ type page struct { ptr uintptr } +// typ returns a human readable page type string used for debugging. +func (p *page) typ() string { + if (p.flags & p_branch) != 0 { + return "branch" + } else if (p.flags & p_leaf) != 0 { + return "leaf" + } else if (p.flags & p_meta) != 0 { + return "meta" + } else if (p.flags & p_sys) != 0 { + return "system" + } else if (p.flags & p_freelist) != 0 { + return "freelist" + } + return fmt.Sprintf("unknown<%02x>", p.flags) +} + // meta returns a pointer to the metadata section of the page. func (p *page) meta() *meta { return (*meta)(unsafe.Pointer(&p.ptr)) } -// lnode retrieves the leaf node by index -func (p *page) lnode(index int) *lnode { - return &((*[maxNodesPerPage]lnode)(unsafe.Pointer(&p.ptr)))[index] +// leafPageElement retrieves the leaf node by index +func (p *page) leafPageElement(index uint16) *leafPageElement { + n := &((*[maxNodesPerPage]leafPageElement)(unsafe.Pointer(&p.ptr)))[index] + return n } -// lnodes retrieves a list of leaf nodes. -func (p *page) lnodes() []lnode { - return ((*[maxNodesPerPage]lnode)(unsafe.Pointer(&p.ptr)))[:] +// leafPageElements retrieves a list of leaf nodes. +func (p *page) leafPageElements() []leafPageElement { + return ((*[maxNodesPerPage]leafPageElement)(unsafe.Pointer(&p.ptr)))[:] } -// bnode retrieves the branch node by index -func (p *page) bnode(index int) *bnode { - return &((*[maxNodesPerPage]bnode)(unsafe.Pointer(&p.ptr)))[index] +// branchPageElement retrieves the branch node by index +func (p *page) branchPageElement(index uint16) *branchPageElement { + return &((*[maxNodesPerPage]branchPageElement)(unsafe.Pointer(&p.ptr)))[index] } -// bnodes retrieves a list of branch nodes. -func (p *page) bnodes() []bnode { - return ((*[maxNodesPerPage]bnode)(unsafe.Pointer(&p.ptr)))[:] +// branchPageElements retrieves a list of branch nodes. +func (p *page) branchPageElements() []branchPageElement { + return ((*[maxNodesPerPage]branchPageElement)(unsafe.Pointer(&p.ptr)))[:] } // freelist retrieves a list of page ids from a freelist page. @@ -58,8 +80,47 @@ func (p *page) freelist() []pgid { return ((*[maxNodesPerPage]pgid)(unsafe.Pointer(&p.ptr)))[0:p.count] } +// 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) +} + 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 } + +// 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] +} |