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;
7 import org.apache.lucene.index.TermPositions;
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed with
12 * this work for additional information regarding copyright ownership.
13 * The ASF licenses this file to You under the Apache License, Version 2.0
14 * (the "License"); you may not use this file except in compliance with
15 * the License. You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
27 * A utility class for iterating through a posting list of a given term and
28 * retrieving the payload of the first occurrence in every document. Comes with
29 * its own working space (buffer).
31 * @lucene.experimental
33 public class PayloadIterator {
35 protected byte[] buffer;
36 protected int payloadLength;
40 private boolean hasMore;
42 public PayloadIterator(IndexReader indexReader, Term term)
44 this(indexReader, term, new byte[1024]);
47 public PayloadIterator(IndexReader indexReader, Term term, byte[] buffer)
50 this.tp = indexReader.termPositions(term);
54 * (re)initialize the iterator. Should be done before the first call to
55 * {@link #setdoc(int)}. Returns false if there is no category list found
56 * (no setdoc() will never return true).
58 public boolean init() throws IOException {
64 * Skip forward to document docId. Return true if this document exists and
67 * Users should call this method with increasing docIds, and implementations
68 * can assume that this is the case.
70 public boolean setdoc(int docId) throws IOException {
75 if (tp.doc() > docId) {
79 // making sure we have the requested document
80 if (tp.doc() < docId) {
81 // Skipping to requested document
82 if (!tp.skipTo(docId)) {
87 // If document not found (skipped to much)
88 if (tp.doc() != docId) {
93 // Prepare for payload extraction
96 this.payloadLength = tp.getPayloadLength();
97 if (this.payloadLength == 0) {
101 if (this.payloadLength > this.buffer.length) {
102 // Growing if necessary.
103 this.buffer = new byte[this.payloadLength * 2 + 1];
105 // Loading the payload
106 tp.getPayload(this.buffer, 0);
112 * Get the buffer with the content of the last read payload.
114 public byte[] getBuffer() {
119 * Get the length of the last read payload.
121 public int getPayloadLength() {
122 return payloadLength;