pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / FreqProxFieldMergeState.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
22 import org.apache.lucene.index.FieldInfo.IndexOptions;
23 import org.apache.lucene.index.FreqProxTermsWriterPerField.FreqProxPostingsArray;
24
25 // TODO FI: some of this is "generic" to TermsHash* so we
26 // should factor it out so other consumers don't have to
27 // duplicate this code
28
29 /** Used by DocumentsWriter to merge the postings from
30  *  multiple ThreadStates when creating a segment */
31 final class FreqProxFieldMergeState {
32
33   final FreqProxTermsWriterPerField field;
34   final int numPostings;
35   final CharBlockPool charPool;
36   final int[] termIDs;
37   final FreqProxPostingsArray postings;
38   int currentTermID;
39   
40   char[] text;
41   int textOffset;
42
43   private int postingUpto = -1;
44
45   final ByteSliceReader freq = new ByteSliceReader();
46   final ByteSliceReader prox = new ByteSliceReader();
47
48   int docID;
49   int termFreq;
50
51   public FreqProxFieldMergeState(FreqProxTermsWriterPerField field) {
52     this.field = field;
53     this.charPool = field.perThread.termsHashPerThread.charPool;
54     this.numPostings = field.termsHashPerField.numPostings;
55     this.termIDs = field.termsHashPerField.sortPostings();
56     this.postings = (FreqProxPostingsArray) field.termsHashPerField.postingsArray;
57   }
58
59   boolean nextTerm() throws IOException {
60     postingUpto++;
61     if (postingUpto == numPostings)
62       return false;
63
64     currentTermID = termIDs[postingUpto];
65     docID = 0;
66
67     final int textStart = postings.textStarts[currentTermID];
68     text = charPool.buffers[textStart >> DocumentsWriter.CHAR_BLOCK_SHIFT];
69     textOffset = textStart & DocumentsWriter.CHAR_BLOCK_MASK;
70
71     field.termsHashPerField.initReader(freq, currentTermID, 0);
72     if (field.fieldInfo.indexOptions == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)
73       field.termsHashPerField.initReader(prox, currentTermID, 1);
74
75     // Should always be true
76     boolean result = nextDoc();
77     assert result;
78
79     return true;
80   }
81
82   public String termText() {
83     int upto = textOffset;
84     while(text[upto] != 0xffff) {
85       upto++;
86     }
87     return new String(text, textOffset, upto-textOffset);
88   }
89
90   public boolean nextDoc() throws IOException {
91     if (freq.eof()) {
92       if (postings.lastDocCodes[currentTermID] != -1) {
93         // Return last doc
94         docID = postings.lastDocIDs[currentTermID];
95         if (field.indexOptions != IndexOptions.DOCS_ONLY)
96           termFreq = postings.docFreqs[currentTermID];
97         postings.lastDocCodes[currentTermID] = -1;
98         return true;
99       } else
100         // EOF
101         return false;
102     }
103
104     final int code = freq.readVInt();
105     if (field.indexOptions == IndexOptions.DOCS_ONLY)
106       docID += code;
107     else {
108       docID += code >>> 1;
109       if ((code & 1) != 0)
110         termFreq = 1;
111       else
112         termFreq = freq.readVInt();
113     }
114
115     assert docID != postings.lastDocIDs[currentTermID];
116
117     return true;
118   }
119 }