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 /** Consumes doc & freq, writing them using the current
21 * index file format */
23 import java.io.Closeable;
24 import java.io.IOException;
26 import org.apache.lucene.util.IOUtils;
27 import org.apache.lucene.util.UnicodeUtil;
28 import org.apache.lucene.index.FieldInfo.IndexOptions;
29 import org.apache.lucene.store.IndexOutput;
31 final class FormatPostingsDocsWriter extends FormatPostingsDocsConsumer implements Closeable {
33 final IndexOutput out;
34 final FormatPostingsTermsWriter parent;
35 final FormatPostingsPositionsWriter posWriter;
36 final DefaultSkipListWriter skipListWriter;
37 final int skipInterval;
38 final int totalNumDocs;
40 boolean omitTermFreqAndPositions;
41 boolean storePayloads;
45 FormatPostingsDocsWriter(SegmentWriteState state, FormatPostingsTermsWriter parent) throws IOException {
47 out = parent.parent.dir.createOutput(IndexFileNames.segmentFileName(parent.parent.segment, IndexFileNames.FREQ_EXTENSION));
48 boolean success = false;
50 totalNumDocs = parent.parent.totalNumDocs;
52 // TODO: abstraction violation
53 skipInterval = parent.parent.termsOut.skipInterval;
54 skipListWriter = parent.parent.skipListWriter;
55 skipListWriter.setFreqOutput(out);
57 posWriter = new FormatPostingsPositionsWriter(state, this);
61 IOUtils.closeWhileHandlingException(out);
66 void setField(FieldInfo fieldInfo) {
67 this.fieldInfo = fieldInfo;
68 omitTermFreqAndPositions = fieldInfo.indexOptions == IndexOptions.DOCS_ONLY;
69 storePayloads = fieldInfo.storePayloads;
70 posWriter.setField(fieldInfo);
76 /** Adds a new doc in this term. If this returns null
77 * then we just skip consuming positions/payloads. */
79 FormatPostingsPositionsConsumer addDoc(int docID, int termDocFreq) throws IOException {
81 final int delta = docID - lastDocID;
83 if (docID < 0 || (df > 0 && delta <= 0))
84 throw new CorruptIndexException("docs out of order (" + docID + " <= " + lastDocID + " )");
86 if ((++df % skipInterval) == 0) {
87 // TODO: abstraction violation
88 skipListWriter.setSkipData(lastDocID, storePayloads, posWriter.lastPayloadLength);
89 skipListWriter.bufferSkip(df);
92 assert docID < totalNumDocs: "docID=" + docID + " totalNumDocs=" + totalNumDocs;
95 if (omitTermFreqAndPositions)
97 else if (1 == termDocFreq)
98 out.writeVInt((delta<<1) | 1);
100 out.writeVInt(delta<<1);
101 out.writeVInt(termDocFreq);
107 private final TermInfo termInfo = new TermInfo(); // minimize consing
108 final UnicodeUtil.UTF8Result utf8 = new UnicodeUtil.UTF8Result();
110 /** Called when we are done adding docs to this term */
112 void finish() throws IOException {
113 long skipPointer = skipListWriter.writeSkip(out);
115 // TODO: this is abstraction violation -- we should not
116 // peek up into parents terms encoding format
117 termInfo.set(df, parent.freqStart, parent.proxStart, (int) (skipPointer - parent.freqStart));
119 // TODO: we could do this incrementally
120 UnicodeUtil.UTF16toUTF8(parent.currentTerm, parent.currentTermStart, utf8);
123 parent.termsOut.add(fieldInfo.number,
133 public void close() throws IOException {
134 IOUtils.close(out, posWriter);