db

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

README

TODO: syndtr/goleveldb should be replaced with actual LevelDB instance

Documentation

Index

Constants

View Source
const IdealBatchSize = 100 * 1024

Code using batches should try to add this much data to the batch. The value was determined empirically.

Variables

This section is empty.

Functions

func IsKeyInDomain

func IsKeyInDomain(key, start, end []byte, isReverse bool) bool

See DB interface documentation for more information.

func NewDebugBatch

func NewDebugBatch(label string, bch Batch) debugBatch

For printing all operationgs to the console for debugging.

func NewDebugDB

func NewDebugDB(label string, db DB) debugDB

For printing all operationgs to the console for debugging.

func NewDebugIterator

func NewDebugIterator(label string, itr Iterator) debugIterator

For printing all operationgs to the console for debugging.

func NewPrefixDB

func NewPrefixDB(db DB, prefix []byte) *prefixDB

NewPrefixDB lets you namespace multiple DBs within a single DB.

func PrefixToEnd

func PrefixToEnd(prefix []byte) []byte

PrefixToEnd returns end that satisfy the given prefix. This only applicable for the standard 'bytes comparer'.

Types

type BadgerDB

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

func NewBadgerDB

func NewBadgerDB(name string, dir string, counts uint64) (*BadgerDB, error)

func (*BadgerDB) Close

func (db *BadgerDB) Close()

Implements DB.

func (*BadgerDB) Del

func (db *BadgerDB) Del(key []byte) error

func (*BadgerDB) Delete

func (db *BadgerDB) Delete(key []byte)

Implements DB.

func (*BadgerDB) DeleteSync

func (db *BadgerDB) DeleteSync(key []byte)

Implements DB.

func (*BadgerDB) Dir

func (db *BadgerDB) Dir() string

func (*BadgerDB) Exist

func (db *BadgerDB) Exist(key []byte) (bool, error)

func (*BadgerDB) Get

func (db *BadgerDB) Get(key []byte) []byte

Implements DB.

func (*BadgerDB) Has

func (db *BadgerDB) Has(key []byte) bool

Implements DB.

func (*BadgerDB) Iterator

func (db *BadgerDB) Iterator(start, end []byte) Iterator

Implements DB.

func (*BadgerDB) Load

func (db *BadgerDB) Load(key []byte) ([]byte, error)

func (*BadgerDB) NewBatch

func (db *BadgerDB) NewBatch() Batch

Implements DB.

func (*BadgerDB) NewIteratorWithPrefix

func (db *BadgerDB) NewIteratorWithPrefix(prefix []byte) Iterator

func (*BadgerDB) Print

func (db *BadgerDB) Print()

Implements DB.

func (*BadgerDB) Put

func (db *BadgerDB) Put(key []byte, val []byte) error

func (*BadgerDB) ReverseIterator

func (db *BadgerDB) ReverseIterator(start, end []byte) Iterator

Implements DB.

func (*BadgerDB) Set

func (db *BadgerDB) Set(key []byte, val []byte)

Implements DB.

func (*BadgerDB) SetSync

func (db *BadgerDB) SetSync(key []byte, val []byte)

Implements DB.

func (*BadgerDB) Stats

func (db *BadgerDB) Stats() map[string]string

Implements DB.

type Batch

type Batch interface {
	SetDeleter
	Write()
	Commit() error
	WriteSync()
	ValueSize() int // amount of data in the batch
	Reset()         // Reset resets the batch for reuse
}

type BoltDB

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

func NewBoltDB

func NewBoltDB(name string, dir string, count uint64) (*BoltDB, error)

func (*BoltDB) Close

func (db *BoltDB) Close()

func (*BoltDB) Del

func (db *BoltDB) Del(key []byte) error

func (*BoltDB) Delete

func (db *BoltDB) Delete(key []byte)

func (*BoltDB) DeleteSync

func (db *BoltDB) DeleteSync(key []byte)

func (*BoltDB) Dir

func (db *BoltDB) Dir() string

func (*BoltDB) Exist

func (db *BoltDB) Exist(key []byte) (bool, error)

func (*BoltDB) Get

func (db *BoltDB) Get(key []byte) []byte

Implements DB.

