morm

package
v0.1.0 Latest Latest
Warning

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

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

README

Model ORM

This is a refinement of the ModelBucket from weave/orm. I am testing it out here to ease development of the models in tutorial, but this should be merged into the main weave codebase.

You can see here how all parts of weave (including orm, middleware, etc) are extensible in third-party packages. But if you don't like looking under the hood, you can ignore this package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthCodec = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowCodec   = fmt.Errorf("proto: integer overflow")
)

Functions

This section is empty.

Types

type Counter

type Counter struct {
	ID    []byte `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	Count int64  `protobuf:"varint,2,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() orm.CloneableData

Copy produces a new copy to fulfill the Model interface

func (*Counter) Descriptor

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

func (*Counter) GetCount

func (m *Counter) GetCount() int64

func (*Counter) GetID

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

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) SetID

func (c *Counter) SetID(id []byte) error

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

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 is always succesful

func (*Counter) XXX_DiscardUnknown

func (m *Counter) XXX_DiscardUnknown()

func (*Counter) XXX_Marshal

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

func (*Counter) XXX_Merge

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

func (*Counter) XXX_Size

func (m *Counter) XXX_Size() int

func (*Counter) XXX_Unmarshal

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

type Model

type Model interface {
	weave.Persistent
	Validate() error
	Copy() orm.CloneableData
	GetID() []byte
	SetID([]byte) 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.

Model stores both the Key and the Value. GetID/SetID are used to store and access the Key. The ID is always set to nil before serializing and storing the Value.

type ModelBucket

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

	// PrefixScan will scan for all models with a primary key (ID)
	// that begins with the given prefix.
	// The function returns a (possibly empty) iterator, which can
	// load each model as it arrives.
	// If reverse is true, iterates in descending order (highest value first),
	// otherwise, it iterates in
	PrefixScan(db weave.ReadOnlyKVStore, prefix []byte, reverse bool) (ModelIterator, 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) 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) (ModelIterator, 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, m Model) 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

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 ModelBucketOption

type ModelBucketOption func(mb *modelBucket)

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

func WithIndex

func WithIndex(name string, indexer orm.Indexer, 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.

type ModelIterator

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

	// Release releases the Iterator.
	Release()
}

type ModelSlicePtr

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.

Jump to

Keyboard shortcuts

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