When should I use a PersistentDict, and when should I use a Folder? What is the difference between them in terms of updates, internal structure, performance, etc?
A PersistentMapping
is simply a implementation of the python dict
type (via the standard library UserDict
base class) adjusted for the Persistence semantics of the ZODB; it saves from having to set the _p_changed
flag on the nearest class that inherits from Persistent
every time you alter the mapping
.
A Folder
is a much richer type, implementing events, integration with the Zope web interface (ZMI), through-the-web arbitrary properties (attributes with type validation), management of Zope permissions, validation of sub-item ids, import/export, etc. Sub-items folders are stored as attributes on the object itself, with some metadata stored in a private dict on the instance.
Use a Folder
when you need any of those extra services (delegation of permissions, id validation, etc), use a PersistentMapping
otherwise. Performance wise looking up or storing items is not going to differ much; one is a straight python dict
underneath, the other is the instance __dict__
storing the items.
If you are looking for conflict avoidance, you should look into BTrees, the OOBTree
class is basically a persistent mapping where values are stored in persistent buckets, avoiding collisions in most cases, and providing conflict resolution for the rest.
If you want Folder
semantics with BTree
storage semantics, look at Products.BTreeFolder2, and add-on that implements the Folder
interfaces but stores sub-objects in a OOBTree
instead of as attributes directly on the instances.