func (*BoltDB) Has

func (db *BoltDB) Has(key []byte) bool

func (*BoltDB) Iterator

func (db *BoltDB) Iterator(start, end []byte) Iterator

func (*BoltDB) Load

func (db *BoltDB) Load(key []byte) ([]byte, error)

func (*BoltDB) NewBatch

func (db *BoltDB) NewBatch() Batch

func (*BoltDB) NewIteratorWithPrefix

func (db *BoltDB) NewIteratorWithPrefix(prefix []byte) Iterator

func (*BoltDB) Print

func (db *BoltDB) Print()

func (*BoltDB) Put

func (db *BoltDB) Put(key []byte, val []byte) error

func (*BoltDB) ReverseIterator

func (db *BoltDB) ReverseIterator(start, end []byte) Iterator

func (*BoltDB) Set

func (db *BoltDB) Set(key []byte, val []byte)

func (*BoltDB) SetSync

func (db *BoltDB) SetSync(key []byte, val []byte)

func (*BoltDB) Stats

func (db *BoltDB) Stats() map[string]string

type DB

type DB interface {

	// Get returns nil iff key doesn't exist.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Get([]byte) []byte
	Load([]byte) ([]byte, error)

	// Has checks if a key exists.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Has(key []byte) bool
	Exist(key []byte) (bool, error)

	// Set sets the key.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key, value readonly []byte
	Set([]byte, []byte)
	Put([]byte, []byte) error
	SetSync([]byte, []byte)

	// Delete deletes the key.
	// A nil key is interpreted as an empty byteslice.
	// CONTRACT: key readonly []byte
	Delete([]byte)
	Del([]byte) error
	DeleteSync([]byte)

	// Iterate over a domain of keys in ascending order. End is exclusive.
	// Start must be less than end, or the Iterator is invalid.
	// A nil start is interpreted as an empty byteslice.
	// If end is nil, iterates up to the last item (inclusive).
	// CONTRACT: No writes may happen within a domain while an iterator exists over it.
	// CONTRACT: start, end readonly []byte
	Iterator(start, end []byte) Iterator

	// Iterate over a domain of keys in descending order. End is exclusive.
	// Start must be greater than end, or the Iterator is invalid.
	// If start is nil, iterates from the last/greatest item (inclusive).
	// If end is nil, iterates up to the first/least item (inclusive).
	// CONTRACT: No writes may happen within a domain while an iterator exists over it.
	// CONTRACT: start, end readonly []byte
	ReverseIterator(start, end []byte) Iterator
	NewIteratorWithPrefix(prefix []byte) Iterator

	Dir() string

	// Closes the connection.
	Close()

	// Creates a batch for atomic updates.
	NewBatch() Batch

	// For debugging
	Print()

	// Stats returns a map of property values for all keys and the size of the cache.
	Stats() map[string]string
}

DBs are goroutine safe.

func NewDB

func NewDB(name string, backend DBBackendType, dir string, counts uint64) DB

type DBBackendCounts

type DBBackendCounts uint64

type DBBackendType

type DBBackendType string
const (
	LevelDBBackend   DBBackendType = "leveldb" // legacy, defaults to goleveldb unless +gcc
	CLevelDBBackend  DBBackendType = "cleveldb"
	GoLevelDBBackend DBBackendType = "goleveldb"
	MemDBBackend     DBBackendType = "memdb"
	FSDBBackend      DBBackendType = "fsdb"   // using the filesystem naively
	BadgerBackend    DBBackendType = "badger" // using badger
	BoltBackend      DBBackendType = "bolt"   // using bolt
)

type FSDB

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

It's slow.

func NewFSDB

func NewFSDB(dir string) *FSDB

func (*FSDB) Close

func (db *FSDB) Close()

func (*FSDB) Del

func (db *FSDB) Del(key []byte) error

func (*FSDB) Delete

func (db *FSDB) Delete(key []byte)

func (*FSDB) DeleteNoLock

func (db *FSDB) DeleteNoLock(key []byte)

NOTE: Implements atomicSetDeleter.

func (*FSDB) DeleteNoLockNoPanic

func (db *FSDB) DeleteNoLockNoPanic(key []byte) error

func (*FSDB) DeleteSync

