diff options
Diffstat (limited to '')
-rw-r--r-- | src/dedo.go | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/src/dedo.go b/src/dedo.go index f280c3b..e443a44 100644 --- a/src/dedo.go +++ b/src/dedo.go @@ -585,17 +585,21 @@ func newBucket(tx *transactionT) bucketT { /// bucketT.Cursor() creates a cursor associated with the bucket. The cursor is /// only valid as long as the transaction is open. Do not use a cursor after /// the transaction is closed. -func (b *bucketT) Cursor() *cursorT { +func bucketCursor(b *bucketT) *cursorT { return &cursorT{ bucket: b, stack: make([]elemRef, 0), } } +func (b *bucketT) Cursor() *cursorT { + return bucketCursor(b) +} + /// bucketT.Bucket() retrieves a nested bucket by name. Returns nil if the /// bucket does not exist. The bucket instance is only valid for the lifetime /// of the transaction. -func (b *bucketT) Bucket(name []byte) *bucketT { +func bucketGetNested(b *bucketT, name []byte) *bucketT { if b.buckets != nil { child := b.buckets[string(name)] if child != nil { @@ -621,6 +625,10 @@ func (b *bucketT) Bucket(name []byte) *bucketT { return child } +func (b *bucketT) Bucket(name []byte) *bucketT { + return bucketGetNested(b, name) +} + /// bucketT.Helper() method that re-interprets a sub-bucket value from a parent /// into a bucketT func (b *bucketT) openBucket(value []byte) *bucketT { @@ -647,7 +655,7 @@ func (b *bucketT) openBucket(value []byte) *bucketT { /// new bucket. Returns an error if the key already exists, if the bucket name /// is blank, or if the bucket name is too long. The bucket instance is only /// valid for the lifetime of the transaction. -func (b *bucketT) CreateBucket(key []byte) (*bucketT, error) { +func bucketCreateBucket(b *bucketT, key []byte) (*bucketT, error) { if b.tx.db == nil { return nil, ErrTxClosed } else if !b.tx.writable { @@ -688,11 +696,15 @@ func (b *bucketT) CreateBucket(key []byte) (*bucketT, error) { return b.Bucket(key), nil } +func (b *bucketT) CreateBucket(key []byte) (*bucketT, error) { + return bucketCreateBucket(b, key) +} + /// bucketT.CreateBucketIfNotExists() creates a new bucket if it doesn't already /// exist and returns a reference to it. Returns an error if the bucket name is /// blank, or if the bucket name is too long. The bucket instance is only valid /// for the lifetime of the transaction. -func (b *bucketT) CreateBucketIfNotExists(key []byte) (*bucketT, error) { +func bucketCreateBucketIfNotExists(b *bucketT, key []byte) (*bucketT, error) { child, err := b.CreateBucket(key) if err == ErrBucketExists { return b.Bucket(key), nil @@ -702,9 +714,13 @@ func (b *bucketT) CreateBucketIfNotExists(key []byte) (*bucketT, error) { return child, nil } +func (b *bucketT) CreateBucketIfNotExists(key []byte) (*bucketT, error) { + return bucketCreateBucketIfNotExists(b, key) +} + /// bucketT.DeleteBucket() deletes a bucket at the given key. Returns an error /// if the bucket does not exists, or if the key represents a non-bucket value. -func (b *bucketT) DeleteBucket(key []byte) error { +func bucketDeleteBucket(b *bucketT, key []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.tx.writable { @@ -751,10 +767,14 @@ func (b *bucketT) DeleteBucket(key []byte) error { return nil } +func (b *bucketT) DeleteBucket(key []byte) error { + return bucketDeleteBucket(b, key) +} + /// bucketT.Get() retrieves the value for a key in the bucket. Returns a nil /// value if the key does not exist or if the key is a nested bucket. The /// returned value is only valid for the life of the transaction. -func (b *bucketT) Get(key []byte) []byte { +func bucketGet(b *bucketT, key []byte) []byte { k, v, flags := b.Cursor().seek(key) // Return nil if this is a bucket. @@ -770,12 +790,16 @@ func (b *bucketT) Get(key []byte) []byte { return v } +func (b *bucketT) Get(key []byte) []byte { + return bucketGet(b, key) +} + /// bucketT.Put() sets the value for a key in the bucket. If the key exist then /// its previous value will be overwritten. Supplied value must remain valid /// for the life of the transaction. Returns an error if the bucket was created /// from a read-only transaction, if the key is blank, if the key is too large, /// or if the value is too large. -func (b *bucketT) Put(key []byte, value []byte) error { +func bucketPut(b *bucketT, key []byte, value []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.tx.writable { @@ -804,10 +828,14 @@ func (b *bucketT) Put(key []byte, value []byte) error { return nil } +func (b *bucketT) Put(key []byte, value []byte) error { + return bucketPut(b, key, value) +} + /// bucketT.Delete() removes a key from the bucket. If the key does not exist /// then nothing is done and a nil error is returned. Returns an error if the /// bucket was created from a read-only transaction. -func (b *bucketT) Delete(key []byte) error { +func bucketDelete(b *bucketT, key []byte) error { if b.tx.db == nil { return ErrTxClosed } else if !b.tx.writable { @@ -829,13 +857,17 @@ func (b *bucketT) Delete(key []byte) error { return nil } +func (b *bucketT) Delete(key []byte) error { + return bucketDelete(b, key) +} + func bytesLE(num uint64) []byte { arr := make([]byte, 8) binary.LittleEndian.PutUint64(arr, uint64(num)) return arr } -func (b *bucketT) NextID() []byte { +func bucketNextID(b *bucketT) []byte { id, err := b.NextSequence() if err != nil { panic(err) @@ -843,8 +875,12 @@ func (b *bucketT) NextID() []byte { return bytesLE(id) } +func (b *bucketT) NextID() []byte { + return bucketNextID(b) +} + /// bucketT.NextSequence() returns an autoincrementing integer for the bucket. -func (b *bucketT) NextSequence() (uint64, error) { +func bucketNextSequence(b *bucketT) (uint64, error) { if b.tx.db == nil { return 0, ErrTxClosed } else if !b.tx.writable { @@ -862,11 +898,15 @@ func (b *bucketT) NextSequence() (uint64, error) { return b.ref.sequence, nil } +func (b *bucketT) NextSequence() (uint64, error) { + return bucketNextSequence(b) +} + /// bucketT.ForEach() executes a function for each key/value pair in a bucket. /// If the provided function returns an error then the iteration is stopped and /// the error is returned to the caller. The provided function must not modify /// the bucket; this will result in undefined behavior. -func (b *bucketT) ForEach(fn func(k, v []byte) error) error { +func bucketForEach(b *bucketT, fn func(k, v []byte) error) error { if b.tx.db == nil { return ErrTxClosed } @@ -880,6 +920,10 @@ func (b *bucketT) ForEach(fn func(k, v []byte) error) error { return nil } +func (b *bucketT) ForEach(fn func(k, v []byte) error) error { + return bucketForEach(b, fn) +} + /// bucketT.forEachPageNode() iterates over every page (or node) in a bucket. /// This also includes inline pages. func bucketForEachPageNode(b *bucketT, fn func(*page, *node, int)) { |