orm

package
v0.25.1-staging Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: Apache-2.0 Imports: 13 Imported by: 23

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 ErrBucket = errors.Register(101, "bucket already initialized")

ErrBucket is returned when already initialized bucket is tried to be indexed again

View Source
var ErrInvalidIndex = errors.Register(100, "invalid index")

ErrInvalidIndex is returned when an index specified is invalid

Functions

func MarshalVersionedID added in v0.21.0

func MarshalVersionedID(key VersionedIDRef) []byte

MarshalVersionedID is used to guarantee determinism while serializing a VersionedIDRef. It comes with the option to omit empty version should you want to do a prefix query.

func RegisterQuery

func RegisterQuery(qr weave.QueryRouter)

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

func ToSlice added in v0.22.0

func ToSlice(iter SerialModelIterator, destination SerialModelSlicePtr) error

func ValidateSequence added in v0.21.3

func ValidateSequence(id []byte) error

ValidateSequence returns an error if this is not an 8-byte as expected for orm.IDGenBucket

Types

type Bucket

type Bucket interface {
	weave.QueryHandler

	DBKey(key []byte) []byte
	Delete(db weave.KVStore, key []byte) error
	Get(db weave.ReadOnlyKVStore, key []byte) (Object, error)
	// Index returns an index with given name maintained for this bucket.
	Index(name string) (Index, error)
	GetIndexed(db weave.ReadOnlyKVStore, name string, key []byte) ([]Object, error)
	Parse(key, value []byte) (Object, error)
	Register(name string, r weave.QueryRouter)
	Save(db weave.KVStore, model Object) error
	Sequence(name string) Sequence

	// WithIndex returns a copy of this bucket with given index. Index is
	// maintained as a single set. This implementation is suitable for
	// small collections.
	// Panics if it an index with that name is already registered.
	WithIndex(name string, indexer Indexer, unique bool) Bucket

	// WithMultiKeyIndex returns a copy of this bucket with given index.
	// Index is maintained as a single set. This implementation is suitable
	// for small collections.
	//
	// Panics if it an index with that name is already registered.
	WithMultiKeyIndex(name string, indexer MultiKeyIndexer, unique bool) Bucket

	// WithNativeIndex returns a copy of this bucket with given index.
	// Index is maintained using database native support. Each index entry
	// is stored as a separate database entry, lookups are using database
	// iterator. This implementation is suitable for big collections.
	//
	// Panics if it an index with that name is already registered.
	WithNativeIndex(name string, indexer MultiKeyIndexer) Bucket
}

func NewBucket

func NewBucket(name string, emptyModel Model) Bucket

NewBucket creates a bucket to store data

type CloneableData

type CloneableData interface {
	x.Validater
	weave.Persistent
}

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

CloneableData interface is deprecated and must not be used anymore.

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 CounterWithID added in v0.22.0

type CounterWithID struct {
	PrimaryKey []byte `protobuf:"bytes,1,opt,name=primary_key,json=primaryKey,proto3" json:"primary_key,omitempty"`
	Count      int64  `protobuf:"varint,2,opt,name=count,proto3" json:"count,omitempty"`
}

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

func (*CounterWithID) Descriptor added in v0.22.0

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

func (*CounterWithID) GetCount added in v0.22.0

func (m *CounterWithID) GetCount() int64

func (*CounterWithID) GetPrimaryKey added in v0.22.0

func (m *CounterWithID) GetPrimaryKey() []byte

func (*CounterWithID) Marshal added in v0.22.0

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

func (*CounterWithID) MarshalTo added in v0.22.0

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

func (*CounterWithID) ProtoMessage added in v0.22.0

func (*CounterWithID) ProtoMessage()

func (*CounterWithID) Reset added in v0.22.0

func (m *CounterWithID) Reset()

func (*CounterWithID) SetPrimaryKey added in v0.22.0

func (c *CounterWithID) SetPrimaryKey(pk []byte) error

SetPrimaryKey is a minimal implementation, useful when the ID is a separate protobuf field

func (*CounterWithID) Size added in v0.22.0

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

func (*CounterWithID) String added in v0.22.0

func (m *CounterWithID) String() string

func (*CounterWithID) Unmarshal added in v0.22.0

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

func (*CounterWithID) Validate added in v0.22.0

func (c *CounterWithID) Validate() error

Validate is always succesful

func (*CounterWithID) XXX_DiscardUnknown added in v0.22.0

