add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / PayloadIterator.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 import org.apache.lucene.index.TermPositions;
8
9 /**
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
16  *
17  *     http://www.apache.org/licenses/LICENSE-2.0
18  *
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.
24  */
25
26 /**
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).
30  * 
31  * @lucene.experimental
32  */
33 public class PayloadIterator {
34
35   protected byte[] buffer;
36   protected int payloadLength;
37
38   TermPositions tp;
39
40   private boolean hasMore;
41
42   public PayloadIterator(IndexReader indexReader, Term term)
43       throws IOException {
44     this(indexReader, term, new byte[1024]);
45   }
46
47   public PayloadIterator(IndexReader indexReader, Term term, byte[] buffer)
48       throws IOException {
49     this.buffer = buffer;
50     this.tp = indexReader.termPositions(term);
51   }
52
53   /**
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).
57    */
58   public boolean init() throws IOException {
59     hasMore = tp.next();
60     return hasMore;
61   }
62
63   /**
64    * Skip forward to document docId. Return true if this document exists and
65    * has any payload.
66    * <P>
67    * Users should call this method with increasing docIds, and implementations
68    * can assume that this is the case.
69    */
70   public boolean setdoc(int docId) throws IOException {
71     if (!hasMore) {
72       return false;
73     }
74     
75     if (tp.doc() > docId) {
76       return false;
77     }
78
79     // making sure we have the requested document
80     if (tp.doc() < docId) {
81       // Skipping to requested document
82       if (!tp.skipTo(docId)) {
83         this.hasMore = false;
84         return false;
85       }
86
87       // If document not found (skipped to much)
88       if (tp.doc() != docId) {
89         return false;
90       }
91     }
92
93     // Prepare for payload extraction
94     tp.nextPosition();
95
96     this.payloadLength = tp.getPayloadLength();
97     if (this.payloadLength == 0) {
98       return false;
99     }
100
101     if (this.payloadLength > this.buffer.length) {
102       // Growing if necessary.
103       this.buffer = new byte[this.payloadLength * 2 + 1];
104     }
105     // Loading the payload
106     tp.getPayload(this.buffer, 0);
107
108     return true;
109   }
110
111   /**
112    * Get the buffer with the content of the last read payload.
113    */
114   public byte[] getBuffer() {
115     return buffer;
116   }
117
118   /**
119    * Get the length of the last read payload.
120    */
121   public int getPayloadLength() {
122     return payloadLength;
123   }
124
125 }