On-memory Tree Database — NDB

New in version 0.2.1.

NDB

class tokyocabinet.NDB

Example:

from tokyocabinet import *

ndb = NDB()

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

# retrieve one record
print(ndb["foo"])

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

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).

len(ndb)
Return the number of records in the database ndb.
ndb[key]
Return the value of ndb‘s record corresponding to key. Raises KeyError if key is not in the database.
ndb[key] = value
Set ndb[key] to value.
del ndb[key]
Remove ndb[key] from ndb. Raises KeyError if key is not in the database.
key in ndb
Return True if ndb has a key key, else False.
key not in ndb
Equivalent to not key in ndb.
iter(ndb)
Return an iterator over the keys of the database.
clear()
Remove all records from the database.
get(key)
Return the value corresponding to key. Equivalent to ndb[key].
remove(key)
Delete a record from the database. Equivalent to del ndb[key].
put(key, value)
Store a record in the database. Equivalent to ndb[key] = value.
putkeep(key, value)
Store a record in the database, unlike the standard forms (ndb[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 NDB as _NDB

class NDB(_NDB):

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

Table Of Contents

Previous topic

B+ Tree Database — BDB

Next topic

Fixed-length Database — FDB

This Page