func (m *CounterWithID) XXX_DiscardUnknown()

func (*CounterWithID) XXX_Marshal added in v0.22.0

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

func (*CounterWithID) XXX_Merge added in v0.22.0

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

func (*CounterWithID) XXX_Size added in v0.22.0

func (m *CounterWithID) XXX_Size() int

func (*CounterWithID) XXX_Unmarshal added in v0.22.0

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

type IDGenBucket added in v0.15.0

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

func WithIDGenerator added in v0.15.0

func WithIDGenerator(b Bucket, gen IDGenerator) IDGenBucket

WithIDGenerator creates a bucket with uses the given id generator on top of the given bucket implementation.

func WithSeqIDGenerator added in v0.15.0

func WithSeqIDGenerator(b Bucket, seqName string) IDGenBucket

WithSeqIDGenerator adds a Sequence for primary ID key generation on top fo the given bucket implementation

func (IDGenBucket) Create added in v0.15.0

func (b IDGenBucket) Create(db weave.KVStore, data CloneableData) (Object, error)

Create saves the given data in a persistent bucket with a new generated ID key.

type IDGenerator added in v0.15.0

type IDGenerator interface {
	// NextVal returns a new unique ID key
	NextVal(db weave.KVStore, obj CloneableData) ([]byte, error)
}

IDGenerator defines an interface for custom id generators

type IDGeneratorFunc added in v0.15.0

type IDGeneratorFunc func(db weave.KVStore, obj CloneableData) ([]byte, error)

IDGeneratorFunc provides IDGenerator interface support.

func (IDGeneratorFunc) NextVal added in v0.15.0

func (i IDGeneratorFunc) NextVal(db weave.KVStore, obj CloneableData) ([]byte, error)

NextVal returns a new unique ID key

type Index

type Index interface {
	// Name returns the name of this index.
	Name() string

	// Update updates the index. It should be called when any of the bucket
	// entities has changed in the store.
	//
	// prev == nil means insert
	// save == nil means delete
	// both == nil is error
	// if both != nil and prev.Key() != save.Key() this is an error
	Update(db weave.KVStore, prev Object, save Object) error

	// Keys returns an iteator that returns all entity keys that were
	// indexed under given value.
	//
	// Values of returned iterator are always nil to optimize for a lazy
	// loading flows and avoid loading into memory values from the database
	// when they might not be needed.
	Keys(db weave.ReadOnlyKVStore, value []byte) weave.Iterator

	// Query handles queries from the QueryRouter.
	Query(db weave.ReadOnlyKVStore, mod string, data []byte) ([]weave.Model, error)
}

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 NewNativeIndex added in v0.24.0

func NewNativeIndex(name string, indexer MultiKeyIndexer) Index

NewNativeIndex returns an index implementation that is using a database native storage and query in order to maintain and provide access to an index.

type Indexer

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

Indexer calculates the secondary index key for a given object

type Model added in v0.16.0

type Model interface {
	weave.Persistent
	Validate() error
}

Model is implemented by any entity that can be stored using ModelBucket.

This is the same interface as CloneableData. Using the right type names provides an easier to read API.

type ModelBucket added in v0.16.0

type ModelBucket interface {
	// One query the database for a single model instance. Lookup is done
	// by the primary index key. Result is loaded into given destination
	// model.
	// This method returns ErrNotFound if the entity does not exist in the
	// database.
	// If given model type cannot be used to contain stored entity, ErrType
	// is returned.
	One(db weave.ReadOnlyKVStore, key []byte, dest Model) error

	// ByIndex returns all objects that secondary index with given name and
	// given key. Main index is always unique but secondary indexes can
	// return more than one value for the same key.
	// All matching entities are appended to given destination slice. If no
	// result was found, no error is returned and destination slice is not
	// modified.
	ByIndex(db weave.ReadOnlyKVStore, indexName string, key []byte, dest ModelSlicePtr) (keys [][]byte, err error)

	// Index returns the index with given name that is maintained for this
	// bucket. This function can return ErrInvalidIndex if an index with
	// requested name does not exist.
	//
	// This method allows for lower level access and more control than
	// ByIndex method provides. When iterating over huge native indexes
	// where you do not want to load everything into memory, using an index
	// directly is a way to go.
	Index(name string) (Index, error)

	// Put saves given model in the database. Before inserting into
	// database, model is validated using its Validate method.
	// If the key is nil or zero length then a sequence generator is used
	// to create a unique key value.
	// Using a key that already exists in the database cause the value to
	// be overwritten.
	Put(db weave.KVStore, key []byte, m Model) ([]byte, error)

	// Delete removes an entity with given primary key from the database.
	// It returns ErrNotFound if an entity with given key does not exist.
	Delete(db weave.KVStore, key []byte) error

	// Has returns nil if an entity with given primary key value exists. It
	// returns ErrNotFound if no entity can be found.
	// Has is a cheap operation that that does not read the data and only
	// checks the existence of it.
	Has(db weave.KVStore, key []byte) error

	// Register registers this buckets content to be accessible via query
	// requests under the given name.
	Register(name string, r weave.QueryRouter)
}

