pull/34/head
Ben Johnson 2014-01-30 17:04:56 -05:00
parent 5f1c96f08a
commit 8d5757e9dd
7 changed files with 24 additions and 26 deletions

View File

@ -8,7 +8,7 @@ import (
// branch represents a temporary in-memory branch page.
type branch struct {
parent *branch
items branchItems
items branchItems
}
// size returns the size of the branch after serialization.
@ -80,7 +80,7 @@ func (b *branch) write(p *page) {
func (b *branch) split(pageSize int) []*branch {
// Ignore the split if the page doesn't have at least enough nodes for
// multiple pages or if the data can fit on a single page.
if len(b.items) <= (minKeysPerPage * 2) || b.size() < pageSize {
if len(b.items) <= (minKeysPerPage*2) || b.size() < pageSize {
return []*branch{b}
}
@ -95,7 +95,7 @@ func (b *branch) split(pageSize int) []*branch {
for index, item := range b.items {
nodeSize := bnodeSize + len(item.key)
if (len(current.items) >= minKeysPerPage && index < len(b.items)-minKeysPerPage && size+nodeSize > threshold) {
if len(current.items) >= minKeysPerPage && index < len(b.items)-minKeysPerPage && size+nodeSize > threshold {
size = pageHeaderSize
branches = append(branches, current)
current = &branch{}
@ -113,10 +113,9 @@ type branchItems []branchItem
type branchItem struct {
pgid pgid
key []byte
key []byte
}
func (s branchItems) Len() int { return len(s) }
func (s branchItems) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s branchItems) Less(i, j int) bool { return bytes.Compare(s[i].key, s[j].key) == -1 }

View File

@ -7,7 +7,7 @@ type Bucket struct {
}
type bucket struct {
root pgid
root pgid
}
// Name returns the name of the bucket.
@ -24,7 +24,7 @@ func (b *Bucket) Get(key []byte) []byte {
func (b *Bucket) Cursor() *Cursor {
return &Cursor{
transaction: b.transaction,
root: b.root,
stack: make([]elem, 0),
root: b.root,
stack: make([]elem, 0),
}
}

View File

@ -7,8 +7,8 @@ import (
type Cursor struct {
transaction *Transaction
root pgid
stack []elem
root pgid
stack []elem
}
// elem represents a node on a page that's on the cursor's stack.
@ -72,7 +72,7 @@ func (c *Cursor) nsearch(key []byte, p *page) {
// Binary search for the correct leaf node index.
nodes := p.lnodes()
e.index = sort.Search(int(p.count), func(i int) bool {
e.index = sort.Search(int(p.count), func(i int) bool {
return bytes.Compare(nodes[i].key(), key) != -1
})
}

2
db.go
View File

@ -228,7 +228,7 @@ func (db *DB) RWTransaction() (*RWTransaction, error) {
// Create a transaction associated with the database.
t := &RWTransaction{
branches: make(map[pgid]*branch),
leafs: make(map[pgid]*leaf),
leafs: make(map[pgid]*leaf),
}
t.init(db, db.meta())

View File

@ -9,7 +9,7 @@ import (
// leaf represents an in-memory, deserialized leaf page.
type leaf struct {
parent *branch
items leafItems
items leafItems
}
// size returns the size of the leaf after serialization.
@ -78,7 +78,7 @@ func (l *leaf) write(p *page) {
func (l *leaf) split(pageSize int) []*leaf {
// Ignore the split if the page doesn't have at least enough nodes for
// multiple pages or if the data can fit on a single page.
if len(l.items) <= (minKeysPerPage * 2) || l.size() < pageSize {
if len(l.items) <= (minKeysPerPage*2) || l.size() < pageSize {
return []*leaf{l}
}
@ -93,7 +93,7 @@ func (l *leaf) split(pageSize int) []*leaf {
for index, item := range l.items {
nodeSize := lnodeSize + len(item.key) + len(item.value)
if (len(current.items) >= minKeysPerPage && index < len(l.items)-minKeysPerPage && size+nodeSize > threshold) {
if len(current.items) >= minKeysPerPage && index < len(l.items)-minKeysPerPage && size+nodeSize > threshold {
size = pageHeaderSize
leafs = append(leafs, current)
current = &leaf{}
@ -117,4 +117,3 @@ type leafItem struct {
func (s leafItems) Len() int { return len(s) }
func (s leafItems) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s leafItems) Less(i, j int) bool { return bytes.Compare(s[i].key, s[j].key) == -1 }

View File

@ -9,7 +9,7 @@ import (
type RWTransaction struct {
Transaction
branches map[pgid]*branch
leafs map[pgid]*leaf
leafs map[pgid]*leaf
}
// CreateBucket creates a new bucket.

View File

@ -28,16 +28,16 @@ func TestTransactionCreateBucket(t *testing.T) {
assert.NoError(t, err)
/*
// Open a separate read-only transaction.
rtxn, err := db.Transaction()
assert.NotNil(t, txn)
assert.NoError(t, err)
// Open a separate read-only transaction.
rtxn, err := db.Transaction()
assert.NotNil(t, txn)
assert.NoError(t, err)
b, err := rtxn.Bucket("widgets")
assert.NoError(t, err)
if assert.NotNil(t, b) {
assert.Equal(t, b.Name(), "widgets")
}
b, err := rtxn.Bucket("widgets")
assert.NoError(t, err)
if assert.NotNil(t, b) {
assert.Equal(t, b.Name(), "widgets")
}
*/
})
}