[Checkins] SVN: lovely.relation/trunk/ added repair kit to repair
relation indexes if objects can not be loaded eg.
Juergen Kartnaller
juergen at kartnaller.at
Wed Oct 17 07:50:20 EDT 2007
Log message for revision 80897:
added repair kit to repair relation indexes if objects can not be loaded eg.
in case an object was removed from intid utility but a relation still
exists.
Changed:
U lovely.relation/trunk/CHANGES.txt
U lovely.relation/trunk/setup.py
U lovely.relation/trunk/src/lovely/relation/README.txt
U lovely.relation/trunk/src/lovely/relation/app.py
U lovely.relation/trunk/src/lovely/relation/interfaces.py
-=-
Modified: lovely.relation/trunk/CHANGES.txt
===================================================================
--- lovely.relation/trunk/CHANGES.txt 2007-10-16 21:16:07 UTC (rev 80896)
+++ lovely.relation/trunk/CHANGES.txt 2007-10-17 11:50:19 UTC (rev 80897)
@@ -5,6 +5,13 @@
After
=====
+2007/10/17 1.1.1a2
+==================
+
+- added repair kit to repair relation indexes if objects can not be loaded eg.
+ in case an object was removed from intid utility but a relation still
+ exists.
+
- Add find-links to buildout.cfg.
Modified: lovely.relation/trunk/setup.py
===================================================================
--- lovely.relation/trunk/setup.py 2007-10-16 21:16:07 UTC (rev 80896)
+++ lovely.relation/trunk/setup.py 2007-10-17 11:50:19 UTC (rev 80897)
@@ -21,7 +21,7 @@
from setuptools import setup, find_packages, Extension
setup(name='lovely.relation',
- version='1.1.1a1',
+ version='1.1.1a2',
url='http://svn.zope.org/lovely.relation',
license='ZPL 2.1',
description='Lovely Relation Packages for Zope3',
Modified: lovely.relation/trunk/src/lovely/relation/README.txt
===================================================================
--- lovely.relation/trunk/src/lovely/relation/README.txt 2007-10-16 21:16:07 UTC (rev 80896)
+++ lovely.relation/trunk/src/lovely/relation/README.txt 2007-10-17 11:50:19 UTC (rev 80897)
@@ -140,10 +140,16 @@
>>> list(relations.findRelationTargets(relType))
[<Target 'o1 of s1'>, <Target 'o2 of s2'>]
+ >>> list(intids.getObject(s) for s in relations.findRelationTargetTokens(relType))
+ [<Target 'o1 of s1'>, <Target 'o2 of s2'>]
+
>>> list(relations.findRelationSources(relType))
[<Source 's1'>, <Source 's2'>]
+ >>> list(intids.getObject(s).sources for s in relations.findRelationTokens(relType))
+ [<Source 's1'>, <Source 's2'>]
+
Relation Types
--------------
@@ -425,3 +431,49 @@
... name="lovely.relation.o2oStringTypeRelations")
<lovely.relation.configurator.SetUpO2OStringTypeRelationships object at ...>
+
+Repairing Relations
+-------------------
+
+ >>> from lovely.relation.app import RepairOneToOne
+ >>> component.provideAdapter(RepairOneToOne)
+
+ >>> from lovely.relation.interfaces import IRepair
+ >>> repairer = IRepair(relations)
+
+We can call the repair method to repair the relation container.
+
+ >>> repairer.repair()
+
+We can get the targets of our source.
+
+ >>> [o for o in relations.findTargets(sourceId)]
+ [<Target 'o1 of s1'>]
+
+Now we unregister the target from the intids utility.
+
+ >>> intids.unregister(target)
+
+and get a key error if we try to get targets of our source.
+This happens because the intid is still stored in the relation.
+
+ >>> [o for o in relations.findTargets(sourceId)]
+ Traceback (most recent call last):
+ ...
+ KeyError: ...
+
+If we repair the relation container
+
+ >>> repairer.repair()
+
+we can ask for the targets of the source without a key error.
+
+ >>> [o for o in relations.findTargets(sourceId)]
+ []
+
+Warning:
+
+The use of the integrated repair function removes a relation if at least one
+of the referenced items can not be loaded. It should only be used on one to
+one relations.
+
Modified: lovely.relation/trunk/src/lovely/relation/app.py
===================================================================
--- lovely.relation/trunk/src/lovely/relation/app.py 2007-10-16 21:16:07 UTC (rev 80896)
+++ lovely.relation/trunk/src/lovely/relation/app.py 2007-10-17 11:50:19 UTC (rev 80897)
@@ -43,6 +43,7 @@
IOneToManyRelationships,
IO2OStringTypeRelationship,
IO2OStringTypeRelationships,
+ IRepair,
)
@@ -464,3 +465,32 @@
class O2OStringTypeRelationship(Relationship):
interface.implements(IO2OStringTypeRelationship)
+
+class RepairOneToOne(object):
+ interface.implements(IRepair)
+ component.adapts(IRelations)
+
+ def __init__(self, context):
+ self.context = context
+
+ def repair(self):
+ # repair relations with missing objects:
+ # check for all relation instances in the container:
+ # - source token can be loaded
+ # - target tokens can be loaded
+ # if one of them can not be loaded the relation is deleted.
+ index = self.context.relationIndex
+ for key, name in list(index._reltoken_name_TO_objtokenset.keys()):
+ objs = index._reltoken_name_TO_objtokenset[(key, name)]
+ if objs is None:
+ continue
+ data = index._attrs[name]
+ for objId in list(objs):
+ try:
+ obj = data['load'](objId, index, {})
+ except KeyError:
+ # the object can not be resolved: remove the relation
+ rel = data['load'](key, index, {})
+ self.context.remove(rel)
+ break
+
Modified: lovely.relation/trunk/src/lovely/relation/interfaces.py
===================================================================
--- lovely.relation/trunk/src/lovely/relation/interfaces.py 2007-10-16 21:16:07 UTC (rev 80896)
+++ lovely.relation/trunk/src/lovely/relation/interfaces.py 2007-10-17 11:50:19 UTC (rev 80897)
@@ -179,3 +179,10 @@
annotatable to allow data to be added to a relation.
"""
+
+class IRepair(interface.Interface):
+ """Repair relations."""
+
+ def repair():
+ """Do the repair"""
+
More information about the Checkins
mailing list