ModelBucket is implemented by buckets that operates on Models rather than Objects.

func NewModelBucket added in v0.16.0

func NewModelBucket(name string, m Model, opts ...ModelBucketOption) ModelBucket

NewModelBucket returns a ModelBucket instance. This implementation relies on a bucket instance. Final implementation should operate directly on the KVStore instead.

type ModelBucketIterator added in v0.24.1

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

ModelBucketIterator allows for iteration over all entities of a single bucket.

func IterAll added in v0.24.1

func IterAll(bucketName string) *ModelBucketIterator

IterAll returns an iterator instance that loops through all entities kept by given bucket. Returned iterator does not support schema migration. It always returns the entity in version it is stored in the database.

func (*ModelBucketIterator) Next added in v0.24.1

func (it *ModelBucketIterator) Next(db weave.ReadOnlyKVStore, dest Model) ([]byte, error)

Next returns the next item key. Loads the value of the item into the provided model. For each call a new iterator is created so that a database modification can be done between this method calls.

type ModelBucketOption added in v0.16.0

type ModelBucketOption func(mb *modelBucket)

ModelBucketOption is implemented by any function that can configure ModelBucket during creation.

func WithIDSequence added in v0.19.0

func WithIDSequence(s Sequence) ModelBucketOption

WithIDSequence configures the bucket to use the given sequence instance for generating ID.

func WithIndex added in v0.16.0

func WithIndex(name string, indexer interface{}, unique bool) ModelBucketOption

WithIndex configures the bucket to build an index with given name. All entities stored in the bucket are indexed using value returned by the indexer function. If an index is unique, there can be only one entity referenced per index value. Indexer value must be a function that implements either Indexer or MultiKeyIndexer interface.

func WithNativeIndex added in v0.24.0

func WithNativeIndex(name string, indexer MultiKeyIndexer) ModelBucketOption

WithNativeIndex configures a bucket to maintain an index. Used index implementation is using the underlying storage mechanisms to maintain and iterate over indexed data. This implementation should be used to maintain an index for big collections. For small collections, use WithIndex function that configures a compact index implementation.

type ModelSlicePtr added in v0.16.0

type ModelSlicePtr interface{}

ModelSlicePtr represents a pointer to a slice of models. Think of it as *[]Model Because of Go type system, using []Model would not work for us. Instead we use a placeholder type and the validation is done during the runtime.

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 {
	// 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

	Key() []byte
	SetKey([]byte)
}

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 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, error)

NextInt increments the sequence and returns its state as int.

func (*Sequence) NextVal

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

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

type SerialModel added in v0.22.0

type SerialModel interface {
	weave.Persistent
	Validate() error
	GetPrimaryKey() []byte
	SetPrimaryKey([]byte) error
}

SerialModel is implemented by any entity that can be stored using SerialModelBucket. GetPrimaryKey/SetPrimaryKey are used to store and access the Key. The ID is always set to nil before serializing and storing the Value.

type SerialModelBucket added in v0.22.0

