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 org.apache.lucene.index.FieldInfo.IndexOptions;
21 import org.apache.lucene.store.IndexOutput;
22 import org.apache.lucene.util.IOUtils;
25 import java.io.Closeable;
26 import java.io.IOException;
28 final class FormatPostingsPositionsWriter extends FormatPostingsPositionsConsumer implements Closeable {
30 final FormatPostingsDocsWriter parent;
31 final IndexOutput out;
33 boolean omitTermFreqAndPositions;
34 boolean storePayloads;
35 int lastPayloadLength = -1;
37 FormatPostingsPositionsWriter(SegmentWriteState state, FormatPostingsDocsWriter parent) throws IOException {
39 omitTermFreqAndPositions = parent.omitTermFreqAndPositions;
40 if (parent.parent.parent.fieldInfos.hasProx()) {
41 // At least one field does not omit TF, so create the
43 out = parent.parent.parent.dir.createOutput(IndexFileNames.segmentFileName(parent.parent.parent.segment, IndexFileNames.PROX_EXTENSION));
44 parent.skipListWriter.setProxOutput(out);
46 // Every field omits TF so we will write no prox file
52 /** Add a new position & payload */
54 void addPosition(int position, byte[] payload, int payloadOffset, int payloadLength) throws IOException {
55 assert !omitTermFreqAndPositions: "omitTermFreqAndPositions is true";
58 final int delta = position - lastPosition;
59 lastPosition = position;
62 if (payloadLength != lastPayloadLength) {
63 lastPayloadLength = payloadLength;
64 out.writeVInt((delta<<1)|1);
65 out.writeVInt(payloadLength);
67 out.writeVInt(delta << 1);
68 if (payloadLength > 0)
69 out.writeBytes(payload, payloadLength);
74 void setField(FieldInfo fieldInfo) {
75 omitTermFreqAndPositions = fieldInfo.indexOptions == IndexOptions.DOCS_ONLY;
76 storePayloads = omitTermFreqAndPositions ? false : fieldInfo.storePayloads;
79 /** Called when we are done adding positions & payloads */
83 lastPayloadLength = -1;
86 public void close() throws IOException {