baradb

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 17 Imported by: 3

README

saint-yellow/baradb

K/V storage engine based on Bitcask and inspired by rosedblabs/rosedb

bara: バラ, Japanese name of rose

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyIsEmpty                = errors.New("the key is empty")
	ErrIndexUpdateFailed         = errors.New("failed to update index")
	ErrFileNotFound              = errors.New("file not found")
	ErrKeyNotFound               = errors.New("key not found")
	ErrDirectoryIsEmpty          = errors.New("data path is empty")
	ErrMaxDataFileSizeIsNegative = errors.New("the maximum size of data file is negative")
	ErrDirectoryCorrupted        = errors.New("maybe the directory of the DB is corrupted")
	ErrExceedMaxBatchNumber      = errors.New("exceed the maximum batch number")
	ErrMergenceIsInProgress      = errors.New("mergence is in progress, try again later")
	ErrDatabaseIsUsed            = errors.New("the database is used by other process")
	ErrInvalidMergenceThreshold  = errors.New("invalid mergence threshold")
	ErrNoMoreDiskSpace           = errors.New("no more disk space to store data")
)

User-defined errors

View Source
var (
	// DefaultDBOptions Default options for launching DB engine
	DefaultDBOptions = DBOptions{
		Directory:       "/tmp/baradb",
		MaxDataFileSize: 512 * 1024 * 1024,
		SyncWrites:      false,
		IndexType:       index.ARtree,
		MMapAtStartup:   false,
	}
	// DefaultWriteBatchOptions Default options for batch writing
	DefaultWriteBatchOptions = WriteBatchOptions{
		MaxBatchNumber: 100,
		SyncWrites:     true,
	}
)

Functions

This section is empty.

Types

type DB

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

DB represents a baradb engine

func Launch added in v0.1.1

func Launch(options DBOptions) (*DB, error)

Launch launches a DB engine instance

func (*DB) Backup

func (db *DB) Backup(directory string) error

Backup copies the DB's data files to a given directory.

NOTE: only data files will be copied, other files like tran-no, flock will be ignored.

func (*DB) Close

func (db *DB) Close() error

Close closes the DB engine

func (*DB) Delete

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

Delete Delete data by the given key

func (*DB) Fold

func (db *DB) Fold(fn userOperationFunc) error

Fold retrieves all the data and iteratively executes an user-specified operation (UDF) Once the UDF failed, the iteration will stop intermediatelly

func (*DB) Fork

func (db *DB) Fork(directory string) (*DB, error)

Fork creates a new DB engine instance mainly for merging data

func (*DB) Get

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

Get Reads data from the DB engine by a given key

func (*DB) ListKeys

func (db *DB) ListKeys() [][]byte

ListKeys gets all keys in the DB engine

func (*DB) Merge

func (db *DB) Merge() error

Merge clears invalid data files and generates hint files

func (*DB) NewItrerator

func (db *DB) NewItrerator(options index.IteratorOptions) *Iterator

NewItrerator initializes an iterator of DB engine

func (*DB) NewWriteBatch

func (db *DB) NewWriteBatch(options WriteBatchOptions) *WriteBatch

NewWriteBatch initializes a write batch in the DB engine

func (*DB) Put

func (db *DB) Put(key, value []byte) error

Put Writes data to the DB engine

func (*DB) Stat

func (db *DB) Stat() *Stat

Stat returns statistical information of the DB engine

func (*DB) Sync

func (db *DB) Sync() error

Sync persistence of data in active data file of the DB engine

type DBOptions

type DBOptions struct {
	// Directory of a DB engine instance where data files are stored in
	Directory string

	// Maximum size of every single data file (uint: Byte).
	//
	// It should be greater than 0.
	MaxDataFileSize int64

	// SyncWrites indicates whether persist data after writing
	SyncWrites bool

	// IndexType indicates the type of the index of the DB engine
	IndexType index.IndexType

	// SyncThreshold indicates a threshold for persisting data.
	//
	// It should equals 0 or be greater than 0 (uint: Byte).
	//
	// When the DB engine wrote bytes more than this threshold, the DB engine will sync the wirtten bytes to the acitve data file.
	SyncThreshold uint

	// MMapAtStartup indicates whether load memory mapping while starting up the DB engine
	MMapAtStartup bool

	// MergenceThreshold indicates a threshold for merging data.
	//
	// When proportion of the invalid data in the DB engine is greater than this threshold,
	// the DB engine will merge all its data.
	//
	// The value of this threshold should be between 0 and 1.
	//
	// If the value is 0, then this threshold is disabled and the DB engine will not merge its data.
	MergenceThreshold float64
}

Options represents options of a DB engine instance

type Iterator

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

Iterator an iterator of DB

func (*Iterator) Close

func (it *Iterator) Close()

func (*Iterator) Key

func (it *Iterator) Key() []byte

func (*Iterator) Next

func (it *Iterator) Next()

func (*Iterator) Rewind

func (it *Iterator) Rewind()

func (*Iterator) Seek

func (it *Iterator) Seek(key []byte)

func (*Iterator) Valid

func (it *Iterator) Valid() bool

func (*Iterator) Value

func (it *Iterator) Value() ([]byte, error)

type Stat

type Stat struct {
	KeyNumber       uint  `json:"keyNumber"`       // Number of key(s) in the DB engine
	DataFileNumber  uint  `json:"dataFileNumber"`  // Number of data file(s) in the DB engine
	ReclaimableSize int64 `json:"reclaimableSize"` // Amount of mergable data (unit: byte)
	DiskSize        int64 `json:"diskSize"`        // Size of the DB engine occuppied in disk (unit: byte)
}

Stat represents statistical information of a DB engine

func (Stat) String

func (s Stat) String() string

type WriteBatch

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

WriteBatch A transaction that batch writes data and makes sure atomicity

func (*WriteBatch) Commit

func (wb *WriteBatch) Commit() error

Commit commits the transaction, writes the pending data to the disk and updates the in-memory index

func (*WriteBatch) Delete

func (wb *WriteBatch) Delete(key []byte) error

Delete deletes data

func (*WriteBatch) Put

func (wb *WriteBatch) Put(key, value []byte) error

Put writes data

type WriteBatchOptions

type WriteBatchOptions struct {
	MaxBatchNumber int  // Maximum amount of data in one batch
	SyncWrites     bool // Sync data after writing if true
}

WriteBatchOptions options for batch writing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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