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
- Variables
- func RegisterQuery(qr weave.QueryRouter)
- type Bucket
- func (b Bucket) DBKey(key []byte) []byte
- func (b Bucket) Delete(db weave.KVStore, key []byte) error
- func (b Bucket) Get(db weave.ReadOnlyKVStore, key []byte) (Object, error)
- func (b Bucket) GetIndexed(db weave.ReadOnlyKVStore, name string, key []byte) ([]Object, error)
- func (b Bucket) GetIndexedLike(db weave.ReadOnlyKVStore, name string, pattern Object) ([]Object, error)
- func (b Bucket) Parse(key, value []byte) (Object, error)
- func (b Bucket) Query(db weave.ReadOnlyKVStore, mod string, data []byte) ([]weave.Model, error)
- func (b Bucket) Register(name string, r weave.QueryRouter)
- func (b Bucket) Save(db weave.KVStore, model Object) error
- func (b Bucket) Sequence(name string) Sequence
- func (b Bucket) WithIndex(name string, indexer Indexer, unique bool) Bucket
- func (b Bucket) WithMultiKeyIndex(name string, indexer MultiKeyIndexer, unique bool) Bucket
- type Cloneable
- type CloneableData
- type Counter
- func (c *Counter) Copy() CloneableData
- func (*Counter) Descriptor() ([]byte, []int)
- func (m *Counter) GetCount() int64
- func (m *Counter) Marshal() (dAtA []byte, err error)
- func (m *Counter) MarshalTo(dAtA []byte) (int, error)
- func (*Counter) ProtoMessage()
- func (m *Counter) Reset()
- func (m *Counter) Size() (n int)
- func (m *Counter) String() string
- func (m *Counter) Unmarshal(dAtA []byte) error
- func (c *Counter) Validate() error
- func (m *Counter) XXX_DiscardUnknown()
- func (m *Counter) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Counter) XXX_Merge(src proto.Message)
- func (m *Counter) XXX_Size() int
- func (m *Counter) XXX_Unmarshal(b []byte) error
- type Index
- func (i Index) GetAt(db weave.ReadOnlyKVStore, index []byte) ([][]byte, error)
- func (i Index) GetLike(db weave.ReadOnlyKVStore, pattern Object) ([][]byte, error)
- func (i Index) GetPrefix(db weave.ReadOnlyKVStore, prefix []byte) ([][]byte, error)
- func (i Index) IndexKey(key []byte) []byte
- func (i Index) Query(db weave.ReadOnlyKVStore, mod string, data []byte) ([]weave.Model, error)
- func (i Index) Update(db weave.KVStore, prev Object, save Object) error
- type Indexer
- type Keyed
- type MultiKeyIndexer
- type MultiRef
- func (m *MultiRef) Add(ref []byte) error
- func (m *MultiRef) Copy() CloneableData
- func (*MultiRef) Descriptor() ([]byte, []int)
- func (m *MultiRef) GetRefs() [][]byte
- func (m *MultiRef) Marshal() (dAtA []byte, err error)
- func (m *MultiRef) MarshalTo(dAtA []byte) (int, error)
- func (*MultiRef) ProtoMessage()
- func (m *MultiRef) Remove(ref []byte) error
- func (m *MultiRef) Reset()
- func (m *MultiRef) Size() (n int)
- func (m *MultiRef) Sort()
- func (m *MultiRef) String() string
- func (m *MultiRef) Unmarshal(dAtA []byte) error
- func (m *MultiRef) Validate() error
- func (m *MultiRef) XXX_DiscardUnknown()
- func (m *MultiRef) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *MultiRef) XXX_Merge(src proto.Message)
- func (m *MultiRef) XXX_Size() int
- func (m *MultiRef) XXX_Unmarshal(b []byte) error
- type Object
- type Reader
- type Sequence
- type SimpleObj
Constants ¶
const (
// SeqID is a constant to use to get a default ID sequence
SeqID = "id"
)
Variables ¶
var ( ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowCodec = fmt.Errorf("proto: integer overflow") )
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 (Bucket) DBKey ¶
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) GetIndexed ¶
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
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) 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) WithIndex ¶
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 (*Counter) Copy ¶
func (c *Counter) Copy() CloneableData
Copy produces another counter with the same data
func (*Counter) Descriptor ¶
func (*Counter) ProtoMessage ¶
func (*Counter) ProtoMessage()
func (*Counter) XXX_DiscardUnknown ¶ added in v0.12.0
func (m *Counter) XXX_DiscardUnknown()
func (*Counter) XXX_Marshal ¶ added in v0.12.0
func (*Counter) XXX_Unmarshal ¶ added in v0.12.0
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 ¶
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) GetLike ¶
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 ¶
GetPrefix returns all references that have an index that begins with a given prefix
func (Index) IndexKey ¶
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) Update ¶
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 MultiKeyIndexer ¶ added in v0.8.0
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 ¶
NewMultiRef creates a MultiRef with any number of initial references
func (*MultiRef) Add ¶
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) ProtoMessage ¶
func (*MultiRef) ProtoMessage()
func (*MultiRef) Remove ¶
Remove removes this reference from the multiref. Returns an error if already there
func (*MultiRef) XXX_DiscardUnknown ¶ added in v0.12.0
func (m *MultiRef) XXX_DiscardUnknown()
func (*MultiRef) XXX_Marshal ¶ added in v0.12.0
func (*MultiRef) XXX_Unmarshal ¶ added in v0.12.0
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 ¶
NewSequence returns a sequence counter. Sequence is using following pattern to construct a key:
_s.<bucket>:<name>
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) Validate ¶
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