pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / FormatPostingsPositionsWriter.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 import org.apache.lucene.index.FieldInfo.IndexOptions;
21 import org.apache.lucene.store.IndexOutput;
22 import org.apache.lucene.util.IOUtils;
23
24
25 import java.io.Closeable;
26 import java.io.IOException;
27
28 final class FormatPostingsPositionsWriter extends FormatPostingsPositionsConsumer implements Closeable {
29
30   final FormatPostingsDocsWriter parent;
31   final IndexOutput out;
32
33   boolean omitTermFreqAndPositions;
34   boolean storePayloads;
35   int lastPayloadLength = -1;
36
37   FormatPostingsPositionsWriter(SegmentWriteState state, FormatPostingsDocsWriter parent) throws IOException {
38     this.parent = parent;
39     omitTermFreqAndPositions = parent.omitTermFreqAndPositions;
40     if (parent.parent.parent.fieldInfos.hasProx()) {
41       // At least one field does not omit TF, so create the
42       // prox file
43       out = parent.parent.parent.dir.createOutput(IndexFileNames.segmentFileName(parent.parent.parent.segment, IndexFileNames.PROX_EXTENSION));
44       parent.skipListWriter.setProxOutput(out);
45     } else
46       // Every field omits TF so we will write no prox file
47       out = null;
48   }
49
50   int lastPosition;
51
52   /** Add a new position & payload */
53   @Override
54   void addPosition(int position, byte[] payload, int payloadOffset, int payloadLength) throws IOException {
55     assert !omitTermFreqAndPositions: "omitTermFreqAndPositions is true";
56     assert out != null;
57
58     final int delta = position - lastPosition;
59     lastPosition = position;
60
61     if (storePayloads) {
62       if (payloadLength != lastPayloadLength) {
63         lastPayloadLength = payloadLength;
64         out.writeVInt((delta<<1)|1);
65         out.writeVInt(payloadLength);
66       } else
67         out.writeVInt(delta << 1);
68       if (payloadLength > 0)
69         out.writeBytes(payload, payloadLength);
70     } else
71       out.writeVInt(delta);
72   }
73
74   void setField(FieldInfo fieldInfo) {
75     omitTermFreqAndPositions = fieldInfo.indexOptions == IndexOptions.DOCS_ONLY;
76     storePayloads = omitTermFreqAndPositions ? false : fieldInfo.storePayloads;
77   }
78
79   /** Called when we are done adding positions & payloads */
80   @Override
81   void finish() {       
82     lastPosition = 0;
83     lastPayloadLength = -1;
84   }
85
86   public void close() throws IOException {
87     IOUtils.close(out);
88   }
89 }