bh

package module
v0.0.0-...-5f8bac9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2016 License: BSD-3-Clause Imports: 9 Imported by: 9

README

bolthelper

Helper types for bolt.

They wraps the following things:

Documentation

Overview

Package bh provides some helper classes for convenience of using github.com/boltdb/bolt package. All wrapper classes will use their friend wrapper classes whenever possible. All returned errors are with call stackes using github.com/golangplus/errors.

Index

Constants

This section is empty.

Variables

View Source
var ErrBoxDataPathNotSpecified = errors.New("DataPath func of RefCountBox was not specified")

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	*bolt.Bucket
}

A wrapper to *bolt.Bucket.

func (Bucket) CreateBucketIfNotExists

func (b Bucket) CreateBucketIfNotExists(folders [][]byte) (Bucket, error)

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 (Bucket) Cursor

func (b Bucket) Cursor() Cursor

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 (Bucket) Delete

func (b Bucket) Delete(k [][]byte) error

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 (Bucket) ForEach

func (b Bucket) ForEach(folders [][]byte, f func(k, v bytesp.Slice) error) error

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 (Bucket) ForEachGob

func (b Bucket) ForEachGob(folders [][]byte, f func(bytesp.Slice, interface{}) error) error

ForEachGob iterates each values of a folder, returns a Gob decoded object.

func (Bucket) GobValue

func (b Bucket) GobValue(k [][]byte, f func(interface{}) error) error

GobValue retrieves a value written by PutGob and decode it.

func (Bucket) NextSequence

func (b Bucket) NextSequence() (uint64, error)

NextSequence returns an autoincrementing integer for the bucket.

func (Bucket) OpenBucket

func (b Bucket) OpenBucket(folders [][]byte, f func(Bucket) error) error

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. folders can be empty, in which case the b itself is sent to f.

func (Bucket) Put

func (b Bucket) Put(k [][]byte, v []byte) error

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 (Bucket) PutGob

func (b Bucket) PutGob(k [][]byte, v interface{}) error

PutGob serialize v using gob and put it into the key.

func (Bucket) Tx

func (b Bucket) Tx() Tx

Tx returns the tx of the bucket.

func (Bucket) Value

func (b Bucket) Value(k [][]byte, f func(bytesp.Slice) error) error

Get retrieves the value for a key in the bucket. f is not called if the key does not exist or if the key is a nested bucket.

type Cursor

type Cursor struct {
	*bolt.Cursor
}

A wrapper to *bolt.Cursor.

func (Cursor) Bucket

func (c Cursor) Bucket() Bucket

Bucket returns the bucket that this cursor was created from.

func (Cursor) Delete

func (c Cursor) Delete() error

Delete removes the current key/value under the cursor from the bucket. Delete fails if current key/value is a bucket or if the transaction is not writable.

type DB

type DB struct {
	*bolt.DB
}

A wrapper to *bolt.DB.

func Open

func Open(path string, mode os.FileMode, options *bolt.Options) (DB, error)

Open creates and opens a database at the given path. If the file does not exist then it will be created automatically. Passing in nil options will cause Bolt to open the database with the default options.

func (DB) Batch

func (db DB) Batch(f func(Tx) error) error

Batch wraps bolt.DB.Batch.

func (DB) Begin

func (db DB) Begin(writable bool) (Tx, error)

Begin wraps bolt.DB.Begin.

func (DB) Close

func (db DB) Close() error

Close releases all database resources. All transactions must be closed before closing the database.

func (DB) Sync

func (db DB) Sync() error

Sync wraps bolt.DB.Sync.

func (DB) Update

func (db DB) Update(f func(Tx) error) error

Update wraps bolt.DB.Update.

func (DB) View

func (db DB) View(f func(Tx) error) error

View wraps bolt.DB.View.

type RefCountBox

type RefCountBox struct {
	sync.Mutex

	// The path to the bolt database file.
	DataPath func() string

	// Used to open a bolt DB. If not specified, bh.Open with 0644 mode and
	// default options will be used.
	OpenFunc func(path string) (DB, error)
	// contains filtered or unexported fields
}

RefCountBox is a structure maintaining a reference-count guarded instance of DB.

func (*RefCountBox) Alloc

func (b *RefCountBox) Alloc() (DB, error)

Alloc opens a DB if not openned yet. It adds a reference if already openned.

func (*RefCountBox) Free

func (b *RefCountBox) Free()

Free decreases the reference count. It close the DB if the count reaches zero.

func (*RefCountBox) Update

func (b *RefCountBox) Update(f func(Tx) error) error

func (*RefCountBox) View

func (b *RefCountBox) View(f func(Tx) error) error

type Tx

type Tx struct {
	*bolt.Tx
}

A wrapper to *bolt.Tx.

func (Tx) Bucket

func (tx Tx) Bucket(folders [][]byte, f func(Bucket) error) error

func (Tx) Commit

func (tx Tx) Commit() error

Commit writes all changes to disk and updates the meta page. Returns an error if a disk write error occurs, or if Commit is called on a read-only transaction.

func (Tx) CopyFile

func (tx Tx) CopyFile(path string, mode os.FileMode) error

CopyFile copies the entire database to file at the given path. A reader transaction is maintained during the copy so it is safe to continue using the database while a copy is in progress.

func (Tx) CreateBucketIfNotExists

func (tx Tx) CreateBucketIfNotExists(folders [][]byte) (Bucket, error)

CreateBucketIfNotExists creates a new bucket if it doesn't already exist. 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 (Tx) Cursor

func (tx Tx) Cursor(folders [][]byte, f func(Cursor) error) error

func (Tx) DB

func (tx Tx) DB() DB

DB returns a reference to the database that created the transaction.

func (Tx) Delete

func (tx Tx) Delete(k [][]byte) error

Delete deletes a key.

func (Tx) ForEach

func (tx Tx) ForEach(folders [][]byte, f func(Bucket, bytesp.Slice, bytesp.Slice) error) error

ForEach iterates over all key values of a folder.

func (Tx) ForEachGob

func (tx Tx) ForEachGob(folders [][]byte, f func(Bucket, bytesp.Slice, interface{}) error) error

ForEach iterates over all key values of a folder, decode non-nil values using gob.

func (Tx) GobValue

func (tx Tx) GobValue(k [][]byte, f func(interface{}) error) error

GobValue retrieves a value written by PutGob and decode it.

func (Tx) Put

func (tx Tx) Put(k [][]byte, v []byte) error

Put sets the value for a key in the transaction. 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 (Tx) PutGob

func (tx Tx) PutGob(k [][]byte, v interface{}) error

PutGob serialize v using gob and put it into the key.

func (Tx) Rollback

func (tx Tx) Rollback() error

Rollback closes the transaction and ignores all previous updates. Read-only transactions must be rolled back and not committed.

func (Tx) Update

func (tx Tx) Update(k [][]byte, f func(bytesp.Slice) (bytesp.Slice, error)) error

Updates fetches the current value and updates to a new value. If a nil value is returned by f, the item is deleted.

func (Tx) Value

func (tx Tx) Value(k [][]byte, f func(v bytesp.Slice) error) error

Value tries to get a value from the transaction. If the key does not exist, the f is not called and nil is return.

func (*Tx) WriteTo

func (tx *Tx) WriteTo(w io.Writer) (int64, error)

WriteTo writes the entire database to a writer. If err == nil then exactly tx.Size() bytes will be written into the writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL