Documentation ¶
Overview ¶
Package zoom is a blazing-fast datastore and querying engine for Go built on Redis. It supports models of any arbitrary struct type and provides basic querying functionality. It also supports atomic transactions, lua scripts, and running Redis commands directly if needed.
Index ¶
- func Close() error
- func Init(config *Configuration) error
- func Interfaces(in interface{}) []interface{}
- func NewConn() redis.Conn
- type Action
- type ActionKind
- type Configuration
- type DefaultData
- type MarshalerUnmarshaler
- type Model
- type ModelNotFoundError
- type ModelType
- func (mt *ModelType) AllIndexKey() string
- func (mt *ModelType) Count() (int, error)
- func (mt *ModelType) Delete(id string) (bool, error)
- func (mt *ModelType) DeleteAll() (int, error)
- func (mt *ModelType) FieldIndexKey(fieldName string) (string, error)
- func (mt *ModelType) Find(id string, model Model) error
- func (mt *ModelType) FindAll(models interface{}) error
- func (mt *ModelType) ModelKey(id string) (string, error)
- func (mt *ModelType) Name() string
- func (modelType *ModelType) NewQuery() *Query
- func (mt *ModelType) Save(model Model) error
- type Query
- func (q *Query) Count() (uint, error)
- func (q *Query) Exclude(fields ...string) *Query
- func (q *Query) Filter(filterString string, value interface{}) *Query
- func (q *Query) Ids() ([]string, error)
- func (q *Query) Include(fields ...string) *Query
- func (q *Query) Limit(amount uint) *Query
- func (q *Query) Offset(amount uint) *Query
- func (q *Query) Order(fieldName string) *Query
- func (q *Query) Run(models interface{}) error
- func (q *Query) RunOne(model Model) error
- func (q *Query) String() string
- type ReplyHandler
- type Transaction
- func (t *Transaction) Command(name string, args redis.Args, handler ReplyHandler)
- func (t *Transaction) Count(mt *ModelType, count *int)
- func (t *Transaction) Delete(mt *ModelType, id string, deleted *bool)
- func (t *Transaction) DeleteAll(mt *ModelType, count *int)
- func (t *Transaction) Exec() error
- func (t *Transaction) Find(mt *ModelType, id string, model Model)
- func (t *Transaction) FindAll(mt *ModelType, models interface{})
- func (t *Transaction) Save(mt *ModelType, model Model)
- func (t *Transaction) Script(script *redis.Script, args redis.Args, handler ReplyHandler)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
func Close() error
Close closes the connection pool and shuts down the Zoom library. It should be run when application exits, e.g. using defer.
func Init ¶
func Init(config *Configuration) error
Init starts the Zoom library and creates a connection pool. It accepts a Configuration struct as an argument. Any zero values in the configuration will fallback to their default values. Init should be called once during application startup.
func Interfaces ¶ added in v0.4.0
func Interfaces(in interface{}) []interface{}
Interfaces converts in to []interface{}. It will panic if the underlying type of in is not a slice.
func NewConn ¶ added in v0.9.0
NewConn gets a connection from the connection pool and returns it. It can be used for directly interacting with the database. See http://godoc.org/github.com/garyburd/redigo/redis for full documentation on the redis.Conn type.
Types ¶
type Action ¶ added in v0.9.0
type Action struct {
// contains filtered or unexported fields
}
Action is a single step in a transaction and must be either a command or a script with optional arguments and a reply handler.
type ActionKind ¶ added in v0.9.0
type ActionKind int
ActionKind is either a command or a script
const ( CommandAction ActionKind = iota ScriptAction )
type Configuration ¶
type Configuration struct { // Address to connect to. Default: "localhost:6379" Address string // Network to use. Default: "tcp" Network string // Database id to use (using SELECT). Default: 0 Database int // Password for a password-protected redis database. If not empty, // every connection will use the AUTH command during initialization // to authenticate with the database. Default: "" Password string }
Configuration contains various options. It should be created once and passed in to the Init function during application startup.
type DefaultData ¶ added in v0.3.0
type DefaultData struct {
// contains filtered or unexported fields
}
DefaultData should be embedded in any struct you wish to save. It includes important fields and required methods to implement Model.
func (DefaultData) Id ¶ added in v0.3.0
func (d DefaultData) Id() string
Id returns the id of the model, satisfying the Model interface
func (*DefaultData) SetId ¶ added in v0.3.0
func (d *DefaultData) SetId(id string)
SetId sets the id of the model, satisfying the Model interface
type MarshalerUnmarshaler ¶ added in v0.4.0
type MarshalerUnmarshaler interface { Marshal(v interface{}) ([]byte, error) // Return a byte-encoded representation of v Unmarshal(data []byte, v interface{}) error // Parse byte-encoded data and store the result in the value pointed to by v. }
Interface MarshalerUnmarshaler defines a handler for marshaling arbitrary data structures into a byte format and unmarshaling bytes into arbitrary data structures. Any struct which correctly implements the interface should have the property that what you put in using Marshal is identical to what you get out using Unmarshal.
type Model ¶
Model is an interface encapsulating anything that can be saved. Any struct which includes an embedded DefaultData field satisfies the Model interface.
type ModelNotFoundError ¶ added in v0.6.0
type ModelNotFoundError struct {
Msg string
}
ModelNotFoundError is returned from Find and Query methods if a model that fits the given criteria is not found.
func (ModelNotFoundError) Error ¶ added in v0.6.0
func (e ModelNotFoundError) Error() string
type ModelType ¶ added in v0.9.0
type ModelType struct {
// contains filtered or unexported fields
}
ModelType represents a specific registered type of model. It has methods for saving, finding, and deleting models of a specific type. Use the Register and RegisterName functions to register new types.
func Register ¶
Register adds a model type to the list of registered types. Any model you wish to save must be registered first. The type of model must be unique, i.e., not already registered, and must be a pointer to a struct. Each registered model gets a name, which is a unique string identifier used as a prefix when storing this type of model in the database. By default the name is just its type without the package prefix or dereference operators. So for example, the default name corresponding to *models.User would be "User". See RegisterName if you need to specify a custom name.
func RegisterName ¶ added in v0.4.0
RegisterName is like Register but allows you to specify a custom name to use for the model type. The custom name will be used as a prefix for all models of this type that are stored in the database. Both the name and the model must be unique, i.e., not already registered. The type of model must be a pointer to a struct.
func (*ModelType) AllIndexKey ¶ added in v0.9.0
AllIndexKey returns the key that identifies a set in the database that stores all the ids for models of the given type.
func (*ModelType) Count ¶ added in v0.9.0
Count returns the number of models of the given type that exist in the database. It returns an error if there was a problem connecting to the database.
func (*ModelType) Delete ¶ added in v0.9.0
Delete removes the model with the given type and id from the database. It will not return an error if the model corresponding to the given id was not found in the database. Instead, it will return a boolean representing whether or not the model was found and deleted, and will only return an error if there was a problem connecting to the database.
func (*ModelType) DeleteAll ¶ added in v0.9.0
DeleteAll deletes all the models of the given type in a single transaction. See http://redis.io/topics/transactions. It returns the number of models deleted and an error if there was a problem connecting to the database.
func (*ModelType) FieldIndexKey ¶ added in v0.9.0
FieldIndexKey returns the key for the sorted set used to index the field identified by fieldName. It returns an error if fieldName does not identify a field in the spec or if the field it identifies is not an indexed field.
func (*ModelType) Find ¶ added in v0.9.0
Find retrieves a model with the given id from redis and scans its values into model. model should be a pointer to a struct of a registered type corresponding to the ModelType. Find will mutate the struct, filling in its fields and overwriting any previous values. It returns an error if a model with the given id does not exist, if the given model was the wrong type, or if there was a problem connecting to the database.
func (*ModelType) FindAll ¶ added in v0.9.0
FindAll finds all the models of the given type. It executes the commands needed to retrieve the models in a single transaction. See http://redis.io/topics/transactions. models must be a pointer to a slice of models with a type corresponding to the ModelType. FindAll will grow or shrink the models slice as needed and if any of the models in the models slice are nil, FindAll will use reflection to allocate memory for them. FindAll returns an error if models is the wrong type or if there was a problem connecting to the database.
func (*ModelType) ModelKey ¶ added in v0.9.0
ModelKey returns the key that identifies a hash in the database which contains all the fields of the model corresponding to the given id. It returns an error iff id is empty.
func (*ModelType) Name ¶ added in v0.9.0
Name returns the name for the given ModelType. The name is a unique string identifier which is used as a prefix when storing this type of model in the database.
func (*ModelType) NewQuery ¶ added in v0.9.0
NewQuery is used to construct a query. The query returned can be chained together with one or more query modifiers (e.g. Filter or Order), and then executed using the Run, RunOne, Count, or Ids methods. If no query modifiers are used, running the query will return all models of the given type in uspecified order. Queries use delated execution, so nothing touches the database until you execute it.
func (*ModelType) Save ¶ added in v0.9.0
Save writes a model (a struct which satisfies the Model interface) to the redis database. Save throws an error if the type of model does not match the registered ModelType. If the Id field of the struct is empty, Save will mutate the struct by setting the Id. To make a struct satisfy the Model interface, you can embed zoom.DefaultData.
type Query ¶ added in v0.3.0
type Query struct {
// contains filtered or unexported fields
}
Query represents a query which will retrieve some models from the database. A Query may consist of one or more query modifiers (e.g. Filter or Order) and may be executed with a query finisher (e.g. Run or Ids).
func (*Query) Count ¶ added in v0.4.0
Count counts the number of models that would be returned by the query without actually retreiving the models themselves. Count will also return the first error that occured during the lifetime of the query object (if any). Otherwise, the second return value will be nil.
func (*Query) Exclude ¶ added in v0.4.0
Exclude specifies one or more field names which will *not* be read from the database and scanned. Any other fields *will* be read and scanned into the resulting models when the query is run. You can only use one of Include or Exclude, not both on the same query. Exclude will set an error if you try to use it with Include on the same query. The error, same as any other error that occurs during the lifetime of the query, is not returned until the query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Filter ¶ added in v0.4.0
Filter applies a filter to the query, which will cause the query to only return models with attributes matching the expression. filterString should be an expression which includes a fieldName, a space, and an operator in that order. Operators must be one of "=", "!=", ">", "<", ">=", or "<=". You can only use Filter on fields which are indexed, i.e. those which have the `zoom:"index"` struct tag. If multiple filters are applied to the same query, the query will only return models which have matches for ALL of the filters. I.e. applying multiple filters is logially equivalent to combining them with a AND or INTERSECT operator. Filter will set an error on the query if the arguments are improperly formated, if the field you are attempting to filter is not indexed, or if the type of value does not match the type of the field. The error, same as any other error that occurs during the lifetime of the query, is not returned until the query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Ids ¶ added in v0.9.0
Ids returns only the ids of the models without actually retreiving the models themselves. Ids will return the first error that occured during the lifetime of the query object (if any).
func (*Query) Include ¶ added in v0.4.0
Include specifies one or more field names which will be read from the database and scanned into the resulting models when the query is run. Field names which are not specified in Include will not be read or scanned. You can only use one of Include or Exclude, not both on the same query. Include will set an error if you try to use it with Exclude on the same query. The error, same as any other error that occurs during the lifetime of the query, is not returned until the query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Limit ¶ added in v0.4.0
Limit specifies an upper limit on the number of records to return. If amount is 0, no limit will be applied. The default value is 0.
func (*Query) Offset ¶ added in v0.4.0
Offset specifies a starting index (inclusive) from which to start counting records that will be returned. The default value is 0.
func (*Query) Order ¶ added in v0.4.0
Order specifies a field by which to sort the models. fieldName should be a field in the struct type corresponding to the ModelType used in the query constructor. By default, the records are sorted by ascending order by the given field. To sort by descending order, put a negative sign before the field name. Zoom can only sort by fields which have been indexed, i.e. those which have the `zoom:"index"` struct tag. However, in the future this may change. Only one order may be specified per query. However in the future, secondary orders may be allowed, and will take effect when two or more models have the same value for the primary order field. Order will set an error on the query if the fieldName is invalid, if another order has already been applied to the query, or if the fieldName specified does not correspond to an indexed field. The error, same as any other error that occurs during the lifetime of the query, is not returned until the query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Run ¶ added in v0.3.0
Run executes the query and scans the results into models. The type of models should be a pointer to a slice of pointers to a registered Model. Run will return the first error that occured during the lifetime of the query object (if any). It will also return an error if models is the wrong type.
type ReplyHandler ¶ added in v0.9.0
type ReplyHandler func(interface{}) error
ReplyHandler is a function which does something with the reply from a redis command or script.
type Transaction ¶ added in v0.9.0
type Transaction struct {
// contains filtered or unexported fields
}
Transaction is an abstraction layer around a redis transaction. Transactions consist of a set of actions which are either redis commands or lua scripts. Transactions feature delayed execution, so nothing toches the database until you call Exec.
func NewTransaction ¶ added in v0.9.0
func NewTransaction() *Transaction
NewTransaction instantiates and returns a new transaction.
func (*Transaction) Command ¶ added in v0.9.0
func (t *Transaction) Command(name string, args redis.Args, handler ReplyHandler)
Command adds a command action to the transaction with the given args. handler will be called with the reply from this specific command when the transaction is executed.
func (*Transaction) Count ¶ added in v0.9.0
func (t *Transaction) Count(mt *ModelType, count *int)
Count counts the number of models of the given type in the database in an existing transaction. It sets the value of count to the number of models. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) Delete ¶ added in v0.9.0
func (t *Transaction) Delete(mt *ModelType, id string, deleted *bool)
Delete removes a model with the given type and id in an existing transaction. deleted will be set to true iff the model was successfully deleted when the transaction is executed. If the no model with the given type and id existed, the value of deleted will be set to false. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) DeleteAll ¶ added in v0.9.0
func (t *Transaction) DeleteAll(mt *ModelType, count *int)
DeleteAll delets all models for the given model type in an existing transaction. The value of count will be set to the number of models that were successfully deleted when the transaction is executed. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) Exec ¶ added in v0.9.0
func (t *Transaction) Exec() error
Exec executes the transaction, sequentially sending each action and calling all the action handlers with the corresponding replies.
func (*Transaction) Find ¶ added in v0.9.0
func (t *Transaction) Find(mt *ModelType, id string, model Model)
Find retrieves a model with the given id from redis and scans its values into model in an existing transaction. model should be a pointer to a struct of a registered type corresponding to the ModelType. find will mutate the struct, filling in its fields and overwriting any previous values. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) FindAll ¶ added in v0.9.0
func (t *Transaction) FindAll(mt *ModelType, models interface{})
FindAll finds all the models of the given type and scans the values of the models into models in an existing transaction. See http://redis.io/topics/transactions. models must be a pointer to a slice of models with a type corresponding to the ModelType. findAll will grow the models slice as needed and if any of the models in the models slice are nil, FindAll will use reflection to allocate memory for them. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) Save ¶ added in v0.9.0
func (t *Transaction) Save(mt *ModelType, model Model)
Save writes a model (a struct which satisfies the Model interface) to the redis database inside an existing transaction. save will set the err property of the transaction if the type of model does not matched the registered ModelType, which will cause exec to fail immediately and return the error. If the Id field of the struct is empty, save will mutate the struct by setting the Id. To make a struct satisfy the Model interface, you can embed zoom.DefaultData. Any errors encountered will be added to the transaction and returned as an error when the transaction is executed.
func (*Transaction) Script ¶ added in v0.9.0
func (t *Transaction) Script(script *redis.Script, args redis.Args, handler ReplyHandler)
Script adds a script action to the transaction with the given args. handler will be called with the reply from this specific script when the transaction is executed.