Commit db9cf10c authored by Tim Peters's avatar Tim Peters

Use the new SetOps for mass union/intersection.

parent f4c2c29b
...@@ -14,8 +14,10 @@ ...@@ -14,8 +14,10 @@
"""Generic parser support: exception and parse tree nodes.""" """Generic parser support: exception and parse tree nodes."""
from BTrees.IIBTree import difference, weightedIntersection, weightedUnion from BTrees.IIBTree import difference
from Products.ZCTextIndex.NBest import NBest
from Products.ZCTextIndex.SetOps import mass_weightedIntersection, \
mass_weightedUnion
class QueryError(Exception): class QueryError(Exception):
pass pass
...@@ -67,19 +69,13 @@ class AndNode(ParseTreeNode): ...@@ -67,19 +69,13 @@ class AndNode(ParseTreeNode):
Nots = [] Nots = []
for subnode in self.getValue(): for subnode in self.getValue():
if subnode.nodeType() == "NOT": if subnode.nodeType() == "NOT":
Nots.append(subnode.getValue().executeQuery(index)) Nots.append((subnode.getValue().executeQuery(index), 1))
else: else:
L.append(subnode.executeQuery(index)) L.append((subnode.executeQuery(index), 1))
assert L assert L
L.sort(lambda x, y: cmp(len(x), len(y))) set = mass_weightedIntersection(L)
set = L[0]
for x in L[1:]:
dummy, set = weightedIntersection(set, x)
if Nots: if Nots:
Nots.sort(lambda x, y: cmp(len(x), len(y))) notset = mass_weightedUnion(Nots)
notset = Nots[0]
for x in Nots[1:]:
dummy, notset = weightedUnion(notset, x)
set = difference(set, notset) set = difference(set, notset)
return set return set
...@@ -88,20 +84,9 @@ class OrNode(ParseTreeNode): ...@@ -88,20 +84,9 @@ class OrNode(ParseTreeNode):
_nodeType = "OR" _nodeType = "OR"
def executeQuery(self, index): def executeQuery(self, index):
# Balance unions as closely as possible, smallest to largest. weighted = [(node.executeQuery(index), 1)
allofem = self.getValue() for node in self.getValue()]
merge = NBest(len(allofem)) return mass_weightedUnion(weighted)
for subnode in allofem:
result = subnode.executeQuery(index)
merge.add(result, len(result))
while len(merge) > 1:
# Merge the two smallest so far, and add back to the queue.
x, dummy = merge.pop_smallest()
y, dummy = merge.pop_smallest()
dummy, z = weightedUnion(x, y)
merge.add(z, len(z))
result, dummy = merge.pop_smallest()
return result
class AtomNode(ParseTreeNode): class AtomNode(ParseTreeNode):
......
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