leafdb

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: Unlicense Imports: 9 Imported by: 1

README

LeafDB is an embedded database for Golang that uses LMDB underneath but allows for seamless encoding and decoding of entities of various sorts and on-the-fly creation of arbitrary indexes.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = fmt.Errorf("not found")

Functions

This section is empty.

Types

type DB

type DB[V Type] struct {
	Logger *log.Logger

	Options[V]
	// contains filtered or unexported fields
}

func New

func New[V Type](path string, options Options[V]) (*DB[V], error)

func (*DB[V]) AddOrReplace added in v0.0.4

func (db *DB[V]) AddOrReplace(indexName string, t DataType, newValue V) (id uint32, err error)

func (*DB[V]) Close

func (db *DB[V]) Close() error

func (*DB[V]) Create

func (db *DB[V]) Create(t DataType, value ...V) (lastId uint32, err error)

func (*DB[V]) Delete added in v0.0.2

func (db *DB[V]) Delete(id uint32) (err error)

Delete deletes a record by absolute id.

func (*DB[V]) DeleteQuery added in v0.0.4

func (db *DB[V]) DeleteQuery(params QueryParams) (ids []QueryResult[V], err error)

DeleteQuery deletes everything that matches the query in a single transaction.

func (*DB[V]) Get

func (db *DB[V]) Get(id uint32) (t DataType, value V, err error)

func (*DB[V]) ListAllKeys added in v0.0.7

func (db *DB[V]) ListAllKeys() iter.Seq[[]byte]

ListAllKeys exists mostly for debugging purposes.

func (*DB[V]) Query

func (db *DB[V]) Query(params QueryParams) iter.Seq[V]

func (*DB[V]) QueryComplete added in v0.0.2

func (db *DB[V]) QueryComplete(params QueryParams) iter.Seq2[DataType, QueryResult[V]]

func (*DB[V]) Update

func (db *DB[V]) Update(id uint32, modify func(t DataType, newValue V) (V, error)) (err error)

func (*DB[V]) UpdateQuery added in v0.0.3

func (db *DB[V]) UpdateQuery(params QueryParams, modify func(t DataType, newValue V) (V, error)) (err error)

func (*DB[V]) Upsert added in v0.0.3

func (db *DB[V]) Upsert(
	indexName string,
	indexKey []byte,
	t DataType,
	modifyOrInsert func(t DataType, value V) (V, error),
) (id uint32, err error)

func (*DB[V]) View added in v0.0.3

func (db *DB[V]) View(params QueryParams) iter.Seq[V]

func (*DB[V]) ViewWithKeys added in v0.0.4

func (db *DB[V]) ViewWithKeys(params QueryParams) iter.Seq2[[]byte, V]

type DataType

type DataType uint8 // this is a user-defined type identifier that will be stored to facilitate decoding

type IndexDefinition

type IndexDefinition[V any] struct {
	// Version should start at 1, increase by 1 on every modification
	Version int

	// The DataTypes this index will be applied to, if nil it will apply to all
	Types []DataType

	// Function that emits keys to build the index
	Emit func(t DataType, value V, emit func([]byte)) `json:"-"`

	// When this index is not used anymore
	Deleted bool
	// contains filtered or unexported fields
}

type Options

type Options[V Type] struct {
	Indexes map[string]IndexDefinition[V]
	Views   map[string]ViewDefinition[V]
	Encode  func(t DataType, value V) ([]byte, error)
	Decode  func(t DataType, data []byte) (V, error)
}

type QueryParams

type QueryParams struct {
	Index    string
	StartKey []byte
	EndKey   []byte
	Skip     int
	Limit    int
}

func AnyQuery

func AnyQuery(index string) QueryParams

func ExactQuery added in v0.0.3

func ExactQuery(index string, indexKey []byte) QueryParams

func PrefixQuery

func PrefixQuery(index string, prefix []byte) QueryParams

type QueryResult added in v0.0.2

type QueryResult[V any] struct {
	DataType DataType
	ID       uint32
	Value    V
	Error    error
}

type Type

type Type any // the type this database will encode/decode to

type ViewDefinition added in v0.0.3

type ViewDefinition[V any] struct {
	// Version should start at 1, increase by 1 on every modification
	Version int

	// The DataTypes this index will be applied to, if nil it will apply to all
	Types []DataType

	// Function that emits keys and values to build the view
	Emit func(t DataType, value V, emit func(idxkey []byte, t DataType, value V)) `json:"-"`

	// When this index is not used anymore
	Deleted bool
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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