orm

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package orm provides an easy to use db wrapper

Break state space into prefixed sections called Buckets. * Each bucket contains only one type of object. * It has a primary index (which may be composite), and may possess secondary indexes. * It may possess one or more secondary indexes (1:1 or 1:N) * Easy queries for one and iteration.

For inspiration, look at [storm](https://github.com/asdine/storm) built on top of [bolt kvstore](https://github.com/boltdb/bolt#using-buckets). * Do not use so much reflection magic. Better do stuff compile-time static, even if it is a bit of boilerplate. * Consider general usability flow from that project

Index

Constants

View Source
const (
	// SeqID is a constant to use to get a default ID sequence
	SeqID = "id"
)

Variables

View Source
var (
	ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowCodec   = fmt.Errorf("proto: integer overflow")
)
View Source
var ErrInvalidIndex = errors.Register(100, "invalid index")

ErrInvalidIndex is returned when an index specified is invalid

Functions

func RegisterQuery

func RegisterQuery(qr weave.QueryRouter)

RegisterQuery will register a root query (literal keys) under "/"

Types

type Bucket

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

Bucket is a generic holder that stores data as well as references to secondary indexes and sequences.

This is a generic building block that should generally be embedded in a type-safe wrapper to ensure all data is the same type. Bucket is a prefixed subspace of the DB proto defines the default Model, all elements of this type

func NewBucket

func NewBucket(name string, proto Cloneable) Bucket

NewBucket creates a bucket to store data

func (Bucket) DBKey

func (b Bucket) DBKey(key []byte) []byte

DBKey is the full key we store in the db, including prefix We copy into a new array rather than use append, as we don't want consequetive calls to overwrite the same byte array.

func (Bucket) Delete

func (b Bucket) Delete(db weave.KVStore, key []byte) error

Delete will remove the value at a key

func (Bucket) Get

func (b Bucket) Get(db weave.ReadOnlyKVStore, key []byte) (Object, error)

Get one element

func (Bucket) GetIndexed

func (b Bucket) GetIndexed(db weave.ReadOnlyKVStore, name string, key []byte) ([]Object, error)

GetIndexed queries the named index for the given key

func (Bucket) GetIndexedLike

func (b Bucket) GetIndexedLike(db weave.ReadOnlyKVStore, name string, pattern Object) ([]Object, error)

GetIndexedLike querys the named index with the given pattern

func (Bucket) Parse added in v0.3.0

func (b Bucket) Parse(key, value []byte) (Object, error)

Parse takes a key and value data (weave.Model) and reconstructs the data this Bucket would return.

Used internally as part of Get. It is exposed mainly as a test helper, but can work for any code that wants to parse

func (Bucket) Query

func (b Bucket) Query(db weave.ReadOnlyKVStore, mod string,
	data []byte) ([]weave.Model, error)

Query handles queries from the QueryRouter

func (Bucket) Register

func (b Bucket) Register(name string, r weave.QueryRouter)

Register registers this Bucket and all indexes. You can define a name here for queries, which is different than the bucket name used to prefix the data

func (Bucket) Save

func (b Bucket) Save(db weave.KVStore, model Object) error

Save will write a model, it must be of the same type as proto

func (Bucket) Sequence

func (b Bucket) Sequence(name string) Sequence

Sequence returns a Sequence by name

func (Bucket) WithIndex

func (b Bucket) WithIndex(name string, indexer Indexer, unique bool) Bucket

WithIndex returns a copy of this bucket with given index, panics if it an index with that name is already registered.

Designed to be chained.

func (Bucket) WithMultiKeyIndex added in v0.8.0

func (b Bucket) WithMultiKeyIndex(name string, indexer MultiKeyIndexer, unique bool) Bucket

type Cloneable

type Cloneable interface {
	Clone() Object
}

Cloneable will create a new object that can be loaded into

type CloneableData

type CloneableData interface {
	x.Validater
	weave.Persistent
	Copy() CloneableData
}

CloneableData is an intelligent Value that can be embedded in a simple object to handle much of the details.

type Counter

type Counter struct {
	Count int64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"`
}

Counter could be used for sequence, but mainly just for test

func NewCounter

func NewCounter(count int64) *Counter

NewCounter returns an initialized counter

func (*Counter) Copy

func (c *Counter) Copy() CloneableData

Copy produces another counter with the same data

func (*Counter) Descriptor

func (*Counter) Descriptor() ([]byte, []int)

func (*Counter) GetCount

func (m *Counter) GetCount() int64

func (*Counter) Marshal

func (m *Counter) Marshal() (dAtA []byte, err error)

func (*Counter) MarshalTo

func (m *Counter) MarshalTo(dAtA []byte) (int, error)

func (*Counter) ProtoMessage

func (*Counter) ProtoMessage()

func (*Counter) Reset

func (m *Counter) Reset()

func (*Counter) Size

func (m *Counter) Size() (n int)

func (*Counter) String

func (m *Counter) String() string

func (*Counter) Unmarshal

func (m *Counter) Unmarshal(dAtA []byte) error

func (*Counter) Validate

func (c *Counter) Validate() error

Validate returns error on negative numbers

func (*Counter) XXX_DiscardUnknown added in v0.12.0

func (m *Counter) XXX_DiscardUnknown()

func (*Counter) XXX_Marshal added in v0.12.0

func (m *Counter) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Counter) XXX_Merge added in v0.12.0

