Skip to content

Commit

Permalink
Unify Unindex and DateIndex search logic (_apply_index) adding `not…
Browse files Browse the repository at this point in the history
…` support to DateIndexes.
  • Loading branch information
hannosch committed Jun 2, 2012
1 parent 1c91963 commit b2d1b05
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 85 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,8 @@ Changelog
3.0a3 (unreleased)
------------------

- Unify Unindex and DateIndex search logic (`_apply_index`) adding `not`
support to DateIndexes.

3.0a2 (2012-04-26)
------------------
Expand Down
79 changes: 1 addition & 78 deletions src/Products/PluginIndexes/DateIndex/DateIndex.py
Expand Up @@ -87,7 +87,7 @@ class DateIndex(UnIndex, PropertyManager):
implements(IDateIndex)

meta_type = 'DateIndex'
query_options = ('query', 'range')
query_options = ('query', 'range', 'not')

index_naive_time_as_local = True # False means index as UTC
_properties = ({'id': 'index_naive_time_as_local',
Expand Down Expand Up @@ -150,83 +150,6 @@ def index_object(self, documentId, obj, threshold=None):

return returnStatus

def _apply_index(self, request, resultset=None):
"""Apply the index to query parameters given in the argument
Normalize the 'query' arguments into integer values at minute
precision before querying.
"""
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None

keys = map(self._convert, record.keys)
index = self._index
r = None
opr = None

# experimental code for specifying the operator
operator = record.get('operator', self.useOperator)
if not operator in self.operators:
raise RuntimeError("operator not valid: %s" % operator)

# depending on the operator we use intersection or union
if operator == "or":
set_func = union
else:
set_func = intersection

# range parameter
range_arg = record.get('range', None)
if range_arg:
opr = "range"
opr_args = []
if range_arg.find("min") > -1:
opr_args.append("min")
if range_arg.find("max") > -1:
opr_args.append("max")

if record.get('usage', None):
# see if any usage params are sent to field
opr = record.usage.lower().split(':')
opr, opr_args = opr[0], opr[1:]

if opr == "range": # range search
if 'min' in opr_args:
lo = min(keys)
else:
lo = None

if 'max' in opr_args:
hi = max(keys)
else:
hi = None

if hi:
setlist = index.values(lo, hi)
else:
setlist = index.values(lo)

r = multiunion(setlist)

else: # not a range search
for key in keys:
set = index.get(key, None)
if set is not None:
if isinstance(set, int):
set = IISet((set,))
else:
# set can't be bigger than resultset
set = intersection(set, resultset)
r = set_func(r, set)

if isinstance(r, int):
r = IISet((r,))

if r is None:
return IISet(), (self.id, )
return r, (self.id, )

def _convert(self, value, default=None):
"""Convert Date/Time value to our internal representation"""
if isinstance(value, DateTime):
Expand Down
16 changes: 13 additions & 3 deletions src/Products/PluginIndexes/DateIndex/tests.py
Expand Up @@ -10,8 +10,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""DateIndex unit tests.
"""

import unittest

Expand Down Expand Up @@ -205,7 +203,7 @@ def test_empty(self):
'range': 'min:max'}},
[])

def test_retrieval( self ):
def test_retrieval(self):
from DateTime import DateTime
index = self._makeOne()
self._populateIndex(index)
Expand Down Expand Up @@ -248,6 +246,18 @@ def test_retrieval( self ):
self._checkApply(index,
{'date': 1072742900}, [values[7]])


def test_not(self):
from DateTime import DateTime
index = self._makeOne()
self._populateIndex(index)
values = self._getValues()
# all but the None value
self._checkApply(index, {'date': {'not': 123}}, values[1:])
self._checkApply(index, {'date': {'not': DateTime(0)}}, values[2:])
self._checkApply(index, {'date': {'not':
[DateTime(0), DateTime('2002-05-08 15:16:17')]}}, values[3:])

def test_naive_convert_to_utc(self):
index = self._makeOne()
values = self._getValues()
Expand Down
3 changes: 1 addition & 2 deletions src/Products/PluginIndexes/FieldIndex/FieldIndex.py
Expand Up @@ -20,14 +20,13 @@ class FieldIndex(UnIndex):
"""Index for simple fields.
"""
meta_type = "FieldIndex"
query_options = ('query', 'range', 'not')

manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
{'label': 'Browse', 'action': 'manage_browse'},
)

query_options = ["query", "range", "not"]

manage = manage_main = DTMLFile('dtml/manageFieldIndex', globals())
manage_main._setName('manage_main')
manage_browse = DTMLFile('../dtml/browseIndex', globals())
Expand Down
3 changes: 1 addition & 2 deletions src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py
Expand Up @@ -31,14 +31,13 @@ class KeywordIndex(UnIndex):
This should have an _apply_index that returns a relevance score
"""
meta_type = "KeywordIndex"
query_options = ('query', 'range', 'not', 'operator')

manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
{'label': 'Browse', 'action': 'manage_browse'},
)

query_options = ("query", "operator", "range", "not")

def _index_object(self, documentId, obj, threshold=None, attr=''):
""" index an object 'obj' with integer id 'i'
Expand Down
8 changes: 8 additions & 0 deletions src/Products/PluginIndexes/common/UnIndex.py
Expand Up @@ -295,6 +295,9 @@ def _apply_not(self, not_parm, resultset=None):
setlist.append(s)
return multiunion(setlist)

def _convert(self, value, default=None):
return value

def _apply_index(self, request, resultset=None):
"""Apply the index to query parameters given in the request arg.
Expand Down Expand Up @@ -341,8 +344,13 @@ def _apply_index(self, request, resultset=None):
# not / exclude parameter
not_parm = record.get('not', None)
if not record.keys and not_parm:
# convert into indexed format
not_parm = map(self._convert, not_parm)
# we have only a 'not' query
record.keys = [k for k in index.keys() if k not in not_parm]
else:
# convert query arguments into indexed format
record.keys = map(self._convert, record.keys)

# experimental code for specifing the operator
operator = record.get('operator', self.useOperator)
Expand Down

0 comments on commit b2d1b05

Please sign in to comment.