aboutsummaryrefslogtreecommitdiff
path: root/bucket.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-05-23 13:04:18 -0600
committerBen Johnson <benbjohnson@yahoo.com>2014-05-23 13:04:18 -0600
commit271129e40ca0cbbaad299d467488528680a22dbd (patch)
tree98e9600c83083994a6b9cd5e4ddbaa6f02e8b779 /bucket.go
parentMerge pull request #169 from benbjohnson/allocation (diff)
parentRemove allocations from read-only buckets. (diff)
downloaddedo-271129e40ca0cbbaad299d467488528680a22dbd.tar.gz
dedo-271129e40ca0cbbaad299d467488528680a22dbd.tar.xz
Merge pull request #172 from benbjohnson/allocation
Remove allocations from read-only buckets.
Diffstat (limited to 'bucket.go')
-rw-r--r--bucket.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/bucket.go b/bucket.go
index 877c310..2338a8b 100644
--- a/bucket.go
+++ b/bucket.go
@@ -76,8 +76,8 @@ type bucket struct {
// newBucket returns a new bucket associated with a transaction.
func newBucket(tx *Tx) Bucket {
var b = Bucket{tx: tx}
- b.buckets = make(map[string]*Bucket)
if tx.writable {
+ b.buckets = make(map[string]*Bucket)
b.nodes = make(map[pgid]*node)
}
return b
@@ -115,8 +115,10 @@ func (b *Bucket) Cursor() *Cursor {
// Bucket retrieves a nested bucket by name.
// Returns nil if the bucket does not exist.
func (b *Bucket) Bucket(name []byte) *Bucket {
- if child := b.buckets[string(name)]; child != nil {
- return child
+ if b.buckets != nil {
+ if child := b.buckets[string(name)]; child != nil {
+ return child
+ }
}
// Move cursor to key.
@@ -130,7 +132,9 @@ func (b *Bucket) Bucket(name []byte) *Bucket {
// Otherwise create a bucket and cache it.
var child = b.openBucket(v)
- b.buckets[string(name)] = child
+ if b.buckets != nil {
+ b.buckets[string(name)] = child
+ }
return child
}
@@ -139,8 +143,15 @@ func (b *Bucket) Bucket(name []byte) *Bucket {
// from a parent into a Bucket
func (b *Bucket) openBucket(value []byte) *Bucket {
var child = newBucket(b.tx)
- child.bucket = &bucket{}
- *child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))
+
+ // If this is a writable transaction then we need to copy the bucket entry.
+ // Read-only transactions can point directly at the mmap entry.
+ if b.tx.writable {
+ child.bucket = &bucket{}
+ *child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))
+ } else {
+ child.bucket = (*bucket)(unsafe.Pointer(&value[0]))
+ }
// Save a reference to the inline page if the bucket is inline.
if child.root == 0 {