On-memory Hash Database — MDB

New in version 0.1.2.


class tokyocabinet.MDB([bnum])
Parameter:bnum – the number of elements in a bucket array. If omitted or specified as 0, the default value (65536) is used.


from tokyocabinet import *

mdb = MDB()

# store records
for key, value in [("foo", "hop"), ("bar", "step"), ("baz", "jump")]:
    mdb[key] = value

# retrieve one record

# traverse records
for key in mdb:
    print(key, mdb[key])


For all methods taking either a key argument or a pair (key, value), key and value must be either str (Python2) or bytes (Python3).

Return the number of records in the database mdb.
Return the value of mdb‘s record corresponding to key. Raises KeyError if key is not in the database.
mdb[key] = value
Set mdb[key] to value.
del mdb[key]
Remove mdb[key] from mdb. Raises KeyError if key is not in the database.
key in mdb
Return True if mdb has a key key, else False.
key not in mdb
Equivalent to not key in mdb.
Return an iterator over the keys of the database.
Remove all records from the database.

Return the value corresponding to key. Equivalent to mdb[key].

New in version 0.2.0.


Delete a record from the database. Equivalent to del mdb[key].

New in version 0.2.0.

put(key, value)
Store a record in the database. Equivalent to mdb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (mdb[key] = value or put()), this method raises KeyError if key is already in the database.
putcat(key, value)
Concatenate a value at the end of an existing one. If there is no corresponding record, a new record is stored.
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.

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 MDB as _MDB

class MDB(_MDB):

    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 size in bytes of the database.

Table Of Contents

Previous topic

Hash Database — HDB

Next topic

B+ Tree Database — BDB

This Page