1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.Term;
8 import org.apache.lucene.util.UnsafeByteArrayInputStream;
9 import org.apache.lucene.util.encoding.IntDecoder;
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
19 * http://www.apache.org/licenses/LICENSE-2.0
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.
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:
35 * IndexReader reader = [open your reader];
36 * Term t = new Term("field", "where-payload-exists");
37 * CategoryListIterator cli = new PayloadIntDecodingIterator(reader, t);
39 * // it means there are no payloads / documents associated with that term.
40 * // Usually a sanity check. However, init() must be called.
42 * DocIdSetIterator disi = [you usually iterate on something else, such as a Scorer];
44 * while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
47 * while ((category = cli.nextCategory()) < Integer.MAX_VALUE) {
52 * @lucene.experimental
54 public class PayloadIntDecodingIterator implements CategoryListIterator {
56 private final UnsafeByteArrayInputStream ubais;
57 private final IntDecoder decoder;
59 private final IndexReader indexReader;
60 private final Term term;
61 private final PayloadIterator pi;
62 private final int hashCode;
64 public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder)
66 this(indexReader, term, decoder, new byte[1024]);
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();
76 this.indexReader = indexReader;
80 public boolean equals(Object other) {
81 if (!(other instanceof PayloadIntDecodingIterator)) {
84 PayloadIntDecodingIterator that = (PayloadIntDecodingIterator) other;
85 if (hashCode != that.hashCode) {
89 // Hash codes are the same, check equals() to avoid cases of hash-collisions.
90 return indexReader.equals(that.indexReader) && term.equals(that.term);
94 public int hashCode() {
98 public boolean init() throws IOException {
102 public long nextCategory() throws IOException {
103 return decoder.decode();
106 public boolean skipTo(int docId) throws IOException {
107 if (!pi.setdoc(docId)) {
111 // Initializing the decoding mechanism with the new payload data
112 ubais.reInit(pi.getBuffer(), 0, pi.getPayloadLength());
113 decoder.reInit(ubais);