Tokyo Cabinet backend

A storage/query backend for Tokyo Cabinet.

Allows direct access to the database and is thus extremely fast. However, it locks the database and is therefore not suitable for environments where concurrent access is required. Please use Tokyo Tyrant for such environments.

database:Tokyo Cabinet
status:experimental
dependencies:tc (rsms)

Warning

this module is not intended for production, it’s just a (working) example. Patches, improvements, rewrites are welcome.

Usage:

>>> import os
>>> import pymodels
>>> DB_SETTINGS = {
...     'backend': 'pymodels.backends.tokyo_cabinet',
...     'kind': 'TABLE',
...     'path': '_tc_test.tct',
... }
>>> assert not os.path.exists(DB_SETTINGS['path']), 'test database must not exist'
>>> db = pymodels.get_storage(DB_SETTINGS)
>>> class Person(pymodels.Model):
...     name = pymodels.Property()
...     __unicode__ = lambda self: self.name
>>> Person.objects(db)    # the database is expected to be empty
[]
>>> db.connection.put('john', {'name': 'John'})
>>> mary = Person(name='Mary')
>>> mary_pk = mary.save(db)
>>> q = Person.objects(db)
>>> q
[<Person John>, <Person Mary>]
>>> q.where(name__matches='^J')
[<Person John>]
>>> q    # the original query was not modified by the descendant
[<Person John>, <Person Mary>]
>>> os.unlink(DB_SETTINGS['path'])
class pymodels.backends.tokyo_cabinet.Storage(path, kind=None)
Parameters:
  • path – relative or absolute path to the database file (e.g. test.tct)
  • kind – storage flavour, one of: ‘BTREE’, ‘HASH’, ‘TABLE’ (default).
get(model, primary_key)
Returns model instance for given model and primary key.
save(model, data, primary_key=None)

Saves given model instance into the storage. Returns primary key.

Parameters:
  • model – model class
  • data – dict containing all properties to be saved
  • primary_key – the key for given object; if undefined, will be generated

Note that you must provide current primary key for a model instance which is already in the database in order to update it instead of copying it.

class pymodels.backends.tokyo_cabinet.Query(*args, **kw)

The Query class. Experimental.

count()
Same as __len__.
order_by(name, numeric=False)

Defines order in which results should be retrieved.

Parameters:
  • name – the column name. If prefixed with -, direction changes from ascending (default) to descending.

Examples:

q.order_by('name')     # ascending
q.order_by('-name')    # descending
where(**conditions)
Returns Query instance filtered by given conditions. The conditions are defined exactly as in Pyrant’s high-level query API. See pyrant.query.Query.filter documentation for details.
where_not(**conditions)
Returns Query instance. Inverted version of where().

Previous topic

Tokyo Tyrant backend

Next topic

MongoDB backend

This Page