SpanOrQuery simplification and test: LUCENE-742

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@486295 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2006-12-12 19:52:03 +00:00
parent f9cc865eac
commit 86442612f3
2 changed files with 92 additions and 40 deletions

View File

@ -133,6 +133,7 @@ public class SpanOrQuery extends SpanQuery {
return h; return h;
} }
private class SpanQueue extends PriorityQueue { private class SpanQueue extends PriorityQueue {
public SpanQueue(int size) { public SpanQueue(int size) {
initialize(size); initialize(size);
@ -159,30 +160,24 @@ public class SpanOrQuery extends SpanQuery {
return ((SpanQuery)clauses.get(0)).getSpans(reader); return ((SpanQuery)clauses.get(0)).getSpans(reader);
return new Spans() { return new Spans() {
private List all = new ArrayList(clauses.size()); private SpanQueue queue = null;
private SpanQueue queue = new SpanQueue(clauses.size());
{ private boolean initSpanQueue(int target) throws IOException {
queue = new SpanQueue(clauses.size());
Iterator i = clauses.iterator(); Iterator i = clauses.iterator();
while (i.hasNext()) { // initialize all while (i.hasNext()) {
all.add(((SpanQuery)i.next()).getSpans(reader)); Spans spans = ((SpanQuery)i.next()).getSpans(reader);
if ( ((target == -1) && spans.next())
|| ((target != -1) && spans.skipTo(target))) {
queue.put(spans);
} }
} }
return queue.size() != 0;
private boolean firstTime = true; }
public boolean next() throws IOException { public boolean next() throws IOException {
if (firstTime) { // first time -- initialize if (queue == null) {
for (int i = 0; i < all.size(); i++) { return initSpanQueue(-1);
Spans spans = (Spans)all.get(i);
if (spans.next()) { // move to first entry
queue.put(spans); // build queue
} else {
all.remove(i--);
}
}
firstTime = false;
return queue.size() != 0;
} }
if (queue.size() == 0) { // all done if (queue.size() == 0) { // all done
@ -194,31 +189,22 @@ public class SpanOrQuery extends SpanQuery {
return true; return true;
} }
all.remove(queue.pop()); // exhausted a clause queue.pop(); // exhausted a clause
return queue.size() != 0; return queue.size() != 0;
} }
private Spans top() { return (Spans)queue.top(); } private Spans top() { return (Spans)queue.top(); }
public boolean skipTo(int target) throws IOException { public boolean skipTo(int target) throws IOException {
if (firstTime) { if (queue == null) {
for (int i = 0; i < all.size(); i++) { return initSpanQueue(target);
Spans spans = (Spans)all.get(i);
if (spans.skipTo(target)) { // skip each spans in all
queue.put(spans); // build queue
} else {
all.remove(i--);
} }
}
firstTime = false;
} else {
while (queue.size() != 0 && top().doc() < target) { while (queue.size() != 0 && top().doc() < target) {
if (top().skipTo(target)) { if (top().skipTo(target)) {
queue.adjustTop(); queue.adjustTop();
} else { } else {
all.remove(queue.pop()); queue.pop();
}
} }
} }
@ -231,7 +217,7 @@ public class SpanOrQuery extends SpanQuery {
public String toString() { public String toString() {
return "spans("+SpanOrQuery.this+")@"+ return "spans("+SpanOrQuery.this+")@"+
(firstTime?"START" ((queue == null)?"START"
:(queue.size()>0?(doc()+":"+start()+"-"+end()):"END")); :(queue.size()>0?(doc()+":"+start()+"-"+end()):"END"));
} }

View File

@ -190,4 +190,70 @@ public class TestSpans extends TestCase {
assertFalse("third range", spans.next()); assertFalse("third range", spans.next());
} }
private Spans orSpans(String[] terms) throws Exception {
SpanQuery[] sqa = new SpanQuery[terms.length];
for (int i = 0; i < terms.length; i++) {
sqa[i] = makeSpanTermQuery(terms[i]);
}
return (new SpanOrQuery(sqa)).getSpans(searcher.getIndexReader());
}
private void tstNextSpans(Spans spans, int doc, int start, int end)
throws Exception {
assertTrue("next", spans.next());
assertEquals("doc", doc, spans.doc());
assertEquals("start", start, spans.start());
assertEquals("end", end, spans.end());
}
public void testSpanOrEmpty() throws Exception {
Spans spans = orSpans(new String[0]);
assertFalse("empty next", spans.next());
}
public void testSpanOrSingle() throws Exception {
Spans spans = orSpans(new String[] {"w5"});
tstNextSpans(spans, 0, 4, 5);
assertFalse("final next", spans.next());
}
public void testSpanOrDouble() throws Exception {
Spans spans = orSpans(new String[] {"w5", "yy"});
tstNextSpans(spans, 0, 4, 5);
tstNextSpans(spans, 2, 3, 4);
tstNextSpans(spans, 3, 4, 5);
tstNextSpans(spans, 7, 3, 4);
assertFalse("final next", spans.next());
}
public void testSpanOrDoubleSkip() throws Exception {
Spans spans = orSpans(new String[] {"w5", "yy"});
assertTrue("initial skipTo", spans.skipTo(3));
assertEquals("doc", 3, spans.doc());
assertEquals("start", 4, spans.start());
assertEquals("end", 5, spans.end());
tstNextSpans(spans, 7, 3, 4);
assertFalse("final next", spans.next());
}
public void testSpanOrUnused() throws Exception {
Spans spans = orSpans(new String[] {"w5", "unusedTerm", "yy"});
tstNextSpans(spans, 0, 4, 5);
tstNextSpans(spans, 2, 3, 4);
tstNextSpans(spans, 3, 4, 5);
tstNextSpans(spans, 7, 3, 4);
assertFalse("final next", spans.next());
}
public void testSpanOrTripleSameDoc() throws Exception {
Spans spans = orSpans(new String[] {"t1", "t2", "t3"});
tstNextSpans(spans, 11, 0, 1);
tstNextSpans(spans, 11, 1, 2);
tstNextSpans(spans, 11, 2, 3);
tstNextSpans(spans, 11, 3, 4);
tstNextSpans(spans, 11, 4, 5);
tstNextSpans(spans, 11, 5, 6);
assertFalse("final next", spans.next());
}
} }