func (m *Counter) XXX_Merge(src proto.Message)

func (*Counter) XXX_Size added in v0.12.0

func (m *Counter) XXX_Size() int

func (*Counter) XXX_Unmarshal added in v0.12.0

func (m *Counter) XXX_Unmarshal(b []byte) error

type Index

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

Index represents a secondary index on some data. It is indexed by an arbitrary key returned by Indexer. The value is one primary key (unique), Or an array of primary keys (!unique).

func NewIndex

func NewIndex(name string, indexer Indexer, unique bool,
	refKey func([]byte) []byte) Index

NewIndex constructs an index with single key Indexer. Indexer calculates the index for an object unique enforces a unique constraint on the index refKey calculates the absolute dbkey for a ref

func NewMultiKeyIndex added in v0.8.0

func NewMultiKeyIndex(name string, indexer MultiKeyIndexer, unique bool,
	refKey func([]byte) []byte) Index

NewMultiKeyIndex constructs an index with multi key indexer. Indexer calculates the index for an object unique enforces a unique constraint on the index refKey calculates the absolute dbkey for a ref

func (Index) GetAt

func (i Index) GetAt(db weave.ReadOnlyKVStore, index []byte) ([][]byte, error)

GetAt returns a list of all pk at that index (may be empty), or an error

func (Index) GetLike

func (i Index) GetLike(db weave.ReadOnlyKVStore, pattern Object) ([][]byte, error)

GetLike calculates the index for the given pattern, and returns a list of all pk that match (may be nil when empty), or an error

func (Index) GetPrefix

func (i Index) GetPrefix(db weave.ReadOnlyKVStore, prefix []byte) ([][]byte, error)

GetPrefix returns all references that have an index that begins with a given prefix

func (Index) IndexKey

func (i Index) IndexKey(key []byte) []byte

IndexKey is the full key we store in the db, including prefix We copy into a new array rather than use append, as we don't want consequetive calls to overwrite the same byte array.

func (Index) Query

func (i Index) Query(db weave.ReadOnlyKVStore, mod string,
	data []byte) ([]weave.Model, error)

Query handles queries from the QueryRouter

func (Index) Update

func (i Index) Update(db weave.KVStore, prev Object, save Object) error

Update handles updating the reference to the object in the secondary index.

prev == nil means insert save == nil means delete both == nil is error if both != nil and prev.Key() != save.Key() this is an error

Otherwise, it will check indexer(prev) and indexer(save) and make sure the key is now stored in the right location

type Indexer

type Indexer func(Object) ([]byte, error)

Indexer calculates the secondary index key for a given object

type Keyed

type Keyed interface {
	Key() []byte
	SetKey([]byte)
}

Keyed is anything that can identify itself

type MultiKeyIndexer added in v0.8.0

type MultiKeyIndexer func(Object) ([][]byte, error)

MultiKeyIndexer calculates the secondary index keys for a given object

type MultiRef

type MultiRef struct {
	Refs [][]byte `protobuf:"bytes,1,rep,name=refs,proto3" json:"refs,omitempty"`
}

MultiRef contains a list of references to pks

func NewMultiRef

