001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.util.Arrays;
022:
023: import org.apache.lucene.store.IndexOutput;
024:
025: /**
026: * Implements the skip list writer for the default posting list format
027: * that stores positions and payloads.
028: *
029: */
030: class DefaultSkipListWriter extends MultiLevelSkipListWriter {
031: private int[] lastSkipDoc;
032: private int[] lastSkipPayloadLength;
033: private long[] lastSkipFreqPointer;
034: private long[] lastSkipProxPointer;
035:
036: private IndexOutput freqOutput;
037: private IndexOutput proxOutput;
038:
039: private int curDoc;
040: private boolean curStorePayloads;
041: private int curPayloadLength;
042: private long curFreqPointer;
043: private long curProxPointer;
044:
045: DefaultSkipListWriter(int skipInterval, int numberOfSkipLevels,
046: int docCount, IndexOutput freqOutput, IndexOutput proxOutput) {
047: super (skipInterval, numberOfSkipLevels, docCount);
048: this .freqOutput = freqOutput;
049: this .proxOutput = proxOutput;
050:
051: lastSkipDoc = new int[numberOfSkipLevels];
052: lastSkipPayloadLength = new int[numberOfSkipLevels];
053: lastSkipFreqPointer = new long[numberOfSkipLevels];
054: lastSkipProxPointer = new long[numberOfSkipLevels];
055: }
056:
057: /**
058: * Sets the values for the current skip data.
059: */
060: void setSkipData(int doc, boolean storePayloads, int payloadLength) {
061: this .curDoc = doc;
062: this .curStorePayloads = storePayloads;
063: this .curPayloadLength = payloadLength;
064: this .curFreqPointer = freqOutput.getFilePointer();
065: this .curProxPointer = proxOutput.getFilePointer();
066: }
067:
068: protected void resetSkip() {
069: super .resetSkip();
070: Arrays.fill(lastSkipDoc, 0);
071: Arrays.fill(lastSkipPayloadLength, -1); // we don't have to write the first length in the skip list
072: Arrays.fill(lastSkipFreqPointer, freqOutput.getFilePointer());
073: Arrays.fill(lastSkipProxPointer, proxOutput.getFilePointer());
074: }
075:
076: protected void writeSkipData(int level, IndexOutput skipBuffer)
077: throws IOException {
078: // To efficiently store payloads in the posting lists we do not store the length of
079: // every payload. Instead we omit the length for a payload if the previous payload had
080: // the same length.
081: // However, in order to support skipping the payload length at every skip point must be known.
082: // So we use the same length encoding that we use for the posting lists for the skip data as well:
083: // Case 1: current field does not store payloads
084: // SkipDatum --> DocSkip, FreqSkip, ProxSkip
085: // DocSkip,FreqSkip,ProxSkip --> VInt
086: // DocSkip records the document number before every SkipInterval th document in TermFreqs.
087: // Document numbers are represented as differences from the previous value in the sequence.
088: // Case 2: current field stores payloads
089: // SkipDatum --> DocSkip, PayloadLength?, FreqSkip,ProxSkip
090: // DocSkip,FreqSkip,ProxSkip --> VInt
091: // PayloadLength --> VInt
092: // In this case DocSkip/2 is the difference between
093: // the current and the previous value. If DocSkip
094: // is odd, then a PayloadLength encoded as VInt follows,
095: // if DocSkip is even, then it is assumed that the
096: // current payload length equals the length at the previous
097: // skip point
098: if (curStorePayloads) {
099: int delta = curDoc - lastSkipDoc[level];
100: if (curPayloadLength == lastSkipPayloadLength[level]) {
101: // the current payload length equals the length at the previous skip point,
102: // so we don't store the length again
103: skipBuffer.writeVInt(delta * 2);
104: } else {
105: // the payload length is different from the previous one. We shift the DocSkip,
106: // set the lowest bit and store the current payload length as VInt.
107: skipBuffer.writeVInt(delta * 2 + 1);
108: skipBuffer.writeVInt(curPayloadLength);
109: lastSkipPayloadLength[level] = curPayloadLength;
110: }
111: } else {
112: // current field does not store payloads
113: skipBuffer.writeVInt(curDoc - lastSkipDoc[level]);
114: }
115: skipBuffer
116: .writeVInt((int) (curFreqPointer - lastSkipFreqPointer[level]));
117: skipBuffer
118: .writeVInt((int) (curProxPointer - lastSkipProxPointer[level]));
119:
120: lastSkipDoc[level] = curDoc;
121: //System.out.println("write doc at level " + level + ": " + curDoc);
122:
123: lastSkipFreqPointer[level] = curFreqPointer;
124: lastSkipProxPointer[level] = curProxPointer;
125: }
126:
127: }
|