New in version 0.2.0.
Example:
from tokyo.cabinet import *
bdb = BDB()
# if need be, you should call tune/setcache/setxmsiz/setdfunit before
# open, ex. with default values:
bdb.tune(0, 0, 0, -1, -1, 0)
bdb.setcache(0, 0)
bdb.setxmsiz(0)
bdb.setdfunit(0)
# open the database
bdb.open("casket.tcb", BDBOWRITER | BDBOCREAT)
# store records
for key, value in [("foo", "hop"), ("bar", "step"), ("baz", "jump")]:
bdb[key] = value
# retrieve one record
print(bdb["foo"])
# traverse records
for key in bdb:
print(key, bdb[key])
# close the database
bdb.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 parameters.
Parameters: |
|
---|
Note
Setting the cache parameters 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). |
---|
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.
Set the compare callback function.
Parameter: | callback – if it is an int, it must be one of BDB.setcmpfunc() compare callback constants. Otherwise, it must be a callable taking two arguments, a and b, and returning 1 if a is greater than b, 0 if a is equal to b, and -1 if a is less than b. |
---|
Warning
Note
New in version 0.6.0.
Open a database.
Parameters: |
|
---|
Close the database.
Note
BDBs are closed when garbage-collected.
Copy the database file.
Parameter: | path – path to the destination file. |
---|
Store duplicate records in the database, this is equivalent to:
def putdup(self, key, values):
for value in values:
self.put(key, value, duplicate=True)
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:
>>> bdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> bdb.addint('id', 1) # adding 1 to 'id' returns INT_MIN
-2147483648
>>>
Trying to access a value set with addint() using get() or bdb[key] will not return an int. It will instead return the internal binary representation of the value. Example:
>>> bdb.addint('id', INT_MAX) # setting 'id' to INT_MAX
2147483647
>>> bdb['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 bdb[key] will not return a float.
New in version 0.5.0.
Return an iterator over the database’s keys.
New in version 0.6.1.
Return an iterator over the database’s values.
New in version 0.6.1.
Return an iterator over the database’s items ((key, value) pairs).
New in version 0.6.1.
Return a frozenset of all the keys between begin and end (inclusive). If given, max is the maximum number of keys to fetch, if omitted or specified as a negative value no limit is applied. Example:
>>> from tokyo.cabinet import *
>>> bdb = BDB()
>>> bdb.open("range.tcb", BDBOWRITER | BDBOCREAT)
>>> for key, value in [("a", "a"), ("b", "b"), ("c", "c"),
... ("d", "d"), ("dodo", "dodo")]:
... bdb[key] = value
...
>>> bdb.range()
frozenset(['a', 'c', 'b', 'dodo', 'd'])
>>> bdb.range(end="c")
frozenset(['a', 'c', 'b'])
>>>
>>> bdb.range("b", "d") # begin and end are inclusive
frozenset(['c', 'b', 'd'])
>>> bdb.range("b", "z")
frozenset(['c', 'b', 'dodo', 'd'])
>>> bdb.range("z", "b") # end < begin doesn't work
frozenset([])
>>> bdb.range("a", "a") # end == begin works
frozenset(['a'])
>>> bdb.range("A", "Z") # it is case sensitive
frozenset([])
>>> bdb.range("A", "z") # uppercase < lowercase
frozenset(['a', 'c', 'b', 'dodo', 'd'])
>>> bdb.range("z", "A")
frozenset([])
>>> bdb.close()
>>>
Optimize a database.
Parameters: |
|
---|
Note
Optimizing a read only database, or during a transaction, is an invalid operation.
The following constants can only be combined with BDBOWRITER :
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 BDBOREADER or BDBOWRITER :
Warning
When first returned by BDB.cursor() a cursor is not fully initialized. You need to call one of the positioning method (first(), last() or jump()) before you can do anything useful with it.
Store a value at/around the cursor’s current position.
Parameter: | mode – see BDBCursor.put() modes. |
---|