store

package
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound     = errors.New("key not found")
	ErrCursorItemEmpty = errors.New("empty item found")
)

Functions

This section is empty.

Types

type Cursor

type Cursor interface {
	// Move the iterator to the provided key.
	// Returns the next smallest key if travelled in forward.
	// If reverse, returns the next largest key
	Seek(key []byte) error

	// Advance the iterator by one.
	// If forward, it goes to the larger key and reverse does the reverse of that
	Next()

	// Returns true when iteration is done
	IsDone() bool

	// Returns the item at the current iteration
	Item() (Item, error)

	// Crucial to call this after finished with the iteration
	Close() error
}

type Item

type Item struct {
	Key   []byte
	Value []byte
}

type Store

type Store interface {
	// Start a transaction.
	//
	// Set isWrite to false for read operations
	// since it avoids Badger's overhead for write operations.
	Start(isWrite bool) (Transaction, error)

	// Close the database. Can be called multiple times
	Close() error
}

func Open

func Open(dir string) (Store, error)

Initialize or open existing Badger KV database

func OpenWithOptions

func OpenWithOptions(opts badger.Options) (Store, error)

Initialize or open existing Badger KV database with options

type Transaction

type Transaction interface {
	// Set a value to be associated to a key
	Set(key, value []byte) error

	// Get the value based on the key
	Get(key []byte) ([]byte, error)

	// Delete the value based on the key
	Delete(key []byte) error

	// Returns a cursor for iterating multiple values.
	//
	// Set isForward to true if needed to iterate from smallest key to largest
	Cursor(isForward bool) (Cursor, error)

	// Commit the trasaction.
	// This is crucial for changes to be made into the database
	Commit() error

	// Rollback, cancel the transaction. It's okay to be called after the commit
	Rollback() error
}

Jump to

Keyboard shortcuts

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