[Zope3-checkins] SVN: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/ Updated catalog code to reflect indexing api changes.

Jim Fulton jim at zope.com
Wed Dec 8 18:35:13 EST 2004


Log message for revision 28595:
  Updated catalog code to reflect indexing api changes.
  
  Also removed keyword indexes, which were incomplete.
  

Changed:
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/browser/configure.zcml
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/catalog.py
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/configure.zcml
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/interfaces.py
  D   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/keyword.py
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/tests.py
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/text.py
  U   Zope3/branches/jim-index-restructure-2004-12/src/zope/app/zptpage/textindex/configure.zcml

-=-
Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/browser/configure.zcml
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/browser/configure.zcml	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/browser/configure.zcml	2004-12-08 23:35:13 UTC (rev 28595)
@@ -70,33 +70,6 @@
     />
 
 <addform
-    name="AddKeywordIndex"
-    label="Add a keyword index"
-    schema="..interfaces.IAttributeIndex"
-    permission="zope.ManageServices"
-    content_factory="..keyword.KeywordIndex"
-    arguments="field_name"
-    keyword_arguments="interface field_callable"
-    />
-
-<addMenuItem
-    title="Keyword Index"
-    description="Index items based on multi-value fields with
-                 orderable values"
-    class="..keyword.KeywordIndex"
-    permission="zope.ManageServices"
-    view="AddKeywordIndex"
-   />
-
-<schemadisplay
-    name="index.html"
-    schema="..keyword.IKeywordIndex"
-    label="Keyword Index"
-    permission="zope.ManageServices"
-    menu="zmi_views" title="Configuration"
-    />
-
-<addform
     name="AddTextIndex"
     label="Add a text index"
     schema="..interfaces.IAttributeIndex"

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/catalog.py
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/catalog.py	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/catalog.py	2004-12-08 23:35:13 UTC (rev 28595)
@@ -20,15 +20,15 @@
 from zope.app.zapi import getUtility
 from zope.security.proxy import removeSecurityProxy
 from zope.app.container.btree import BTreeContainer
+import zope.index.interfaces
 
 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
 from zope.app.intid.interfaces import IIntIds
-from zope.index.interfaces import ISimpleQuery
+from BTrees.IIBTree import weightedIntersection
 
-
 class ResultSet:
     """Lazily accessed set of objects."""
 
@@ -47,7 +47,11 @@
 
 class Catalog(BTreeContainer):
 
-    implements(ICatalog, IContainer, IAttributeAnnotatable)
+    implements(ICatalog,
+               IContainer,
+               IAttributeAnnotatable,
+               zope.index.interfaces.IIndexSearch,
+               )
 
     def clear(self):
         for index in self.values():
@@ -76,28 +80,35 @@
             for index in self.values():
                 index.index_doc(uid, obj)
 
+    def apply(self, query):
+        results = []
+        for index_name, index_query in query.items():
+            index = self[index_name]
+            r = index.apply(index_query)
+            if r is None:
+                continue
+            if not r:
+                # empty results
+                return r
+            results.append((len(r), r))
+
+        if not results:
+            # no applicable indexes, so catalog was not applicable
+            return None
+
+        results.sort() # order from smallest to largest
+        
+        _, result = results.pop(0)
+        for _, r in results:
+            _, result = weightedIntersection(result, r)
+            
+        return result
+
     def searchResults(self, **searchterms):
-        from BTrees.IIBTree import intersection
-        pendingResults = None
-        for key, value in searchterms.items():
-            index = self.get(key)
-            if not index:
-                raise ValueError, "no such index %s" % (key, )
-            index = ISimpleQuery(index)
-            results = index.query(value)
-            # Hm. As a result of calling getAdapter, I get back
-            # security proxy wrapped results from anything that
-            # needed to be adapted.
-            results = removeSecurityProxy(results)
-            if pendingResults is None:
-                pendingResults = results
-            else:
-                pendingResults = intersection(pendingResults, results)
-            if not pendingResults:
-                break # nothing left, short-circuit
-        # Next we turn the IISet of docids into a generator of objects
-        uidutil = zapi.getUtility(IIntIds)
-        results = ResultSet(pendingResults, uidutil)
+        results = self.apply(searchterms)
+        if results is not None:
+            uidutil = zapi.getUtility(IIntIds)
+            results = ResultSet(results, uidutil)
         return results
 
 def indexAdded(index, event):

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/configure.zcml
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/configure.zcml	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/configure.zcml	2004-12-08 23:35:13 UTC (rev 28595)
@@ -59,16 +59,6 @@
       />
 </content>
 
-<content class=".keyword.KeywordIndex">
-  <require
-      permission="zope.ManageServices"
-      interface=".interfaces.IAttributeIndex
-                 zope.index.interfaces.IStatistics
-                "
-      set_schema=".interfaces.IAttributeIndex"
-      />
-</content>
-
 <content class=".text.TextIndex">
   <require
       permission="zope.ManageServices"

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/interfaces.py
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/interfaces.py	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/interfaces.py	2004-12-08 23:35:13 UTC (rev 28595)
@@ -39,7 +39,7 @@
 
 
 class ICatalogIndex(zope.index.interfaces.IInjection,
-                    zope.index.interfaces.ISimpleQuery,
+                    zope.index.interfaces.IIndexSearch,
                     ):
     """An index to be used in a catalog
     """

