store

package
v1.0.0-...-e80de26 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2015 License: MIT Imports: 9 Imported by: 5

Documentation

Overview

Package store provides an interface for data store operations, to allow for easy extensibility to support various datastores. Also, provides the standardized Update and Query interfaces to data stores.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoParent = errors.New("No parent found")
)

Functions

func Parent

func Parent(id string) (parentid string, rerr error)

Retrieve the parent id for given entity id. Return ErrNoParent if parent is not present. Otherwise, if an error occurs during retrieval, returns that.

func Register

func Register(name string, store Store)

Types

type Object

type Object struct {
	Value  interface{}
	Source string
	NanoTs int64
}

type Query

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

Query stores the read instrutions, storing the instruction set for the entities Query relates to.

func NewQuery

func NewQuery(id string) *Query

NewQuery is the main entrypoint to data store queries. Returns back a Query object pointer, to run read instructions on.

func (*Query) Collect

func (q *Query) Collect(kind string) *Query

Collect specifies the kind of child entities to retrieve. Returns back a new Query pointer pointing to those children entities as a collective.

Any further operations on this returned pointer would attribute to those children entities, and not the caller query entity.

func (*Query) FilterOut

func (q *Query) FilterOut(property string) *Query

FilterOut provides a way to well, filter out, any entities which have the given property.

func (*Query) Run

func (q *Query) Run() (result *Result, rerr error)

Run finds the root from the given Query pointer, recursively executes the read operations, and returns back pointer to Result object. Any errors encountered during these stpeps is returned as well.

func (*Query) UptoDepth

func (q *Query) UptoDepth(level int) *Query

UptoDepth specifies the number of levels of descendants that would be retrieved for the entity Query points to.

You can think of this in terms of a tree structure, where the Entity pointed to by Query points to other Entities, which in turn point to more Entities, and so on. For e.g. Post -> Comments -> Likes.

type Result

type Result struct {
	Id       string
	Kind     string
	Columns  map[string]*Versions
	Children []*Result
}

Result stores the final entity state retrieved from Store upon running the instructions provided by Query.

func (*Result) Debug

func (r *Result) Debug(level int)

func (*Result) Drop

func (r *Result) Drop(pred string)

func (*Result) ToJson

func (r *Result) ToJson() ([]byte, error)

ToJson creates the JSON for the data pointed by the Result pointer.

Note that this doesn't find the "root" from the Result pointer, instead doing the processing only from the current Result pointer.

func (*Result) ToMap

func (r *Result) ToMap() (data map[string]interface{})

func (*Result) WriteJsonResponse

func (r *Result) WriteJsonResponse(w http.ResponseWriter)

WriteJsonResponse does the same as ToJson. But also writes the JSON generated to http.ResponseWriter. In case of error, writes that error instead, in this format:

{
 "code":    "E_ERROR",
 "message": "error message"
}

type Store

type Store interface {
	// Init is used to initialize store driver.
	Init(args ...string)

	// Commit writes the array of instructions to the data store.
	Commit(its []*x.Instruction) error

	// IsNew returns true if the entity id provided doesn't exist in the
	// store. Note that this entity id is never solely the row primary key,
	// because multiple rows can (and most surely will) have the same entity id.
	IsNew(entityId string) bool

	// GetEntity retrieves all the rows for the given subject id, parses them
	// into instructions, appends them to the array, and returns it. Any error
	// encountered during these steps is also returned.
	GetEntity(entityId string) ([]x.Instruction, error)

	// Iterate allows for a way to page over all the entities stored in the table.
	// Iteration starts from id fromId and stops after num results are processed.
	// Note that depending upon database, number of distinct entities might be
	// less than the number of results retrieved from the store. That's normal.
	//
	// Returns the number of entities found, the last entity returned
	// and error, if any. If the number of entities found are zero, assume
	// that we've reached the end of the table.
	Iterate(fromId string, num int, ch chan x.Entity) (int, x.Entity, error)
}

All the data CRUD operations are run via this Store interface. Implement this interface to add support for a datastore.

func Get

func Get() Store

type Update

type Update struct {
	NanoTs int64
	// contains filtered or unexported fields
}

Update stores the create and update instructions, acting as the modifier to the entity Update relates to.

func NewUpdate

func NewUpdate(kind, id string) *Update

NewUpdate is the main entrypoint to updates. Returns back a Update object pointer, to run create and update operations on.

func (*Update) AddChild

func (n *Update) AddChild(kind string) *Update

AddChild creates a new entity with the given kind, and creates a directed relationship from current entity to this new child entity. This is useful to generate arbitrarily deep and complex data structures, for e.g. Posts by Users, Comments on Posts, Likes on Posts etc.

Retuns the Update pointer for the child entity, so any update operations done on this pointer would be reflected in the child entity. Note that a child can only have one parent by design.

func (*Update) Execute

func (n *Update) Execute(c *req.Context) error

Execute finds the root from the given Update pointer, recursively generates the set of instructions to store, and commits them. Returns any errors encountered during these steps.

func (*Update) Id

func (n *Update) Id() string

func (*Update) MarkDeleted

func (n *Update) MarkDeleted() *Update

Marks the current entity for deletion. This is equivalent to doing a Set("delete_me", true), and then running q.FilterOut("delete_me") during query phase. Nothing is actually deleted though, as per the retention principle.

func (*Update) Print

func (n *Update) Print() *Update

Print finds the root from the given Update pointer, and does a recursive print on the tree for debugging purposes.

func (*Update) Set

func (n *Update) Set(property string, value interface{}) *Update

Set allows you to set the property and value on the current entity. This would effectively replace any other value this property had, on this entity node pointer represents. But, no actual data would be overwritten though, as per the retention principle.

func (*Update) SetCommitTs

func (n *Update) SetCommitTs(tsNano int64) *Update

SetCommitTs allows you to set the commit timestamp of the update. The timestamp provided should be in nano-seconds. SetCommitTs can only be called on the root update node.

func (*Update) SetSource

func (n *Update) SetSource(source string) *Update

SetSource sets the author of the update. Generally, the userid of the modifier.

type Versions

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

func (Versions) Count

func (v Versions) Count() int

func (Versions) Latest

func (v Versions) Latest() Object

func (Versions) Oldest

func (v Versions) Oldest() Object

Jump to

Keyboard shortcuts

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