hrd

package module
v0.0.0-...-98fda95 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2016 License: MIT Imports: 6 Imported by: 0

README

hrd Build Status Coverage Status GoDoc

This Go package extends the package appengine.datastore with very useful additional features.

Disclaimer

This package is currently undergoing a massive restructuring. Use at own risk

Features

  • caching: great performance through memcache
  • fluent API: concise code for read, query, write and delete actions
  • hybrid query: queries that have strong consistency and use memcache
  • lifecycle hooks: BeforeLoad/AfterLoad and BeforeSave/AfterSave
  • caching control: turn caching on/off for queries and entities
  • logging: every datastore action is logged for debugging

Internally it uses nds, structor and iszero.

ToDos

  • validated projection query
  • in-memory cache
  • field name & name transformer
  • allow to pass-in logger
  • RPC listener
  • catch more errors at codec creation
  • delete from query
  • namespace support

Install

go get github.com/stephanos/hrd

Documentation

godoc.org

Credit

Without those projects this library would not exist. Thanks!

License

Apache License 2.0 (see LICENSE).

Usage

I suggest having a look at the E2E tests to see how it is used.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deleter

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

Deleter can delete entities from a kind.

func (*Deleter) Entities

func (d *Deleter) Entities(srcs interface{}) error

Entities deletes the provided entities.

func (*Deleter) Entity

func (d *Deleter) Entity(src interface{}) error

Entity deletes the provided entity.

func (*Deleter) ID

func (d *Deleter) ID(id int64, parent ...*Key) error

ID deletes a single entity by id from the datastore.

func (*Deleter) IDs

func (d *Deleter) IDs(ids ...int64) error

IDs deletes multiple keys by id from the datastore.

func (*Deleter) Key

func (d *Deleter) Key(key *Key) error

Key deletes a single entity by key from the datastore.

func (*Deleter) Keys

func (d *Deleter) Keys(keys []*Key) error

Keys deletes multiple entities by key from the datastore.

func (Deleter) Kind

func (sa Deleter) Kind() *types.Kind

func (*Deleter) TextID

func (d *Deleter) TextID(id string, parent ...*Key) error

TextID deletes a single key by text id from the datastore.

func (*Deleter) TextIDs

func (d *Deleter) TextIDs(ids ...string) error

TextIDs deletes multiple keys by text id from the datastore.

type Iterator

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

Iterator is the result of running a query.

func (*Iterator) Cursor

func (it *Iterator) Cursor() (string, error)

Cursor returns a cursor for the Iterator's current location.

func (*Iterator) GetAll

func (it *Iterator) GetAll(dsts interface{}) ([]*Key, error)

GetAll loads all entities from the Iterator into the passed destination.

func (*Iterator) GetOne

func (it *Iterator) GetOne(dst interface{}) (*Key, error)

GetOne loads an entity from the Iterator into the passed destination.

type Key

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

Key represents the datastore key of an entity.

func (*Key) Error

func (k *Key) Error() error

Error returns an error associated with the key.

func (*Key) Exists

func (k *Key) Exists() bool

Exists is whether an entity with this key exists in the datastore.

func (*Key) Incomplete

func (k *Key) Incomplete() bool

Incomplete returns whether the key does not refer to a stored entity. In particular, whether the key has a zero StringID and a zero IntID.

func (*Key) IntID

func (k *Key) IntID() int64

IntID returns the key's integer ID, which may be zero.

func (*Key) Kind

func (k *Key) Kind() string

Kind returns the key's kind (also known as entity type).

func (*Key) Namespace

func (k *Key) Namespace() string

Namespace returns the key's namespace.

func (*Key) Parent

func (k *Key) Parent() *Key

Parent returns the key's parent key, which may be nil.

func (*Key) String

func (k *Key) String() string

func (*Key) StringID

func (k *Key) StringID() string

StringID returns the key's string ID (also known as an entity name or key name), which may be empty.

func (*Key) ToDSKey

func (k *Key) ToDSKey(ctx ae.Context) *ds.Key

ToDSKey returns the respective datastore.Key.

type Kind

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

Kind represents a entity category in the datastore.

func (*Kind) Delete

func (k *Kind) Delete(ctx ae.Context) *Deleter

Delete returns a Deleter action object. It allows to delete entities from the datastore.

func (*Kind) Load

func (k *Kind) Load(ctx ae.Context) *Loader