func NewMultiRef(refs ...[]byte) (*MultiRef, error)

NewMultiRef creates a MultiRef with any number of initial references

func (*MultiRef) Add

func (m *MultiRef) Add(ref []byte) error

Add inserts this reference in the multiref, sorted by order. Returns an error if already there

func (*MultiRef) Copy

func (m *MultiRef) Copy() CloneableData

Copy does a shallow copy of the slice of refs and creates a new MultiRef

func (*MultiRef) Descriptor

func (*MultiRef) Descriptor() ([]byte, []int)

func (*MultiRef) GetRefs

func (m *MultiRef) GetRefs() [][]byte

func (*MultiRef) Marshal

func (m *MultiRef) Marshal() (dAtA []byte, err error)

func (*MultiRef) MarshalTo

func (m *MultiRef) MarshalTo(dAtA []byte) (int, error)

func (*MultiRef) ProtoMessage

func (*MultiRef) ProtoMessage()

func (*MultiRef) Remove

func (m *MultiRef) Remove(ref []byte) error

Remove removes this reference from the multiref. Returns an error if already there

func (*MultiRef) Reset

func (m *MultiRef) Reset()

func (*MultiRef) Size

func (m *MultiRef) Size() (n int)

func (*MultiRef) Sort

func (m *MultiRef) Sort()

Sort will make sure everything is in order

func (*MultiRef) String

func (m *MultiRef) String() string

func (*MultiRef) Unmarshal

func (m *MultiRef) Unmarshal(dAtA []byte) error

func (*MultiRef) Validate

func (m *MultiRef) Validate() error

Validate just returns an error if empty

func (*MultiRef) XXX_DiscardUnknown added in v0.12.0

func (m *MultiRef) XXX_DiscardUnknown()

func (*MultiRef) XXX_Marshal added in v0.12.0

func (m *MultiRef) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*MultiRef) XXX_Merge added in v0.12.0

func (m *MultiRef) XXX_Merge(src proto.Message)

func (*MultiRef) XXX_Size added in v0.12.0

func (m *MultiRef) XXX_Size() int

func (*MultiRef) XXX_Unmarshal added in v0.12.0

func (m *MultiRef) XXX_Unmarshal(b []byte) error

type Object

type Object interface {
	Keyed
	Cloneable
	// Validate returns error if the object is not in a valid
	// state to save to the db (eg. field missing, out of range, ...)
	x.Validater
	Value() weave.Persistent
}

Object is what is stored in the bucket Key is joined with the prefix to set the full key Value is the data stored

this can be light wrapper around a protobuf-defined type

type Reader added in v0.8.0

type Reader interface {
	Get(db weave.ReadOnlyKVStore, key []byte) (Object, error)
}

Reader defines an interface that allows reading objects from the db

type Sequence

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

Sequence maintains a counter, and generates a series of keys. Each key is greater than the last, both NextInt() as well as bytes.Compare() on NextVal().

func NewSequence

func NewSequence(bucket, name string) Sequence

NewSequence returns a sequence counter. Sequence is using following pattern to construct a key:

_s.<bucket>:<name>

func (*Sequence) NextInt

func (s *Sequence) NextInt(db weave.KVStore) int64

NextInt increments the sequence and returns its state as int.

func (*Sequence) NextVal

func (s *Sequence) NextVal(db weave.KVStore) []byte

NextVal increments the sequence and returns its state as 8 bytes.

type SimpleObj

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

SimpleObj wraps a key and a value together It can be used as a template for type-safe objects

func NewSimpleObj

func NewSimpleObj(key []byte, value CloneableData) *SimpleObj

NewSimpleObj will combine a key and value into an object

func (*SimpleObj) Clone

func (o *SimpleObj) Clone() Object

Clone will make a copy of this object

func (SimpleObj) Key

func (o SimpleObj) Key() []byte

Key returns the key to store the object under

func (*SimpleObj) SetKey

func (o *SimpleObj) SetKey(key []byte)

SetKey may be used to update a simple obj key

func (SimpleObj) Validate

func (o SimpleObj) Validate() error

Validate makes sure the fields aren't empty. And delegates to the value validator if present

func (SimpleObj) Value

func (o SimpleObj) Value() weave.Persistent

Value gets the value stored in the object

Jump to

Keyboard shortcuts

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