New in version 0.3.0.
Example:
from tokyocabinet import *
fdb = FDB()
# if need be, you should call tune before open, ex. with default values:
fdb.tune(0, 0)
# open the database
fdb.open("casket.tcf", FDBOWRITER | FDBOCREAT)
# store records
for key, value in [(1, "hop"), (2, "step"), (3, "jump")]:
fdb[key] = value
# retrieve one record
print(fdb[1])
# traverse records
for key in fdb:
print(key, fdb[key])
# close the database
fdb.close()
Note
For all methods taking either a key argument or a pair (key, value):
On top of that key must always be > 0. See Special keys for exceptions to this rule.
See also
Tune a database.
Parameters: |
|
---|
See Note on tuning/optimizing a fixed-length database.
Note
Tuning an open database is an invalid operation.
Open a database.
Parameters: |
|
---|
Close the database.
Note
FDBs are closed when garbage-collected.
Copy the database file.
Parameter: | path – path to the destination file. |
---|
Optimize a database.
Parameters: |
|
---|
See Note on tuning/optimizing a fixed-length database.
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:
>>> fdb.addint(100, INT_MAX) # setting key 100 to INT_MAX
2147483647
>>> fdb.addint(100, 1) # adding 1 to key 100 returns INT_MIN
-2147483648
>>>
Trying to access a value set with addint() using get() or fdb[key] will not return an int. It will instead return the internal binary representation of the value. Example:
>>> fdb.addint(100, INT_MAX) # setting key 100 to INT_MAX
2147483647
>>> fdb[100]
'\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 fdb[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 FDB as _FDB
class FDB(_FDB):
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 maximum length (and highest possible key) of a database is in direct relation to the width and size parameters and can be determined using the following function:
import os
PAGESIZE = os.sysconf("SC_PAGESIZE")
def fdb_max_len(width, size):
diff = size & (PAGESIZE - 1)
if diff > 0: # align to page size
size = size + PAGESIZE - diff
# 256 is the database header size
return int((size - 256) / (width + 1))
The following constants can only be combined with FDBOWRITER :
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 FDBOREADER or FDBOWRITER :