Load returns a Loader action object. It allows to load entities from the datastore.

func (*Kind) Name

func (k *Kind) Name() string

Name returns the name of the kind.

func (*Kind) NewNumKey

func (k *Kind) NewNumKey(id int64, parent ...*Key) *Key

NewNumKey returns a key for the passed kind and numeric ID. It can also receive an optional parent key.

func (*Kind) NewNumKeys

func (k *Kind) NewNumKeys(ids ...int64) []*Key

NewNumKeys returns a sequence of key for the passed kind and sequence of numeric ID.

func (*Kind) NewTextKey

func (k *Kind) NewTextKey(id string, parent ...*Key) *Key

NewTextKey returns a key for the passed kind and string ID. It can also receive an optional parent key.

func (*Kind) NewTextKeys

func (k *Kind) NewTextKeys(ids ...string) []*Key

NewTextKeys returns a sequence of keys for the passed kind and sequence of string ID.

func (*Kind) Query

func (k *Kind) Query(ctx ae.Context) *Query

Query returns a Query object It allows to query entities from the datastore.

func (*Kind) Save

func (k *Kind) Save(ctx ae.Context) *Saver

Save returns a Saver action object. It allows to save entities to the datastore.

type Loader

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

Loader can load entities from a kind.

func (*Loader) ID

func (l *Loader) ID(id int64, parent ...*Key) *SingleLoader

ID loads a single entity by id from the datastore.

func (*Loader) IDs

func (l *Loader) IDs(ids ...int64) *MultiLoader

IDs load multiple keys by id from the datastore.

func (*Loader) Key

func (l *Loader) Key(key *Key) *SingleLoader

Key loads a single entity by key from the datastore.

func (*Loader) Keys

func (l *Loader) Keys(keys []*Key) *MultiLoader

Keys load multiple entities by key from the datastore.

func (Loader) Kind

func (sa Loader) Kind() *types.Kind

func (*Loader) NoGlobalCache

func (l *Loader) NoGlobalCache() *Loader

NoGlobalCache prevents reading/writing entities from/to memcache.

func (*Loader) TextID

func (l *Loader) TextID(id string, parent ...*Key) *SingleLoader

TextID loads a single key by text id from the datastore.

func (*Loader) TextIDs

func (l *Loader) TextIDs(ids ...string) *MultiLoader

TextIDs load multiple keys by text id from the datastore.

type MultiLoader

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

MultiLoader is a special Loader that allows to fetch multiple entities from the datastore.

func (*MultiLoader) GetAll

func (l *MultiLoader) GetAll(dsts interface{}) ([]*Key, error)

GetAll loads entities from the datastore into the passed destination.

type Query

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

Query represents a datastore query.

func (*Query) Ancestor

func (qry *Query) Ancestor(k *Key) (ret *Query)

Ancestor returns a derivative Query with an ancestor filter. The ancestor should not be nil.

func (*Query) Distinct

func (qry *Query) Distinct() (ret *Query)

Distinct returns a derivative query that yields de-duplicated entities with respect to the set of projected fields. It is only used for projection queries.

func (*Query) End

func (qry *Query) End(c string) (ret *Query)

End returns a derivative Query with the passed end point.

func (*Query) EventualConsistency

func (qry *Query) EventualConsistency() (ret *Query)

EventualConsistency returns a derivative query that returns eventually consistent results. It only has an effect on ancestor queries.

func (*Query) Filter

func (qry *Query) Filter(q string, val interface{}) (ret *Query)

Filter returns a derivative Query with a field-based filter. The filterStr argument must be a field name followed by optional space, followed by an operator, one of ">", "<", ">=", "<=", or "=". Fields are compared against the provided value using the operator. Multiple filters are AND'ed together.

func (*Query) GetAll

func (qry *Query) GetAll(dsts interface{}) ([]*Key, string, error)

GetAll runs the query and writes the entities to the passed destination.

Note that, if not manually disabled, queries for more than 1 item use a "hybrid query". This means that first a keys-only query is executed and then the keys are used to lookup the local and global cache as well as the datastore eventually. For a warm cache this usually is faster and cheaper than the regular query.

func (*Query) GetCount

func (qry *Query) GetCount() (int, error)

GetCount returns the number of results for the query.

func (*Query) GetFirst

func (qry *Query) GetFirst(dst interface{}) (*Key, error)

