db

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EachBreak = errors.New("each break (not an error)")

EachBreak is an error that Each callbacks could return to stop the loop and return nil.

View Source
var ErrKeyNotFound = errors.New("key not found in database")

ErrKeyNotFound is returned if either a key or a bucket is not found.

Functions

func IsBucketError

func IsBucketError(err error) bool

IsBucketError returns true if the given error is a bucket error.

func StringFunc

func StringFunc(s *string) func(b []byte) error

StringFunc returns a new function for setting a string with Get

Types

type BucketError

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

BucketError wraps an error for bucket traversal errors.

func (BucketError) Unwrap

func (err BucketError) Unwrap() error

Unwrap unwraps the BucketError.

type KV

type KV struct {
	Marshaler
	// contains filtered or unexported fields
}

func NewKVFile

func NewKVFile(path string) (*KV, error)

func (*KV) Close

func (kv *KV) Close() error

Close closes the database.

func (*KV) DropPrefix

func (kv *KV) DropPrefix(path NodePath) error

DropPrefix drops the whole given prefix.

func (*KV) Node

func (kv *KV) Node(names ...string) Node

Node creates a new Node from the given names joined as paths.

func (*KV) NodeFromPath

func (kv *KV) NodeFromPath(path NodePath) Node

NodeFromPath creates a new Node from path.

type Marshaler

type Marshaler interface {
	Marshal(interface{}) ([]byte, error)
	Unmarshal([]byte, interface{}) error
}
var JSONMarshaler Marshaler = jsonMarshaler{}

JSONMarshaler is the default key-value marshaler.

type Node

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

func (Node) Delete

func (n Node) Delete(k string) error

func (Node) Drop

func (n Node) Drop() error

Drop drops the entire node and all its values.

func (Node) DropExceptLast

func (n Node) DropExceptLast(last int) error

DropExceptLast drops the entire node except for the last few values. This method heavily relies on keyed values being sorted properly, and that the stored values are NOT nested.

func (Node) Each

func (n Node) Each(fn func(k string, b []byte, len int) error) error

Each iterates over the bucket all possible keys with the prefix, or no prefix. It takes in a pointer.

Caveats

Since the pointer is reused, the user will need to manually copy it if they want to store the reference to that matched struct. Key includes the prefix.

Example

For iterating, as mentioned above, the user will need to manually copy the pointer by dereferencing and re-referencing it.

var obj Struct
var objs []Struct

n.Each(&obj, "", func(k string) error {
    if obj.Thing == "what I want" {
        objs = append(objs, obj)
        return EachBreak
    }
    return nil
})

func (Node) EachReverse

func (n Node) EachReverse(fn func(k string, b []byte, len int) error) error

EachReverse is like Each, except the iteration is done in reverse.

func (Node) Exists

func (n Node) Exists(k string) (exists bool)

Exists returns true if the given key exists.

func (Node) FromPath

func (n Node) FromPath(path NodePath) Node

FromPath creates a new node with the given full path. The path will completely override the old path.

func (Node) Get

func (n Node) Get(k string, f func([]byte) error) error

Get gets the given key from the node.

func (Node) GetAny

func (n Node) GetAny(k string, v interface{}) error

Get calls get and unmarshals on any value.

func (Node) Length

func (n Node) Length(prefix string) (int, error)

Length queries the number of keys within the node, similarly to running AllKeys and taking the length of what was returned.

func (Node) Node

func (n Node) Node(names ...string) Node

Node creates a child node with the given names appended to its path. If the node has an ongoing transaction, then it is inherited over.

func (Node) Set

func (n Node) Set(k string, v []byte) error

Set sets the key into the database.

func (Node) SetAny

func (n Node) SetAny(k string, v interface{}) error

SetAny marshals v before setting.

func (Node) SetIfNone

func (n Node) SetIfNone(k string, v []byte) error

SetIfNone sets the key into the database only if the key does not exist. This method is useful primarily for filling up the cache with data fetched from the API while data from /sync should be prioritized.

func (Node) TxUpdate

func (n Node) TxUpdate(f func(n Node) error) error

TxUpdate creates a new Node with an active transaction and calls f. If this method is called in a Node that already has a transaction, then that transaction is reused.

func (Node) TxView

func (n Node) TxView(f func(n Node) error) error

TxUpdate creates a new Node with an active read-only transaction and calls f. If this method is called in a Node that already has a transaction, then that transaction is reused.

func (Node) Unmarshal

func (n Node) Unmarshal(b []byte, v interface{}) error

Unmarshal calls te node's unmarshaler.

func (Node) UnmarshalFunc

func (n Node) UnmarshalFunc(v interface{}) func([]byte) error

UnmarshalFunc creates a new function for unmarshaling with Get.

type NodePath

type NodePath [][]byte

NodePath contains the full path to a node. It can be used as a lighter way to store nodes.

func NewNodePath

func NewNodePath(names ...string) NodePath

NewNodePath creates a new NodePath.

func (NodePath) Bucket

func (p NodePath) Bucket(tx *bbolt.Tx) (*bbolt.Bucket, error)

NodePath traverses the given transaction and returns the bucket. If any of the buckets don't exist, then a new one is created, unless the transaction is read-only.

If NodePath is empty, then a Bucket with a nil byte is returned.

func (NodePath) BucketExists

func (p NodePath) BucketExists(tx *bbolt.Tx) (*bbolt.Bucket, bool)

BucketExists traverses and returns true if the bucket exists.

func (NodePath) Tail

func (p NodePath) Tail(tails ...string) NodePath

Tail creates a copy of NodePath with the given tail.

Jump to

Keyboard shortcuts

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