01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21: import org.apache.lucene.index.*;
22:
23: /**
24: * Position of a term in a document that takes into account the term offset within the phrase.
25: */
26: final class PhrasePositions {
27: int doc; // current doc
28: int position; // position in doc
29: int count; // remaining pos in this doc
30: int offset; // position in phrase
31: TermPositions tp; // stream of positions
32: PhrasePositions next; // used to make lists
33: boolean repeats; // there's other pp for same term (e.g. query="1st word 2nd word"~1)
34:
35: PhrasePositions(TermPositions t, int o) {
36: tp = t;
37: offset = o;
38: }
39:
40: final boolean next() throws IOException { // increments to next doc
41: if (!tp.next()) {
42: tp.close(); // close stream
43: doc = Integer.MAX_VALUE; // sentinel value
44: return false;
45: }
46: doc = tp.doc();
47: position = 0;
48: return true;
49: }
50:
51: final boolean skipTo(int target) throws IOException {
52: if (!tp.skipTo(target)) {
53: tp.close(); // close stream
54: doc = Integer.MAX_VALUE; // sentinel value
55: return false;
56: }
57: doc = tp.doc();
58: position = 0;
59: return true;
60: }
61:
62: final void firstPosition() throws IOException {
63: count = tp.freq(); // read first pos
64: nextPosition();
65: }
66:
67: /**
68: * Go to next location of this term current document, and set
69: * <code>position</code> as <code>location - offset</code>, so that a
70: * matching exact phrase is easily identified when all PhrasePositions
71: * have exactly the same <code>position</code>.
72: */
73: final boolean nextPosition() throws IOException {
74: if (count-- > 0) { // read subsequent pos's
75: position = tp.nextPosition() - offset;
76: return true;
77: } else
78: return false;
79: }
80: }
|