GetFirst executes the query and writes the result's first entity to the passed destination.

func (*Query) GetKeys

func (qry *Query) GetKeys() ([]*Key, string, error)

GetKeys executes the query as keys-only: No entities are retrieved, just their keys.

func (*Query) Limit

func (qry *Query) Limit(limit int) (ret *Query)

Limit returns a derivative Query that has a limit on the number of results returned. A negative value means unlimited.

func (*Query) NoGlobalCache

func (qry *Query) NoGlobalCache() (ret *Query)

NoGlobalCache prevents reading/writing entities from/to memcache.

func (*Query) NoLimit

func (qry *Query) NoLimit() (ret *Query)

NoLimit returns a derivative Query that has no limit on the number of results returned.

func (*Query) Offset

func (qry *Query) Offset(off int) (ret *Query)

Offset returns a derivative Query that has an offset of how many keys to skip over before returning results. A negative value is invalid.

func (*Query) OrderAsc

func (qry *Query) OrderAsc(s string) (ret *Query)

OrderAsc returns a derivative Query with a field-based sort order, ascending. Orders are applied in the order they are added.

func (*Query) OrderDesc

func (qry *Query) OrderDesc(s string) (ret *Query)

OrderDesc returns a derivative Query with a field-based sort order, descending. Orders are applied in the order they are added.

func (*Query) Project

func (qry *Query) Project(fields ...string) (ret *Query)

Project returns a derivative Query that yields only the passed fields. It cannot be used in a keys-only query.

func (*Query) Run

func (qry *Query) Run() *Iterator

Run executes the query and returns an Iterator.

func (*Query) Start

func (qry *Query) Start(c string) (ret *Query)

Start returns a derivative Query with the passed start point.

type Saver

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

Saver can save entities to the datastore.

func (*Saver) CompleteKeys

func (s *Saver) CompleteKeys() *Saver

CompleteKeys prevents saving an entity with an incomplete key.

func (*Saver) Entities

func (s *Saver) Entities(srcs interface{}) ([]*Key, error)

Entities saves the passed entities into the datastore. If an entity's key is incomplete, the returned keys will contain a unique key generated by the datastore.

func (*Saver) Entity

func (s *Saver) Entity(src interface{}) (*Key, error)

Entity saves the passed entity into the datastore. If its key is incomplete, the returned key will be a unique key generated by the datastore.

func (Saver) Kind

func (sa Saver) Kind() *types.Kind

type SingleLoader

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

SingleLoader is a special Loader that allows to fetch exactly one entity from the datastore.

func (*SingleLoader) GetOne

func (l *SingleLoader) GetOne(dst interface{}) (*Key, error)

GetOne loads an entity from the datastore into the passed destination.

type Store

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

Store represents the App Engine datastore. Usually there should only be one per application.

func NewStore

func NewStore() *Store

NewStore creates a new store.

func (*Store) CreatedAt

func (s *Store) CreatedAt() time.Time

CreatedAt returns the time the store was created.

func (*Store) Kind

func (s *Store) Kind(name string) *Kind

Kind returns a kind for the passed name.

func (*Store) NoGlobalCache

func (s *Store) NoGlobalCache() *Store

NoGlobalCache prevents reading/writing entities from/to memcache.

func (*Store) RegisterEntity

func (s *Store) RegisterEntity(entity interface{}) error

RegisterEntity prepares the passed-in struct type for the datastore. It returns an error if the type is invalid.

func (*Store) RegisterEntityMust

func (s *Store) RegisterEntityMust(entity interface{})

RegisterEntityMust prepares the passed-in struct type for the datastore. It panics if the type is invalid.

func (*Store) TX

func (s *Store) TX(ctx ae.Context) *Transactor

TX creates a Transactor to run a transaction on the store.

type TX

type TX interface {
	ae.Context
}

TX represents an App Engine Context inside a transaction. It should only be used inside a transaction.

type Transactor

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

Transactor can run multiple datastore operations inside a transaction. By default it does not handle multiple entity groups.

func (*Transactor) Run

func (tx *Transactor) Run(f func(_ TX) error) error

Run executes a function in a transaction.

func (*Transactor) XG

func (tx *Transactor) XG(enable ...bool) *Transactor

XG defines whether the transaction can cross multiple entity groups. If no parameter is passed, true is assumed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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