kv

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound      = errors.New("key not found")
	ErrKeyEmpty         = errors.New("key is empty")
	ErrKeyAlreadyExists = errors.New("key already exists")
	ErrUnknownWriteOp   = errors.New("unknown write operation")
)
View Source
var DefaultFormat = &Format{
	PutKey:      '-',
	PutKeyValue: '=',
	Delete:      '!',
	KeyPrefix:   ' ',
	ValuePrefix: ' ',
	RowEnd:      '\n',
}

TextFileFormat with default values.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

func NewDB

func NewDB(fpath string, chars *Format) (*DB, error)

Instanciates a new DB. Opens underlying file handles for the given path (for reading and writing data to a file on disk) and extract initial data.

func (*DB) Close

func (db *DB) Close() error

Gracefully closes the database. Close underyling file handles.

func (*DB) CompactTo

func (db *DB) CompactTo(w io.Writer) (int, map[string]FileRef, error)

Writes the database data to the writer, skipping deleted and stale data.

func (*DB) Count

func (db *DB) Count() int

Reports the number of unique keys in the database.

func (*DB) Delete

func (db *DB) Delete(k []byte) error

Removes a key from the database. Further attempts to access this key will result in a ErrKeyNotFound.

func (*DB) FilePath

func (db *DB) FilePath() string

Reports the underlying datafile path.

func (*DB) ForEachKey

func (db *DB) ForEachKey(callback func(k []byte) (stop bool))

Iterates over all the keys in the database. The callback may return true to exit the loop. Keys are not ordered.

func (*DB) Get

func (db *DB) Get(k []byte) ([]byte, error)

Returns the value associated with the given key.

func (*DB) KeyExists

func (db *DB) KeyExists(k []byte) bool

Reports whether a key is known (has been set before).

func (*DB) KeyFileRef

func (db *DB) KeyFileRef(k []byte) (FileRef, bool)

Get the corresponding offset and size of a row on file. If the key file ref is not found, the key does not exist.

func (*DB) Lock

func (db *DB) Lock()

Calls lock on the underlying mutex.

func (*DB) Put

func (db *DB) Put(k, v []byte) error

Put set a key or key-value pair in the database.

func (*DB) Sync

func (db *DB) Sync() error

Calls Sync on the underlying write-only file handle.

func (*DB) Unlock

func (db *DB) Unlock()

func (*DB) WriteTo

func (db *DB) WriteTo(w io.Writer) (int64, error)

Implements the io.WriterTo interface. Writes database data to the given writer.

type FileRef

type FileRef struct {
	Offset int
	Size   int
}

Reference to a specific range of bytes in a file. Used to map a row to its location on file.

type Format

type Format struct {
	PutKey        byte // Default: '-'
	PutKeyValue   byte // Default: '='
	Delete        byte // Default: '!'
	KeyPrefix     byte // Default: ' ' (whitespace)
	ValuePrefix   byte // Default: ' ' (whitespace)
	RowEnd        byte // Default: '\n' (line-break)
	SizeStart     byte // Default: '('
	SizeSeparator byte // Default: '|'
	SizeEnd       byte // Default: ')'
	SizeBase      int  // Default: 10 (decimal)
}

Holds characters used for encoding and decoding row data.

func (*Format) Encode

func (chars *Format) Encode(kind WriteOp, k, v []byte) ([]byte, error)

func (*Format) ParseRowFromBytes

func (chars *Format) ParseRowFromBytes(b []byte) (WriteOp, []byte, []byte, error)

Parse a row assuming a byte slice containing the whole row (without trailing line-break)

type WriteOp

type WriteOp string
const (
	WriteOpUnknown     WriteOp = "unknown row kind"
	WriteOpPutKey      WriteOp = "put key"
	WriteOpPutKeyValue WriteOp = "put key-value"
	WriteOpDelete      WriteOp = "delete row by key"
)

Jump to

Keyboard shortcuts

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