type SerialModelBucket interface {
	// ByID query the database for a single SerialModel instance by ID. Lookup is done
	// by the primary index key. Result is loaded into given destination
	// SerialModel.
	// This method returns ErrNotFound if the entity does not exist in the
	// database.
	// If given SerialModel type cannot be used to contain stored entity, ErrType
	// is returned.
	ByID(db weave.ReadOnlyKVStore, key []byte, dest SerialModel) error

	// PrefixScan will scan for all SerialModels with a primary key (ID)
	// that begins with the given prefix.
	// The function returns a (possibly empty) iterator, which can
	// load each SerialModel as it arrives.
	// If reverse is true, iterates in descending order (highest value first),
	// otherwise, it iterates in ascending order.
	PrefixScan(db weave.ReadOnlyKVStore, prefix []byte, reverse bool) (SerialModelIterator, error)

	// ByIndex returns all objects that secondary index with given name and
	// given key. Main index is always unique but secondary indexes can
	// return more than one value for the same key.
	// All matching entities are appended to given destination slice. If no
	// result was found, no error is returned and destination slice is not
	// modified.
	ByIndex(db weave.ReadOnlyKVStore, indexName string, key []byte, dest SerialModelSlicePtr) error

	// IndexScan does a PrefixScan, but on the named index. This would let us eg. load all counters
	// in order of their count. Or easily find the lowest or highest count.
	IndexScan(db weave.ReadOnlyKVStore, indexName string, prefix []byte, reverse bool) (SerialModelIterator, error)

	// Save saves given SerialModel in the database.
	//If ID field is provided uses it otherwise generates an auto-incremented key.
	// Before inserting into database, SerialModel is validated using Validate method.
	Save(db weave.KVStore, m SerialModel) error

	// Delete removes an entity with given primary key from the database.
	// Returns ErrNotFound if an entity with given key does not exist.
	Delete(db weave.KVStore, key []byte) error

	// Has returns nil if an entity with given primary key value exists. It
	// returns ErrNotFound if no entity can be found.
	// Has is a cheap operation that that does not read the data and only
	// checks the existence of it.
	Has(db weave.KVStore, key []byte) error

	// Register registers this buckets content to be accessible via query
	// requests under the given name.
	Register(name string, r weave.QueryRouter)
}

SerialModelBucket is implemented by buckets that operates on SerialModels rather than Objects.

func NewSerialModelBucket added in v0.22.0

func NewSerialModelBucket(name string, m SerialModel, opts ...SerialModelBucketOption) SerialModelBucket

NewSerialModelBucket returns a SerialModelBucket instance. This implementation relies on a bucket instance. Final implementation should operate directly on the KVStore instead.

type SerialModelBucketOption added in v0.22.0

type SerialModelBucketOption func(smb *serialModelBucket)

SerialModelBucketOption is implemented by any function that can configure SerialModelBucket during creation.

func WithIndexSerial added in v0.22.0

func WithIndexSerial(name string, indexer Indexer, unique bool) SerialModelBucketOption

WithIndexSerial configures the bucket to build an index with given name. All entities stored in the bucket are indexed using value returned by the indexer function. If an index is unique, there can be only one entity referenced per index value.

type SerialModelIterator added in v0.22.0

type SerialModelIterator interface {
	// LoadNext moves the iterator to the next sequential key in the database and
	// loads the current value at the given key into the passed destination.
	LoadNext(dest SerialModel) error

	// Release releases the Iterator.
	Release()
}

SerialModelIterator over a domain of keys in ascending order. End is exclusive. Start must be less than end, or the Iterator is invalid. CONTRACT: No writes may happen within a domain while an iterator exists over it.

func LimitIterator added in v0.22.0

func LimitIterator(iter SerialModelIterator, limit int) SerialModelIterator

type SerialModelSlicePtr added in v0.22.0

type SerialModelSlicePtr interface{}

SerialModelSlicePtr represents a pointer to a slice of SerialModels. Think of it as *[]SerialModel Because of Go type system, using []SerialModel would not work for us. Instead we use a placeholder type and the validation is done during the runtime.

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 Model) *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

type VersionedIDRef added in v0.15.0

type VersionedIDRef struct {
	// Unique identifier
	ID []byte `protobuf:"bytes,4,opt,name=id,proto3" json:"id,omitempty"`
	// Document version, starting with 1.
	Version uint32 `protobuf:"varint,5,opt,name=version,proto3" json:"version,omitempty"`
}

VersionedID is the combination of document ID and version number.

func UnmarshalVersionedID added in v0.21.0

func UnmarshalVersionedID(b []byte) (VersionedIDRef, error)

UnmarshalVersionedID is used to deserialize a VersionedIDRef from a deterministic format. It expects version to be stored in the last 4 bytes of the passed slice.

func (*VersionedIDRef) Descriptor added in v0.15.0

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

func (*VersionedIDRef) GetID added in v0.15.0

func (m *VersionedIDRef) GetID() []byte

func (*VersionedIDRef) GetVersion added in v0.15.0

func (m *VersionedIDRef) GetVersion() uint32

func (*VersionedIDRef) Marshal added in v0.15.0

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

func (*VersionedIDRef) MarshalTo added in v0.15.0

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

