Re: [ZODB-Dev] Object uniqueness

2008-10-03 Thread Tres Seaver
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Markus Läll wrote:
> I think I know now -- objects compare identical and equal only if they are
> in the same container object. (Check the code below)
> 
> I don't mean to be negative, but why can't ZODB be like a big RAM? Like
> when i store an object to multiple places on the same DB, then it would
> actually be the same in each place. (Like if you comment out the "Commit,
> close and reload" part of the below code, all the comparisons evaluate to
> True)

If your classes derive from persistence.Persistent, then their
instances' identities will be preserved across serializations.


Tres.
- --
===
Tres Seaver  +1 540-429-0999  [EMAIL PROTECTED]
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

iD8DBQFI5kb7+gerLs4ltQ4RAq3yAJ9USY6EX+cssrxT0ZOOlS6EG+OREwCfYn5g
MqQBgyaT2JnWYe18jJSI/pE=
=Z0o3
-END PGP SIGNATURE-

___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Object uniqueness

2008-10-03 Thread Christian Theune
Hi,

On Fri, 2008-10-03 at 15:57 +0300, Markus Läll wrote:
> I think I know now -- objects compare identical and equal only if they are
> in the same container object. (Check the code below)
> 
> I don't mean to be negative, but why can't ZODB be like a big RAM? Like
> when i store an object to multiple places on the same DB, then it would
> actually be the same in each place. (Like if you comment out the "Commit,
> close and reload" part of the below code, all the comparisons evaluate to
> True)

It does, you just need to do a little more work. See below.

> # -*- coding: cp1252 -*-
> # ZODB
> from ZODB import FileStorage, DB
> import transaction
> 
> # Trees
> from BTrees.IOBTree import IOBTree as IO
> from BTrees.OOBTree import OOBTree as OO, OOSet as OSet
> 
> # Open db
> storage = FileStorage.FileStorage('test.fs')
> db = DB(storage)
> conn = db.open()
> root = conn.root()
> 
> # The  one and only class
> class X(object):
--^
Here's the Deal: ZODB is pretty much transparent. However, to work its
full magic, your objects have to subclass `persistent.Persistent`.
Otherwise ZODB will only pickle them in one large database record and
thus not know about their identity anymore. Also, object changes won't
trigger persistency updates automatically.

Your example will work as soon as you change this to:

class X(persistent.Persistent):

Cheers,
Christian

-- 
Christian Theune · [EMAIL PROTECTED]
gocept gmbh & co. kg · forsterstraße 29 · 06112 halle (saale) · germany
http://gocept.com · tel +49 345 1229889 7 · fax +49 345 1229889 1
Zope and Plone consulting and development


signature.asc
Description: This is a digitally signed message part
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] Object uniqueness

2008-10-03 Thread Markus Läll
I think I know now -- objects compare identical and equal only if they are
in the same container object. (Check the code below)

I don't mean to be negative, but why can't ZODB be like a big RAM? Like
when i store an object to multiple places on the same DB, then it would
actually be the same in each place. (Like if you comment out the "Commit,
close and reload" part of the below code, all the comparisons evaluate to
True)

Markus

# -*- coding: cp1252 -*-
# ZODB
from ZODB import FileStorage, DB
import transaction

# Trees
from BTrees.IOBTree import IOBTree as IO
from BTrees.OOBTree import OOBTree as OO, OOSet as OSet

# Open db
storage = FileStorage.FileStorage('test.fs')
db = DB(storage)
conn = db.open()
root = conn.root()

# The  one and only class
class X(object):
attr1 = 1
def __init__(self, attr2):
self.attr2 = attr2

# Initiate three containers, OOBTree and OOSet
root[0] = OO()
root[1] = OSet()
root[0]['subBTree'] = OO()

# The only object to be stored...
x = X('abc')

# ...and storing it
root[0]['a'] = x
root[0]['b'] = x
root[0]['subBTree']['a'] = x
root[1].update([ x ])

# Commit, close and reload
transaction.commit()
db.close()
storage = FileStorage.FileStorage('test.fs')
db = DB(storage)
conn = db.open()
root = conn.root()

# Shorthands...
a = root[0]['a']
asub = root[0]['subBTree']['a']
b = root[0]['b']
c = iter(root[1]).next()

# ...and the comparison
print 'a is asub:', a is asub
print 'a == asub:', a == asub

print 'a is b:', a is b
print 'a == b:', a == b

print 'a is c:', a is c
print 'a == c:', a == c


___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev