1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import org.apache.lucene.util.BitVector;
22 import org.apache.lucene.index.FieldInfo.IndexOptions;
23 import org.apache.lucene.store.IndexInput;
25 class SegmentTermDocs implements TermDocs {
26 protected SegmentReader parent;
27 protected IndexInput freqStream;
30 protected BitVector deletedDocs;
34 private int skipInterval;
35 private int maxSkipLevels;
36 private DefaultSkipListReader skipListReader;
38 private long freqBasePointer;
39 private long proxBasePointer;
41 private long skipPointer;
42 private boolean haveSkipped;
44 protected boolean currentFieldStoresPayloads;
45 protected IndexOptions indexOptions;
47 protected SegmentTermDocs(SegmentReader parent) {
49 this.freqStream = (IndexInput) parent.core.freqStream.clone();
50 synchronized (parent) {
51 this.deletedDocs = parent.deletedDocs;
53 this.skipInterval = parent.core.getTermsReader().getSkipInterval();
54 this.maxSkipLevels = parent.core.getTermsReader().getMaxSkipLevels();
57 public void seek(Term term) throws IOException {
58 TermInfo ti = parent.core.getTermsReader().get(term);
62 public void seek(TermEnum termEnum) throws IOException {
66 // use comparison of fieldinfos to verify that termEnum belongs to the same segment as this SegmentTermDocs
67 if (termEnum instanceof SegmentTermEnum && ((SegmentTermEnum) termEnum).fieldInfos == parent.core.fieldInfos) { // optimized case
68 SegmentTermEnum segmentTermEnum = ((SegmentTermEnum) termEnum);
69 term = segmentTermEnum.term();
70 ti = segmentTermEnum.termInfo();
72 term = termEnum.term();
73 ti = parent.core.getTermsReader().get(term);
79 void seek(TermInfo ti, Term term) throws IOException {
81 FieldInfo fi = parent.core.fieldInfos.fieldInfo(term.field);
82 indexOptions = (fi != null) ? fi.indexOptions : IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
83 currentFieldStoresPayloads = (fi != null) ? fi.storePayloads : false;
89 freqBasePointer = ti.freqPointer;
90 proxBasePointer = ti.proxPointer;
91 skipPointer = freqBasePointer + ti.skipOffset;
92 freqStream.seek(freqBasePointer);
97 public void close() throws IOException {
99 if (skipListReader != null)
100 skipListReader.close();
103 public final int doc() { return doc; }
104 public final int freq() { return freq; }
106 protected void skippingDoc() throws IOException {
109 public boolean next() throws IOException {
113 final int docCode = freqStream.readVInt();
115 if (indexOptions == IndexOptions.DOCS_ONLY) {
119 doc += docCode >>> 1; // shift off low bit
120 if ((docCode & 1) != 0) // if low bit is set
121 freq = 1; // freq is one
123 freq = freqStream.readVInt(); // else read freq
128 if (deletedDocs == null || !deletedDocs.get(doc))
135 /** Optimized implementation. */
136 public int read(final int[] docs, final int[] freqs)
138 final int length = docs.length;
139 if (indexOptions == IndexOptions.DOCS_ONLY) {
140 return readNoTf(docs, freqs, length);
143 while (i < length && count < df) {
144 // manually inlined call to next() for speed
145 final int docCode = freqStream.readVInt();
146 doc += docCode >>> 1; // shift off low bit
147 if ((docCode & 1) != 0) // if low bit is set
148 freq = 1; // freq is one
150 freq = freqStream.readVInt(); // else read freq
153 if (deletedDocs == null || !deletedDocs.get(doc)) {
163 private final int readNoTf(final int[] docs, final int[] freqs, final int length) throws IOException {
165 while (i < length && count < df) {
166 // manually inlined call to next() for speed
167 doc += freqStream.readVInt();
170 if (deletedDocs == null || !deletedDocs.get(doc)) {
172 // Hardware freq to 1 when term freqs were not
173 // stored in the index
182 /** Overridden by SegmentTermPositions to skip in prox stream. */
183 protected void skipProx(long proxPointer, int payloadLength) throws IOException {}
185 /** Optimized implementation. */
186 public boolean skipTo(int target) throws IOException {
187 if ((target - skipInterval) >= doc && df >= skipInterval) { // optimized case
188 if (skipListReader == null)
189 skipListReader = new DefaultSkipListReader((IndexInput) freqStream.clone(), maxSkipLevels, skipInterval); // lazily clone
191 if (!haveSkipped) { // lazily initialize skip stream
192 skipListReader.init(skipPointer, freqBasePointer, proxBasePointer, df, currentFieldStoresPayloads);
196 int newCount = skipListReader.skipTo(target);
197 if (newCount > count) {
198 freqStream.seek(skipListReader.getFreqPointer());
199 skipProx(skipListReader.getProxPointer(), skipListReader.getPayloadLength());
201 doc = skipListReader.getDoc();
206 // done skipping, now just scan
210 } while (target > doc);