Documentation
¶
Index ¶
- Constants
- Variables
- type Barrel
- func (b *Barrel) Delete(k string) error
- func (b *Barrel) ExamineFileSize(evalInterval time.Duration)
- func (b *Barrel) Fold(fn func(k string) error) error
- func (b *Barrel) Get(k string) ([]byte, error)
- func (b *Barrel) Len() int
- func (b *Barrel) List() []string
- func (b *Barrel) Put(k string, val []byte) error
- func (b *Barrel) PutEx(k string, val []byte, ex time.Duration) error
- func (b *Barrel) RunCompaction(evalInterval time.Duration)
- func (b *Barrel) Shutdown() error
- func (b *Barrel) Sync() error
- func (b *Barrel) SyncFile(evalInterval time.Duration)
- type Config
- func WithAlwaysSync() Config
- func WithAutoSync() Config
- func WithBackgrondSync(interval time.Duration) Config
- func WithCheckFileSizeInterval(interval time.Duration) Config
- func WithCompactInterval(interval time.Duration) Config
- func WithDebug() Config
- func WithDir(dir string) Config
- func WithMaxActiveFileSize(size int64) Config
- func WithReadOnly() Config
- type Header
- type KeyDir
- type Meta
- type Options
- type Record
Constants ¶
const ( LOCKFILE = "barrel.lock" HINTS_FILE = "barrel.hints" )
const ( MaxKeySize = 1<<32 - 1 MaxValueSize = 1<<32 - 1 )
Variables ¶
var ( ErrLocked = errors.New("a lockfile already exists") ErrReadOnly = errors.New("operation not allowed in read only mode") ErrChecksumMismatch = errors.New("invalid data: checksum does not match") ErrEmptyKey = errors.New("invalid key: key cannot be empty") ErrExpiredKey = errors.New("invalid key: key is already expired") ErrLargeKey = errors.New("invalid key: size cannot be more than 4294967296 bytes") ErrNoKey = errors.New("invalid key: key is either deleted or expired or unset") ErrLargeValue = errors.New("invalid value: size cannot be more than 4294967296 bytes") )
Functions ¶
This section is empty.
Types ¶
type Barrel ¶
func (*Barrel) Delete ¶
Delete creates a tombstone record for the given key. The tombstone value is simply an empty byte array. Actual deletes happen in background when merge is called. Since the file is opened in append-only mode, the new value of the key is overwritten both on disk and in memory as a tombstone record.
func (*Barrel) ExamineFileSize ¶
ExamineFileSize checks for file size at a periodic interval. It examines the file size of the active db file and marks it as stale if the file size exceeds the configured size.
func (*Barrel) Get ¶
Get takes a key and finds the metadata in the in-memory hashtable (Keydir). Using the offset present in metadata it finds the record in the datafile with a single disk seek. It further decodes the record and returns the value as a byte array for the given key.
func (*Barrel) Put ¶
Put takes a key and value and encodes the data in bytes and writes to the db file. It also stores the key with some metadata in memory. This metadata helps for faster reads as the last position of the file is recorded so only a single disk seek is required to read value.
func (*Barrel) RunCompaction ¶
RunCompaction runs cleanup process to compact the keys and cleanup dead/expired keys at a periodic interval. This helps to save disk space and merge old inactive db files in a single file. It also generates a hints file which helps in caching all the keys during a cold start.
func (*Barrel) Shutdown ¶
Shutdown closes all the open file descriptors and removes any file locks. If non running in a read-only mode, it's essential to call close so that it removes any file locks on the database directory. Not calling close will prevent future startups until it's removed manually.
type Config ¶
Config is a function on the Options for barreldb. These are used to configure particular options.
func WithAlwaysSync ¶
func WithAlwaysSync() Config
func WithAutoSync ¶
func WithAutoSync() Config
func WithBackgrondSync ¶
func WithCompactInterval ¶
func WithMaxActiveFileSize ¶
func WithReadOnly ¶
func WithReadOnly() Config
type KeyDir ¶
KeyDir represents an in-memory hash for faster lookups of the key. Once the key is found in the map, the additional metadata like the offset record and the file ID is used to extract the underlying record from the disk. Advantage is that this approach only requires a single disk seek of the db file since the position offset (in bytes) is already stored.
type Meta ¶
Meta represents some additional properties for the given key. The actual value of the key is not stored in the in-memory hashtable.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options represents configuration options for managing a datastore.
func DefaultOptions ¶
func DefaultOptions() *Options
type Record ¶
Record is a binary representation of how each record is persisted in the disk. Header represents how the record is stored and some metadata with it. For storing CRC checksum hash, timestamp and expiry of record, each field uses 4 bytes. (uint32 == 32 bits). The next field stores the max size of the key which is also represented with uint32. So the max size of the key can not be more than 2^32-1 which is ~ 4.3GB. The next field stores the max size of the value which is also represented with unint32. Max size of value can not be more than 2^32-1 which is ~ 4.3GB.
Each entry cannot exceed more than ~8.6GB as a theoretical limit. In a practical sense, this is also constrained by the memory of the underlying VM where this program would run.
Representation of the record stored on disk. ------------------------------------------------------------------------------ | crc(4) | time(4) | expiry (4) | key_size(4) | val_size(4) | key | val | ------------------------------------------------------------------------------