Commit cfe41292 authored by Tino Wildenhain's avatar Tino Wildenhain

UnIndex.py now accepts any mapping type for

the extra argument. Its parameter 'indexed_attrs'
can be string with comma delimited attribute names
or list/tuple

This fixes: # 1688
parent 8c9fb0ee
...@@ -16,7 +16,7 @@ $Id$""" ...@@ -16,7 +16,7 @@ $Id$"""
import sys import sys
from cgi import escape from cgi import escape
from logging import getLogger from logging import getLogger
from types import IntType from types import IntType, StringTypes
from BTrees.OOBTree import OOBTree, OOSet from BTrees.OOBTree import OOBTree, OOSet
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree
...@@ -65,10 +65,12 @@ class UnIndex(SimpleItem): ...@@ -65,10 +65,12 @@ class UnIndex(SimpleItem):
You will also need to pass in an object in the index and You will also need to pass in an object in the index and
uninded methods for this to work. uninded methods for this to work.
'extra' -- a record-style object that keeps additional 'extra' -- a mapping object that keeps additional
index-related parameters index-related parameters - subitem 'indexed_attrs'
can be string with comma separated attribute names or
a list
'caller' -- reference to the calling object (usually 'caller' -- reference to the calling object (usually
a (Z)Catalog instance a (Z)Catalog instance
""" """
self.id = id self.id = id
...@@ -79,14 +81,16 @@ class UnIndex(SimpleItem): ...@@ -79,14 +81,16 @@ class UnIndex(SimpleItem):
self.useOperator = 'or' self.useOperator = 'or'
# allow index to index multiple attributes # allow index to index multiple attributes
try: try:
self.indexed_attrs = extra.indexed_attrs.split(',') ia=extra['indexed_attrs']
self.indexed_attrs = [ if type(ia) in StringTypes:
attr.strip() for attr in self.indexed_attrs if attr ] self.indexed_attrs = ia.split(',')
if len(self.indexed_attrs) == 0: self.indexed_attrs = [ self.id ] else:
except: self.indexed_attrs = list(ia)
self.indexed_attrs = [ self.id ] self.indexed_attrs = [ attr.strip() for attr in self.indexed_attrs if attr ] or [self.id]
except:
self.indexed_attrs = [ self.id ]
self._length = BTrees.Length.Length() self._length = BTrees.Length.Length()
self.clear() self.clear()
...@@ -144,22 +148,22 @@ class UnIndex(SimpleItem): ...@@ -144,22 +148,22 @@ class UnIndex(SimpleItem):
if not indexRow: if not indexRow:
del self._index[entry] del self._index[entry]
self._length.change(-1) self._length.change(-1)
except AttributeError: except AttributeError:
# index row is an int # index row is an int
del self._index[entry] del self._index[entry]
self._length.change(-1) self._length.change(-1)
except: except:
LOG.error('%s: unindex_object could not remove ' LOG.error('%s: unindex_object could not remove '
'documentId %s from index %s. This ' 'documentId %s from index %s. This '
'should not happen.' % (self.__class__.__name__, 'should not happen.' % (self.__class__.__name__,
str(documentId), str(self.id)), str(documentId), str(self.id)),
exc_info=sys.exc_info()) exc_info=sys.exc_info())
else: else:
LOG.error('%s: unindex_object tried to retrieve set %s ' LOG.error('%s: unindex_object tried to retrieve set %s '
'from index %s but couldn\'t. This ' 'from index %s but couldn\'t. This '
'should not happen.' % (self.__class__.__name__, 'should not happen.' % (self.__class__.__name__,
repr(entry), str(self.id))) repr(entry), str(self.id)))
...@@ -193,10 +197,10 @@ class UnIndex(SimpleItem): ...@@ -193,10 +197,10 @@ class UnIndex(SimpleItem):
res = 0 res = 0
for attr in fields: for attr in fields:
res += self._index_object(documentId, obj, threshold, attr) res += self._index_object(documentId, obj, threshold, attr)
return res > 0
return res > 0
def _index_object(self, documentId, obj, threshold=None, attr=''): def _index_object(self, documentId, obj, threshold=None, attr=''):
""" index and object 'obj' with integer id 'documentId'""" """ index and object 'obj' with integer id 'documentId'"""
...@@ -245,7 +249,7 @@ class UnIndex(SimpleItem): ...@@ -245,7 +249,7 @@ class UnIndex(SimpleItem):
def unindex_object(self, documentId): def unindex_object(self, documentId):
""" Unindex the object with integer id 'documentId' and don't """ Unindex the object with integer id 'documentId' and don't
raise an exception if we fail raise an exception if we fail
""" """
unindexRecord = self._unindex.get(documentId, _marker) unindexRecord = self._unindex.get(documentId, _marker)
if unindexRecord is _marker: if unindexRecord is _marker:
...@@ -374,7 +378,7 @@ class UnIndex(SimpleItem): ...@@ -374,7 +378,7 @@ class UnIndex(SimpleItem):
def getIndexSourceNames(self): def getIndexSourceNames(self):
""" return sequence of indexed attributes """ """ return sequence of indexed attributes """
try: try:
return self.indexed_attrs return self.indexed_attrs
except: except:
return [ self.id ] return [ self.id ]
...@@ -400,15 +404,15 @@ class UnIndex(SimpleItem): ...@@ -400,15 +404,15 @@ class UnIndex(SimpleItem):
else: else:
l = len(set) l = len(set)
rl.append((i, l)) rl.append((i, l))
return tuple(rl) return tuple(rl)
def keyForDocument(self, id): def keyForDocument(self, id):
# This method is superceded by documentToKeyMap # This method is superceded by documentToKeyMap
return self._unindex[id] return self._unindex[id]
def documentToKeyMap(self): def documentToKeyMap(self):
return self._unindex return self._unindex
def items(self): def items(self):
items = [] items = []
for k,v in self._index.items(): for k,v in self._index.items():
......
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