Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor PluginIndexes.unindex.UnIndex.query_index fixing #55 and #56 #57

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
4.5 (unreleased)
----------------

- Nothing changed yet.
- Refactor ``Products.PluginIndexes.unindex.Unindex.query_index``
(`#56 <https://github.com/zopefoundation/Products.ZCatalog/issues/56>`_)
and fix
(`#55 <https://github.com/zopefoundation/Products.ZCatalog/issues/55>`_).


4.4 (2019-03-08)
Expand Down
1 change: 1 addition & 0 deletions src/Products/PluginIndexes/FieldIndex/FieldIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FieldIndex(UnIndex):
"""
meta_type = 'FieldIndex'
query_options = ('query', 'range', 'not')
potentially_multivalued = False # optimization for exclude terms

manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
Expand Down
23 changes: 17 additions & 6 deletions src/Products/PluginIndexes/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ def clear():
class ILimitedResultIndex(IPluggableIndex):

def _apply_index(request, resultset=None):
"""Same as IPluggableIndex' _apply_index method. The additional
resultset argument contains the resultset, as already calculated by
ZCatalog's search method.
"""Same as IPluggableIndex' _apply_index method.

The additional *resultset* argument contains the resultset,
as already calculated by ZCatalog's search method.
If it is not `None` and `_apply_index` does not return
`None`, then the preliminary result must be intersected
with *resultset*.
"""


Expand All @@ -103,9 +107,16 @@ class IQueryIndex(IPluggableIndex):
useOperator = Attribute('A string specifying the default operator.')
query_options = Attribute('Supported query options for the index.')

def query_index(record, resultset=None):
"""Same as _apply_index, but the query is already a pre-parsed
IndexQuery object.
def query_index(query, resultset=None):
"""return the result of searching for *query*.

*query* is a `Products.ZCatalog.query.IndexQuery` and
describes what is searched for.

The return value is an `IISet` or `IITreeSet`.

If *resultset* is not `None`, then the preliminary result
must be intersected with *resultset*.
"""


Expand Down
53 changes: 52 additions & 1 deletion src/Products/PluginIndexes/tests/test_unindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import unittest

from BTrees.IIBTree import difference
from BTrees.IIBTree import difference, IISet
from OFS.SimpleItem import SimpleItem
from Testing.makerequest import makerequest

Expand Down Expand Up @@ -193,3 +193,54 @@ class Dummy(object):
query = IndexQuery({'counter': 42}, 'counter')
res = index.query_index(query)
self.assertListEqual(list(res), [])

def test_resultset_intersection(self):
"""see #55
`https://github.com/zopefoundation/Products.ZCatalog/issues/55`.
"""
index = self._makeOne("rsi")
for i in (1, 2):
index.insertForwardIndexEntry(i, i)
ers = IISet()
for q in ((1,), (1, 2)):
qd = dict(rsi=dict(query=q))
self.assertEqual(q, tuple(index._apply_index(qd)[0]))
self.assertEqual((), tuple(index._apply_index(qd, ers)[0]))

def test_not(self):
index = self._makeOne("idx")
index.query_options = "not", "operator" # activate `not`, `operator`
apply = index._apply_index
for i, vals in enumerate(((10, 11, 12), (11, 12, 13))):
for v in vals:
index.insertForwardIndexEntry(v, i)
query = {"query": (10, 11), "not": (10,)}
req = dict(idx=query)
# or(10,11), not(10)
self.assertEqual((1, ), tuple(apply(req)[0]), "or(10,11), not(10)")
# and(10, 11), not(10)
query["operator"] = "and"
self.assertEqual((), tuple(apply(req)[0]), "and(10, 11), not(10)")
# 11, not 10
query["query"] = 11
self.assertEqual((1,), tuple(apply(req)[0]), "11, not(10)")

def test_range(self):
index = self._makeOne("idx")
index.query_options = "range", "usage" # activate `range`, `usage`
apply = index._apply_index
docs = tuple(range(10))
for i in docs:
index.insertForwardIndexEntry(i, i)
ranges = (9, None), (None, 1), (5, 6), (None, None),
for op in ("range", "usage"):
for r in ranges:
spec = (["range"] if op == "usage" else []) \
+ (["min"] if r[0] is not None else []) \
+ (["max"] if r[1] is not None else [])
query = {"query": [v for v in r if v is not None],
op: ":".join(spec)}
self.assertEqual(
docs[r[0]: (r[1] + 1 if r[1] is not None else None)],
tuple(apply(dict(idx=query))[0]),
"%s: %s" % (op, r))
Loading