/usr/share/pyshared/zope/keyreference/persistent.txt is in python-zope.keyreference 3.6.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | =====================================
Key References for Persistent Objects
=====================================
`zope.keyreference.persistent.KeyReferenceToPersistent` provides an
`zope.keyreference.interfaces.IKeyReference` reference for persistent
objects.
Let's look at an example. First, we'll create some persistent objects
in a database:
>>> from ZODB.MappingStorage import DB
>>> import transaction
>>> from persistent.mapping import PersistentMapping
>>> db = DB()
>>> conn = db.open()
>>> root = conn.root()
>>> root['ob1'] = PersistentMapping()
>>> root['ob2'] = PersistentMapping()
>>> transaction.commit()
Then we'll create some key references:
>>> from zope.keyreference.persistent import KeyReferenceToPersistent
>>> key1 = KeyReferenceToPersistent(root['ob1'])
>>> key2 = KeyReferenceToPersistent(root['ob2'])
We can call the keys to get the objects:
>>> key1() is root['ob1'], key2() is root['ob2']
(True, True)
New keys to the same objects are equal to the old:
>>> KeyReferenceToPersistent(root['ob1']) == key1
True
and have the same hashes:
>>> hash(KeyReferenceToPersistent(root['ob1'])) == hash(key1)
True
Other key reference implementations are differed by their key type id.
Key references should sort first on their key type and second on any
type-specific information:
>>> from zope.interface import implements
>>> from zope.keyreference.interfaces import IKeyReference
>>> class DummyKeyReference(object):
... implements(IKeyReference)
... key_type_id = 'zope.app.keyreference.object'
... def __init__(self, obj):
... self.object = obj
... def __cmp__(self, other):
... if self.key_type_id == other.key_type_id:
... return cmp(self.object, other.object)
... return cmp(self.key_type_id, other.key_type_id)
>>> dummy_key1 = DummyKeyReference(object())
>>> dummy_key2 = DummyKeyReference(object())
>>> dummy_key3 = DummyKeyReference(object())
>>> keys = [key1, dummy_key1, dummy_key2, key2, dummy_key3]
>>> keys.sort()
>>> key_type_ids = [key.key_type_id for key in keys]
>>> key_type_ids[0:3].count('zope.app.keyreference.object')
3
>>> key_type_ids[3:].count('zope.app.keyreference.persistent')
2
We'll store the key references in the database:
>>> root['key1'] = key1
>>> root['key2'] = key2
and use the keys to store the objects again:
>>> root[key1] = root['ob1']
>>> root[key2] = root['ob2']
>>> transaction.commit()
Now we'll open another connection:
>>> conn2 = db.open()
And verify that we can use the keys to look up the objects:
>>> root2 = conn2.root()
>>> key1 = root2['key1']
>>> root2[key1] is root2['ob1']
True
>>> key2 = root2['key2']
>>> root2[key2] is root2['ob2']
True
and that we can also call the keys to get the objects:
>>> key1() is root2['ob1']
True
>>> key2() is root2['ob2']
True
We can't get the key reference for an object that hasn't been saved
yet:
>>> KeyReferenceToPersistent(PersistentMapping())
... # doctest: +ELLIPSIS
Traceback (most recent call last):
...
NotYet: ...
Note that we get a NotYet error. This indicates that we might be able
to get a key reference later.
We can get references to unsaved objects if they have an adapter to
`ZODB.interfaces.IConnection`. The `add` method on the connection
will be used to give the object an object id, which is enough
information to compute the reference. To see this, we'll create an
object that conforms to `IConnection` in a silly way:
>>> import persistent
>>> from ZODB.interfaces import IConnection
>>> class C(persistent.Persistent):
... def __conform__(self, iface):
... if iface is IConnection:
... return conn2
>>> ob3 = C()
>>> key3 = KeyReferenceToPersistent(ob3)
>>> transaction.abort()
Conflict Resolution
-------------------
During conflict resolution, as discussed in ZODB/ConflictResolution.txt,
references to persistent objects are actually instances of
ZODB.ConflictResolution.PersistentReference. This is pertinent in two ways
for KeyReferenceToPersistent. First, it explains a subtlety of the class: it
does not inherit from persistent.Persistent. If it did, it would not be
available for conflict resolution, just its PersistentReference stand-in.
Second, it explains some of the code in the __hash__ and __cmp__
methods. These methods not only handle persistent.Persistent objects,
but PersistentReference objects. Without this behavior, objects, such
as the classic ZODB BTrees, that use KeyReferenceToPersistent as keys or
set members will be unable to resolve conflicts. Even with the special
code, in some cases the KeyReferenceToPersistent will refuse to compare
and hash during conflict resolution because it cannot reliably do so.
__hash__ will work relatively rarely during conflict resolution: only for
multidatabase references. Here are a couple of examples.
>>> from ZODB.ConflictResolution import PersistentReference
>>> def factory(ref):
... res = KeyReferenceToPersistent.__new__(
... KeyReferenceToPersistent, ref)
... res.object = ref
... return res
...
>>> hash(factory(PersistentReference(
... ('an oid', 'class metadata')))) # a typical reference
Traceback (most recent call last):
...
ValueError: database name unavailable at this time
>>> bool(hash(factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])))) # multidatabase
True
This means that KeyReferenceToPersistent will often hinder conflict resolution
for classes such as PersistentMapping.
__cmp__ works unless one object is a multidatabase reference and the other is
not. Here are a few examples.
>>> cmp(factory(PersistentReference(
... ('an oid', 'class metadata'))),
... factory(PersistentReference(
... ('an oid', 'class metadata'))))
0
>>> cmp(factory(PersistentReference(
... ('an oid', 'class metadata'))),
... factory(PersistentReference(
... ('another oid', 'class metadata'))))
-1
>>> cmp(factory(PersistentReference('an oid')),
... factory(PersistentReference(
... ('an oid', 'class metadata'))))
0
>>> cmp(factory(PersistentReference('an oid')),
... factory(PersistentReference(
... ('an oid', 'class metadata'))))
0
>>> cmp(factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])),
... factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])))
0
>>> cmp(factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])),
... factory(PersistentReference(
... ['n', ('a database', 'an oid')])))
0
>>> cmp(factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])),
... factory(PersistentReference(
... ['m', ('another database', 'an oid', 'class metadata')])))
-1
>>> cmp(factory(PersistentReference(
... ['m', ('a database', 'an oid', 'class metadata')])),
... factory(PersistentReference(
... ('an oid', 'class metadata'))))
Traceback (most recent call last):
...
ValueError: cannot sort reliably
Location-based connection adapter
---------------------------------
The function `zope.keyreference.connectionOfPersistent` adapts
objects to connections using a simple location-based heuristic. It
checked to see if the object has a `__parent__` that has a connection:
>>> from zope.keyreference.persistent import connectionOfPersistent
>>> ob3 = PersistentMapping()
>>> print connectionOfPersistent(ob3)
None
>>> ob3.__parent__ = root2['ob1']
>>> connectionOfPersistent(ob3) is conn2
True
|