Remote Table Database — RTDB

RTDB

class tokyo.tyrant.RTDB

Example:

# First you need to start a ttserver, example command (starting a
# server on default address localhost:1978): ttserver test.tct
# 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 *

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.

len(rtdb)
Return the number of records in the database rtdb.
rtdb[key]
Return the value of rtdb‘s record corresponding to key. Raises KeyError if key is not in the database.
rtdb[key] = value
Set rtdb[key] to value.
del rtdb[key]
Remove rtdb[key] from rtdb. Raises KeyError if key is not in the database.
key in rtdb
Return True if rtdb has a key key, else False.
key not in rtdb
Equivalent to not key in rtdb.
iter(rtdb)
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

RTDBs 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 rtdb[key].
remove(key)
Delete a record from the database. Equivalent to del rtdb[key].
put(key, value)
Store a record in the database. Equivalent to rtdb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (rtdb[key] = value or put()), this method raises KeyError if key is already in the database.
putcat(key, value)
Merge a value with an existing one, does not override existing items in current value. If there is no corresponding record, a new record is stored.
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.

itervalueskeys()

Return an iterator over the database’s values’ keys.

New in version 0.6.1.

itervaluesvals()

Return an iterator over the database’s values’ values.

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.
setindex(column, type)

Add an index to a column.

Parameters:
  • column (str (Python2)/bytes (Python3)) – name of the column. An empty string means key.
  • type – index type, see TDB.setindex() types.
uid()
Return a new unique id.
query()
Return a query object (RTDBQuery). See Querying a Remote Table Database — RTDBQuery.
static metasearch(queries, type)

Combine queries and return the result set as a tuple of keys.

Parameters:
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.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.

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

Querying a Remote Table Database — RTDBQuery

class tokyo.tyrant.RTDBQuery

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(column, condition, expr)

Filter the result set on the condition expressed by the parameters.

Parameters:
  • column (str (Python2)/bytes (Python3)) – name of the column. An empty string means key.
  • condition – see TDBQuery.filter() conditions.
  • expr (str (Python2)/bytes (Python3)) – expression.

Note

Calling filter() multiple times with different conditions is equivalent to applying a logical AND between the conditions.

Unfortunately, due to a Tokyo Tyrant limitation, neither column nor expr can contain null bytes.

sort(column, type)

Sort the result set.

Parameters:
  • column (str (Python2)/bytes (Python3)) – name of the column. An empty string means key.
  • type – sort type (and direction), see TDBQuery.sort() types.

Note

Unfortunately, due to a Tokyo Tyrant limitation, column cannot contain null bytes.

limit([max[, skip]])

Limit the number of keys in the result set.

Parameters:
  • max – the maximum number of keys to return. If specified as a negative value no limit is applied.
  • skip – the number of keys to skip (from the beginning). If specified as a 0 or as a negative value no skipping is done.
search()
Execute the query and return the result set as a tuple of keys.
remove()
Remove all records corresponding to the result set from the database.
count()
Return the length of the result set.
hint
TODO.

Table Of Contents

Previous topic

Remote Database — RDB

Next topic

tokyo.dystopia — Python Tokyo Dystopia interface.

This Page