There are 3 main interfaces supplied by the ZODB:
Storage, DB, and Connection classes. The
DB and Connection interfaces both have single
implementations, but there are several different classes that
implement the Storage interface.
- Storage classes are the lowest layer, and handle
storing and retrieving objects from some form of long-term storage.
A few different types of Storage have been written, such as
FileStorage, which uses regular disk files, and
BerkeleyStorage, which uses Sleepycat Software's BerkeleyDB
database. You could write a new Storage that stored objects in a
relational database or Metakit file, for example, if that would
better suit your application. Two example storages,
DemoStorage and MappingStorage, are available to use
as models if you want to write a new Storage.
- The DB class sits on top of a storage, and mediates the
interaction between several connections. One DB instance is
created per process.
- Finally, the Connection class caches objects, and moves
them into and out of object storage. A multi-threaded program can
open a separate Connection instance for each thread.
Different threads can then modify objects and commit their
modifications independently.
Preparing to use a ZODB requires 3 steps: you have to open the
Storage, then create a DB instance that uses the Storage, and then get
a Connection from the DB instance. All this is only a few lines of
code:
from ZODB import FileStorage, DB
storage = FileStorage.FileStorage('/tmp/test-filestorage.fs')
db = DB(storage)
conn = db.open()
Note that you can use a completely different data storage mechanism by
changing the first line that opens a Storage; the above example uses a
FileStorage. In section 3, ``How ZEO Works'',
you'll see how ZEO uses this flexibility to good effect.