bolt/page.go

59 lines
1.1 KiB
Go
Raw Normal View History

2014-01-08 15:06:17 +00:00
package bolt
import (
"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
const (
2014-01-24 23:32:18 +00:00
p_branch = 0x01
p_leaf = 0x02
p_meta = 0x04
2014-01-13 17:35:04 +00:00
)
2014-01-09 16:07:10 +00:00
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-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
2014-01-24 23:32:18 +00:00
m.pageSize = uint32(pageSize)
m.pgid = 1
2014-01-27 15:11:54 +00:00
m.sys.root = 0
}
// lnode retrieves the leaf node by index
func (p *page) lnode(index int) *lnode {
return &((*[maxNodesPerPage]lnode)(unsafe.Pointer(&p.ptr)))[index]
}