Example:
# First you need to start a ttserver, example command (starting a
# server on default address localhost:1978): ttserver test.tct
# see http://1978th.net/tokyotyrant/spex.html#serverprog and
# http://1978th.net/tokyotyrant/spex.html#tutorial for more info
# about ttserver
from tokyo.tyrant import *
rtdb = RTDB()
# if need be, you should call tune before open, ex. with default values:
rtdb.tune(0, 0)
# open the database
rtdb.open() # this will open the server at localhost:1978
# store records
for key, value in [
("foo", {"age": "30", "name": "John", "sex": "m"}),
("bar", {"age": "56", "name": "Paul", "sex": "m"}),
("baz", {"age": "22", "name": "Ella", "sex": "f"})
]:
rtdb[key] = value
# retrieve one record
print(rtdb["foo"])
# traverse records
for key in rtdb:
print(key, rtdb[key])
# close the database
rtdb.close()
Warning
This client works only with Tokyo Tyrant daemon serving table databases.
Note
For all methods taking either a key argument or a pair (key, value), key must be either str (Python2) or bytes (Python3) and value must be a dict.
All items in value must be pairs of str (Python2) or bytes (Python3). Empty keys in value are not allowed.
See also
Tune a database.
Parameters: |
|
---|
Note
Tuning an open database is an invalid operation.
Open a database.
Parameters: |
|
---|
Close the database.
Note
RTDBs are closed when garbage-collected.
Copy the database file.
Parameter: | path – path to the destination file. This path refers to the server, not the client, the database is not copied locally. |
---|
Restore a database from an update log.
Parameters: |
|
---|
Set the replication master of a database.
Parameters: |
|
---|
Add an index to a column.
Parameters: |
|
---|
Combine queries and return the result set as a tuple of keys.
Parameters: |
|
---|
Optimize a database. This method only accepts keyword arguments. Each argument must be a str (Python2) or bytes (Python3) representation of its real value. See tokyo.cabinet.TDB.optimize() for valid arguments and values. Example:
rtdb.optimize(bnum='0', apow='-1', fpow='-1', opts='255')
Note
Optimizing a read only database is an invalid operation.
Methods keys(), values() and items() are not yet implemented (mainly because I didn’t settle on how to do it: should they return Iterable, Iterator, MappingView, etc.?). Any help would be greatly appreciated in this matter.
For the time being, for those of you who really need these methods, it’s trivial to implement them in python. Here is an example using generators:
from tokyo.tyrant import RTDB as _RTDB
class RTDB(_RTDB):
def keys(self):
return (key for key in self)
def values(self):
return (self[key] for key in self)
def items(self):
return ((key, self[key]) for key in self)
When first returned by RTDB.query() a query result set potentially includes all keys contained in the database. You can narrow down a search by calling filter() and limit the number of results with limit().
Filter the result set on the condition expressed by the parameters.
Parameters: |
|
---|
Note
Calling filter() multiple times with different conditions is equivalent to applying a logical AND between the conditions.
Sort the result set.
Parameters: |
|
---|
Limit the number of keys in the result set.
Parameters: |
|
---|