badgerstore

package
Version: v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Index

type Index struct {
	// Index name
	Name string
	// Key callback is called with a resource item of the type defined by Type,
	// and should return the string to use as index value.
	// It does not have to be unique.
	//
	// Example index by Country and lower case Name on a user model:
	// 	func(v interface{}) {
	// 		user := v.(UserModel)
	// 		return []byte(user.Country + "_" + strings.ToLower(user.Name))
	// 	}
	Key func(interface{}) []byte
}

Index defines an index used for a resource.

When used on Model resource, an index entry will be added for each model entry. An index entry will have no value (nil), and the key will have the following structure:

<Name>:<Key>\x00<RID>

Where: * <Name> is the name of the Index (so keep it rather short) * <Key> is the index value as returned from the Key callback * <RID> is the resource ID of the indexed model

type IndexQuery

type IndexQuery struct {
	// Index used
	Index Index
	// KeyPrefix to match against the index key
	KeyPrefix []byte
	// FilterKeys for keys in the query collection. May be nil.
	FilterKeys func(key []byte) bool
	// Offset from which item to start.
	Offset int
	// Limit how many items to read. Negative means unlimited.
	Limit int
	// Reverse flag to tell if order is reversed
	Reverse bool
}

IndexQuery represents a query towards an index.

func (*IndexQuery) FetchCollection

func (iq *IndexQuery) FetchCollection(db *badger.DB) ([]string, error)

FetchCollection fetches a collection of resource references based on the query.

type QueryStore

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

QueryStore allows for querying resources in a Store.

It implements the res.QueryStore interface.

A QueryStore should be created using NewQueryStore.

func NewQueryStore

func NewQueryStore(st *Store, iq func(qs *QueryStore, q url.Values) (*IndexQuery, error)) *QueryStore

NewQueryStore creates a new QueryStore and initializes it.

The type of typ will be used as value. If the type supports both the encoding.BinaryMarshaler and the encoding.BinaryUnmarshaler, those method will be used for marshaling the values. Otherwise, encoding/json will be used for marshaling.

The index query callback, iq, will be called on queries to transform a set of url.Values to an *IndexQuery and a normalized query string. In case the query callback returns an error, both the IndexQuery value and the normalized query string will be ignored.

func (*QueryStore) AddIndex

func (qs *QueryStore) AddIndex(idx Index) *QueryStore

AddIndex adds an index to the query store.

func (*QueryStore) Flush

func (qs *QueryStore) Flush()

Flush waits for the indexing queue to be cleared.

func (*QueryStore) Index

func (qs *QueryStore) Index(name string) Index

Index returns the named index. Panics if the index does not exist.

func (*QueryStore) OnQueryChange

func (qs *QueryStore) OnQueryChange(cb func(store.QueryChange))

OnQueryChange adds a listener callback that is called whenever a value change may have affected the results of the queries.

func (*QueryStore) Query

func (qs *QueryStore) Query(q url.Values) (interface{}, error)

Query performs a query towards the Store. If error is non-nil the result is nil.

func (*QueryStore) RebuildIndexes

func (qs *QueryStore) RebuildIndexes() error

RebuildIndexes drops current index entries and creates new ones.

func (*QueryStore) SetLogger

func (qs *QueryStore) SetLogger(l logger.Logger) *QueryStore

SetLogger sets the logger.

type Store

type Store struct {
	DB *badger.DB
	// contains filtered or unexported fields
}

Store is a database CRUD store implementation for BadgerDB.

It implements the store.Store interface.

A Store must not be copied after first call to Read or Write.

func NewStore

func NewStore(db *badger.DB) *Store

NewStore creates a new Store and initializes it.

The type of typ will be used as value. If the type supports both the encoding.BinaryMarshaler and the encoding.BinaryUnmarshaler, those method will be used for marshaling the values. Otherwise, encoding/json will be used for marshaling.

func (*Store) BeforeChange

func (st *Store) BeforeChange(cb func(id string, before, after interface{}) error)

BeforeChange adds a listener callback that is called before a value is created, updated, or deleted from the database.

If the callback returns an error, the change will be canceled.

func (*Store) Get

func (st *Store) Get(id string) (interface{}, error)

Get reads the value from the store without locking.

func (*Store) Init

func (st *Store) Init(cb func(add func(id string, v interface{})) error) error

Init adds initial resources for the store. If the store has been previously initialized, no resources will be added. It uses a key, "$<prefix>.init" (where <prefix> is the set prefix), to mark the store as initialized.

func (*Store) OnChange

func (st *Store) OnChange(cb func(id string, before, after interface{}))

OnChange adds a listener callback that is called whenever a value is created, updated, or deleted from the database.

If a value is created, before will be set to nil.

If a value is deleted, after will be set to nil.

func (*Store) Read

func (st *Store) Read(id string) store.ReadTxn

Read makes a read-lock for the resource that lasts until Close is called.

func (*Store) SetPrefix

func (st *Store) SetPrefix(prefix string) *Store

SetPrefix sets the prefix that will be prepended to all resource ID's, using a dot (.) as separator between the prefix and the rest of the ID.

func (*Store) SetType

func (st *Store) SetType(typ interface{}) *Store

SetType sets the type, typ, that will be used to unmarshal stored values into. If the type supports both the encoding.BinaryMarshaler and the encoding.BinaryUnmarshaler, those method will be used for marshaling the values. Otherwise, encoding/json will be used for marshaling.

func (*Store) Type

func (st *Store) Type() interface{}

Type returns a zero-value of the type used by the store for unmarshaling values.

func (*Store) Write

func (st *Store) Write(id string) store.WriteTxn

Write makes a write-lock for the resource that lasts until Close is called.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to