storage

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotExists        = errors.New("specified key does not exists")
	ErrKeyNotFound         = errors.New("key not found")
	ErrTransactionExists   = errors.New("can not create two transactions")
	ErrTransactionClosed   = errors.New("the transaction is closed")
	ErrDatabasePanic       = errors.New("database panic")
	ErrOnlySupportBatchOpt = errors.New("flush is only for batch opt")
)

error

Functions

func Register

func Register(dbname string, fn newDBFunc)

Register registers a new DB implementation

Types

type Batch

type Batch interface {
	// put the value to entry associate with the key
	Put(key, value []byte)

	// delete the entry associate with the key in the Storage
	Del(key []byte)

	// remove all the enqueued put/delete
	Clear()

	// returns the number of updates in the batch
	Count() int

	// atomic writes all enqueued put/delete
	Write() error

	// close the batch, it must be called to close the batch
	Close()
}

Batch defines the batch of put, del operations

type Config

type Config struct {
	Name    string  `mapstructure:"name"`
	Path    string  `mapstructure:"path"`
	Options Options `mapstructure:"options"`
}

Config defines the database configuration

type Database

type Database struct {
	Storage
	// contains filtered or unexported fields
}

Database is a wrapper of Storage, implementing the database life cycle

func NewDatabase

func NewDatabase(parent goprocess.Process, cfg *Config) (*Database, error)

NewDatabase creates a database instance

func (*Database) Close

func (db *Database) Close() error

Close closes the database

func (*Database) Proc

func (db *Database) Proc() goprocess.Process

Proc returns the gopreocess of database

type Operations

type Operations interface {
	Writer
	Reader
}

Operations defines common data operations on database/table

type Options

type Options map[string]interface{}

Options defines the db options

type Reader

type Reader interface {
	// return value associate with the key in the Storage
	Get(key []byte) ([]byte, error)

	// return values associate with the keys in the Storage
	MultiGet(key ...[]byte) ([][]byte, error)

	// check if the entry associate with key exists
	Has(key []byte) (bool, error)

	// return a set of keys in the Storage
	Keys() [][]byte

	// return a chan to iter all keys
	IterKeys(ctx context.Context) <-chan []byte

	// return a set of keys with specified prefix in the Storage
	KeysWithPrefix(prefix []byte) [][]byte

	// return a chan to iter all keys with specified prefix
	IterKeysWithPrefix(ctx context.Context, prefix []byte) <-chan []byte
}

Reader defines common read operations on database/table

type Storage

type Storage interface {
	Table

	// Create or Get the table associated with the name
	Table(string) (Table, error)
	DropTable(string) error

	Close() error
}

Storage defines the data persistence methods

type Table

type Table interface {
	Operations

	EnableBatch()
	DisableBatch()
	IsInBatch() bool
	Flush() error

	// NewTransaction creates a new transaction on the Storage.
	NewTransaction() (Transaction, error)
}

Table defines all methods of database table.

type Transaction

type Transaction interface {
	Operations

	// Commit finalizes a transaction, attempting to commit it to the Storage.
	Commit() error

	// Discard throws away changes recorded in a transaction without committing.
	// them to the underlying Storage. Any calls made to Discard after Commit
	// has been successfully called will have no effect on the transaction and
	// state of the Storage, making it safe to defer.
	Discard()
}

Transaction allow user to read/write Storage atomicly. Actions performed on a transaction will not take hold until a successful call to Commit has been made.

type Writer

type Writer interface {
	// put the value to entry associate with the key
	Put(key, value []byte) error

	// delete the entry associate with the key in the Storage
	Del(key []byte) error
}

Writer defines the write operations on database/table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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