Example:
# First you need to start a ttserver, example commands (starting a
# server on default address localhost:1978):
# - on-memory hash database: ttserver
# - on-memory tree database: ttserver +
# - hash database: ttserver test.tch
# - B+ tree database: ttserver test.tcb
# see http://fallabs.com/tokyotyrant/spex.html#serverprog and
# http://fallabs.com/tokyotyrant/spex.html#tutorial for more info
# about ttserver
from tokyo.tyrant import *
rdb = RDB()
# if need be, you should call tune before open, ex. with default values:
rdb.tune(0, 0)
# open the database
rdb.open() # this will open the server at localhost:1978
# store records
for key, value in [("foo", "hop"), ("bar", "step"), ("baz", "jump")]:
rdb[key] = value
# retrieve one record
print(rdb["foo"])
# traverse records
for key in rdb:
print(key, rdb[key])
# close the database
rdb.close()
Warning
This client works only with Tokyo Tyrant daemon serving one of the following:
Note
For all methods taking either a key argument or a pair (key, value), key and value must be either str (Python2) or bytes (Python3).
See also
Tune a database.
Parameters: |
|
---|
Note
Tuning an open database is an invalid operation.
Open a database.
Parameters: |
|
---|
Close the database.
Note
RDBs 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. |
---|
Store an int in the database. If key is not in the database, this method stores num in the database and returns it. If key is already in the database, then it will add num to its current value and return the result. If key exists but its value cannot be treated as an int this method raises KeyError.
Note
The returned value will wrap around tokyo.cabinet.INT_MAX and tokyo.cabinet.INT_MIN. Example:
>>> rdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> rdb.addint('id', 1) # adding 1 to 'id' returns INT_MIN
-2147483648
>>>
Trying to access a value set with addint() using get() or rdb[key] will not return an int. It will instead return the internal binary representation of the value. Example:
>>> rdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> rdb['id']
'\xff\xff\xff\x7f'
>>>
Store a float in the database. If key is not in the database, this method stores num in the database and returns it. If key is already in the database, then it will add num to its current value and return the result. If key exists but its value cannot be treated as a float this method raises KeyError.
Note
Trying to access a value set with adddouble() using get() or rdb[key] will not return a float.
Return an iterator over the database’s keys.
New in version 0.6.1.
Return an iterator over the database’s values.
New in version 0.6.1.
Return an iterator over the database’s items ((key, value) pairs).
New in version 0.6.1.
Restore a database from an update log.
Parameters: |
|
---|
Set the replication master of a database.
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.HDB.optimize() and tokyo.cabinet.BDB.optimize(), respectively, for valid arguments and values. Examples:
# optimizing a hash database:
rdb.optimize(bnum='0', apow='-1', fpow='-1', opts='255')
# optimizing a B+ tree database:
rdb.optimize(lmemb='0', nmemb='0', bnum='0', apow='-1', fpow='-1', opts='255')
Note
Optimizing a read only database is an invalid operation.