bolt/page.go

152 lines
3.4 KiB
Go
Raw Normal View History

2014-01-08 15:06:17 +00:00
package bolt
import (
"bytes"
2014-01-08 15:06:17 +00:00
"unsafe"
)
2014-01-10 14:32:12 +00:00
const maxPageSize = 0x8000
2014-01-12 05:51:01 +00:00
const minKeyCount = 2
2014-01-10 14:32:12 +00:00
var _page page
2014-01-12 05:51:01 +00:00
const pageHeaderSize = int(unsafe.Offsetof(_page.ptr))
2014-01-10 14:32:12 +00:00
const minPageKeys = 2
const fillThreshold = 250 // 25%
2014-01-08 15:06:17 +00:00
const (
2014-01-10 14:32:12 +00:00
p_branch = 0x01
p_leaf = 0x02
p_overflow = 0x04
p_meta = 0x08
2014-01-24 19:51:56 +00:00
p_dirty = 0x10 /**< dirty page, also set for #P_SUBP pages */
2014-01-12 05:51:01 +00:00
p_invalid = ^pgno(0)
2014-01-08 15:06:17 +00:00
)
2014-01-09 16:07:10 +00:00
// maxCommitPages is the maximum number of pages to commit in one writev() call.
2014-01-12 05:51:01 +00:00
const maxCommitPages = 64
2014-01-09 16:07:10 +00:00
/* max bytes to write in one call */
2014-01-12 05:51:01 +00:00
const maxWriteByteCount uint = 0x80000000 // TODO: #define MAX_WRITE 0x80000000U >> (sizeof(ssize_t) == 4))
2014-01-09 16:07:10 +00:00
// TODO:
// #if defined(IOV_MAX) && IOV_MAX < MDB_COMMIT_PAGES
// #undef MDB_COMMIT_PAGES
// #define MDB_COMMIT_PAGES IOV_MAX
// #endif
2014-01-13 17:35:04 +00:00
const (
MDB_PS_MODIFY = 1
MDB_PS_ROOTONLY = 2
MDB_PS_FIRST = 4
MDB_PS_LAST = 8
)
2014-01-09 16:07:10 +00:00
// TODO: #define MDB_SPLIT_REPLACE MDB_APPENDDUP /**< newkey is not new */
2014-01-10 14:32:12 +00:00
type pgno uint64
2014-01-12 05:51:01 +00:00
type txnid uint64
type indx uint16
2014-01-09 16:07:10 +00:00
2014-01-08 15:06:17 +00:00
type page struct {
2014-01-10 14:32:12 +00:00
id pgno
flags uint16
2014-01-12 05:51:01 +00:00
lower indx
upper indx
overflow uint32
ptr uintptr
2014-01-09 16:07:10 +00:00
}
2014-01-10 14:32:12 +00:00
// meta returns a pointer to the metadata section of the page.
func (p *page) meta() (*meta, error) {
// Exit if page is not a meta page.
2014-01-12 05:51:01 +00:00
if (p.flags & p_meta) == 0 {
return nil, InvalidMetaPageError
2014-01-10 14:32:12 +00:00
}
// Cast the meta section and validate before returning.
m := (*meta)(unsafe.Pointer(&p.ptr))
if err := m.validate(); err != nil {
return nil, err
}
return m, nil
}
2014-01-13 15:25:56 +00:00
// init initializes a page as a new meta page.
func (p *page) init(pageSize int) {
2014-01-12 05:51:01 +00:00
p.flags = p_meta
m := (*meta)(unsafe.Pointer(&p.ptr))
m.magic = magic
m.version = version
m.free.pad = uint32(pageSize)
m.pgno = 1
m.free.root = p_invalid
2014-01-14 20:01:02 +00:00
m.buckets.root = p_invalid
2014-01-12 05:51:01 +00:00
}
2014-01-10 14:32:12 +00:00
// branchNode retrieves the branch node at the given index within the page.
func (p *page) branchNode(index indx) *branchNode {
b := (*[maxPageSize]byte)(unsafe.Pointer(&p.ptr))
2014-01-21 21:37:55 +00:00
return (*branchNode)(unsafe.Pointer(&b[index*indx(unsafe.Sizeof(index))]))
}
// leafNode retrieves the leaf node at the given index within the page.
func (p *page) leafNode(index indx) *leafNode {
b := (*[maxPageSize]byte)(unsafe.Pointer(&p.ptr))
2014-01-21 21:37:55 +00:00
return (*leafNode)(unsafe.Pointer(&b[index*indx(unsafe.Sizeof(index))]))
}
// numkeys returns the number of nodes in the page.
func (p *page) numkeys() int {
return int((p.lower - indx(pageHeaderSize)) >> 1)
2014-01-08 15:06:17 +00:00
}
// remainingSize returns the number of bytes left in the page.
func (p *page) remainingSize() int {
2014-01-12 05:51:01 +00:00
return int(p.upper - p.lower)
2014-01-08 15:06:17 +00:00
}
// find returns the node with the smallest entry larger or equal to the key.
// This function also returns a boolean stating if an exact match was made.
func (p *page) find(key []byte, pageSize int) (*node, int, bool) {
// TODO: MDB_page *mp = mc->mc_pg[mc->mc_top];
var node *node
nkeys := p.numkeys()
2014-01-21 21:37:55 +00:00
low, high := 1, nkeys-1
if (p.flags & p_leaf) != 0 {
low = 0
}
// Perform a binary search to find the correct node.
var i, rc int
2014-01-21 21:37:55 +00:00
for low <= high {
i = (low + high) / 2
node = p.node(indx(i))
rc = bytes.Compare(key, node.key())
if rc == 0 {
2014-01-21 21:37:55 +00:00
break
} else if rc > 0 {
low = i + 1
} else {
high = i - 1
}
}
// Found entry is less than key so grab the next one.
if rc > 0 {
i++
}
// If index is beyond key range then return nil.
if i >= nkeys {
node = nil
}
exact := (rc == 0 && nkeys > 0)
return node, i, exact
}