func (VersionedIDRef) NextVersion added in v0.15.0

func (m VersionedIDRef) NextVersion() (VersionedIDRef, error)

NextVersion returns a new VersionedIDRef with the same ID as current but version +1.

func (*VersionedIDRef) ProtoMessage added in v0.15.0

func (*VersionedIDRef) ProtoMessage()

func (*VersionedIDRef) Reset added in v0.15.0

func (m *VersionedIDRef) Reset()

func (*VersionedIDRef) SetVersion added in v0.15.0

func (m *VersionedIDRef) SetVersion(v uint32)

func (*VersionedIDRef) Size added in v0.15.0

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

func (*VersionedIDRef) String added in v0.15.0

func (m *VersionedIDRef) String() string

func (*VersionedIDRef) Unmarshal added in v0.15.0

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

func (VersionedIDRef) Validate added in v0.15.0

func (m VersionedIDRef) Validate() error

func (*VersionedIDRef) XXX_DiscardUnknown added in v0.15.0

func (m *VersionedIDRef) XXX_DiscardUnknown()

func (*VersionedIDRef) XXX_Marshal added in v0.15.0

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

func (*VersionedIDRef) XXX_Merge added in v0.15.0

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

func (*VersionedIDRef) XXX_Size added in v0.15.0

func (m *VersionedIDRef) XXX_Size() int

func (*VersionedIDRef) XXX_Unmarshal added in v0.15.0

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

type VersioningBucket added in v0.15.0

type VersioningBucket struct {
	IDGenBucket
}

func WithVersioning added in v0.15.0

func WithVersioning(b IDGenBucket) VersioningBucket

WithVersioning add versioning functionality to the underlying bucket. This means objects can not be overwritten anymore via Save function which must not be used with this type. Instead Create and Update methods are provided to support a history of object versions.

func (VersioningBucket) Create added in v0.15.0

func (b VersioningBucket) Create(db weave.KVStore, data versionedData) (*VersionedIDRef, error)

Create stores the given data. It assigns an ID and initial version number to the object instance and returns the VersionedIDRef which won't be nil on success.

func (VersioningBucket) CreateWithID added in v0.19.0

func (b VersioningBucket) CreateWithID(db weave.KVStore, id []byte, data versionedData) (*VersionedIDRef, error)

CreateWithID stores the given data. It accepts an ID and assigns an initial version number to the object instance and returns the VersionedIDRef which won't be nil on success. This method is designed to be used for scenarios where an ID is needed to generate data within the entity before saving it.

func (VersioningBucket) Delete added in v0.15.0

func (b VersioningBucket) Delete(db weave.KVStore, id []byte) (*VersionedIDRef, error)

Delete stores a tombstone value for the new highest version. It will return this key on success. A version for the given ID must exists or ErrNotFound is returned. When already deleted Err

func (VersioningBucket) Exists added in v0.15.0

func (b VersioningBucket) Exists(db weave.KVStore, idRef VersionedIDRef) (bool, error)

Exists returns if an object is persisted for that given VersionedIDRef. If it points to the tombstone as deletion marker, ErrDeleted is returned.

func (VersioningBucket) Get added in v0.15.0

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

Get works with a marshalled VersionedIDRef key. Direct usage should be avoided in favour of GetVersion or GetLatestVersion. Unlike the classic Get function it returns:

  • ErrNotFound when not found
  • ErrDeleted when deleted

Object won't be nil in success case

func (VersioningBucket) GetLatestVersion added in v0.15.0

func (b VersioningBucket) GetLatestVersion(db weave.ReadOnlyKVStore, id []byte) (*VersionedIDRef, Object, error)

GetLatestVersion finds the latest version for the given id and returns the VersionedIDRef and loaded object. Unlike the classic Get function it returns:

  • ErrNotFound when not found
  • ErrDeleted when deleted

Object won't be nil in success case

func (VersioningBucket) GetVersion added in v0.15.0

func (VersioningBucket) Save deprecated added in v0.15.0

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

Deprecated: Save will always return an error. Use Create or Update instead.

func (VersioningBucket) Update added in v0.15.0

func (b VersioningBucket) Update(db weave.KVStore, id []byte, data versionedData) (*VersionedIDRef, error)

Update persists the given data object with a new derived version key in the storage. The VersionedIDRef returned won't be nil on success and contains the new version number. The currentKey must be the latest one in usage or an ErrDuplicate is returned.

Jump to

Keyboard shortcuts

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