db

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: AGPL-3.0 Imports: 7 Imported by: 19

Documentation

Overview

Package db contains a generalized data provider interface aswell as a number of built-in implementations.

Index

Constants

View Source
const (
	// Invalid datatype, must raise error if attempted used.
	DATATYPE_UNKNOWN = 0
	// Bytecode
	DATATYPE_BIN = 1
	// Menu symbol
	DATATYPE_MENU = 2
	// Template symbol
	DATATYPE_TEMPLATE = 4
	// Static LOAD symbols
	DATATYPE_STATICLOAD = 8
	// State and cache from persister
	DATATYPE_STATE = 16
	// Application data
	DATATYPE_USERDATA = 32
)

Variables

View Source
var (
	ErrTxExist  = errors.New("tx already exists")
	ErrNoTx     = errors.New("tx does not exist")
	ErrSingleTx = errors.New("not a multi-instruction tx")
)

Functions

func FromDbKey added in v0.3.0

func FromDbKey(b []byte) ([]byte, error)

func IsNotFound

func IsNotFound(err error) bool

func NewErrNotFound

func NewErrNotFound(k []byte) error

NewErrNotFound creates a new ErrNotFound with the given storage key.

func ToDbKey

func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte

ToDbKey generates a key to use Db to store a value for a particular context.

If language is nil, then default language storage context will be used.

If language is not nil, and the context does not support language, the language value will silently will be ignored.

Types

type CloseFunc added in v0.3.0

type CloseFunc func() error

type Db

type Db interface {
	// Connect prepares the storage backend for use.
	//
	// If called more than once, consecutive calls should be ignored.
	Connect(ctx context.Context, connStr string) error
	// MUST be called before termination after a Connect().
	Close(context.Context) error
	// Get retrieves the value belonging to a key.
	//
	// Errors if the key does not exist, or if the retrieval otherwise fails.
	Get(ctx context.Context, key []byte) ([]byte, error)
	// Put stores a value under a key.
	//
	// Any existing value will be replaced.
	//
	// Errors if the value could not be stored.
	Put(ctx context.Context, key []byte, val []byte) error
	// SetPrefix sets the storage context prefix to use for consecutive Get and Put operations.
	SetPrefix(pfx uint8)
	// SetSession sets the session context to use for consecutive Get and Put operations.
	//
	// Session only affects the following datatypes:
	// * DATATYPE_STATE
	// * DATATYPE_USERSTART
	SetSession(sessionId string)
	// SetLock disables modification of data that is readonly in the vm context.
	//
	// If called with typ value 0, it will permanently lock all readonly members.
	SetLock(typ uint8, locked bool) error
	// Safe returns true if db is safe for use with a vm.
	Safe() bool
	// SetLanguage sets the language context to use on consecutive gets or puts
	//
	// Language only affects the following datatypes:
	// * DATATYPE_MENU
	// * DATATYPE_TEMPLATE
	// * DATATYPE_STATICLOAD
	SetLanguage(*lang.Language)
	// Prefix returns the current active datatype prefix
	Prefix() uint8
	Dump(context.Context, []byte) (*Dumper, error)
	DecodeKey(ctx context.Context, key []byte) ([]byte, error)
	Start(context.Context) error
	Stop(context.Context) error
	Abort(context.Context)
	Connection() string
}

Db abstracts all data storage and retrieval as a key-value store

type DbBase

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

DbBase is a base class that must be extended by all db.Db implementers.

It must be created with NewDbBase()

func NewDbBase

func NewDbBase() *DbBase

NewDbBase instantiates a new DbBase.

func (*DbBase) Abort added in v0.3.0

func (bd *DbBase) Abort(ctx context.Context)

func (*DbBase) CheckPut

func (bd *DbBase) CheckPut() bool

CheckPut returns true if the current selected data type can be written to.

func (*DbBase) Connect added in v0.3.0

func (bd *DbBase) Connect(ctx context.Context, connStr string) error

func (*DbBase) Connection added in v0.3.0

func (bd *DbBase) Connection() string

func (*DbBase) DecodeKey added in v0.3.0

func (bd *DbBase) DecodeKey(ctx context.Context, key []byte) ([]byte, error)

func (*DbBase) FromSessionKey added in v0.3.0

func (bd *DbBase) FromSessionKey(key []byte) ([]byte, error)

func (*DbBase) Prefix

func (bd *DbBase) Prefix() uint8

func (*DbBase) Safe

func (bd *DbBase) Safe() bool

func (*DbBase) SetLanguage

func (bd *DbBase) SetLanguage(ln *lang.Language)

SetLanguage implements the Db interface.

func (*DbBase) SetLock

func (bd *DbBase) SetLock(pfx uint8, lock bool) error

SetLock implements the Db interface.

func (*DbBase) SetPrefix

func (bd *DbBase) SetPrefix(pfx uint8)

SetPrefix implements the Db interface.

func (*DbBase) SetSession

func (bd *DbBase) SetSession(sessionId string)

SetSession implements the Db interface.

func (*DbBase) Start added in v0.3.0

func (bd *DbBase) Start(ctx context.Context) error

func (*DbBase) Stop added in v0.3.0

func (bd *DbBase) Stop(ctx context.Context) error

func (*DbBase) ToKey

func (bd *DbBase) ToKey(ctx context.Context, key []byte) (LookupKey, error)

ToKey creates a DbKey within the current session context.

TODO: hard to read, clean up

func (*DbBase) ToSessionKey added in v0.3.0

func (bd *DbBase) ToSessionKey(pfx uint8, key []byte) []byte

type Dumper added in v0.2.2

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

func NewDumper added in v0.2.2

func NewDumper(fn DumperFunc) *Dumper

func (*Dumper) Close added in v0.3.0

func (d *Dumper) Close() error

func (*Dumper) Next added in v0.2.2

func (d *Dumper) Next(ctx context.Context) ([]byte, []byte)

func (*Dumper) WithClose added in v0.3.0

func (d *Dumper) WithClose(fn func() error) *Dumper

func (*Dumper) WithFirst added in v0.2.2

func (d *Dumper) WithFirst(k []byte, v []byte) *Dumper

type DumperFunc added in v0.2.2

type DumperFunc func(ctx context.Context) ([]byte, []byte)

type ErrNotFound

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

ErrNotFound is returned with a key was successfully queried, but did not match a stored key.

func (ErrNotFound) Error

func (e ErrNotFound) Error() string

Error implements Error.

func (ErrNotFound) Is

func (e ErrNotFound) Is(err error) bool

type LookupKey

type LookupKey struct {
	Default     []byte
	Translation []byte
}

Source Files

  • db.go
  • doc.go
  • dump.go
  • error.go
  • log.go

Directories

Path Synopsis
Package dbtest provides test vectors to verify correct operation of a db.Db implementation.
Package dbtest provides test vectors to verify correct operation of a db.Db implementation.
Package fs is a filesystem backed implementation of the db.Db interface.
Package fs is a filesystem backed implementation of the db.Db interface.
Package gdbm is a gdbm database backed implementation of the db.Db interface.
Package gdbm is a gdbm database backed implementation of the db.Db interface.
Package mem is a volatile in-process memory implementation of the db.Db interface.
Package mem is a volatile in-process memory implementation of the db.Db interface.
Package postgres is a Postgres database backed implementation of the db.Db interface.
Package postgres is a Postgres database backed implementation of the db.Db interface.

Jump to

Keyboard shortcuts

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