pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / DefaultSkipListWriter.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 java.io.IOException;
21 import java.util.Arrays;
22
23 import org.apache.lucene.store.IndexOutput;
24
25
26 /**
27  * Implements the skip list writer for the default posting list format
28  * that stores positions and payloads.
29  *
30  */
31 class DefaultSkipListWriter extends MultiLevelSkipListWriter {
32   private int[] lastSkipDoc;
33   private int[] lastSkipPayloadLength;
34   private long[] lastSkipFreqPointer;
35   private long[] lastSkipProxPointer;
36   
37   private IndexOutput freqOutput;
38   private IndexOutput proxOutput;
39
40   private int curDoc;
41   private boolean curStorePayloads;
42   private int curPayloadLength;
43   private long curFreqPointer;
44   private long curProxPointer;
45   
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;
50     
51     lastSkipDoc = new int[numberOfSkipLevels];
52     lastSkipPayloadLength = new int[numberOfSkipLevels];
53     lastSkipFreqPointer = new long[numberOfSkipLevels];
54     lastSkipProxPointer = new long[numberOfSkipLevels];
55   }
56
57   void setFreqOutput(IndexOutput freqOutput) {
58     this.freqOutput = freqOutput;
59   }
60
61   void setProxOutput(IndexOutput proxOutput) {
62     this.proxOutput = proxOutput;
63   }
64
65   /**
66    * Sets the values for the current skip data. 
67    */
68   void setSkipData(int doc, boolean storePayloads, int payloadLength) {
69     this.curDoc = doc;
70     this.curStorePayloads = storePayloads;
71     this.curPayloadLength = payloadLength;
72     this.curFreqPointer = freqOutput.getFilePointer();
73     if (proxOutput != null)
74       this.curProxPointer = proxOutput.getFilePointer();
75   }
76   
77   @Override
78   protected void resetSkip() {
79     super.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());
85   }
86   
87   @Override
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
91     // the same length.
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
108     //         skip point
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);
115       } else {
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;
121       }
122     } else {
123       // current field does not store payloads
124       skipBuffer.writeVInt(curDoc - lastSkipDoc[level]);
125     }
126     skipBuffer.writeVInt((int) (curFreqPointer - lastSkipFreqPointer[level]));
127     skipBuffer.writeVInt((int) (curProxPointer - lastSkipProxPointer[level]));
128
129     lastSkipDoc[level] = curDoc;
130     //System.out.println("write doc at level " + level + ": " + curDoc);
131     
132     lastSkipFreqPointer[level] = curFreqPointer;
133     lastSkipProxPointer[level] = curProxPointer;
134   }
135
136 }