Documentation
¶
Overview ¶
Package nutsdb implements a simple, fast, embeddable and persistent key/value store written in pure Go. It supports fully serializable transactions.
The design of NutsDB is based on the bitcask storage engine model, and to do some optimization, using B+ tree instead of hash index, so it supports range scanning and prefix scanning, using mmap technology to improve write performance.
NutsDB currently works on Mac OS X and Linux.
Usage ¶
NutsDB has the following main types: DB, BPTree, Entry, DataFile And Tx. and NutsDB supports bucket, A bucket is a collection of unique keys that are associated with values.
All operations happen inside a Tx. Tx represents a transaction, which can be read-only or read-write. Read-only transactions can read values for a given key , or iterate over a set of key-value pairs (prefix scanning or range scanning). read-write transactions can also update and delete keys from the DB.
See the examples for more usage details.
Index ¶
- Constants
- Variables
- func IsExpired(ttl uint32, timestamp uint64) bool
- func SortedEntryKeys(m map[string]*Entry) (keys []string, es map[string]*Entry)
- type BPTree
- func (t *BPTree) Find(key []byte) (*Record, error)
- func (t *BPTree) FindLeaf(key []byte) *Node
- func (t *BPTree) Insert(key []byte, e *Entry, h *Hint, countFlag bool) error
- func (t *BPTree) PrefixScan(prefix []byte, limitNum int) (records Records, err error)
- func (t *BPTree) Range(start, end []byte) (records Records, err error)
- type BPTreeIdx
- type DB
- func (db *DB) Backup(dir string) error
- func (db *DB) Begin(writable bool) (tx *Tx, err error)
- func (db *DB) Close() error
- func (db *DB) GetValidKeyCount(bucket string) (keyCount int, err error)
- func (db *DB) Merge() error
- func (db *DB) Update(fn func(tx *Tx) error) error
- func (db *DB) View(fn func(tx *Tx) error) error
- type DataFile
- type Entries
- type Entry
- type EntryIdxMode
- type Hint
- type MergeStrategy
- type MetaData
- type Node
- type Options
- type Record
- type Records
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Delete(bucket string, key []byte) error
- func (tx *Tx) Get(bucket string, key []byte) (e *Entry, err error)
- func (tx *Tx) PrefixScan(bucket string, prefix []byte, limitNum int) (es Entries, err error)
- func (tx *Tx) Put(bucket string, key, value []byte, ttl uint32) error
- func (tx *Tx) RangeScan(bucket string, start, end []byte) (entries Entries, err error)
- func (tx *Tx) Rollback() error
Constants ¶
const ( RangeScan = "RangeScan" PrefixScan = "PrefixScan" // CountFlag CountFlagEnabled = true CountFlagDisabled = false )
const ( DataSuffix = ".dat" DataEntryHeaderSize = 40 )
Variables ¶
var ( // ErrStartKey is returned when Range is called by a error start key. ErrStartKey = errors.New("err start key") // ErrScansNoResult is returned when Range or prefixScan are called no result to found. ErrScansNoResult = errors.New("range scans or prefix scans no result") // ErrPrefixScansNoResult is returned when prefixScan is called no result to found. ErrPrefixScansNoResult = errors.New("prefix scans no result") // ErrKeyNotFound is returned when the key is not in the b+ tree. ErrKeyNotFound = errors.New("key not found") )
var ( // ErrCrcZero is returned when crc is 0 ErrCrcZero = errors.New("error crc is 0") // ErrCrc is returned when crc is error ErrCrc = errors.New(" crc error") )
var ( // ErrDBClosed is returned when db is closed ErrDBClosed = errors.New("db is closed") // ErrBucket is returned when bucket is not in the HintIdx ErrBucket = errors.New("err bucket") )
var ( // ErrKeyAndValSize is returned when given key and value size is too big. ErrKeyAndValSize = errors.New("key and value size too big") // ErrTxClosed is returned when committing or rolling back a transaction // that has already been committed or rolled back. ErrTxClosed = errors.New("tx is closed") // ErrTxNotWritable is returned when performing a write operation on // a read-only transaction. ErrTxNotWritable = errors.New("tx not writable") // ErrKeyEmpty is returned if an empty key is passed on an update function. ErrKeyEmpty = errors.New("key cannot be empty") // ErrRangeScan is returned when range scanning not found the result ErrRangeScan = errors.New("range scans not found") // ErrPrefixScan is returned when prefix scanning not found the result ErrPrefixScan = errors.New("prefix scans not found") // ErrNotFoundKey is returned when key not found int the bucket on an view function. ErrNotFoundKey = errors.New("key not found in the bucket") )
var DefaultOptions = Options{ EntryIdxMode: HintAndRAMIdxMode, SegmentSize: defaultSegmentSize, NodeNum: 1, }
Functions ¶
Types ¶
type BPTree ¶
type BPTree struct {
// contains filtered or unexported fields
}
BPTree records root node and valid key number.
func NewTree ¶
func NewTree() *BPTree
NewTree returns a newly initialized BPTree Object that implements the BPTree.
func (*BPTree) Insert ¶
Insert inserts record to the b+ tree, and if the key exists, update the record and the counter(if countFlag set true,it will start count).
func (*BPTree) PrefixScan ¶
PrefixScan returns records at the given prefix and limitNum limitNum: limit the number of the scanned records return.
type DB ¶
type DB struct {
HintIdx BPTreeIdx // Hint Index
ActiveFile *DataFile
MaxFileId int64
KeyCount int // total key number ,include expired, deleted, repeated.
// contains filtered or unexported fields
}
DB represents a collection of buckets that persist on disk.
func (*DB) Begin ¶
Begin opens a new transaction. Multiple read-only transactions can be opened at the same time but there can only be one read/write transaction at a time. Attempting to open a read/write transactions while another one is in progress will result in blocking until the current read/write transaction is completed. All transactions must be closed by calling Commit() or Rollback() when done.
func (*DB) GetValidKeyCount ¶
GetValidKeyCount returns the number of the key which not expired and not deleted.
func (*DB) Merge ¶
Merge removes dirty data and reduce data redundancy,following these steps:
1. Filter delete or expired entry.
2. Write entry to activeFile if the key not exist,if exist miss this write operation.
3. Filter the entry which is committed.
4. At last remove the merged files.
Caveat: Merge is Called means starting multiple write transactions, and it will effect the other write request. so execute it at the appropriate time.
type DataFile ¶
type DataFile struct {
ActualSize int64
// contains filtered or unexported fields
}
DataFile records about data file information.
func NewDataFile ¶
NewDataFile returns a newly initialized DataFile object.
type Entry ¶
type Entry struct {
Key []byte
Value []byte
Meta *MetaData
// contains filtered or unexported fields
}
Entry represents the data item.
func (*Entry) Encode ¶
Encode returns the slice after the entry be encoded.
the entry stored format: |----------------------------------------------------------------------------------------------------------------| | crc | timestamp | ksz | valueSize | flag | TTL |bucketSize| status | txId | bucket | key | value | |----------------------------------------------------------------------------------------------------------------| | uint32 | uint64 | uint32 | uint32 | uint16 | uint32| uint32 | uint16 | uint64 | []byte | []byte | []byte | |----------------------------------------------------------------------------------------------------------------|
type EntryIdxMode ¶
type EntryIdxMode int
const ( HintAndRAMIdxMode EntryIdxMode = iota HintAndMemoryMapIdxMode )
type Hint ¶
type Hint struct {
// contains filtered or unexported fields
}
Hint represents the index of the key
type MergeStrategy ¶
type MergeStrategy string
type Options ¶
type Options struct {
Dir string
EntryIdxMode EntryIdxMode
SegmentSize int64
NodeNum int64
}
Options records params for creating DB object.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx represents a transaction.
func (*Tx) Commit ¶
Commit commits the transaction, following these steps:
1. check the length of pendingWrites.If there are no writes, return immediately.
2. check if the ActiveFile has not enough space to store entry. if not, call rotateActiveFile function.
3. write pendingWrites to disk, if a non-nil error,return the error.
4. build Hint index.
5. Unlock the database and clear the db field.
func (*Tx) Get ¶
Get retrieves the value for a key in the bucket. The returned value is only valid for the life of the transaction.
func (*Tx) PrefixScan ¶
PrefixScan iterates over a key prefix at given bucket, prefix and limitNum. LimitNum will limit the number of entries return.