func (db *FSDB) DeleteSync(key []byte)

func (*FSDB) Dir

func (db *FSDB) Dir() string

func (*FSDB) Exist

func (db *FSDB) Exist(key []byte) (bool, error)

func (*FSDB) Get

func (db *FSDB) Get(key []byte) []byte

func (*FSDB) Has

func (db *FSDB) Has(key []byte) bool

func (*FSDB) Iterator

func (db *FSDB) Iterator(start, end []byte) Iterator

func (*FSDB) Load

func (db *FSDB) Load(key []byte) ([]byte, error)

func (*FSDB) MakeIterator

func (db *FSDB) MakeIterator(start, end []byte, isReversed bool) Iterator

func (*FSDB) Mutex

func (db *FSDB) Mutex() *sync.Mutex

func (*FSDB) NewBatch

func (db *FSDB) NewBatch() Batch

func (*FSDB) NewIteratorWithPrefix

func (db *FSDB) NewIteratorWithPrefix(prefix []byte) Iterator

func (*FSDB) Print

func (db *FSDB) Print()

func (*FSDB) Put

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

func (*FSDB) ReverseIterator

func (db *FSDB) ReverseIterator(start, end []byte) Iterator

func (*FSDB) Set

func (db *FSDB) Set(key []byte, value []byte)

func (*FSDB) SetNoLock

func (db *FSDB) SetNoLock(key []byte, value []byte)

NOTE: Implements atomicSetDeleter.

func (*FSDB) SetNoLockNoPanic

func (db *FSDB) SetNoLockNoPanic(key []byte, value []byte) error

func (*FSDB) SetSync

func (db *FSDB) SetSync(key []byte, value []byte)

func (*FSDB) Stats

func (db *FSDB) Stats() map[string]string

type GoLevelDB

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

func NewGoLevelDB

func NewGoLevelDB(name string, dir string, counts uint64) (*GoLevelDB, error)

func (*GoLevelDB) Close

func (db *GoLevelDB) Close()

Implements DB.

func (*GoLevelDB) Del

func (db *GoLevelDB) Del(key []byte) error

func (*GoLevelDB) Delete

func (db *GoLevelDB) Delete(key []byte)

Implements DB.

func (*GoLevelDB) DeleteSync

func (db *GoLevelDB) DeleteSync(key []byte)

Implements DB.

func (*GoLevelDB) Dir

func (db *GoLevelDB) Dir() string

func (*GoLevelDB) Exist

func (db *GoLevelDB) Exist(key []byte) (bool, error)

func (*GoLevelDB) Get

func (db *GoLevelDB) Get(key []byte) []byte

Implements DB.

func (*GoLevelDB) Has

func (db *GoLevelDB) Has(key []byte) bool

Implements DB.

func (*GoLevelDB) Iterator

func (db *GoLevelDB) Iterator(start, end []byte) Iterator

Implements DB.

func (*GoLevelDB) Load

func (db *GoLevelDB) Load(key []byte) ([]byte, error)

func (*GoLevelDB) NewBatch

func (db *GoLevelDB) NewBatch() Batch

Implements DB.

func (*GoLevelDB) NewIteratorWithPrefix

func (db *GoLevelDB) NewIteratorWithPrefix(prefix []byte) Iterator

func (*GoLevelDB) Print

func (db *GoLevelDB) Print()

Implements DB.

func (*GoLevelDB) Put

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

func (*GoLevelDB) ReverseIterator

func (db *GoLevelDB) ReverseIterator(start, end []byte) Iterator

Implements DB.

func (*GoLevelDB) Set

func (db *GoLevelDB) Set(key []byte, value []byte)

Implements DB.

func (*GoLevelDB) SetSync

func (db *GoLevelDB) SetSync(key []byte, value []byte)

Implements DB.

func (*GoLevelDB) Stats

func (db *GoLevelDB) Stats() map[string]string

Implements DB.

type Iterator

