New in version 0.4.0.
Example:
from tokyocabinet import *
tdb = TDB()
# if need be, you should call tune/setcache/setxmsiz/setdfunit before
# open, ex. with default values:
tdb.tune(0, -1, -1, 0)
tdb.setcache(0, 0, 0)
tdb.setxmsiz(0)
tdb.setdfunit(0)
# open the database
tdb.open("casket.tct", TDBOWRITER | TDBOCREAT)
# 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"})
]:
tdb[key] = value
# retrieve one record
print(tdb["foo"])
# traverse records
for key in tdb:
print(key, tdb[key])
# close the database
tdb.close()
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.
Set the cache size.
Parameters: |
|
---|
Note
Setting the cache size on an open database is an invalid operation.
Set the extra mapped memory size.
Parameter: | xmsiz – the amount of extra mapped memory (in what unit?). If specified as 0 or as a negative value, the extra mapped memory is disabled. Default is 67108864 (unit?). |
---|
Note
Setting the extra memory size on an open database is an invalid operation.
Set auto defragmentation’s unit step number.
Parameter: | dfunit – the unit step number(?). If specified as 0 or as a negative value, auto defragmentation is disabled (default). |
---|
Note
Setting this on an open database is an invalid operation.
Open a database.
Parameters: |
|
---|
Close the database.
Note
TDBs are closed when garbage-collected.
Copy the database file.
Parameter: | path – path to the destination file. |
---|
Add an index to a column.
Parameters: |
|
---|
Combine queries and return the result set as a tuple of keys.
Parameters: |
|
---|
Optimize a database.
Parameters: |
|
---|
Note
Optimizing a read only database, or during a transaction, 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 tokyocabinet import TDB as _TDB
class TDB(_TDB):
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)
The following constants can only be combined with TDBOWRITER :
Create a new database file if it does not exists.
Create a new database file even if one already exists (truncates existing file).
Sync the database file on every transaction.
The following constants can be combined with either TDBOREADER or TDBOWRITER :
When first returned by TDB.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: |
|
---|
Apply callback to each record in the result set.
Parameter: | callback – callback must be a callable that accept a pair key, value as its arguments. callback can return one, or a combination, of these constants to trigger post-processing or to stop iterating. |
---|
value refers to tdb[key][column] if column is not an empty string otherwise value is key (that will need a better description).
String conditions :
value == expr
expr in value #substring test
value.startswith(expr)
value.endswith(expr)
if expr is expressed as "expr1,expr2":
expr1 in value and expr2 in value
if expr is expressed as "expr1,expr2":
expr1 in value or expr2 in value
if expr is expressed as "expr1,expr2":
value == expr1 or value == expr2
Numeric conditions :
value == expr
value > expr
value >= expr
value < expr
value <= expr
if expr is expressed as "expr1,expr2":
value >= min(expr1, expr2) and value <= max(expr1, expr2)
if expr is expressed as "expr1,expr2":
value == expr1 or value == expr2
Full-text search :
All conditions above can be combined with the following :