A pure-Python implementation of Tokyo Tyrant protocol. Python 2.4+ is required.
Usage example (note the automatically managed support for table database):
>>> import pyrant
>>> TEST_HOST, TEST_PORT = '127.0.0.1', 1983
>>> t = pyrant.Tyrant(host=TEST_HOST, port=TEST_PORT) # default port is 1978
>>> if t.table_enabled:
... t['key'] = {'name': 'foo'}
... print t['key']['name']
... else:
... t['key'] = 'foo'
... print t['key']
foo
>>> del t['key']
>>> print t['key']
Traceback (most recent call last):
...
KeyError: 'key'
A Python dictionary API for Tokyo Tyrant.
Parameters: |
|
---|
Usage:
>>> import pyrant
>>> t = pyrant.Tyrant(host=TEST_HOST, port=TEST_PORT)
# remove anything that could be left from previous time
>>> t.clear()
# make sure there are zero records in the database
>>> len(t)
0
Tyrant provides pythonic syntax and data pythonification, while the the lower-lever TyrantProtocol closely follows the orginal Tokyo Tyrant API and only converts incoming data to strings (Unicode). You decide which to use. Generally you would want more pythonic API (Tyrant) for most cases and the lower-level interface to reduce overhead or to fix broken data which cannot be properly converted by means of the higher-level API.
It is also important that Tokyo Cabinet has a great query extension for table databases. This extension is supported by pyrant.query.Query which only requires TyrantProtocol to work and can be easily accessed via Tyrant.query:
>>> t.query
[]
Adds given values as new records and returns a list of automatically generated primary keys for these records. Wrapper for multi_set().
Parameters: |
|
---|
Returns records that match given keys. Missing keys are silently ignored, i.e. the number of results may be lower than the number of requested keys. The records are returned as key/value pairs. Usage:
>>> g = t.multi_get(['foo', 'bar', 'galakteko opasnoste'])
>>> g
[('foo', {'one': 'one'}), ('bar', {'two': 'two'})]
Parameters: |
|
---|
Stores the given records in the database. The records may be given as an iterable sequence. Usage:
>>> t.multi_set([('foo', {'one': 'one'}), ('bar', {'two': 'two'})])
which is equevalent with the call:
>>> t.multi_set({'foo': {'one': 'one'}, 'bar':{'two': 'two'}})
Parameters: |
|
---|
Returns a Query object for the database.
Note
Only available for Table Databases.
>>> t.setdefault('foo', {'one': 'one'})
{u'one': u'one'}
>>> t.setdefault('foo', {'two': 'two'})
{u'one': u'one'}
Synchronizes updated content with the database.
Tokyo Cabinet is not durable, as data committed to the database is not immediately flushed to disk. Unwritten data may lost in the event of e.g. a system crash.
Use sync to force a flush of unwritten data to disk, but be aware that this also locks the writer process and blocks queries.
The better approach is to use database replication (copy the data to another database instance) and backup often.
Updates given objects from a dict, list of key and value pairs or a list of named params. Usage:
mapping = (
('john', dict(name='John')),
)
t.update(mapping, mary={'name': 'Mary'})
See built-in dict.update method for more information.