Remote Database — RDB

RDB

class tokyo.tyrant.RDB

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:

  • hash databases.
  • on-memory hash databases.
  • B+ tree databases.
  • on-memory tree databases.

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).

len(rdb)
Return the number of records in the database rdb.
rdb[key]
Return the value of rdb‘s record corresponding to key. Raises KeyError if key is not in the database.
rdb[key] = value
Set rdb[key] to value.
del rdb[key]
Remove rdb[key] from rdb. Raises KeyError if key is not in the database.
key in rdb
Return True if rdb has a key key, else False.
key not in rdb
Equivalent to not key in rdb.
iter(rdb)
Return an iterator over the keys of the database.
tune(timeout, opts)

Tune a database.

Parameters:
  • timeout – timeout in seconds. If specified as 0 or as a negative value, no timeout is applied.
  • opts – options, see RDB.tune() options.

Note

Tuning an open database is an invalid operation.

open([host='localhost'[, port=1978]])

Open a database.

Parameters:
  • host – name/address of the server (defaults to 'localhost').
  • port – port number (defaults to 1978).
close()

Close the database.

Note

RDBs are closed when garbage-collected.

clear()
Remove all records from the database.
copy(path)

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.
get(key)
Return the value corresponding to key. Equivalent to rdb[key].
remove(key)
Delete a record from the database. Equivalent to del rdb[key].
put(key, value)
Store a record in the database. Equivalent to rdb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (rdb[key] = value or put()), this method raises KeyError if key is already in the database.
putcat(key, value)
Concatenate a value at the end of an existing one. If there is no corresponding record, a new record is stored.
putnb(key, value)
Non-blocking put().
addint(key, num)

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'
    >>>
    
adddouble(key, num)

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.

sync()
Flush modifications to the database file.
iterkeys()

Return an iterator over the database’s keys.

New in version 0.6.1.

itervalues()

Return an iterator over the database’s values.

New in version 0.6.1.

iteritems()

Return an iterator over the database’s items ((key, value) pairs).

New in version 0.6.1.

searchkeys(prefix[, max])
Return a frozenset of keys starting with prefix. If given, max is the maximum number of keys to fetch, if omitted or specified as a negative value no limit is applied.
restore(path, timestamp, opts)

Restore a database from an update log.

Parameters:
setmaster(host, port, timestamp, opts)

Set the replication master of a database.

Parameters:
  • host – name/address of the master server.
  • port – port number of the master server.
  • timestamp – start replication from timestamp (in microseconds).
  • opts – options, see RDB.restore()/RDB.setmaster() options.
optimize(**kwargs)

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.

size
The size in bytes of the database file.
status
A dict of status informations about the database.

RDB.tune() options

tokyo.tyrant.RDBTRECON
Try to reconnect automatically if the connection is lost.

RDB.restore()/RDB.setmaster() options

tokyo.tyrant.RDBROCHKCON
Consistency checking.

Table Of Contents

Previous topic

tokyo.tyrant — Python Tokyo Tyrant interface.

Next topic

Remote Table Database — RTDB

This Page