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 java.util.Arrays;
23 import org.apache.lucene.store.IndexOutput;
27 * Implements the skip list writer for the default posting list format
28 * that stores positions and payloads.
31 class DefaultSkipListWriter extends MultiLevelSkipListWriter {
32 private int[] lastSkipDoc;
33 private int[] lastSkipPayloadLength;
34 private long[] lastSkipFreqPointer;
35 private long[] lastSkipProxPointer;
37 private IndexOutput freqOutput;
38 private IndexOutput proxOutput;
41 private boolean curStorePayloads;
42 private int curPayloadLength;
43 private long curFreqPointer;
44 private long curProxPointer;
46 DefaultSkipListWriter(int skipInterval, int numberOfSkipLevels, int docCount, IndexOutput freqOutput, IndexOutput proxOutput) {
47 super(skipInterval, numberOfSkipLevels, docCount);
48 this.freqOutput = freqOutput;
49 this.proxOutput = proxOutput;
51 lastSkipDoc = new int[numberOfSkipLevels];
52 lastSkipPayloadLength = new int[numberOfSkipLevels];
53 lastSkipFreqPointer = new long[numberOfSkipLevels];
54 lastSkipProxPointer = new long[numberOfSkipLevels];
57 void setFreqOutput(IndexOutput freqOutput) {
58 this.freqOutput = freqOutput;
61 void setProxOutput(IndexOutput proxOutput) {
62 this.proxOutput = proxOutput;
66 * Sets the values for the current skip data.
68 void setSkipData(int doc, boolean storePayloads, int payloadLength) {
70 this.curStorePayloads = storePayloads;
71 this.curPayloadLength = payloadLength;
72 this.curFreqPointer = freqOutput.getFilePointer();
73 if (proxOutput != null)
74 this.curProxPointer = proxOutput.getFilePointer();
78 protected void resetSkip() {
80 Arrays.fill(lastSkipDoc, 0);
81 Arrays.fill(lastSkipPayloadLength, -1); // we don't have to write the first length in the skip list
82 Arrays.fill(lastSkipFreqPointer, freqOutput.getFilePointer());
83 if (proxOutput != null)
84 Arrays.fill(lastSkipProxPointer, proxOutput.getFilePointer());
88 protected void writeSkipData(int level, IndexOutput skipBuffer) throws IOException {
89 // To efficiently store payloads in the posting lists we do not store the length of
90 // every payload. Instead we omit the length for a payload if the previous payload had
92 // However, in order to support skipping the payload length at every skip point must be known.
93 // So we use the same length encoding that we use for the posting lists for the skip data as well:
94 // Case 1: current field does not store payloads
95 // SkipDatum --> DocSkip, FreqSkip, ProxSkip
96 // DocSkip,FreqSkip,ProxSkip --> VInt
97 // DocSkip records the document number before every SkipInterval th document in TermFreqs.
98 // Document numbers are represented as differences from the previous value in the sequence.
99 // Case 2: current field stores payloads
100 // SkipDatum --> DocSkip, PayloadLength?, FreqSkip,ProxSkip
101 // DocSkip,FreqSkip,ProxSkip --> VInt
102 // PayloadLength --> VInt
103 // In this case DocSkip/2 is the difference between
104 // the current and the previous value. If DocSkip
105 // is odd, then a PayloadLength encoded as VInt follows,
106 // if DocSkip is even, then it is assumed that the
107 // current payload length equals the length at the previous
109 if (curStorePayloads) {
110 int delta = curDoc - lastSkipDoc[level];
111 if (curPayloadLength == lastSkipPayloadLength[level]) {
112 // the current payload length equals the length at the previous skip point,
113 // so we don't store the length again
114 skipBuffer.writeVInt(delta * 2);
116 // the payload length is different from the previous one. We shift the DocSkip,
117 // set the lowest bit and store the current payload length as VInt.
118 skipBuffer.writeVInt(delta * 2 + 1);
119 skipBuffer.writeVInt(curPayloadLength);
120 lastSkipPayloadLength[level] = curPayloadLength;
123 // current field does not store payloads
124 skipBuffer.writeVInt(curDoc - lastSkipDoc[level]);
126 skipBuffer.writeVInt((int) (curFreqPointer - lastSkipFreqPointer[level]));
127 skipBuffer.writeVInt((int) (curProxPointer - lastSkipProxPointer[level]));
129 lastSkipDoc[level] = curDoc;
130 //System.out.println("write doc at level " + level + ": " + curDoc);
132 lastSkipFreqPointer[level] = curFreqPointer;
133 lastSkipProxPointer[level] = curProxPointer;