Deleted: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/keyword.py
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/keyword.py	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/keyword.py	2004-12-08 23:35:13 UTC (rev 28595)
@@ -1,37 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2003 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Keyword catalog indexes
-
-$Id$
-"""
-
-import zope.index.keyword
-import zope.interface
-
-import zope.app.container.contained
-import zope.app.catalog.attribute
-import zope.app.catalog.interfaces
-
-class IKeywordIndex(zope.app.catalog.interfaces.IAttributeIndex,
-                    zope.app.catalog.interfaces.ICatalogIndex,
-                    ):
-    """Interface-based catalog keyword index
-    """
-
-class KeywordIndex(zope.app.catalog.attribute.AttributeIndex,
-                   zope.index.keyword.KeywordIndex,
-                   zope.app.container.contained.Contained):
-
-    zope.interface.implements(IKeywordIndex)
-

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/tests.py
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/tests.py	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/tests.py	2004-12-08 23:35:13 UTC (rev 28595)
@@ -21,6 +21,8 @@
 import unittest
 import doctest
 
+import BTrees.IIBTree
+
 from zope.interface import implements
 from zope.interface.verify import verifyObject
 from zope.app.tests import ztapi, setup
@@ -28,7 +30,7 @@
 from BTrees.IIBTree import IISet
 from zope.app.intid.interfaces import IIntIds
 
-from zope.index.interfaces import IInjection, ISimpleQuery
+from zope.index.interfaces import IInjection, IIndexSearch
 from zope.app.catalog.interfaces import ICatalog
 from zope.app.catalog.catalog import Catalog
 from zope.app import zapi
@@ -88,7 +90,7 @@
 class StubIndex:
     """A stub for Index."""
 
-    implements(ISimpleQuery, IInjection)
+    implements(IIndexSearch, IInjection)
 
     def __init__(self, field_name, interface=None):
         self._field_name = field_name
@@ -101,7 +103,7 @@
     def unindex_doc(self, docid):
         del self.doc[docid]
 
-    def query(self, term, start=0, count=None):
+    def apply(self, term):
         results = []
         for docid in self.doc:
             obj = self.doc[docid]
@@ -202,7 +204,7 @@
 
         res = catalog.searchResults(simiantype='ape', name='mwumi')
         self.assertEqual(len(res), 0)
-        self.assertRaises(ValueError, catalog.searchResults,
+        self.assertRaises(KeyError, catalog.searchResults,
                           simiantype='monkey', hat='beret')
 
 
@@ -289,33 +291,12 @@
         self.assertEqual(self.cat.regs, [])
 
 
-def test_textindex_simple_query():
-    """
-    >>> class Doc:
-    ...     def __init__(self, text):
-    ...         self.text = text
-    >>> from zope.app.catalog.text import TextIndex
-    >>> index = TextIndex('text')
-    >>> index.index_doc(1, Doc('now time for all good men to come to the aid'))
-    >>> index.index_doc(2, Doc('we should use Zope3 now'))
-    >>> index.index_doc(3, Doc('doctest makes tests more readable'))
-    >>> r = index.query('now')
-    >>> r.sort()
-    >>> r
-    [1, 2]
-    >>> index.query('doctest')
-    [3]
-    
-    
-    """
-
 def test_suite():
     from zope.testing.doctestunit import DocTestSuite
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(Test))
     suite.addTest(unittest.makeSuite(TestEventSubscribers))
     suite.addTest(DocTestSuite('zope.app.catalog.attribute'))
-    suite.addTest(DocTestSuite())
     return suite
 
 

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/text.py
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/text.py	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/catalog/text.py	2004-12-08 23:35:13 UTC (rev 28595)
@@ -16,7 +16,7 @@
 $Id$
 """
 import zope.index.text
-import zope.index.interfaces.searchabletext
+import zope.index.text.interfaces
 import zope.interface
 
 import zope.app.catalog.attribute
@@ -34,7 +34,7 @@
         description=_(u"Objects will be adapted to this interface"),
         vocabulary=_("Interfaces"),
         required=False,
-        default=zope.index.interfaces.searchabletext.ISearchableText,
+        default=zope.index.text.interfaces.ISearchableText,
         )
 
     field_name = zope.schema.BytesLine(
@@ -55,13 +55,3 @@
                 zope.app.container.contained.Contained):
 
     zope.interface.implements(ITextIndex)
-
-    def query(self, text, start=0, count=None):
-        """Return a list of ids matching the text
-
-        This a dumbed-down implementation that matches ISimpleQuery.
-        
-        """
-        result = super(TextIndex, self).query(text, start, count)
-        return [id for (id, rank) in result[0]]
-    

Modified: Zope3/branches/jim-index-restructure-2004-12/src/zope/app/zptpage/textindex/configure.zcml
===================================================================
--- Zope3/branches/jim-index-restructure-2004-12/src/zope/app/zptpage/textindex/configure.zcml	2004-12-08 23:27:40 UTC (rev 28594)
+++ Zope3/branches/jim-index-restructure-2004-12/src/zope/app/zptpage/textindex/configure.zcml	2004-12-08 23:35:13 UTC (rev 28595)
@@ -2,7 +2,7 @@
 
   <adapter
       for="..interfaces.IZPTPage"
-      provides="zope.index.interfaces.searchabletext.ISearchableText"
+      provides="zope.index.text.interfaces.ISearchableText"
       factory=".zptpage.SearchableText"
       />
 



More information about the Zope3-Checkins mailing list