Example:
from tokyocabinet import *
hdb = HDB()
# if need be, you should call tune/setcache/setxmsiz/setdfunit before
# open, ex. with default values:
hdb.tune(0, -1, -1, 0)
hdb.setcache(0)
hdb.setxmsiz(0)
hdb.setdfunit(0)
# open the database
hdb.open("casket.tch", HDBOWRITER | HDBOCREAT)
# store records
for key, value in [("foo", "hop"), ("bar", "step"), ("baz", "jump")]:
hdb[key] = value
# retrieve one record
print(hdb["foo"])
# traverse records
for key in hdb:
print(key, hdb[key])
# close the database
hdb.close()
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.
Set the cache size.
Parameter: | rcnum – the maximum number of records to be cached. If specified as 0 or as a negative value, caching is disabled (default). |
---|
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
HDBs are closed when garbage-collected.
Copy the database file.
Parameter: | path – path to the destination file. |
---|
Return the value corresponding to key. Equivalent to hdb[key].
New in version 0.2.0.
Delete a record from the database. Equivalent to del hdb[key].
New in version 0.2.0.
Optimize a database.
Parameters: |
|
---|
Note
Optimizing a read only database, or during a transaction, is an invalid operation.
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 INT_MAX and INT_MIN. Example:
>>> hdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> hdb.addint('id', 1) # adding 1 to 'id' returns INT_MIN
-2147483648
>>>
Trying to access a value set with addint() using get() or hdb[key] will not return an int. It will instead return the internal binary representation of the value. Example:
>>> hdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> hdb['id']
'\xff\xff\xff\x7f'
>>>
New in version 0.5.0.
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 hdb[key] will not return a float.
New in version 0.5.0.
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 HDB as _HDB
class HDB(_HDB):
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 HDBOWRITER :
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 HDBOREADER or HDBOWRITER :