bolt/page.go

104 lines
2.2 KiB
Go
Raw Normal View History

2014-01-08 15:06:17 +00:00
package bolt
import (
"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
p_dirty = 0x10 /**< dirty page, also set for #P_SUBP pages */
p_sub = 0x40
p_keep = 0x8000 /**< leave this page alone during spill */
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 int
2014-01-12 05:51:01 +00:00
lower indx
upper indx
2014-01-10 14:32:12 +00:00
overflow int
ptr int
2014-01-08 15:06:17 +00:00
}
2014-01-13 17:35:04 +00:00
type pagestate struct {
2014-01-12 05:51:01 +00:00
head int /**< Reclaimed freeDB pages, or NULL before use */
last int /**< ID of last used record, or 0 if !mf_pghead */
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
2014-01-08 15:06:17 +00:00
// nodeCount returns the number of nodes on the page.
func (p *page) nodeCount() int {
return 0 // (p.header.lower - unsafe.Sizeof(p.header) >> 1
}
// 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
}