Index: lucene/facet/src/java/org/apache/lucene/facet/enhancements/EnhancementsPayloadIterator.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/facet/enhancements/EnhancementsPayloadIterator.java (revision 1418874) +++ lucene/facet/src/java/org/apache/lucene/facet/enhancements/EnhancementsPayloadIterator.java (working copy) @@ -70,9 +70,9 @@ // read header - number of enhancements and their lengths Position position = new Position(); - nEnhancements = Vint8.decode(buffer, position); + nEnhancements = Vint8.decode(data.bytes, position); for (int i = 0; i < nEnhancements; i++) { - enhancementLength[i] = Vint8.decode(buffer, position); + enhancementLength[i] = Vint8.decode(data.bytes, position); } // set enhancements start points @@ -96,7 +96,7 @@ public Object getCategoryData(CategoryEnhancement enhancedCategory) { for (int i = 0; i < nEnhancements; i++) { if (enhancedCategory.equals(EnhancedCategories[i])) { - return enhancedCategory.extractCategoryTokenData(buffer, + return enhancedCategory.extractCategoryTokenData(data.bytes, enhancementStart[i], enhancementLength[i]); } } Index: lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIntDecodingIterator.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIntDecodingIterator.java (revision 1418874) +++ lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIntDecodingIterator.java (working copy) @@ -4,7 +4,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; - +import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.UnsafeByteArrayInputStream; import org.apache.lucene.util.encoding.IntDecoder; @@ -61,14 +61,8 @@ private final PayloadIterator pi; private final int hashCode; - public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder) - throws IOException { - this(indexReader, term, decoder, new byte[1024]); - } - - public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder, - byte[] buffer) throws IOException { - pi = new PayloadIterator(indexReader, term, buffer); + public PayloadIntDecodingIterator(IndexReader indexReader, Term term, IntDecoder decoder) throws IOException { + pi = new PayloadIterator(indexReader, term); ubais = new UnsafeByteArrayInputStream(); this.decoder = decoder; hashCode = indexReader.hashCode() ^ term.hashCode(); @@ -95,21 +89,25 @@ return hashCode; } + @Override public boolean init() throws IOException { return pi.init(); } + @Override public long nextCategory() throws IOException { return decoder.decode(); } + @Override public boolean skipTo(int docId) throws IOException { if (!pi.setdoc(docId)) { return false; } // Initializing the decoding mechanism with the new payload data - ubais.reInit(pi.getBuffer(), 0, pi.getPayloadLength()); + BytesRef data = pi.getPayload(); + ubais.reInit(data.bytes, data.offset, data.length + data.offset); decoder.reInit(ubais); return true; } Index: lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIterator.java =================================================================== --- lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIterator.java (revision 1418874) +++ lucene/facet/src/java/org/apache/lucene/facet/search/PayloadIterator.java (working copy) @@ -29,31 +29,30 @@ /** * A utility class for iterating through a posting list of a given term and - * retrieving the payload of the first occurrence in every document. Comes with - * its own working space (buffer). + * retrieving the payload of the first position in every document. For + * efficiency, this class does not check if documents passed to + * {@link #setdoc(int)} are deleted, since it is usually used to iterate on + * documents' payload who matched a query. If you need to skip over deleted + * documents, you should do so before calling {@link #setdoc(int)}. * * @lucene.experimental */ public class PayloadIterator { - protected byte[] buffer; - protected int payloadLength; + protected BytesRef data; - DocsAndPositionsEnum tp; + private final DocsAndPositionsEnum tp; private boolean hasMore; - public PayloadIterator(IndexReader indexReader, Term term) - throws IOException { - this(indexReader, term, new byte[1024]); - } - - public PayloadIterator(IndexReader indexReader, Term term, byte[] buffer) - throws IOException { - this.buffer = buffer; + public PayloadIterator(IndexReader indexReader, Term term) throws IOException { + // this class is usually used to iterate on whatever a Query matched + // if it didn't match deleted documents, we won't receive them. if it + // did, we should iterate on them too ! + Bits liveDocs = null; // TODO (Facet): avoid Multi*? - Bits liveDocs = MultiFields.getLiveDocs(indexReader); - this.tp = MultiFields.getTermPositionsEnum(indexReader, liveDocs, term.field(), term.bytes(), DocsAndPositionsEnum.FLAG_PAYLOADS); + tp = MultiFields.getTermPositionsEnum(indexReader, liveDocs, + term.field(), term.bytes(), DocsAndPositionsEnum.FLAG_PAYLOADS); } /** @@ -99,37 +98,13 @@ // Prepare for payload extraction tp.nextPosition(); - BytesRef br = tp.getPayload(); + data = tp.getPayload(); - if (br == null) { - return false; - } - - assert br.length > 0; - - this.payloadLength = br.length; - - if (this.payloadLength > this.buffer.length) { - // Growing if necessary. - this.buffer = new byte[this.payloadLength * 2 + 1]; - } - // Loading the payload - System.arraycopy(br.bytes, br.offset, this.buffer, 0, payloadLength); - return true; + return data != null; } - - /** - * Get the buffer with the content of the last read payload. - */ - public byte[] getBuffer() { - return buffer; + + public BytesRef getPayload() { + return data; } - /** - * Get the length of the last read payload. - */ - public int getPayloadLength() { - return payloadLength; - } - }