add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / index / FormatPostingsDocsWriter.java
1 package org.apache.lucene.index;
2
3 /**
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
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 /** Consumes doc & freq, writing them using the current
21  *  index file format */
22
23 import java.io.Closeable;
24 import java.io.IOException;
25
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;
30
31 final class FormatPostingsDocsWriter extends FormatPostingsDocsConsumer implements Closeable {
32
33   final IndexOutput out;
34   final FormatPostingsTermsWriter parent;
35   final FormatPostingsPositionsWriter posWriter;
36   final DefaultSkipListWriter skipListWriter;
37   final int skipInterval;
38   final int totalNumDocs;
39
40   boolean omitTermFreqAndPositions;
41   boolean storePayloads;
42   long freqStart;
43   FieldInfo fieldInfo;
44
45   FormatPostingsDocsWriter(SegmentWriteState state, FormatPostingsTermsWriter parent) throws IOException {
46     this.parent = parent;
47     out = parent.parent.dir.createOutput(IndexFileNames.segmentFileName(parent.parent.segment, IndexFileNames.FREQ_EXTENSION));
48     boolean success = false;
49     try {
50       totalNumDocs = parent.parent.totalNumDocs;
51       
52       // TODO: abstraction violation
53       skipInterval = parent.parent.termsOut.skipInterval;
54       skipListWriter = parent.parent.skipListWriter;
55       skipListWriter.setFreqOutput(out);
56       
57       posWriter = new FormatPostingsPositionsWriter(state, this);
58       success = true;
59     } finally {
60       if (!success) {
61         IOUtils.closeWhileHandlingException(out);
62       }
63     }
64   }
65
66   void setField(FieldInfo fieldInfo) {
67     this.fieldInfo = fieldInfo;
68     omitTermFreqAndPositions = fieldInfo.indexOptions == IndexOptions.DOCS_ONLY;
69     storePayloads = fieldInfo.storePayloads;
70     posWriter.setField(fieldInfo);
71   }
72
73   int lastDocID;
74   int df;
75
76   /** Adds a new doc in this term.  If this returns null
77    *  then we just skip consuming positions/payloads. */
78   @Override
79   FormatPostingsPositionsConsumer addDoc(int docID, int termDocFreq) throws IOException {
80
81     final int delta = docID - lastDocID;
82
83     if (docID < 0 || (df > 0 && delta <= 0))
84       throw new CorruptIndexException("docs out of order (" + docID + " <= " + lastDocID + " )");
85
86     if ((++df % skipInterval) == 0) {
87       // TODO: abstraction violation
88       skipListWriter.setSkipData(lastDocID, storePayloads, posWriter.lastPayloadLength);
89       skipListWriter.bufferSkip(df);
90     }
91
92     assert docID < totalNumDocs: "docID=" + docID + " totalNumDocs=" + totalNumDocs;
93
94     lastDocID = docID;
95     if (omitTermFreqAndPositions)
96       out.writeVInt(delta);
97     else if (1 == termDocFreq)
98       out.writeVInt((delta<<1) | 1);
99     else {
100       out.writeVInt(delta<<1);
101       out.writeVInt(termDocFreq);
102     }
103
104     return posWriter;
105   }
106
107   private final TermInfo termInfo = new TermInfo();  // minimize consing
108   final UnicodeUtil.UTF8Result utf8 = new UnicodeUtil.UTF8Result();
109
110   /** Called when we are done adding docs to this term */
111   @Override
112   void finish() throws IOException {
113     long skipPointer = skipListWriter.writeSkip(out);
114
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));
118
119     // TODO: we could do this incrementally
120     UnicodeUtil.UTF16toUTF8(parent.currentTerm, parent.currentTermStart, utf8);
121
122     if (df > 0) {
123       parent.termsOut.add(fieldInfo.number,
124                           utf8.result,
125                           utf8.length,
126                           termInfo);
127     }
128
129     lastDocID = 0;
130     df = 0;
131   }
132
133   public void close() throws IOException {
134     IOUtils.close(out, posWriter);
135   }
136 }