Documentation
¶
Overview ¶
Package bitcask is a high performance embedded key value store that uses an on-disk LSM and WAL data structures and in-memory radix tree of key/value pairs as per the bitcask paper and seen in the Riak database.
Package bitcask implements a high-performance key-value store based on a WAL and LSM.
Example ¶
_, _ = Open("path/to/db")
Output:
Example (WithOptions) ¶
opts := []Option{ WithMaxKeySize(1024), WithMaxValueSize(4096), } _, _ = Open("path/to/db", opts...)
Output:
Index ¶
- Constants
- Variables
- func Byte2Int(b []byte) int64
- func Int2Byte(i int64) []byte
- func ScoreToFloat64(b Score) float64
- func ScoreToInt64(b Score) int64
- type Batch
- type Bitcask
- func (b *Bitcask) Backup(path string) error
- func (b *Bitcask) Batch() *Batch
- func (b *Bitcask) Close() error
- func (b *Bitcask) Collection(name string) *Collection
- func (b *Bitcask) Delete(key Key) error
- func (b *Bitcask) ForEach(f KeyFunc) (err error)
- func (b *Bitcask) Get(key Key) (Value, error)
- func (b *Bitcask) GetReader(key Key) (io.ReadSeeker, error)
- func (b *Bitcask) Has(key Key) bool
- func (b *Bitcask) Hash(key Key) *Hash
- func (b *Bitcask) Iterator(opts ...IteratorOption) *Iterator
- func (b *Bitcask) Len() int
- func (b *Bitcask) List(key Key) *List
- func (b *Bitcask) Merge() error
- func (b *Bitcask) Path() string
- func (b *Bitcask) Put(key Key, value Value) error
- func (b *Bitcask) Range(start, end Key, f KeyFunc) (err error)
- func (b *Bitcask) Readonly() bool
- func (b *Bitcask) Scan(prefix Key, f KeyFunc) (err error)
- func (b *Bitcask) SortedSet(key Key) *SortedSet
- func (b *Bitcask) Stats() (stats Stats, err error)
- func (b *Bitcask) Sync() error
- func (b *Bitcask) Transaction() *Txn
- func (b *Bitcask) WriteBatch(batch *Batch) error
- type Collection
- func (c *Collection) Add(id string, obj any) error
- func (c *Collection) Count() int
- func (c *Collection) Delete(id string) error
- func (c *Collection) Drop() error
- func (c *Collection) Exists() bool
- func (c *Collection) Get(id string, obj any) error
- func (c *Collection) Has(id string) bool
- func (c *Collection) List(dest any) error
- type ErrBadConfig
- type ErrBadMetadata
- type Hash
- func (h *Hash) Drop() error
- func (h *Hash) Get(field []byte) ([]byte, error)
- func (h *Hash) GetAll() (map[string][]byte, error)
- func (h *Hash) MGet(fields ...[]byte) ([][]byte, error)
- func (h *Hash) MSet(pairs ...[]byte) error
- func (h *Hash) Remove(fields ...[]byte) error
- func (h *Hash) Set(field, value []byte) error
- type Item
- type Iterator
- type IteratorOption
- type Key
- type KeyFunc
- type List
- type Option
- func WithAutoRecovery(enabled bool) Option
- func WithDirMode(mode os.FileMode) Option
- func WithFileMode(mode os.FileMode) Option
- func WithMaxDatafileSize(size uint64) Option
- func WithMaxKeySize(size uint32) Option
- func WithMaxValueSize(size uint64) Option
- func WithOpenReadonly(enabled bool) Option
- func WithSyncWrites(enabled bool) Option
- type Score
- type SortedSet
- type Stats
- type Txn
- func (t *Txn) Commit() error
- func (t *Txn) Delete(key Key) error
- func (t *Txn) Discard()
- func (t *Txn) ForEach(f KeyFunc) (err error)
- func (t *Txn) Get(key Key) (Value, error)
- func (t *Txn) GetReader(key Key) (io.ReadSeeker, error)
- func (t *Txn) Has(key Key) bool
- func (t *Txn) Iterator(opts ...IteratorOption) *Iterator
- func (t *Txn) Put(key Key, value Value) error
- func (t *Txn) Range(start Key, end Key, f KeyFunc) (err error)
- func (t *Txn) Scan(prefix Key, f KeyFunc) (err error)
- type Value
Examples ¶
Constants ¶
const ( // DefaultDebug is the default debug mode DefaultDebug = false // DefaultPath is the default path DefaultPath = "/tmp/bitcask.db" // DefaultDirMode is the default os.FileMode used when creating directories DefaultDirMode = os.FileMode(0700) // DefaultFileMode is the default os.FileMode used when creating files DefaultFileMode = 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 // DefaultSyncWrites is the default file synchronization action DefaultSyncWrites = false // DefaultOpenReadonly if true opens the database in read-only mode if it is already opened for writing by another process. DefaultOpenReadonly = false // DefaultAutoRecovery is the default auto-recovery action, if set will attempt to automatically recover the database if required DefaultAutoRecovery = true // CurrentDBVersion is the current database version CurrentDBVersion = uint32(2) )
Variables ¶
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") // ErrDatabaseReadonly is the error returned when the database has been opened in readonly mode ErrDatabaseReadonly = errors.New("error: database is readonly") // ErrInvalidRange is the error returned when the range scan is invalid ErrInvalidRange = errors.New("error: invalid range") // ErrInvalidVersion is the error returned when the database version is invalid ErrInvalidVersion = errors.New("error: invalid db version") // ErrMergeInProgress is the error returned if merge is called when already a merge // is in progress ErrMergeInProgress = errors.New("error: merge already in progress") )
var ( // ErrIteratorClosed ... ErrIteratorClosed = errors.New("error: iterator is closed") // ErrStopIteration ... ErrStopIteration = errors.New("error: iterator has no more items") )
var ( // ErrInvalidKeyFormat ... ErrInvalidKeyFormat = errors.New("invalid key format includes +[],") )
var ( // ErrObjectNotFound is the error returned when an object is not found in the collection. ErrObjectNotFound = errors.New("error: object not found") )
Functions ¶
Types ¶
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch ...
type Bitcask ¶ added in v2.0.7
type Bitcask struct {
// contains filtered or unexported fields
}
Bitcask ...
func Open ¶
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) Backup ¶ added in v2.0.7
Backup copies db directory to given path it creates path if it does not exist
func (*Bitcask) Batch ¶ added in v2.0.7
Batch returns a new batch that allows multiple keys to be deleted and written in a single operation.
func (*Bitcask) Close ¶ added in v2.0.7
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) Collection ¶ added in v2.0.7
func (b *Bitcask) Collection(name string) *Collection
Collection ...
func (*Bitcask) ForEach ¶ added in v2.0.7
ForEach 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 is returned.
func (*Bitcask) GetReader ¶ added in v2.0.7
func (b *Bitcask) GetReader(key Key) (io.ReadSeeker, error)
GetReader fetches value for a key and returns an io.ReadSeeker
func (*Bitcask) Has ¶ added in v2.0.7
Has returns true if the key exists in the database, false otherwise.
func (*Bitcask) Iterator ¶ added in v2.0.7
func (b *Bitcask) Iterator(opts ...IteratorOption) *Iterator
Iterator returns an iterator for iterating through keys in key order
func (*Bitcask) Merge ¶ added in v2.0.7
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) Range ¶ added in v2.0.7
Range performs a range scan of keys matching a range of keys between the start key and end key 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) Readonly ¶ added in v2.0.7
Readonly returns true if the database is currently opened in readonly mode, false otherwise
func (*Bitcask) Scan ¶ added in v2.0.7
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 is returned.
func (*Bitcask) Stats ¶ added in v2.0.7
Stats returns statistics about the database including the number of data files, keys and overall size on disk of the data
func (*Bitcask) Sync ¶ added in v2.0.7
Sync flushes all buffers to disk ensuring all data is written
func (*Bitcask) Transaction ¶ added in v2.0.7
Transaction returns a new transaction that is a snapshot of the key space of the database and isolated from other transactions. Key/Value pairs written in a transaction are batched together. Transactions are not thread safe and should only be used in a single goroutine.
func (*Bitcask) WriteBatch ¶ added in v2.0.7
WriteBatch writes the batch to the database. The batch is not cleared when the write has completed, so call Clear() to reset the batch if needed.
type Collection ¶ added in v2.0.4
type Collection struct {
// contains filtered or unexported fields
}
Collection allows you to manage a collection of objects encoded as JSON documents with a path-based key based on the provided name. This is convenient for storing complex normalized collections of objects.
func (*Collection) Add ¶ added in v2.0.4
func (c *Collection) Add(id string, obj any) error
Add adds a new object to the collection
func (*Collection) Count ¶ added in v2.0.4
func (c *Collection) Count() int
Count returns the number of objects in this collection.
func (*Collection) Delete ¶ added in v2.0.4
func (c *Collection) Delete(id string) error
Delete deletes an object from the collection
func (*Collection) Drop ¶ added in v2.0.4
func (c *Collection) Drop() error
Drop deletes the entire collection
func (*Collection) Exists ¶ added in v2.0.4
func (c *Collection) Exists() bool
Exists returns true if the collection exists at all, which really just means whether there are any objects in the collection, so this is the same as calling Count() > 0.
func (*Collection) Get ¶ added in v2.0.4
func (c *Collection) Get(id string, obj any) error
Get returns an object from the collection
func (*Collection) Has ¶ added in v2.0.4
func (c *Collection) Has(id string) bool
Has returns true if an object exists by the provided id.
func (*Collection) List ¶ added in v2.0.4
func (c *Collection) List(dest any) error
List returns a list of all objects in this collection.
type ErrBadConfig ¶
type ErrBadConfig struct {
Err error
}
ErrBadConfig is the error returned on failure to load the database config.
func (*ErrBadConfig) Error ¶
func (e *ErrBadConfig) Error() string
Error implements the error interface.
func (*ErrBadConfig) Is ¶
func (e *ErrBadConfig) Is(target error) bool
Is returns true if the provided target error is the same as ErrBadConfig.
func (*ErrBadConfig) Unwrap ¶
func (e *ErrBadConfig) Unwrap() error
Unwrap returns the underlying wrapped error that caused ErrBadConfig to be returned.
type ErrBadMetadata ¶
type ErrBadMetadata struct {
Err error
}
ErrBadMetadata is the error returned on failure to load the database metadata.
func (*ErrBadMetadata) Error ¶
func (e *ErrBadMetadata) Error() string
Error implements the error interface.
func (*ErrBadMetadata) Is ¶
func (e *ErrBadMetadata) Is(target error) bool
Is returns true if the provided target error is the same as ErrBadConfig.
func (*ErrBadMetadata) Unwrap ¶
func (e *ErrBadMetadata) Unwrap() error
Unwrap returns the underlying wrapped error that caused ErrBadMetadata to be returned.
type Hash ¶
type Hash struct {
// contains filtered or unexported fields
}
Hash ...
+key,h = "" h[key]name = "James" h[key]age = "21" h[key]sex = "Male"
type Item ¶
type Item struct {
// contains filtered or unexported fields
}
Item is a single key/value pair
type KeyFunc ¶
KeyFunc is a function that takes a key and performs some operation on it possibly returning an error
type List ¶
type List struct {
// contains filtered or unexported fields
}
List ... +key,l = "" l[key]0 = "a" l[key]1 = "b" l[key]2 = "c"
type Option ¶
Option is a function that takes a config struct and modifies it
func WithAutoRecovery ¶
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 WithDirMode ¶
WithDirMode sets the FileMode used for each new file created.
func WithFileMode ¶
WithFileMode sets the FileMode used for each new file created.
func WithMaxDatafileSize ¶
WithMaxDatafileSize sets the maximum datafile size option
func WithMaxKeySize ¶
WithMaxKeySize sets the maximum key size option
func WithMaxValueSize ¶
WithMaxValueSize sets the maximum value size option
func WithOpenReadonly ¶ added in v2.0.6
WithOpenReadonly opens the database in readonly mode.
func WithSyncWrites ¶ added in v2.0.4
WithSyncWrites causes Sync() to be called on every key/value written increasing durability and safety at the expense of write performance.
type Score ¶
type Score []byte
Score indicated that a number can be encoded to sorted []byte Score is use in SortedSet you can implement your own decode & encode function just like below
type SortedSet ¶
type SortedSet struct {
// contains filtered or unexported fields
}
SortedSet ... +key,z = "" z[key]m member = score z[key]s score member = ""
func (*SortedSet) Add ¶
Add add score & member pairs SortedSet.Add(Score, []byte, Score, []byte ...)
type Txn ¶ added in v2.0.7
type Txn struct {
// contains filtered or unexported fields
}
Txn is an transaction that represents a snapshot view of the current key space of the database.Transactions are isolated from each other, and key/value pairs written in a transaction are batched together. Transactions are not thread safe and should only be used in a single goroutine. Transactions writing the same key result in the last transaction wins strategy, and there are not versioning of keys or collision detection.
func (*Txn) Commit ¶ added in v2.0.7
Commit commits the transaction and writes the current batch to the database
func (*Txn) ForEach ¶ added in v2.0.7
ForEach 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 is returned.
func (*Txn) GetReader ¶ added in v2.0.7
func (t *Txn) GetReader(key Key) (io.ReadSeeker, error)
GetReader fetches value for a key and returns an io.ReadSeeker
func (*Txn) Has ¶ added in v2.0.7
Has returns true if the key exists in the database, false otherwise.
func (*Txn) Iterator ¶ added in v2.0.7
func (t *Txn) Iterator(opts ...IteratorOption) *Iterator
Iterator returns an iterator for iterating through keys in key order
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
bitcask
Package main is a simple command-line interface for reading/writing and performing operations on a Bitcask database
|
Package main is a simple command-line interface for reading/writing and performing operations on a Bitcask database |
bitcaskd
Package main is a simple Bitcask server that implements a basic Redis API
|
Package main is a simple Bitcask server that implements a basic Redis API |
examples
|
|
kitchensink
Package main demonstrates all features of Bitcask (the kitchen sink)
|
Package main demonstrates all features of Bitcask (the kitchen sink) |
redisserver
Package main implements a simple Redis-like server using Bitcask as the store
|
Package main implements a simple Redis-like server using Bitcask as the store |
Package internal contains internal implementation details
|
Package internal contains internal implementation details |
codec
Package codec implements binary encoding and decoding for database entries
|
Package codec implements binary encoding and decoding for database entries |
config
Package config defines configuration details and functions to load and save configuration to disk
|
Package config defines configuration details and functions to load and save configuration to disk |
data
Package data implements on disk and in memory storage for data files
|
Package data implements on disk and in memory storage for data files |
index
Package index deals with reading and writing database indexes
|
Package index deals with reading and writing database indexes |
migrations
Package migrations contains functions to run migrations
|
Package migrations contains functions to run migrations |