| |
- __builtin__.object
-
- IndexedRedisHelper
-
- IndexedRedisDelete
- IndexedRedisQuery
- IndexedRedisSave
- IndexedRedisModel
- exceptions.Exception(exceptions.BaseException)
-
- InvalidModelException
class IndexedRedisModel(__builtin__.object) |
|
IndexedRedisModel - This is the model you should extend.
**Required Fields:**
*FIELDS* - REQUIRED. a list of strings which name the fields that can be used for storage.
Example: ['Name', 'Description', 'Model', 'Price']
*INDEXED_FIELDS* - a list of strings containing the names of fields that will be indexed. Can only filter on indexed fields. Adds insert/delete time. Contents must also be in FIELDS.
Example: ['Name', 'Model']
*BASE64_FIELDS* - A list of strings which name the fields that will be stored as base64-encoded strings. All entries must also be present in FIELDS.
Example: ['data', 'blob']
*BINARY_FIELDS* - A list of strings which name the fields that will be stored as unencoded binary data. All entries must also be present in FIELDS. Entries here will be omitted from __repr__ view and replaced with "_BINARY FIELD OF LENGTH N_" where N is the length, in bytes, of the string.
*KEY_NAME* - REQUIRED. A unique name name that represents this model. Think of it like a table name.
Example: 'Items'
*REDIS_CONNECTION_PARAMS* - provides the arguments to pass into "redis.Redis", to construct a redis object.
Usage
-----
Usage is very similar to Django or Flask.
**Query:**
Calling .filter or .filterInline builds a query/filter set. Use one of the *Fetch* methods described below to execute a query.
objects = SomeModel.objects.filter(param1=val).filter(param2=val).all()
**Save:**
obj = SomeModel(field1='value', field2='value')
obj.save()
**Delete Using Filters:**
SomeModel.objects.filter(name='Bad Man').delete()
**Delete Individual Objects:**
obj.delete()
**Atomic Dataset Replacement:**
There is also a powerful method called "reset" which will **atomically** replace all elements belonging to a model. This is useful for cache-replacement, etc.
lst = [SomeModel(...), SomeModel(..)]
SomeModel.reset(lst)
For example, you could have a SQL backend and a cron job that does complex queries (or just fetches the same models) and does an atomic replace every 5 minutes to get massive performance boosts in your application.
Filter objects by SomeModel.objects.filter(key=val, key2=val2) and get objects with .all
Example: SomeModel.objects.filter(name='Tim', colour='purple').filter(number=5).all()
**Fetch Functions**:
Building filtersets do not actually fetch any data until one of these are called (see API for a complete list). All of these functions act on current filterset.
Example: matchingObjects = SomeModel.objects.filter(...).all()
all - Return all objects matching this filter
allOnlyFields - Takes a list of fields and only fetches those fields, using current filterset
delete - Delete objects matching this filter
count - Get the count of objects matching this filter
first - Get the oldest record with current filters
last - Get the newest record with current filters
random - Get a random element with current filters
getPrimaryKeys - Gets primary keys associated with current filters
**Filter Functions**
These functions add filters to the current set. "filter" returns a copy, "filterInline" acts on that object.
filter - Add additional filters, returning a copy of the filter object (moreFiltered = filtered.filter(key2=val2))
filterInline - Add additional filters to current filter object.
**Global Fetch functions**
These functions are available on SomeModel.objects and don't use any filters (they get specific objects):
get - Get a single object by pk
getMultiple - Get multiple objects by a list of pks
**Model Functions**
Actual objects contain methods including:
save - Save this object (create if not exist, otherwise update)
delete - Delete this object
getUpdatedFields - See changes since last fetch
Encodings
---------
IndexedRedis will use by default your system default encoding (sys.getdefaultencoding), unless it is ascii (python2) in which case it will default to utf-8.
You may change this via IndexedRedis.setEncoding |
|
Methods defined here:
- __getstate__(self)
- pickle uses this
- __init__(self, *args, **kwargs)
- __init__ - Set the values on this object. MAKE SURE YOU CALL THE SUPER HERE, or else things will not work.
- __repr__(self)
- __repr__ - Returns a string of the constructor/params to recreate this object.
Example: objCopy = eval(repr(obj))
@return - String of python init call to recreate this object
- __setstate__(self, stateDict)
- pickle uses this
- __str__(self)
- __str__ - Returns a string representation of this object's state.
See implementation.
@return <str>-
Some samples:
(Pdb) str(z)
'<Song obj _id=24 at 0x7f3c6a3a4490>'
(Pdb) z.artist = 'New Artist'
(Pdb) str(z)
'<Song obj _id=24 (Unsaved Changes) at 0x7f3c6a3a4490>'
- asDict(self, includeMeta=False)
- toDict / asDict - Get a dictionary representation of this model.
@param includeMeta - Include metadata in return. For now, this is only pk stored as "_id"
@return - Dictionary reprensetation of this object and all fields
- copy(self, copyPrimaryKey=False)
- copy - Copies this object.
@param copyPrimaryKey <bool> default False - If True, any changes to the copy will save over-top the existing entry in Redis.
If False, only the data is copied, and nothing is saved.
- delete(self)
- delete - Delete this object
deleter = <IndexedRedis.IndexedRedisDelete object>
- getPk(self)
- getPk - Gets the internal primary key associated with this object
- getUpdatedFields(self)
- getUpdatedFields - See changed fields.
@return - a dictionary of fieldName : tuple(old, new)
- hasUnsavedChanges(self)
- hasUnsavedChanges - Check if any unsaved changes are present in this model, or if it has never been saved.
@return <bool> - True if any fields have changed since last fetch, or if never saved. Otherwise, False
objects = <IndexedRedis.IndexedRedisQuery object>
- reload(self)
- reload - Reload this object from the database.
@raises KeyError - if this object has not been saved (no primary key)
@return - True if any updates occured, False if data remained the same.
- save(self)
- save - Save this object
- saveToExternal(self, redisCon)
- saveToExternal - Saves this object to a different Redis than that specified by REDIS_CONNECTION_PARAMS on this model.
@param redisCon <dict/redis.Redis> - Either a dict of connection params, a la REDIS_CONNECTION_PARAMS, or an existing Redis connection.
If you are doing a lot of bulk copies, it is recommended that you create a Redis connection and pass it in rather than establish a new
connection with each call.
@note - You will generate a new primary key relative to the external Redis environment. If you need to reference a "shared" primary key, it is better
to use an indexed field than the internal pk.
saver = <IndexedRedis.IndexedRedisSave object>
- toDict = asDict(self, includeMeta=False)
Class methods defined here:
- connect(cls, redisConnectionParams) from __builtin__.type
- connect - Create a class of this model which will use an alternate connection than the one specified by REDIS_CONNECTION_PARAMS on this model.
@param redisConnectionParams <dict> - Dictionary of arguments to redis.Redis, same as REDIS_CONNECTION_PARAMS.
@return - A class that can be used in all the same ways as the existing IndexedRedisModel, but that connects to a different instance.
- reset(cls, newValues) from __builtin__.type
- validateModel(model) from __builtin__.type
- validateModel - Class method that validates a given model is implemented correctly. Will only be validated once, on first model instantiation.
@param model - Implicit of own class
@return - True
@raises - InvalidModelException if there is a problem with the model, and the message contains relevant information.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- BASE64_FIELDS = []
- BINARY_FIELDS = []
- FIELDS = []
- INDEXED_FIELDS = []
- KEY_NAME = None
- REDIS_CONNECTION_PARAMS = {}
|
class IndexedRedisQuery(IndexedRedisHelper) |
|
IndexedRedisQuery - The query object. This is the return of "Model.objects" and "Model.objects.filter" |
|
- Method resolution order:
- IndexedRedisQuery
- IndexedRedisHelper
- __builtin__.object
Methods defined here:
- __init__(self, *args, **kwargs)
- all(self)
- all - Get the underlying objects which match the filter criteria.
Example: objs = Model.objects.filter(field1='value', field2='value2').all()
@return - Objects of the Model instance associated with this query.
- allByAge(self)
- allByAge - Get the underlying objects which match the filter criteria, ordered oldest -> newest
If you are doing a queue or just need the head/tail, consider .first() and .last() instead.
@return - Objects of the Model instance associated with this query, sorted oldest->newest
- allOnlyFields(self, fields)
- allOnlyFields - Get the objects which match the filter criteria, only fetching given fields.
@param fields - List of fields to fetch
@return - Partial objects with only the given fields fetched
- allOnlyIndexedFields(self)
- allOnlyIndexedFields - Get the objects which match the filter criteria, only fetching indexed fields.
@return - Partial objects with only the indexed fields fetched
- count(self)
- count - gets the number of records matching the filter criteria
Example:
theCount = Model.objects.filter(field1='value').count()
- delete(self)
- delete - Deletes all entries matching the filter criteria
- exists(self, pk)
- exists - Tests whether a record holding the given primary key exists.
@param pk - Primary key (see getPk method)
Example usage: Waiting for an object to be deleted without fetching the object or running a filter.
This is a very cheap operation.
@return <bool> - True if object with given pk exists, otherwise False
- filter(self, **kwargs)
- filter - Add filters based on INDEXED_FIELDS having or not having a value.
Note, no objects are actually fetched until .all() is called
Use the field name [ model.objects.filter(some_field='value')] to filter on items containing that value.
Use the field name suffxed with '__ne' for a negation filter [ model.objects.filter(some_field__ne='value') ]
Example:
query = Model.objects.filter(field1='value', field2='othervalue')
objs1 = query.filter(something__ne='value').all()
objs2 = query.filter(something__ne=7).all()
@returns - A copy of this object, with the additional filters. If you want to work inline on this object instead, use the filterInline method.
- filterInline(self, **kwargs)
- filterInline - @see IndexedRedisQuery.filter. This is the same as filter, but works inline on this object instead of creating a copy.
Use this is you do not need to retain the previous filter object.
- first(self)
- First - Returns the oldest record (lowerst primary key) with current filters.
This makes an efficient queue, as it only fetches a single object.
@return - Instance of Model object, or None if no items match current filters
- get(self, pk)
- get - Get a single value with the internal primary key.
@param pk - internal primary key (can be found via .getPk() on an item)
- getMultiple(self, pks)
- getMultiple - Gets multiple objects with a single atomic operation
@param pks - list of internal keys
- getMultipleOnlyFields(self, pks, fields)
- getMultipleOnlyFields - Gets only certain fields from a list of primary keys. For working on entire filter set, see allOnlyFields
pks list<str> - Primary Keys
fields list<str> - List of fields
return - List of partial objects with only fields applied
- getMultipleOnlyIndexedFields(self, pks)
- getMultipleOnlyIndexedFields - Get only the indexed fields on an object. This is the minimum to delete.
@param pks - List of primary keys
@return - List of objects with only indexed fields fetched
- getOnlyFields(self, pk, fields)
- getOnlyFields - Gets only certain fields from a paticular primary key. For working on entire filter set, see allOnlyFields
pk - Primary Key
fields list<str> - List of fields
return - Partial objects with only fields applied
- getOnlyIndexedFields(self, pk)
- getOnlyIndexedFields - Get only the indexed fields on an object. This is the minimum to delete.
@param pk - Primary key
@return - Object with only indexed fields fetched.
- getPrimaryKeys(self, sortByAge=False)
- getPrimaryKeys - Returns all primary keys matching current filterset.
@param sortByAge <bool> - If False, return will be a set and may not be ordered.
If True, return will be a list and is guarenteed to represent objects oldest->newest
@return <set> - A set of all primary keys associated with current filters.
- last(self)
- Last - Returns the newest record (highest primary key) with current filters.
This makes an efficient queue, as it only fetches a single object.
@return - Instance of Model object, or None if no items match current filters
- random(self)
- Random - Returns a random record in current filterset.
@return - Instance of Model object, or None if no items math current filters
- reindex(self)
- reindex - Reindexes the objects matching current filterset. Use this if you add/remove a field to INDEXED_FIELDS
Data descriptors inherited from IndexedRedisHelper:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class IndexedRedisSave(IndexedRedisHelper) |
|
IndexedRedisClass - Class used to save objects. Used with Model.save is called.
Except for advanced usage, this is probably for internal only. |
|
- Method resolution order:
- IndexedRedisSave
- IndexedRedisHelper
- __builtin__.object
Methods defined here:
- reindex(self, objs, conn=None)
- reindex - Reindexes a given list of objects. Probably you want to do Model.objects.reindex() instead of this directly.
@param objs list<IndexedRedisModel> - List of objects to reindex
@param conn <redis.Redis or None> - Specific Redis connection or None to reuse
- save(self, obj, usePipeline=True, forceID=False, conn=None)
- save - Save an object associated with this model. **Interal Function!!** You probably want to just do object.save() instead of this.
@param obj - The object to save
@param usePipeline - Use a pipeline for saving. You should always want this, unless you are calling this function from within an existing pipeline.
@param forceID - if not False, force ID to this. If obj is list, this is also list. Forcing IDs also forces insert. Up to you to ensure ID will not clash.
@param conn - A connection or None
@note - if no ID is specified
@return - List of pks
Methods inherited from IndexedRedisHelper:
- __init__(self, mdl)
- Internal constructor
@param mdl - IndexedRedisModel implementer
Data descriptors inherited from IndexedRedisHelper:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
|