[ZODB-Dev] Re: Navigating inherently hierarchical data
Tommy Li
tommycli at ucla.edu
Sun Aug 5 17:54:32 EDT 2007
Tres Seaver wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Tommy Li wrote:
>> Hi,
>>
>> I'm just learning about Object databases, and I stumbled across Zope.
>>
>> I'm trying to store data that inherently has a hierarchical (tree)
>> structure. As I have it currently implemented, each object points to
>> another object of the same type by storing the ID of its parent.
>>
>> So currently in SQL, to find all the children of object id=4, I'd query:
>>
>> SELECT * from TreeNodes WHERE parent=4;
>>
>> To find the parent of a given object, obviously, I'd just query (say 3
>> is parent of 4):
>>
>> SELECT * from TreeNodes WHERE id=3;
>>
>> The tree structure is a very important part of the data and cannot be
>> compromised.
>>
>> In the Wikipedia article describing object databases, it mentioned that
>> data access can be faster because related data could be found by
>> following pointers. This makes sense. In the object domain, if I
>> maintain a set of references for the parent and child in each TreeNode,
>> traversal around the tree would be very quick.
>>
>> I don't understand how I can use ZODB to do this, however. From what I
>> can gather from reading the manual, if I simply stored variables
>> referring to the parent and children in every TreeNode, I'd end up
>> storing the whole tree of TreeNodes's every time I wanted to store that
>> one TreeNode. (Presumably, because ZODB would follow all the references
>> recursively.)
>
> ZODB breaks reference cycles like that for persistent objects. You can
> store child nodes as attributes of the parents, using Python's dot operator:
>
> parent = Node('parent')
> parent.one = Node('one')
> parent.two = Node('two')
>
> If the children need backpointers to the parent, you could pass the
> parent to their constructor, or set a '__parent__' attribute on them:
>
> parent.one.__parent__ = parent
> parent.three = ChildNode('three', parent)
>
> In Zope2, we have traditionally *not* set a backpointer, but relied on
> the fact that getting the attribute from parent gives us back an
> acquisition wrapper, which can then be used to find the parent (via
> 'aq_parent'). Zope3 has chosen a more explicit model for most objects,
> with a '__parent__' reference.
>
>> I suppose I could use the old method of storing only the identifiers of
>> the parents/children. Is there a way ZODB can help me with efficient
>> traversal of persistent objects in a tree structure?
>
> Under the covers, ZODB *does* mutate the objects to store "stubs" for
> attributes whire are persistent: the stub consists of a reference to
> the object's class, plus its OID. When you try to *use* the stub, ZODB
> transparently replaces it with an object constructed from the state
> stored in the ZODB for that OID.
Great. So when I store the parent node of a tree structure into zodb,
all the children get recursively stored?
Or do I need to manually store each of these Persistent subobjects? Is
there a specific order I need to store the parent and child in?
>
>
> Tres.
> - --
> ===================================================================
> Tres Seaver +1 540-429-0999 tseaver at palladion.com
> Palladion Software "Excellence by Design" http://palladion.com
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGszpa+gerLs4ltQ4RAmisAJ0TE1ULOX2/CyKKwAFXdLy1wjtyfgCgtq/+
> HAIcchmMLGTaAX7jckWBDXs=
> =kXwn
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> For more information about ZODB, see the ZODB Wiki:
> http://www.zope.org/Wikis/ZODB/
>
> ZODB-Dev mailing list - ZODB-Dev at zope.org
> http://mail.zope.org/mailman/listinfo/zodb-dev
>
More information about the ZODB-Dev
mailing list