[Zope3-checkins] SVN: Zope3/branches/jim-index/src/zope/ Added the
subscribers that update the catalogs on Unique id add/remove events
Albertas Agejevas
alga at pov.lt
Sat Jun 12 11:38:13 EDT 2004
Log message for revision 25403:
Added the subscribers that update the catalogs on Unique id add/remove events
and ObjectModifyEvent.
-=-
Modified: Zope3/branches/jim-index/src/zope/app/catalog/catalog.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/catalog.py 2004-06-12 15:05:39 UTC (rev 25402)
+++ Zope3/branches/jim-index/src/zope/app/catalog/catalog.py 2004-06-12 15:38:12 UTC (rev 25403)
@@ -21,6 +21,7 @@
from zope.security.proxy import trustedRemoveSecurityProxy
from zope.app.container.sample import SampleContainer
+from zope.app import zapi
from zope.app.annotation.interfaces import IAttributeAnnotatable
from zope.app.container.interfaces import IContainer
from zope.app.catalog.interfaces import ICatalog
@@ -64,7 +65,7 @@
index.unindex_doc(docid)
def updateIndexes(self):
- uidutil = getUtility(IUniqueIdUtility)
+ uidutil = zapi.getUtility(IUniqueIdUtility)
for uid, ref in uidutil.items():
obj = ref()
for index in self.values():
@@ -90,7 +91,7 @@
if not pendingResults:
break # nothing left, short-circuit
# Next we turn the IISet of docids into a generator of objects
- uidutil = getUtility(IUniqueIdUtility)
+ uidutil = zapi.getUtility(IUniqueIdUtility)
results = ResultSet(pendingResults, uidutil)
return results
@@ -106,3 +107,31 @@
class Catalog(CatalogBase):
"""A catalog in content-space."""
+
+
+def indexDocSubscriber(event):
+ """A subscriber to UniqueIdAddedEvent"""
+ for cat in zapi.getAllUtilitiesRegisteredFor(ICatalog):
+ ob = event.original_event.object
+ id = zapi.getUtility(IUniqueIdUtility, context=cat).getId(ob)
+ cat.index_doc(id, ob)
+
+
+def reindexDocSubscriber(event):
+ """A subscriber to ObjectModifiedEvent"""
+ for cat in zapi.getAllUtilitiesRegisteredFor(ICatalog):
+ ob = event.object
+ id = zapi.getUtility(IUniqueIdUtility, context=cat).getId(ob)
+ cat.index_doc(id, ob)
+
+
+def unindexDocSubscriber(event):
+ """A subscriber to UniqueIdRemovedEvent"""
+ for cat in zapi.getAllUtilitiesRegisteredFor(ICatalog):
+ ob = event.original_event.object
+ try:
+ id = zapi.getUtility(IUniqueIdUtility, context=cat).getId(ob)
+ except KeyError:
+ pass
+ else:
+ cat.unindex_doc(id)
Modified: Zope3/branches/jim-index/src/zope/app/catalog/configure.zcml
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/configure.zcml 2004-06-12 15:05:39 UTC (rev 25402)
+++ Zope3/branches/jim-index/src/zope/app/catalog/configure.zcml 2004-06-12 15:38:12 UTC (rev 25403)
@@ -48,7 +48,21 @@
permission="zope.Public"/>
</content>
+ <subscriber
+ factory=".catalog.indexDocSubscriber"
+ for="zope.app.uniqueid.interfaces.UniqueIdAddedEvent"
+ />
+ <subscriber
+ factory=".catalog.reindexDocSubscriber"
+ for="zope.app.event.objectevent.ObjectModifiedEvent"
+ />
+
+ <subscriber
+ factory=".catalog.unindexDocSubscriber"
+ for="zope.app.uniqueid.interfaces.UniqueIdRemovedEvent"
+ />
+
<!-- Include browser package -->
<include package=".browser" />
Modified: Zope3/branches/jim-index/src/zope/app/catalog/tests.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/catalog/tests.py 2004-06-12 15:05:39 UTC (rev 25402)
+++ Zope3/branches/jim-index/src/zope/app/catalog/tests.py 2004-06-12 15:38:12 UTC (rev 25403)
@@ -23,7 +23,7 @@
from zope.interface import implements
from zope.interface.verify import verifyObject
-from zope.app.tests import ztapi
+from zope.app.tests import ztapi, setup
from zope.app.tests.placelesssetup import PlacelessSetup
from BTrees.IIBTree import IISet
from zope.app.uniqueid.interfaces import IUniqueIdUtility
@@ -31,8 +31,12 @@
from zope.index.interfaces import IInjection, ISimpleQuery
from zope.app.catalog.interfaces import ICatalog
from zope.app.catalog.catalog import Catalog
+from zope.app import zapi
+from zope.app.utility import LocalUtilityService
+from zope.app.servicenames import Utilities
+
class ReferenceStub:
def __init__(self, obj):
self.obj = obj
@@ -59,6 +63,7 @@
uid = self._generateId()
self.ids[ob] = uid
self.objs[uid] = ob
+ return uid
else:
return self.ids[ob]
@@ -191,12 +196,84 @@
simiantype='monkey', hat='beret')
+class CatalogStub:
+ implements(ICatalog)
+ def __init__(self):
+ self.regs = []
+ self.unregs = []
+
+ def index_doc(self, docid, doc):
+ self.regs.append((docid, doc))
+
+ def unindex_doc(self, docid):
+ self.unregs.append(docid)
+
+class Stub:
+
+ __name__ = None
+ __parent__ = None
+
+class TestEventSubscribers(unittest.TestCase):
+
+ def setUp(self):
+ self.root = setup.placefulSetUp(True)
+ sm = zapi.getServices(self.root)
+ setup.addService(sm, Utilities, LocalUtilityService())
+ self.utility = setup.addUtility(
+ sm, '', IUniqueIdUtility, UniqueIdUtilityStub())
+ self.cat = setup.addUtility(sm, '', ICatalog, CatalogStub())
+
+ def tearDown(self):
+ setup.placefulTearDown()
+
+ def test_indexDocSubscriber(self):
+ from zope.app.catalog.catalog import indexDocSubscriber
+ from zope.app.container.contained import ObjectAddedEvent
+ from zope.app.uniqueid.interfaces import UniqueIdAddedEvent
+
+ ob = Stub()
+
+ id = self.utility.register(ob)
+ indexDocSubscriber(UniqueIdAddedEvent(ObjectAddedEvent(ob)))
+
+ self.assertEqual(self.cat.regs, [(id, ob)])
+ self.assertEqual(self.cat.unregs, [])
+
+ def test_reindexDocSubscriber(self):
+ from zope.app.catalog.catalog import reindexDocSubscriber
+ from zope.app.event.objectevent import ObjectModifiedEvent
+
+ ob = Stub()
+ id = self.utility.register(ob)
+
+ reindexDocSubscriber(ObjectModifiedEvent(ob))
+
+ self.assertEqual(self.cat.regs, [(1, ob)])
+ self.assertEqual(self.cat.unregs, [])
+
+ def test_unindexDocSubscriber(self):
+ from zope.app.catalog.catalog import unindexDocSubscriber
+ from zope.app.container.contained import ObjectRemovedEvent
+ from zope.app.uniqueid.interfaces import UniqueIdRemovedEvent
+
+ ob = Stub()
+ ob2 = Stub()
+ id = self.utility.register(ob)
+
+ unindexDocSubscriber(UniqueIdRemovedEvent(ObjectRemovedEvent(ob2)))
+ self.assertEqual(self.cat.unregs, [])
+ self.assertEqual(self.cat.regs, [])
+
+ unindexDocSubscriber(UniqueIdRemovedEvent(ObjectRemovedEvent(ob)))
+ self.assertEqual(self.cat.unregs, [id])
+ self.assertEqual(self.cat.regs, [])
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))
+ suite.addTest(unittest.makeSuite(TestEventSubscribers))
return suite
if __name__ == "__main__":
unittest.main()
-
Modified: Zope3/branches/jim-index/src/zope/index/interfaces/__init__.py
===================================================================
--- Zope3/branches/jim-index/src/zope/index/interfaces/__init__.py 2004-06-12 15:05:39 UTC (rev 25402)
+++ Zope3/branches/jim-index/src/zope/index/interfaces/__init__.py 2004-06-12 15:38:12 UTC (rev 25403)
@@ -21,11 +21,11 @@
class IInjection(Interface):
"""Interface for injecting documents into an index."""
- def index_doc(docid, texts):
+ def index_doc(docid, doc):
"""Add a document to the index.
docid: int, identifying the document
- texts: list of unicode, the text to be indexed in a list
+ doc: the document to be indexed
return: None
This can also be used to reindex documents.
@@ -147,10 +147,10 @@
"""
class ISimpleQuery(Interface):
- "A simple query interface."
+ """A simple query interface."""
def query(term, start=0, count=None):
- "search for the given term, return a sequence of hubids"
+ """Search for the given term, return a sequence of docids"""
class ITopicFilteredSet(Interface):
More information about the Zope3-Checkins
mailing list