bitcask: Index | Examples | Files | Directories

package bitcask

import ""

Package bitcask implements a high-performance key-value store based on a WAL and LSM.


_, _ = Open("path/to/db")


opts := []Option{
_, _ = Open("path/to/db", opts...)



Package Files

bitcask.go doc.go options.go


const (
    // DefaultDirFileModeBeforeUmask is the default os.FileMode used when creating directories
    DefaultDirFileModeBeforeUmask = os.FileMode(0700)

    // DefaultFileFileModeBeforeUmask is the default os.FileMode used when creating files
    DefaultFileFileModeBeforeUmask = os.FileMode(0600)

    // DefaultMaxDatafileSize is the default maximum datafile size in bytes
    DefaultMaxDatafileSize = 1 << 20 // 1MB

    // DefaultMaxKeySize is the default maximum key size in bytes
    DefaultMaxKeySize = uint32(64) // 64 bytes

    // DefaultMaxValueSize is the default value size in bytes
    DefaultMaxValueSize = uint64(1 << 16) // 65KB

    // DefaultSync is the default file synchronization action
    DefaultSync = false


var (
    // ErrKeyNotFound is the error returned when a key is not found
    ErrKeyNotFound = errors.New("error: key not found")

    // ErrKeyTooLarge is the error returned for a key that exceeds the
    // maximum allowed key size (configured with WithMaxKeySize).
    ErrKeyTooLarge = errors.New("error: key too large")

    // ErrEmptyKey is the error returned for a value with an empty key.
    ErrEmptyKey = errors.New("error: empty key")

    // ErrValueTooLarge is the error returned for a value that exceeds the
    // maximum allowed value size (configured with WithMaxValueSize).
    ErrValueTooLarge = errors.New("error: value too large")

    // ErrChecksumFailed is the error returned if a key/value retrieved does
    // not match its CRC checksum
    ErrChecksumFailed = errors.New("error: checksum failed")

    // ErrDatabaseLocked is the error returned if the database is locked
    // (typically opened by another process)
    ErrDatabaseLocked = errors.New("error: database locked")

type Bitcask Uses

type Bitcask struct {
    // contains filtered or unexported fields

Bitcask is a struct that represents a on-disk LSM and WAL data structure and in-memory hash of key/value pairs as per the Bitcask paper and seen in the Riak database.

func Open Uses

func Open(path string, options ...Option) (*Bitcask, error)

Open opens the database at the given path with optional options. Options can be provided with the `WithXXX` functions that provide configuration options as functions.

func (*Bitcask) Close Uses

func (b *Bitcask) Close() error

Close closes the database and removes the lock. It is important to call Close() as this is the only way to cleanup the lock held by the open database.

func (*Bitcask) Delete Uses

func (b *Bitcask) Delete(key []byte) error

Delete deletes the named key. If the key doesn't exist or an I/O error occurs the error is returned.

func (*Bitcask) DeleteAll Uses

func (b *Bitcask) DeleteAll() (err error)

DeleteAll deletes all the keys. If an I/O error occurs the error is returned.

func (*Bitcask) Fold Uses

func (b *Bitcask) Fold(f func(key []byte) error) (err error)

Fold iterates over all keys in the database calling the function `f` for each key. If the function returns an error, no further keys are processed and the error returned.

func (*Bitcask) Get Uses

func (b *Bitcask) Get(key []byte) ([]byte, error)

Get retrieves the value of the given key. If the key is not found or an/I/O error occurs a null byte slice is returned along with the error.

func (*Bitcask) Has Uses

func (b *Bitcask) Has(key []byte) bool

Has returns true if the key exists in the database, false otherwise.

func (*Bitcask) Keys Uses

func (b *Bitcask) Keys() chan []byte

Keys returns all keys in the database as a channel of keys

func (*Bitcask) Len Uses

func (b *Bitcask) Len() int

Len returns the total number of keys in the database

func (*Bitcask) Merge Uses

func (b *Bitcask) Merge() error

Merge merges all datafiles in the database. Old keys are squashed and deleted keys removes. Duplicate key/value pairs are also removed. Call this function periodically to reclaim disk space.

func (*Bitcask) Put Uses

func (b *Bitcask) Put(key, value []byte) error

Put stores the key and value in the database.

func (*Bitcask) Reopen Uses

func (b *Bitcask) Reopen() error

func (*Bitcask) Scan Uses

func (b *Bitcask) Scan(prefix []byte, f func(key []byte) error) (err error)

Scan performs a prefix scan of keys matching the given prefix and calling the function `f` with the keys found. If the function returns an error no further keys are processed and the first error returned.

func (*Bitcask) Stats Uses

func (b *Bitcask) Stats() (stats Stats, err error)

Stats returns statistics about the database including the number of data files, keys and overall size on disk of the data

func (*Bitcask) Sync Uses

func (b *Bitcask) Sync() error

Sync flushes all buffers to disk ensuring all data is written

type Option Uses

type Option func(*config.Config) error

Option is a function that takes a config struct and modifies it

func WithAutoRecovery Uses

func WithAutoRecovery(enabled bool) Option

WithAutoRecovery sets auto recovery of data and index file recreation. IMPORTANT: This flag MUST BE used only if a proper backup was made of all the existing datafiles.

func WithDirFileModeBeforeUmask Uses

func WithDirFileModeBeforeUmask(mode os.FileMode) Option

WithDirFileModeBeforeUmask sets the FileMode used for each new file created.

func WithFileFileModeBeforeUmask Uses

func WithFileFileModeBeforeUmask(mode os.FileMode) Option

WithFileFileModeBeforeUmask sets the FileMode used for each new file created.

func WithMaxDatafileSize Uses

func WithMaxDatafileSize(size int) Option

WithMaxDatafileSize sets the maximum datafile size option

func WithMaxKeySize Uses

func WithMaxKeySize(size uint32) Option

WithMaxKeySize sets the maximum key size option

func WithMaxValueSize Uses

func WithMaxValueSize(size uint64) Option

WithMaxValueSize sets the maximum value size option

func WithSync Uses

func WithSync(sync bool) Option

WithSync causes Sync() to be called on every key/value written increasing durability and safety at the expense of performance

type Stats Uses

type Stats struct {
    Datafiles int
    Keys      int
    Size      int64

Stats is a struct returned by Stats() on an open Bitcask instance



Package bitcask imports 16 packages (graph) and is imported by 23 packages. Updated 2020-11-17. Refresh now. Tools for package owners.