When is PersistentDict, when is the folder?

When should I use PersistentDict, and when should I use a folder? What is the difference between the two in terms of updates, internal structure, performance, etc.?

+4
source share
2 answers

A PersistentMapping is simply an implementation of the python dict type (through the base class of the UserDict standard library), adjusted for Persistence semantics for ZODB; this eliminates the need to set the _p_changed flag in the nearest class, which inherits from Persistent every time you change the mapping .

A Folder - a much richer type, implementation of events, integration with the Zope web interface (ZMI), arbitrary properties through attributes (attributes with type checking), managing Zope permissions, checking sub-items identifiers, import / export, etc. Subheading folders are stored as attributes of the object itself, and some metadata is stored in a private dict in the instance.

Use Folder if you need any of these additional services (delegation of permissions, verification of identifier, etc.), use PersistentMapping otherwise. The effectiveness of searching or storing items will not be much different; one of them is a direct python dict , the other is an __dict__ instance storing the elements.

If you are looking for conflict avoidance, you should study BTrees , the OOBTree class is basically a constant mapping where values ​​are stored in constant buckets, avoiding collisions in most cases and providing conflict resolution for the rest.

If you want the BTree semantics Folder semantics, see Products.BTreeFolder2 and an add-in that implements Folder but stores sub-objects in OOBTree instead of attributes directly in instances.

+3
source

A PersistentDict (now called PersistentMapping ) is a class that inherits from UserDict.IterableUserDict and persistent.Persistent.

UserDict.IterableUserDict is a built-in python class that mimics an iterable dictionary and persistent.Persistent is a Zope class that allows an instance of itself to be stored in ZODB.

Thus, PersistentDict (or PersistentMapping) is basically a dictionary that can be saved as an object in ZODB.

Normal dictionaries cannot be stored as separate objects in ZODB. They must be attributes of some class that inherits from persistent.Persistent.

PersistentDict stores its keys and values ​​inside the actual dictionary (data attribute).

It is not possible to add PersistentDict through ZMI, and I think it was intended mainly for a special case when you want to store the dictionary directly in zodb.

With Folder , I think you mean the folder in zope.container.folder. A folder stores its children inside an OOBTree object, which is a container that can hold a large number of objects.

If you need a container containing instances of other types of content, then you better go with a folder.

The folder has interfaces that are not supported by PersistentDict, and these interfaces may be required for certain adapters or other components to work. For example, the ContainerModified event will fire only when the folder changes, and not in PersistentDict. There can be all kinds of gotchas like these if you use PersistentDict as a general folder.

When it comes to performance, a dictionary will usually be faster until the key space becomes very large. Then the balance is tilted to OOBTree.

+2
source

Source: https://habr.com/ru/post/1399038/


All Articles