type Iterator interface {

	// The start & end (exclusive) limits to iterate over.
	// If end < start, then the Iterator goes in reverse order.
	//
	// A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
	// over anything with the prefix []byte{12, 13}.
	//
	// The smallest key is the empty byte array []byte{} - see BeginningKey().
	// The largest key is the nil byte array []byte(nil) - see EndingKey().
	// CONTRACT: start, end readonly []byte
	Domain() (start []byte, end []byte)

	// Valid returns whether the current position is valid.
	// Once invalid, an Iterator is forever invalid.
	Valid() bool

	// Next moves the iterator to the next sequential key in the database, as
	// defined by order of iteration.
	//
	// If Valid returns false, this method will panic.
	Next() bool // No panic when returned to false

	Seek(key []byte) bool
	// Key returns the key of the cursor.
	// If Valid returns false, this method will panic.
	// CONTRACT: key readonly []byte
	Key() (key []byte)

	// Value returns the value of the cursor.
	// If Valid returns false, this method will panic.
	// CONTRACT: value readonly []byte
	Value() (value []byte)

	// Close releases the Iterator.
	Close()
}

Usage:

var itr Iterator = ... defer itr.Close()

for ; itr.Valid(); itr.Next() {
	k, v := itr.Key(); itr.Value()
	// ...
}

func IteratePrefix

func IteratePrefix(db DB, prefix []byte) Iterator

IteratePrefix is a convenience function for iterating over a key domain restricted by prefix.

type MemDB

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

func NewMemDB

func NewMemDB() *MemDB

func (*MemDB) Close

func (db *MemDB) Close()

Implements DB.

func (*MemDB) Del

func (db *MemDB) Del(key []byte) error

func (*MemDB) Delete

func (db *MemDB) Delete(key []byte)

Implements DB.

func (*MemDB) DeleteNoLock

func (db *MemDB) DeleteNoLock(key []byte)

Implements atomicSetDeleter.

func (*MemDB) DeleteNoLockSync

func (db *MemDB) DeleteNoLockSync(key []byte)

Implements atomicSetDeleter.

func (*MemDB) DeleteSync

func (db *MemDB) DeleteSync(key []byte)

Implements DB.

func (*MemDB) Dir

func (db *MemDB) Dir() string

func (*MemDB) Exist

func (db *MemDB) Exist(key []byte) (bool, error)

func (*MemDB) Get

func (db *MemDB) Get(key []byte) []byte

Implements DB.

func (*MemDB) Has

func (db *MemDB) Has(key []byte) bool

Implements DB.

func (*MemDB) Iterator

func (db *MemDB) Iterator(start, end []byte) Iterator

Implements DB.

func (*MemDB) Keys

func (db *MemDB) Keys() [][]byte

func (*MemDB) Len

func (db *MemDB) Len() int

func (*MemDB) Load

func (db *MemDB) Load(key []byte) ([]byte, error)

func (*MemDB) Mutex

func (db *MemDB) Mutex() *sync.Mutex

Implements atomicSetDeleter.

func (*MemDB) NewBatch

func (db *MemDB) NewBatch() Batch

Implements DB.

func (*MemDB) NewIteratorWithPrefix

func (db *MemDB) NewIteratorWithPrefix(prefix []byte) Iterator

func (*MemDB) Print

func (db *MemDB) Print()

Implements DB.

func (*MemDB) Put

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

func (*MemDB) ReverseIterator

func (db *MemDB) ReverseIterator(start, end []byte) Iterator

Implements DB.

func (*MemDB) Set

func (db *MemDB) Set(key []byte, value []byte)

Implements DB.

func (*MemDB) SetNoLock

func (db *MemDB) SetNoLock(key []byte, value []byte)

Implements atomicSetDeleter.

func (*MemDB) SetNoLockSync

func (db *MemDB) SetNoLockSync(key []byte, value []byte)

Implements atomicSetDeleter.

func (*MemDB) SetSync

func (db *MemDB) SetSync(key []byte, value []byte)

Implements DB.

func (*MemDB) Stats

func (db *MemDB) Stats() map[string]string

Implements DB.

type Putter

type Putter interface {
	Put(key []byte, value []byte) error
}

Putter wraps the database write operation supported by both batches and regular databases.

type SetDeleter

type SetDeleter interface {
	Set(key, value []byte) // CONTRACT: key, value readonly []byte
	Delete(key []byte)     // CONTRACT: key readonly []byte
}

Jump to

Keyboard shortcuts

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