mirror of https://github.com/hak5/bolt.git
Add freelist page type.
parent
4fc84daf2a
commit
4ad445aa85
|
@ -1,11 +1,5 @@
|
|||
package bolt
|
||||
|
||||
var (
|
||||
InvalidBucketError = &Error{"invalid bucket", nil}
|
||||
)
|
||||
|
||||
type bucketid uint32
|
||||
|
||||
type Bucket struct {
|
||||
*bucket
|
||||
name string
|
||||
|
@ -14,8 +8,6 @@ type Bucket struct {
|
|||
}
|
||||
|
||||
type bucket struct {
|
||||
id bucketid
|
||||
flags uint32
|
||||
root pgid
|
||||
}
|
||||
|
||||
|
|
18
db.go
18
db.go
|
@ -146,18 +146,34 @@ func (db *DB) init() error {
|
|||
db.pageSize = db.os.Getpagesize()
|
||||
|
||||
// Create two meta pages on a buffer.
|
||||
buf := make([]byte, db.pageSize*2)
|
||||
buf := make([]byte, db.pageSize*4)
|
||||
for i := 0; i < 2; i++ {
|
||||
p := db.pageInBuffer(buf[:], pgid(i))
|
||||
p.id = pgid(i)
|
||||
p.flags = p_meta
|
||||
|
||||
// Initialize the meta page.
|
||||
m := p.meta()
|
||||
m.magic = magic
|
||||
m.version = Version
|
||||
m.pageSize = uint32(db.pageSize)
|
||||
m.version = Version
|
||||
m.free = 3
|
||||
m.sys.root = 4
|
||||
}
|
||||
|
||||
// Write an empty freelist at page 3.
|
||||
p := db.pageInBuffer(buf[:], pgid(2))
|
||||
p.id = pgid(3)
|
||||
p.flags = p_freelist
|
||||
p.count = 0
|
||||
|
||||
// Write an empty leaf page at page 4.
|
||||
p = db.pageInBuffer(buf[:], pgid(3))
|
||||
p.id = pgid(4)
|
||||
p.flags = p_leaf
|
||||
p.count = 0
|
||||
|
||||
// Write the buffer to our data file.
|
||||
if _, err := db.metafile.WriteAt(buf, 0); err != nil {
|
||||
return err
|
||||
|
|
1
meta.go
1
meta.go
|
@ -14,6 +14,7 @@ type meta struct {
|
|||
pageSize uint32
|
||||
pgid pgid
|
||||
txnid txnid
|
||||
free pgid
|
||||
sys bucket
|
||||
}
|
||||
|
||||
|
|
12
page.go
12
page.go
|
@ -11,9 +11,10 @@ const minKeysPerPage = 2
|
|||
const maxNodesPerPage = 65535
|
||||
|
||||
const (
|
||||
p_branch = 0x01
|
||||
p_leaf = 0x02
|
||||
p_meta = 0x04
|
||||
p_branch = 0x01
|
||||
p_leaf = 0x02
|
||||
p_meta = 0x04
|
||||
p_freelist = 0x08
|
||||
)
|
||||
|
||||
type pgid uint64
|
||||
|
@ -40,3 +41,8 @@ func (p *page) lnode(index int) *lnode {
|
|||
func (p *page) bnode(index int) *bnode {
|
||||
return &((*[maxNodesPerPage]bnode)(unsafe.Pointer(&p.ptr)))[index]
|
||||
}
|
||||
|
||||
// freelist retrieves a list of page ids from a freelist page.
|
||||
func (p *page) freelist() []pgid {
|
||||
return ((*[maxNodesPerPage]pgid)(unsafe.Pointer(&p.ptr)))[0:p.count]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue