mom

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2016 License: Apache-2.0 Imports: 3 Imported by: 3

README

Merkle Object Mapper

The original name was going to be morm (Merkle ORM), but then I realized it didn't have any relationships....

Anyway, it is a set of helpers to structure multiple data types in go-merkle and efficiently query them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteRange

func ByteRange(key Key) (start []byte, end []byte, err error)

ByteRange attempts to take the range and serialize it, returns error if either fails

func KeyToBytes

func KeyToBytes(key Key) ([]byte, error)

KeyToBytes returns the Key as a byte array for use in go-merkle Returns an error if some required fields are not set

func ModelToBytes

func ModelToBytes(model Model) ([]byte, error)

ModelToBytes converts the model into bytes to store in the db If there are invalid values in the model you can return an error

func RegisterModels

func RegisterModels(models ...Model)

RegisterModels takes a list of Models and registers them with go-wire for Serialization The control byte is based on the order, so if you want to maintain compatibility with an existing data store, do not change the position of any items. You can use nil as a placeholder to not use that byte anymore.

This also registers the serailzers for the model keys with the same prefix

eg. RegisterModels(Account{}, nil, Status{}) gives account Byte 1, Status Byte 3

func Save

func Save(store merkle.Tree, model Model) (updated bool, err error)

Save attempts to save the given model in the given store updated is false on insert, otherwise true error is non-nil if key or value cannot be serialized

Types

type Key

type Key interface {
	// Range assumes the key has one or more elements at the zero (nil) value
	// It should return two keys, the first is with those zero elements set the the minimum possible value
	// The second return value should be with the zero elements set to their maximum possible value
	Range() (min Key, max Key)
}

Key is designed to let you easily build compound indexes using the one database key

func KeyFromBytes added in v0.1.1

func KeyFromBytes(data []byte) (Key, error)

KeyFromBytes recovers a Key object from arbitrary byte data (eg. from API)

type Model

type Model interface {
	// Key returns the db key for this model.
	// This key may have zero values (and designed for range queries), depending on the state of the model
	// The Key should not change with any allowed transformation of the model
	Key() Key
}

Model is an abstraction over an object to be stored in MerkleDB

func List

func List(store merkle.Tree, q Query) ([]Model, error)

List returns all items that match this query

func Load

func Load(store merkle.Tree, key Key) (Model, error)

Load attempts to find the data matching the given key If the key or store data cannot be parsed, returns error If there is no data, Model is nil

func ModelFromBytes

func ModelFromBytes(data []byte) (Model, error)

ModelFromBytes sets the model contents to the passed in data Returns error if the data doesn't match this model

type Query

type Query struct {
	Key     Key
	Reverse bool
	Filter  func(Model) bool
}

Query can define a detailed range query based on a key

Jump to

Keyboard shortcuts

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