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.
Version 0.13.1 ¶
For installation instructions, examples, and more information visit https://github.com/albrow/zoom.
Index ¶
- func Interfaces(in interface{}) []interface{}
- type Action
- type ActionKind
- type Collection
- func (c *Collection) Count() (int, error)
- func (c *Collection) Delete(id string) (bool, error)
- func (c *Collection) DeleteAll() (int, error)
- func (c *Collection) FieldIndexKey(fieldName string) (string, error)
- func (c *Collection) Find(id string, model Model) error
- func (c *Collection) FindAll(models interface{}) error
- func (c *Collection) FindFields(id string, fieldNames []string, model Model) error
- func (c *Collection) IndexKey() string
- func (c *Collection) ModelKey(id string) (string, error)
- func (c *Collection) Name() string
- func (collection *Collection) NewQuery() *Query
- func (c *Collection) Save(model Model) error
- func (c *Collection) UpdateFields(fieldNames []string, model Model) error
- type CollectionOptions
- type MarshalerUnmarshaler
- type Model
- type ModelNotFoundError
- type Pool
- type PoolOptions
- 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 RandomId
- type ReplyHandler
- type Transaction
- func (t *Transaction) Command(name string, args redis.Args, handler ReplyHandler)
- func (t *Transaction) Count(c *Collection, count *int)
- func (t *Transaction) Delete(c *Collection, id string, deleted *bool)
- func (t *Transaction) DeleteAll(c *Collection, count *int)
- func (t *Transaction) Exec() error
- func (t *Transaction) Find(c *Collection, id string, model Model)
- func (t *Transaction) FindAll(c *Collection, models interface{})
- func (t *Transaction) FindFields(c *Collection, id string, fieldNames []string, model Model)
- func (t *Transaction) Save(c *Collection, model Model)
- func (t *Transaction) Script(script *redis.Script, args redis.Args, handler ReplyHandler)
- func (t *Transaction) UpdateFields(c *Collection, fieldNames []string, model Model)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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 Collection ¶ added in v0.11.0
type Collection struct {
// contains filtered or unexported fields
}
Collection represents a specific registered type of model. It has methods for saving, finding, and deleting models of a specific type. Use the NewCollection method to create a new collection.
func (*Collection) Count ¶ added in v0.11.0
func (c *Collection) Count() (int, error)
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 (*Collection) Delete ¶ added in v0.11.0
func (c *Collection) Delete(id string) (bool, error)
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 (*Collection) DeleteAll ¶ added in v0.11.0
func (c *Collection) DeleteAll() (int, error)
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 (*Collection) FieldIndexKey ¶ added in v0.11.0
func (c *Collection) FieldIndexKey(fieldName string) (string, error)
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 (*Collection) Find ¶ added in v0.11.0
func (c *Collection) Find(id string, model Model) error
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 Collection. 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 (*Collection) FindAll ¶ added in v0.11.0
func (c *Collection) FindAll(models interface{}) error
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 Collection. 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 (*Collection) FindFields ¶ added in v0.13.0
func (c *Collection) FindFields(id string, fieldNames []string, model Model) error
FindFields is like Find but finds and sets only the specified fields. Any fields of the model which are not in the given fieldNames are not mutated. FindFields will return an error if any of the given fieldNames are not found in the model type.
func (*Collection) IndexKey ¶ added in v0.11.0
func (c *Collection) IndexKey() string
IndexKey returns the key that identifies a set in the database that stores all the ids for models in the given collection.
func (*Collection) ModelKey ¶ added in v0.11.0
func (c *Collection) ModelKey(id string) (string, error)
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 (*Collection) Name ¶ added in v0.11.0
func (c *Collection) Name() string
Name returns the name for the given collection. The name is a unique string identifier to use for the collection in redis. All models in this collection that are saved in the database will use the collection name as a prefix.
func (*Collection) NewQuery ¶ added in v0.11.0
func (collection *Collection) NewQuery() *Query
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 (*Collection) Save ¶ added in v0.11.0
func (c *Collection) Save(model Model) error
Save writes a model (a struct which satisfies the Model interface) to the redis database. Save returns an error if the type of model does not match the registered Collection. To make a struct satisfy the Model interface, you can embed zoom.RandomId, which will generate pseudo-random ids for each model.
func (*Collection) UpdateFields ¶ added in v0.12.0
func (c *Collection) UpdateFields(fieldNames []string, model Model) error
UpdateFields updates only the given fields of the model. UpdateFields uses "last write wins" semantics. If another caller updates the the same fields concurrently, your updates may be overwritten. It will return an error if the type of model does not match the registered Collection, or if any of the given fieldNames are not found in the registered Collection. If UpdateFields is called on a model that has not yet been saved, it will not return an error. Instead, only the given fields will be saved in the database.
type CollectionOptions ¶ added in v0.11.0
type CollectionOptions struct { // FallbackMarshalerUnmarshaler is used to marshal/unmarshal any type // into a slice of bytes which is suitable for storing in the database. If // Zoom does not know how to directly encode a certain type into bytes, it // will use the FallbackMarshalerUnmarshaler. By default, the value is // GobMarshalerUnmarshaler which uses the builtin gob package. Zoom also // provides JSONMarshalerUnmarshaler to support json encoding out of the box. // Default: GobMarshalerUnmarshaler. FallbackMarshalerUnmarshaler MarshalerUnmarshaler // Iff Index is true, any model in the collection that is saved will be added // to a set in redis which acts as an index. The default value is false. The // key for the set is exposed via the IndexKey method. Queries and the // FindAll, Count, and DeleteAll methods will not work for unindexed // collections. This may change in future versions. Default: false. Index bool // Name is a unique string identifier to use for the collection in redis. All // models in this collection that are saved in the database will use the // collection name as a prefix. If not provided, the default name will be the // name of the model type without the package prefix or pointer declarations. // So for example, the default name corresponding to *models.User would be // "User". If a custom name is provided, it cannot contain a colon. // Default: The name of the model type, excluding package prefix and pointer // declarations. Name string }
CollectionOptions contains various options for a pool.
type MarshalerUnmarshaler ¶ added in v0.4.0
type MarshalerUnmarshaler interface { // Marshal returns a byte-encoded representation of v Marshal(v interface{}) ([]byte, error) // Unmarshal parses byte-encoded data and store the result in the value // pointed to by v. Unmarshal(data []byte, v interface{}) error }
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.
var ( // GobMarshalerUnmarshaler is an object that implements MarshalerUnmarshaler // and uses uses the builtin gob package. Note that not all types are // supported by the gob package. See https://golang.org/pkg/encoding/gob/ GobMarshalerUnmarshaler MarshalerUnmarshaler = gobMarshalerUnmarshaler{} // JSONMarshalerUnmarshaler is an object that implements MarshalerUnmarshaler // and uses the builtin json package. Note that not all types are supported // by the json package. See https://golang.org/pkg/encoding/json/#Marshal JSONMarshalerUnmarshaler MarshalerUnmarshaler = jsonMarshalerUnmarshaler{} )
type Model ¶
Model is an interface encapsulating anything that can be saved and retrieved by Zoom. The only requirement is that a Model must have a getter and a setter for a unique id property.
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 Pool ¶ added in v0.10.0
type Pool struct {
// contains filtered or unexported fields
}
Pool represents a pool of connections. Each pool connects to one database and manages its own set of registered models.
func NewPool ¶ added in v0.10.0
func NewPool(options *PoolOptions) *Pool
NewPool initializes and returns a pool with the given options. To use all the default options, you can pass in nil.
func (*Pool) Close ¶ added in v0.10.0
Close closes the pool. It should be run whenever the pool is no longer needed. It is often used in conjunction with defer.
func (*Pool) NewCollection ¶ added in v0.11.0
func (p *Pool) NewCollection(model Model, options *CollectionOptions) (*Collection, error)
NewCollection registers and returns a new collection of the given model type. You must create a collection for each model type you want to save. The type of model must be unique, i.e., not already registered, and must be a pointer to a struct. To use the default options, pass in nil as the options argument.
func (*Pool) NewConn ¶ added in v0.10.0
NewConn gets a connection from the 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.
func (*Pool) NewTransaction ¶ added in v0.10.0
func (p *Pool) NewTransaction() *Transaction
NewTransaction instantiates and returns a new transaction.
type PoolOptions ¶ added in v0.11.0
type PoolOptions 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 }
PoolOptions contains various options for a pool.
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 Collection 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 RandomId ¶ added in v0.9.2
type RandomId struct {
Id string
}
RandomId can be embedded in any model struct in order to satisfy the Model interface. The first time the ModelId method is called on an embedded RandomId, it will generate a pseudo-random id which is highly likely to be unique.
func (*RandomId) ModelId ¶ added in v0.9.2
ModelId returns the id of the model, satisfying the Model interface. If r.Id is an empty string, it will generate a pseudo-random id which is highly likely to be unique.
func (*RandomId) SetModelId ¶ added in v0.9.2
SetModelId sets the id of the model, satisfying the Model interface
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 (*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(c *Collection, 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(c *Collection, 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. You may pass in nil for deleted if you do not care whether or not the model was deleted.
func (*Transaction) DeleteAll ¶ added in v0.9.0
func (t *Transaction) DeleteAll(c *Collection, 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. You may pass in nil for count if you do not care about the number of models that were deleted.
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(c *Collection, 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 Collection. 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(c *Collection, 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 Collection. 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) FindFields ¶ added in v0.13.0
func (t *Transaction) FindFields(c *Collection, id string, fieldNames []string, model Model)
FindFields is like Find but finds and sets only the specified fields. Any fields of the model which are not in the given fieldNames are not mutated. FindFields will return an error if any of the given fieldNames are not found in the model type.
func (*Transaction) Save ¶ added in v0.9.0
func (t *Transaction) Save(c *Collection, 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 match the registered Collection, which will cause exec to fail immediately and return the error. To make a struct satisfy the Model interface, you can embed zoom.RandomId, which will generate pseudo-random ids for each model. 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.
func (*Transaction) UpdateFields ¶ added in v0.12.0
func (t *Transaction) UpdateFields(c *Collection, fieldNames []string, model Model)
UpdateFields updates only the given fields of the model inside an existing transaction. UpdateFields will set the err property of the transaction if the type of model does not match the registered Collection, or if any of the given fieldNames are not found in the model type. In either case, the transaction will return the error when you call Exec. UpdateFields uses "last write wins" semantics. If another caller updates the the same fields concurrently, your updates may be overwritten. If UpdateFields is called on a model that has not yet been saved, it will not return an error. Instead, only the given fields will be saved in the database.