Commit 2f2ef92f authored by Andreas Jung's avatar Andreas Jung

     - PathIndex and TopicIndex are now using a counter for the number
       of indexed objects instead of using a very expensive calculation
       based on the keys of their indexes.
parent 5dfe4e63
......@@ -13,6 +13,10 @@ Zope Changes
Bugs fixed
- PathIndex and TopicIndex are now using a counter for the number
of indexed objects instead of using a very expensive calculation
based on the keys of their indexes.
- backport of TAL fixes from z3
* i18n and metal interactions
......
......@@ -15,17 +15,16 @@ import os, sys, unittest
from Products.PluginIndexes.PathIndex.PathIndex import PathIndex
class Dummy:
meta_type="foo"
def __init__( self, path):
self.path = path
def getPhysicalPath(self):
return self.path.split('/')
def __str__( self ):
return '<Dummy: %s>' % self.path
......@@ -62,25 +61,35 @@ class TestCase( unittest.TestCase ):
self._index.index_object( k, v )
def testEmpty(self):
assert len( self._index ) == 0
assert self._index.getEntryForObject( 1234 ) is None
self.assertEqual(self._index.numObjects() ,0)
self.assertEqual(self._index.getEntryForObject(1234), None)
self._index.unindex_object( 1234 ) # nothrow
assert self._index._apply_index( {"suxpath":"xxx"} ) is None
self.assertEqual(self._index._apply_index({"suxpath":"xxx"}), None)
def testUnIndex(self):
self._populateIndex()
self.assertEqual(self._index.numObjects(), 18)
for k in self._values.keys():
self._index.unindex_object(k)
assert len(self._index._index)==0
assert len(self._index._unindex)==0
self.assertEqual(self._index.numObjects(), 0)
self.assertEqual(len(self._index._index), 0)
self.assertEqual(len(self._index._unindex), 0)
def testUnIndexError(self):
def testReindex(self):
self._populateIndex()
self.assertEqual(self._index.numObjects(), 18)
o = Dummy('/foo/bar')
self._index.index_object(19, o)
self.assertEqual(self._index.numObjects(), 19)
self._index.index_object(19, o)
self.assertEqual(self._index.numObjects(), 19)
def testUnIndexError(self):
self._populateIndex()
# this should not raise an error
self._index.unindex_object(-1)
......@@ -91,10 +100,7 @@ class TestCase( unittest.TestCase ):
def testRoot(self):
self._populateIndex()
tests = [
("/",0, range(1,19)),
]
tests = ( ("/",0, range(1,19)), )
for comp,level,results in tests:
for path in [comp,"/"+comp,"/"+comp+"/"]:
......@@ -110,14 +116,10 @@ class TestCase( unittest.TestCase ):
lst = list(res[0].keys())
self.assertEqual(lst,results)
def testRoot(self):
self._populateIndex()
tests = [
("/",0, range(1,19)),
]
tests = ( ("/",0, range(1,19)), )
for comp,level,results in tests:
for path in [comp,"/"+comp,"/"+comp+"/"]:
......@@ -137,7 +139,6 @@ class TestCase( unittest.TestCase ):
def testSimpleTests(self):
self._populateIndex()
tests = [
("aa", 0, [1,2,3,4,5,6,7,8,9]),
("aa", 1, [1,2,3,10,11,12] ),
......@@ -172,7 +173,6 @@ class TestCase( unittest.TestCase ):
def testComplexOrTests(self):
self._populateIndex()
tests = [
(['aa','bb'],1,[1,2,3,4,5,6,10,11,12,13,14,15]),
(['aa','bb','xx'],1,[1,2,3,4,5,6,10,11,12,13,14,15]),
......@@ -189,7 +189,6 @@ class TestCase( unittest.TestCase ):
def testComplexANDTests(self):
self._populateIndex()
tests = [
(['aa','bb'],1,[]),
([('aa',0),('bb',1)],0,[4,5,6]),
......@@ -197,7 +196,6 @@ class TestCase( unittest.TestCase ):
]
for lst ,level,results in tests:
res = self._index._apply_index(
{"path":{'query':lst,"level":level,"operator":"and"}})
lst = list(res[0].keys())
......
......@@ -11,22 +11,21 @@
#
##############################################################################
__version__ = '$Id: TopicIndex.py,v 1.13 2003/06/23 08:45:58 andreasjung Exp $'
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.util import parseIndexRequest
__version__ = '$Id: TopicIndex.py,v 1.14 2003/08/16 16:44:48 andreasjung Exp $'
from Globals import Persistent, DTMLFile
from OFS.SimpleItem import SimpleItem
from Acquisition import Implicit
from zLOG import ERROR, LOG
from BTrees.OOBTree import OOBTree
from BTrees.IIBTree import IISet,intersection,union
from zLOG import ERROR, LOG
import FilteredSet
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.util import parseIndexRequest
_marker = []
class TopicIndex(Persistent, Implicit, SimpleItem):
class TopicIndex(Persistent, SimpleItem):
""" A TopicIndex maintains a set of FilteredSet objects.
Every FilteredSet object consists of an expression and
......@@ -37,6 +36,7 @@ class TopicIndex(Persistent, Implicit, SimpleItem):
__implements__ = (PluggableIndex.PluggableIndexInterface,)
meta_type="TopicIndex"
query_options = ('query','operator')
manage_options= (
{'label': 'FilteredSets',
......@@ -44,93 +44,59 @@ class TopicIndex(Persistent, Implicit, SimpleItem):
'help': ('TopicIndex','TopicIndex_searchResults.stx')},
)
manage_workspace = DTMLFile('dtml/manageTopicIndex',globals())
query_options = ('query','operator')
def __init__(self,id,caller=None):
self.id = id
self.filteredSets = OOBTree()
# experimental code for specifing the operator
self.operators = ('or','and')
self.defaultOperator = 'or'
def getId(self): return self.id
def clear(self):
""" clear everything """
for fs in self.filteredSets.values():
fs.clear()
def index_object(self, documentId, obj ,threshold=100):
def index_object(self, docid, obj ,threshold=100):
""" hook for (Z)Catalog """
for fid, filteredSet in self.filteredSets.items():
filteredSet.index_object(documentId,obj)
filteredSet.index_object(docid,obj)
return 1
def unindex_object(self,documentId):
def unindex_object(self,docid):
""" hook for (Z)Catalog """
for fs in self.filteredSets.values():
try:
fs.unindex_object(documentId)
fs.unindex_object(docid)
except KeyError:
LOG(self.__class__.__name__, ERROR,
'Attempt to unindex document'
' with id %s failed' % documentId)
' with id %s failed' % docid)
return 1
def __len__(self):
""" len """
n=0
for fs in self.filteredSets.values():
n = n + len(fs.getIds())
return n
def numObjects(self):
return "N/A"
def keys(self): pass
def values(self): pass
def items(self): pass
def search(self,filterId):
if self.filteredSets.has_key(filterId):
return self.filteredSets[filterId].getIds()
return "n/a"
def search(self,filter_id):
if self.filteredSets.has_key(filter_id):
return self.filteredSets[filter_id].getIds()
def _apply_index(self, request, cid=''):
""" hook for (Z)Catalog
request mapping type (usually {"topic": "..." }
cid ???
'request' -- mapping type (usually {"topic": "..." }
'cid' -- ???
"""
record = parseIndexRequest(request,self.id,self.query_options)
if record.keys==None: return None
# experimental code for specifing the operator
operator = record.get('operator',self.defaultOperator).lower()
if record.keys is None: return None
# depending on the operator we use intersection of union
if operator=="or": set_func = union
operator = record.get('operator', self.defaultOperator).lower()
if operator == 'or': set_func = union
else: set_func = intersection
res = None
for filterId in record.keys:
rows = self.search(filterId)
for filter_id in record.keys:
rows = self.search(filter_id)
res = set_func(res,rows)
if res:
......@@ -138,79 +104,65 @@ class TopicIndex(Persistent, Implicit, SimpleItem):
else:
return IISet(), (self.id,)
def uniqueValues(self,name=None,withLength=0):
def uniqueValues(self,name=None, withLength=0):
""" needed to be consistent with the interface """
return self.filteredSets.keys()
def getEntryForObject(self,documentId,default=_marker):
def getEntryForObject(self,docid, default=_marker):
""" Takes a document ID and returns all the information we have
on that specific object. """
on that specific object.
"""
return self.filteredSets.keys()
def addFilteredSet(self, filter_id, typeFilteredSet, expr):
def addFilteredSet(self, filterId, typeFilteredSet, expr):
if self.filteredSets.has_key(filterId):
if self.filteredSets.has_key(filter_id):
raise KeyError,\
'A FilteredSet with this name already exists: %s' % filterId
self.filteredSets[filterId] = \
FilteredSet.factory(filterId, typeFilteredSet, expr)
'A FilteredSet with this name already exists: %s' % filter_id
self.filteredSets[filter_id] = \
FilteredSet.factory(filter_id, typeFilteredSet, expr)
def delFilteredSet(self,filterId):
if not self.filteredSets.has_key(filterId):
def delFilteredSet(self,filter_id):
if not self.filteredSets.has_key(filter_id):
raise KeyError,\
'no such FilteredSet: %s' % filterId
del self.filteredSets[filterId]
'no such FilteredSet: %s' % filter_id
del self.filteredSets[filter_id]
def clearFilteredSet(self,filterId):
if not self.filteredSets.has_key(filterId):
def clearFilteredSet(self,filter_id):
if not self.filteredSets.has_key(filter_id):
raise KeyError,\
'no such FilteredSet: %s' % filterId
self.filteredSets[filterId].clear()
'no such FilteredSet: %s' % filter_id
self.filteredSets[filter_id].clear()
def manage_addFilteredSet(self, filterId, typeFilteredSet, expr, URL1, \
def manage_addFilteredSet(self, filter_id, typeFilteredSet, expr, URL1, \
REQUEST=None,RESPONSE=None):
""" add a new filtered set """
if len(filterId)==0: raise RuntimeError,'Length of ID too short'
if len(expr)==0: raise RuntimeError,'Length of expression too short'
if len(filter_id) == 0: raise RuntimeError,'Length of ID too short'
if len(expr) == 0: raise RuntimeError,'Length of expression too short'
self.addFilteredSet(filterId, typeFilteredSet, expr)
self.addFilteredSet(filter_id, typeFilteredSet, expr)
if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
'manage_tabs_message=FilteredSet%20added')
def manage_delFilteredSet(self, filterIds=[], URL1=None, \
def manage_delFilteredSet(self, filter_ids=[], URL1=None, \
REQUEST=None,RESPONSE=None):
""" delete a list of FilteredSets"""
for filterId in filterIds:
self.delFilteredSet(filterId)
for filter_id in filter_ids:
self.delFilteredSet(filter_id)
if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20deleted')
def manage_saveFilteredSet(self,filterId, expr, URL1=None,\
def manage_saveFilteredSet(self,filter_id, expr, URL1=None,\
REQUEST=None,RESPONSE=None):
""" save expression for a FilteredSet """
self.filteredSets[filterId].setExpression(expr)
self.filteredSets[filter_id].setExpression(expr)
if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
......@@ -220,21 +172,21 @@ class TopicIndex(Persistent, Implicit, SimpleItem):
""" return names of indexed attributes """
return ('n/a',)
def manage_clearFilteredSet(self, filterIds=[], URL1=None, \
def manage_clearFilteredSet(self, filter_ids=[], URL1=None, \
REQUEST=None,RESPONSE=None):
""" clear a list of FilteredSets"""
for filterId in filterIds:
self.clearFilteredSet(filterId)
for filter_id in filter_ids:
self.clearFilteredSet(filter_id)
if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20cleared')
editFilteredSet = DTMLFile('dtml/editFilteredSet',globals())
index_html = DTMLFile('dtml/index', globals())
manage_workspace = DTMLFile('dtml/manageTopicIndex',globals())
editFilteredSet = DTMLFile('dtml/editFilteredSet',globals())
manage_addTopicIndexForm = DTMLFile('dtml/addTopicIndex', globals())
......
......@@ -11,12 +11,11 @@
#
##############################################################################
import os ,sys, re, unittest
import ZODB
import os,sys,re,unittest
from Products.PluginIndexes.TopicIndex.TopicIndex import TopicIndex
class Obj:
def __init__(self,id,meta_type=''):
......@@ -26,8 +25,6 @@ class Obj:
def getId(self): return self.id
def getPhysicalPath(self): return self.id
class TestBase(unittest.TestCase):
def _searchAnd(self,query,expected):
......@@ -36,19 +33,15 @@ class TestBase(unittest.TestCase):
def _searchOr(self,query,expected):
return self._search(query,'or',expected)
def _search(self,query,operator,expected):
res = self.TI._apply_index({'topic':{'query':query,'operator':operator}})
rows = list(res[0])
rows.sort()
expected.sort()
self.assertEqual(rows,expected,query)
return rows
class TestTopicIndex(TestBase):
def setUp(self):
......@@ -66,7 +59,6 @@ class TestTopicIndex(TestBase):
def testOr(self):
self._searchOr('doc1',[1,2])
self._searchOr(['doc1'],[1,2])
self._searchOr('doc2',[3,4]),
......@@ -75,15 +67,12 @@ class TestTopicIndex(TestBase):
def testAnd(self):
self._searchAnd('doc1',[1,2])
self._searchAnd(['doc1'],[1,2])
self._searchAnd('doc2',[3,4])
self._searchAnd(['doc2'],[3,4])
self._searchAnd(['doc1','doc2'],[])
def test_suite():
return unittest.TestSuite( (
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment