add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / PayloadIntDecodingIterator.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.Term;
7
8 import org.apache.lucene.util.UnsafeByteArrayInputStream;
9 import org.apache.lucene.util.encoding.IntDecoder;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements.  See the NOTICE file distributed with
14  * this work for additional information regarding copyright ownership.
15  * The ASF licenses this file to You under the Apache License, Version 2.0
16  * (the "License"); you may not use this file except in compliance with
17  * the License.  You may obtain a copy of the License at
18  *
19  *     http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 /**
29  * A payload deserializer comes with its own working space (buffer). One need to
30  * define the {@link IndexReader} and {@link Term} in which the payload resides.
31  * The iterator then consumes the payload information of each document and
32  * decodes it into categories. A typical use case of this class is:
33  * 
34  * <pre>
35  * IndexReader reader = [open your reader];
36  * Term t = new Term(&quot;field&quot;, &quot;where-payload-exists&quot;);
37  * CategoryListIterator cli = new PayloadIntDecodingIterator(reader, t);
38  * if (!cli.init()) {
39  *   // it means there are no payloads / documents associated with that term.
40  *   // Usually a sanity check. However, init() must be called.
41  * }
42  * DocIdSetIterator disi = [you usually iterate on something else, such as a Scorer];
43  * int doc;
44  * while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
45  *   cli.setdoc(doc);
46  *   long category;
47  *   while ((category = cli.nextCategory()) &lt; Integer.MAX_VALUE) {
48  *   }
49  * }
50  * </pre>
51  * 
52  * @lucene.experimental
53  */
54 public class PayloadIntDecodingIterator implements CategoryListIterator {
55
56   private final UnsafeByteArrayInputStream ubais;
57   private final IntDecoder decoder;
58
59   private final IndexReader indexReader;
60   private final Term term;
61   private final PayloadIterator pi;
62   private final int hashCode;
63   
64   public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder)
65       throws IOException {
66     this(indexReader, term, decoder, new byte[1024]);
67   }
68
69   public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder,
70                                     byte[] buffer) throws IOException {
71     pi = new PayloadIterator(indexReader, term, buffer);
72     ubais = new UnsafeByteArrayInputStream();
73     this.decoder = decoder;
74     hashCode = indexReader.hashCode() ^ term.hashCode();
75     this.term = term;
76     this.indexReader = indexReader;
77   }
78
79   @Override
80   public boolean equals(Object other) {
81     if (!(other instanceof PayloadIntDecodingIterator)) {
82       return false;
83     }
84     PayloadIntDecodingIterator that = (PayloadIntDecodingIterator) other;
85     if (hashCode != that.hashCode) {
86       return false;
87     }
88     
89     // Hash codes are the same, check equals() to avoid cases of hash-collisions.
90     return indexReader.equals(that.indexReader) && term.equals(that.term);
91   }
92
93   @Override
94   public int hashCode() {
95     return hashCode;
96   }
97
98   public boolean init() throws IOException {
99     return pi.init();
100   }
101   
102   public long nextCategory() throws IOException {
103     return decoder.decode();
104   }
105
106   public boolean skipTo(int docId) throws IOException {
107     if (!pi.setdoc(docId)) {
108       return false;
109     }
110
111     // Initializing the decoding mechanism with the new payload data
112     ubais.reInit(pi.getBuffer(), 0, pi.getPayloadLength());
113     decoder.reInit(ubais);
114     return true;
115   }
116
117 }