5.3 B-tree Modules

When programming with the ZODB, Python dictionaries aren't always what you need. The most important case is where you want to store a very large mapping. When a Python dictionary is accessed in a ZODB, the whole dictionary has to be unpickled and brought into memory. If you're storing something very large, such as a 100,000-entry user database, unpickling such a large object will be slow. B-trees are a balanced tree data structure that behave like a mapping but distribute keys throughout a number of tree nodes. Nodes are then only unpickled and brought into memory as they're accessed, so the entire tree doesn't have to occupy memory (unless you really are touching every single key).

There are four different BTree modules provided. One of them, the BTree module, provides the most general data type; the keys and values in the B-tree can be any Python object. Some specialized B-tree modules require that the keys, and perhaps even the values, to be of a certain type, and provide faster performance because of this limitation.

IOBTree
requires the keys to be integers. The module name reminds you of this; the IOBTree module maps Integers to Objects.

OIBTree
requires the values to be integers, mapping Objects to Integers.

IIBTree
is strictest, requiring that both keys and values must be integers.

To use a B-tree, simply import the desired module and call the constructor, always named BTree(), to get a B-tree instance, and then use it like any other mapping:

import IIBTree
iimap = IIBTree.BTree()
iimap[1972] = 27