From 0f3b19899e00dcc8eb30f992b93be54592a0618c Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sun, 12 Aug 2012 18:45:46 +0000 Subject: [PATCH] revisit payloads API in DocsAndPositionsEnum git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1372171 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 +++ .../MappingMultiDocsAndPositionsEnum.java | 8 +--- .../lucene/codecs/PostingsConsumer.java | 14 +------ .../lucene/codecs/TermVectorsWriter.java | 3 +- .../lucene40/Lucene40PostingsReader.java | 35 +++++++--------- .../lucene40/Lucene40TermVectorsReader.java | 12 ------ .../codecs/memory/DirectPostingsFormat.java | 41 ++++++++----------- .../codecs/memory/MemoryPostingsFormat.java | 14 +------ .../codecs/pulsing/PulsingPostingsReader.java | 12 ++---- .../lucene/codecs/sep/SepPostingsReader.java | 12 +++--- .../simpletext/SimpleTextFieldsReader.java | 13 +----- .../SimpleTextTermVectorsReader.java | 6 --- .../org/apache/lucene/index/CheckIndex.java | 18 ++++---- .../lucene/index/DocsAndPositionsEnum.java | 9 ++-- .../lucene/index/FilterAtomicReader.java | 5 --- .../index/MultiDocsAndPositionsEnum.java | 5 --- .../lucene/search/MultiPhraseQuery.java | 7 +--- .../search/payloads/PayloadTermQuery.java | 4 +- .../search/spans/NearSpansUnordered.java | 4 +- .../lucene/search/spans/SpanNotQuery.java | 2 +- .../lucene/search/spans/SpanOrQuery.java | 2 +- .../search/spans/SpanPositionCheckQuery.java | 2 +- .../org/apache/lucene/search/spans/Spans.java | 2 +- .../apache/lucene/search/spans/TermSpans.java | 9 ++-- .../org/apache/lucene/index/TestCodecs.java | 4 +- .../lucene/index/TestDocumentWriter.java | 6 +-- .../lucene/index/TestDuelingCodecs.java | 10 +---- .../apache/lucene/index/TestLongPostings.java | 10 ++++- .../index/TestPayloadProcessorProvider.java | 4 +- .../org/apache/lucene/index/TestPayloads.java | 19 +-------- .../lucene/index/TestPayloadsOnVectors.java | 2 - .../lucene/index/TestPostingsFormat.java | 11 ++--- .../lucene/index/TestPostingsOffsets.java | 4 +- .../search/spans/MultiSpansWrapper.java | 2 +- .../lucene/facet/search/PayloadIterator.java | 11 ++--- .../lucene/index/memory/MemoryIndex.java | 5 --- .../codecs/ramonly/RAMOnlyPostingsFormat.java | 11 +++-- .../lucene/index/AssertingAtomicReader.java | 12 ++---- 38 files changed, 115 insertions(+), 240 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 34bcd4f22ad..65e565aa484 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -22,6 +22,11 @@ API Changes had positions or offsets, since this can be configured on a per-field-per-document basis. (Robert Muir) +* Removed DocsAndPositionsEnum.hasPayload() and simplified the + contract of getPayload(). It returns null if there is no payload, + otherwise returns the current payload. You can now call it multiple + times per position if you want. (Robert Muir) + Bug Fixes * LUCENE-4297: BooleanScorer2 would multiply the coord() factor diff --git a/lucene/core/src/java/org/apache/lucene/codecs/MappingMultiDocsAndPositionsEnum.java b/lucene/core/src/java/org/apache/lucene/codecs/MappingMultiDocsAndPositionsEnum.java index 1b77736f49a..4d46fad5274 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/MappingMultiDocsAndPositionsEnum.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/MappingMultiDocsAndPositionsEnum.java @@ -126,6 +126,7 @@ public final class MappingMultiDocsAndPositionsEnum extends DocsAndPositionsEnum BytesRef payload = current.getPayload(); if (mergeState.currentPayloadProcessor[upto] != null && payload != null) { // to not violate the D&P api, we must give the processor a private copy + // TODO: reuse a BytesRef if there is a PPP payload = BytesRef.deepCopyOf(payload); mergeState.currentPayloadProcessor[upto].processPayload(payload); if (payload.length == 0) { @@ -135,12 +136,5 @@ public final class MappingMultiDocsAndPositionsEnum extends DocsAndPositionsEnum } return payload; } - - @Override - public boolean hasPayload() { - // TODO: note this is actually bogus if there is a payloadProcessor, - // because it might remove it: but lets just remove this method completely - return current.hasPayload(); - } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/PostingsConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/PostingsConsumer.java index 099a1f26622..0aeb1cdba94 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/PostingsConsumer.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/PostingsConsumer.java @@ -112,12 +112,7 @@ public abstract class PostingsConsumer { totTF += freq; for(int i=0;i payload.bytes.length) { - payload.grow(payloadLength); - } + + if (payloadPending) { + if (payloadLength > payload.bytes.length) { + payload.grow(payloadLength); + } - proxIn.readBytes(payload.bytes, 0, payloadLength); - payload.length = payloadLength; - payloadPending = false; + proxIn.readBytes(payload.bytes, 0, payloadLength); + payload.length = payloadLength; + payloadPending = false; + } return payload; } else { - throw new IOException("No payloads exist for this field!"); + return null; } } - - @Override - public boolean hasPayload() { - return payloadPending && payloadLength > 0; - } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java index e7fa80820b6..9454eb3e265 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java @@ -671,7 +671,6 @@ public class Lucene40TermVectorsReader extends TermVectorsReader { @Override public BytesRef getPayload() { - // TODO: dumb that we have to duplicate hasPayload if (payloadOffsets == null) { return null; } else { @@ -687,17 +686,6 @@ public class Lucene40TermVectorsReader extends TermVectorsReader { } } - @Override - public boolean hasPayload() { - if (payloadOffsets == null) { - return false; - } else { - int off = payloadOffsets[nextPos-1]; - int end = nextPos == payloadOffsets.length ? payloadBytes.length : payloadOffsets[nextPos]; - return end - off > 0; - } - } - @Override public int nextPosition() { assert (positions != null && nextPos < positions.length) || diff --git a/lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java index 24b50dd5d9f..b96d0c45a35 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/memory/DirectPostingsFormat.java @@ -348,9 +348,8 @@ public class DirectPostingsFormat extends PostingsFormat { scratch.add(docsAndPositionsEnum.endOffset()); } if (hasPayloads) { - final BytesRef payload; - if (docsAndPositionsEnum.hasPayload()) { - payload = docsAndPositionsEnum.getPayload(); + final BytesRef payload = docsAndPositionsEnum.getPayload(); + if (payload != null) { scratch.add(payload.length); ros.writeBytes(payload.bytes, payload.offset, payload.length); } else { @@ -421,9 +420,8 @@ public class DirectPostingsFormat extends PostingsFormat { for(int pos=0;pos 0; - } - @Override public BytesRef getPayload() { if (payloadLength > 0) { payload.bytes = payloadBytes; payload.offset = lastPayloadOffset; payload.length = payloadLength; - payloadLength = 0; return payload; } else { return null; @@ -2010,7 +2002,6 @@ public class DirectPostingsFormat extends PostingsFormat { private int upto; private int docID = -1; private int posUpto; - private boolean gotPayload; private int[] curPositions; public HighFreqDocsAndPositionsEnum(Bits liveDocs, boolean hasOffsets) { @@ -2080,7 +2071,6 @@ public class DirectPostingsFormat extends PostingsFormat { @Override public int nextPosition() { posUpto += posJump; - gotPayload = false; return curPositions[posUpto]; } @@ -2214,21 +2204,22 @@ public class DirectPostingsFormat extends PostingsFormat { } } - @Override - public boolean hasPayload() { - return !gotPayload && payloads != null && payloads[upto][posUpto/(hasOffsets ? 3 : 1)] != null; - } - private final BytesRef payload = new BytesRef(); @Override public BytesRef getPayload() { - final byte[] payloadBytes = payloads[upto][posUpto/(hasOffsets ? 3:1)]; - payload.bytes = payloadBytes; - payload.length = payloadBytes.length; - payload.offset = 0; - gotPayload = true; - return payload; + if (payloads == null) { + return null; + } else { + final byte[] payloadBytes = payloads[upto][posUpto/(hasOffsets ? 3:1)]; + if (payloadBytes == null) { + return null; + } + payload.bytes = payloadBytes; + payload.length = payloadBytes.length; + payload.offset = 0; + return payload; + } } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java index 492c37a2928..3fcfeb40594 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java @@ -446,7 +446,6 @@ public class MemoryPostingsFormat extends PostingsFormat { private int numDocs; private int posPending; private int payloadLength; - private boolean payloadRetrieved; final boolean storeOffsets; int offsetLength; int startOffset; @@ -484,7 +483,6 @@ public class MemoryPostingsFormat extends PostingsFormat { payloadLength = 0; this.numDocs = numDocs; posPending = 0; - payloadRetrieved = false; startOffset = storeOffsets ? 0 : -1; // always return -1 if no offsets are stored offsetLength = 0; return this; @@ -577,10 +575,6 @@ public class MemoryPostingsFormat extends PostingsFormat { payload.offset = in.getPosition(); in.skipBytes(payloadLength); payload.length = payloadLength; - // Necessary, in case caller changed the - // payload.bytes from prior call: - payload.bytes = buffer; - payloadRetrieved = false; } //System.out.println(" pos=" + pos + " payload=" + payload + " fp=" + in.getPosition()); @@ -599,13 +593,7 @@ public class MemoryPostingsFormat extends PostingsFormat { @Override public BytesRef getPayload() { - payloadRetrieved = true; - return payload; - } - - @Override - public boolean hasPayload() { - return !payloadRetrieved && payload.length > 0; + return payload.length > 0 ? payload : null; } @Override diff --git a/lucene/core/src/java/org/apache/lucene/codecs/pulsing/PulsingPostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/pulsing/PulsingPostingsReader.java index 7dbdfc3ed29..76fa37acac9 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/pulsing/PulsingPostingsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/pulsing/PulsingPostingsReader.java @@ -532,19 +532,13 @@ public class PulsingPostingsReader extends PostingsReaderBase { } } - @Override - public boolean hasPayload() { - return storePayloads && !payloadRetrieved && payloadLength > 0; - } - @Override public BytesRef getPayload() throws IOException { //System.out.println("PR getPayload payloadLength=" + payloadLength + " this=" + this); if (payloadRetrieved) { - throw new IOException("Either no payload exists at this term position or an attempt was made to load it more than once."); - } - payloadRetrieved = true; - if (payloadLength > 0) { + return payload; + } else if (storePayloads && payloadLength > 0) { + payloadRetrieved = true; if (payload == null) { payload = new BytesRef(payloadLength); } else { diff --git a/lucene/core/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java index 5b5af157f33..9ab080cc3a9 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java @@ -714,7 +714,11 @@ public class SepPostingsReader extends PostingsReaderBase { @Override public BytesRef getPayload() throws IOException { if (!payloadPending) { - throw new IOException("Either no payload exists at this term position or an attempt was made to load it more than once."); + return null; + } + + if (pendingPayloadBytes == 0) { + return payload; } assert pendingPayloadBytes >= payloadLength; @@ -731,15 +735,9 @@ public class SepPostingsReader extends PostingsReaderBase { } payloadIn.readBytes(payload.bytes, 0, payloadLength); - payloadPending = false; payload.length = payloadLength; pendingPayloadBytes = 0; return payload; } - - @Override - public boolean hasPayload() { - return payloadPending && payloadLength > 0; - } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java index 9f31ea092ab..a9aee513fc8 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldsReader.java @@ -472,18 +472,7 @@ class SimpleTextFieldsReader extends FieldsProducer { @Override public BytesRef getPayload() { - // Some tests rely on only being able to retrieve the - // payload once - try { - return payload; - } finally { - payload = null; - } - } - - @Override - public boolean hasPayload() { - return payload != null; + return payload; } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java index a5a3e70f9bb..40dba51ec0a 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java @@ -524,15 +524,9 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader { @Override public BytesRef getPayload() { - // assert hasPayload(); // you should have called this return payloads == null ? null : payloads[nextPos-1]; } - @Override - public boolean hasPayload() { - return payloads != null && payloads[nextPos-1] != null; - } - @Override public int nextPosition() { assert (positions != null && nextPos < positions.length) || diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java index 368fe7377df..34175419450 100644 --- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java +++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java @@ -830,11 +830,9 @@ public class CheckIndex { throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " < lastPos " + lastPos); } lastPos = pos; - if (postings.hasPayload()) { - BytesRef payload = postings.getPayload(); - if (payload.length < 1) { - throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " payload length is out of bounds " + payload.length); - } + BytesRef payload = postings.getPayload(); + if (payload != null && payload.length < 1) { + throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " payload length is out of bounds " + payload.length); } if (hasOffsets) { int startOffset = postings.startOffset(); @@ -1526,10 +1524,10 @@ public class CheckIndex { } } - BytesRef payload = null; - if (postings.hasPayload()) { + BytesRef payload = postings.getPayload(); + + if (payload != null) { assert vectorsHasPayload; - payload = postings.getPayload(); } if (postingsHasPayload && vectorsHasPayload) { @@ -1538,13 +1536,13 @@ public class CheckIndex { if (payload == null) { // we have payloads, but not at this position. // postings has payloads too, it should not have one at this position - if (postingsPostings.hasPayload()) { + if (postingsPostings.getPayload() != null) { throw new RuntimeException("vector term=" + term + " field=" + field + " doc=" + j + " has no payload but postings does: " + postingsPostings.getPayload()); } } else { // we have payloads, and one at this position // postings should also have one at this position, with the same bytes. - if (!postingsPostings.hasPayload()) { + if (postingsPostings.getPayload() == null) { throw new RuntimeException("vector term=" + term + " field=" + field + " doc=" + j + " has payload=" + payload + " but postings does not."); } BytesRef postingsPayload = postingsPostings.getPayload(); diff --git a/lucene/core/src/java/org/apache/lucene/index/DocsAndPositionsEnum.java b/lucene/core/src/java/org/apache/lucene/index/DocsAndPositionsEnum.java index d604439a169..0d2e84ecf63 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DocsAndPositionsEnum.java +++ b/lucene/core/src/java/org/apache/lucene/index/DocsAndPositionsEnum.java @@ -48,11 +48,8 @@ public abstract class DocsAndPositionsEnum extends DocsEnum { public abstract int endOffset() throws IOException; /** Returns the payload at this position, or null if no - * payload was indexed. Only call this once per - * position. You should not modify anything (neither - * members of the returned BytesRef nor bytes in the - * byte[]). */ + * payload was indexed. You should not modify anything + * (neither members of the returned BytesRef nor bytes + * in the byte[]). */ public abstract BytesRef getPayload() throws IOException; - - public abstract boolean hasPayload(); } diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java b/lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java index a6014ed1cd0..415f0c047e4 100644 --- a/lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/FilterAtomicReader.java @@ -307,11 +307,6 @@ public class FilterAtomicReader extends AtomicReader { public BytesRef getPayload() throws IOException { return in.getPayload(); } - - @Override - public boolean hasPayload() { - return in.hasPayload(); - } @Override public AttributeSource attributes() { diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java b/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java index 84ec2472409..062890a844d 100644 --- a/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java +++ b/lucene/core/src/java/org/apache/lucene/index/MultiDocsAndPositionsEnum.java @@ -137,11 +137,6 @@ public final class MultiDocsAndPositionsEnum extends DocsAndPositionsEnum { return current.endOffset(); } - @Override - public boolean hasPayload() { - return current.hasPayload(); - } - @Override public BytesRef getPayload() throws IOException { return current.getPayload(); diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java index 69b4ebcb8c2..88ce67ec68c 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java @@ -548,12 +548,7 @@ class UnionDocsAndPositionsEnum extends DocsAndPositionsEnum { @Override public BytesRef getPayload() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasPayload() { - throw new UnsupportedOperationException(); + return null; } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java index 1ac4dd4a94f..ee62c0d9164 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java @@ -118,8 +118,8 @@ public class PayloadTermQuery extends SpanTermQuery { } protected void processPayload(Similarity similarity) throws IOException { - final DocsAndPositionsEnum postings = termSpans.getPostings(); - if (postings.hasPayload()) { + if (termSpans.isPayloadAvailable()) { + final DocsAndPositionsEnum postings = termSpans.getPostings(); payload = postings.getPayload(); if (payload != null) { payloadScore = function.currentScore(doc, term.field(), diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java b/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java index f59e4c03396..db1cf88ea71 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java @@ -126,7 +126,7 @@ public class NearSpansUnordered extends Spans { // TODO: Remove warning after API has been finalized @Override - public boolean isPayloadAvailable() { + public boolean isPayloadAvailable() throws IOException { return spans.isPayloadAvailable(); } @@ -256,7 +256,7 @@ public class NearSpansUnordered extends Spans { // TODO: Remove warning after API has been finalized @Override - public boolean isPayloadAvailable() { + public boolean isPayloadAvailable() throws IOException { SpansCell pointer = min(); while (pointer != null) { if (pointer.isPayloadAvailable()) { diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java index 0258d137baf..2e50a2e6ecf 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java @@ -157,7 +157,7 @@ public class SpanNotQuery extends SpanQuery implements Cloneable { // TODO: Remove warning after API has been finalized @Override - public boolean isPayloadAvailable() { + public boolean isPayloadAvailable() throws IOException { return includeSpans.isPayloadAvailable(); } diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java index 3f00fc5e898..162a5d5d712 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java @@ -247,7 +247,7 @@ public class SpanOrQuery extends SpanQuery implements Cloneable { } @Override - public boolean isPayloadAvailable() { + public boolean isPayloadAvailable() throws IOException { Spans top = top(); return top != null && top.isPayloadAvailable(); } diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java index 052f5a6c748..65e8f53f8fe 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java @@ -176,7 +176,7 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Clonea // TODO: Remove warning after API has been finalized @Override - public boolean isPayloadAvailable() { + public boolean isPayloadAvailable() throws IOException { return spans.isPayloadAvailable(); } diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java b/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java index d0af3c4dcd6..4006b446b77 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java @@ -82,6 +82,6 @@ public abstract class Spans { * * @return true if there is a payload available at this position that can be loaded */ - public abstract boolean isPayloadAvailable(); + public abstract boolean isPayloadAvailable() throws IOException; } diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java b/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java index e109cbf31b2..314ca982d82 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java @@ -36,6 +36,7 @@ public class TermSpans extends Spans { protected int freq; protected int count; protected int position; + protected boolean readPayload; public TermSpans(DocsAndPositionsEnum postings, Term term) { this.postings = postings; @@ -64,6 +65,7 @@ public class TermSpans extends Spans { } position = postings.nextPosition(); count++; + readPayload = false; return true; } @@ -78,7 +80,7 @@ public class TermSpans extends Spans { count = 0; position = postings.nextPosition(); count++; - + readPayload = false; return true; } @@ -101,6 +103,7 @@ public class TermSpans extends Spans { @Override public Collection getPayload() throws IOException { final BytesRef payload = postings.getPayload(); + readPayload = true; final byte[] bytes; if (payload != null) { bytes = new byte[payload.length]; @@ -113,8 +116,8 @@ public class TermSpans extends Spans { // TODO: Remove warning after API has been finalized @Override - public boolean isPayloadAvailable() { - return postings.hasPayload(); + public boolean isPayloadAvailable() throws IOException { + return readPayload == false && postings.getPayload() != null; } @Override diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java index 5fd9e2d2da5..046472a023b 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java @@ -436,14 +436,14 @@ public class TestCodecs extends LuceneTestCase { final int pos = posEnum.nextPosition(); assertEquals(positions[i].pos, pos); if (positions[i].payload != null) { - assertTrue(posEnum.hasPayload()); + assertNotNull(posEnum.getPayload()); if (random().nextInt(3) < 2) { // Verify the payload bytes final BytesRef otherPayload = posEnum.getPayload(); assertTrue("expected=" + positions[i].payload.toString() + " got=" + otherPayload.toString(), positions[i].payload.equals(otherPayload)); } } else { - assertFalse(posEnum.hasPayload()); + assertNull(posEnum.getPayload()); } } } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java index 13d35d253b7..eade9abeef5 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDocumentWriter.java @@ -205,11 +205,11 @@ public class TestDocumentWriter extends LuceneTestCase { int freq = termPositions.freq(); assertEquals(3, freq); assertEquals(0, termPositions.nextPosition()); - assertEquals(true, termPositions.hasPayload()); + assertNotNull(termPositions.getPayload()); assertEquals(6, termPositions.nextPosition()); - assertEquals(false, termPositions.hasPayload()); + assertNull(termPositions.getPayload()); assertEquals(7, termPositions.nextPosition()); - assertEquals(false, termPositions.hasPayload()); + assertNull(termPositions.getPayload()); reader.close(); } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java b/lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java index 355b9087c27..e80fb62105e 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDuelingCodecs.java @@ -412,12 +412,9 @@ public class TestDuelingCodecs extends LuceneTestCase { assertEquals(info, freq, rightDocs.freq()); for (int i = 0; i < freq; i++) { assertEquals(info, leftDocs.nextPosition(), rightDocs.nextPosition()); - assertEquals(info, leftDocs.hasPayload(), rightDocs.hasPayload()); + assertEquals(info, leftDocs.getPayload(), rightDocs.getPayload()); assertEquals(info, leftDocs.startOffset(), rightDocs.startOffset()); assertEquals(info, leftDocs.endOffset(), rightDocs.endOffset()); - if (leftDocs.hasPayload()) { - assertEquals(info, leftDocs.getPayload(), rightDocs.getPayload()); - } } } assertEquals(info, DocIdSetIterator.NO_MORE_DOCS, rightDocs.nextDoc()); @@ -509,10 +506,7 @@ public class TestDuelingCodecs extends LuceneTestCase { assertEquals(info, freq, rightDocs.freq()); for (int i = 0; i < freq; i++) { assertEquals(info, leftDocs.nextPosition(), rightDocs.nextPosition()); - assertEquals(info, leftDocs.hasPayload(), rightDocs.hasPayload()); - if (leftDocs.hasPayload()) { - assertEquals(info, leftDocs.getPayload(), rightDocs.getPayload()); - } + assertEquals(info, leftDocs.getPayload(), rightDocs.getPayload()); } } } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestLongPostings.java b/lucene/core/src/test/org/apache/lucene/index/TestLongPostings.java index 2851177359e..e162097244b 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestLongPostings.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestLongPostings.java @@ -205,8 +205,11 @@ public class TestLongPostings extends LuceneTestCase { assertTrue(freq >=1 && freq <= 4); for(int pos=0;pos=1 && freq <= 4); for(int pos=0;pos 0; this.payloadLength = br.length; diff --git a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java index fe5893a94fa..80698c0e7d6 100644 --- a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java +++ b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java @@ -1015,11 +1015,6 @@ public class MemoryIndex { return stride == 1 ? -1 : positions.get((posUpto - 1) * stride + 2); } - @Override - public boolean hasPayload() { - return false; - } - @Override public BytesRef getPayload() { return null; diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java index 5809ac13fe6..03595474e15 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/ramonly/RAMOnlyPostingsFormat.java @@ -523,14 +523,13 @@ public class RAMOnlyPostingsFormat extends PostingsFormat { return -1; } - @Override - public boolean hasPayload() { - return current.payloads != null && current.payloads[posUpto-1] != null; - } - @Override public BytesRef getPayload() { - return new BytesRef(current.payloads[posUpto-1]); + if (current.payloads != null && current.payloads[posUpto-1] != null) { + return new BytesRef(current.payloads[posUpto-1]); + } else { + return null; + } } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java index 7f9a196c8bc..0d64888c5af 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java @@ -365,15 +365,9 @@ public class AssertingAtomicReader extends FilterAtomicReader { assert state != DocsEnumState.START : "getPayload() called before nextDoc()/advance()"; assert state != DocsEnumState.FINISHED : "getPayload() called after NO_MORE_DOCS"; assert positionCount > 0 : "getPayload() called before nextPosition()!"; - return super.getPayload(); - } - - @Override - public boolean hasPayload() { - assert state != DocsEnumState.START : "hasPayload() called before nextDoc()/advance()"; - assert state != DocsEnumState.FINISHED : "hasPayload() called after NO_MORE_DOCS"; - assert positionCount > 0 : "hasPayload() called before nextPosition()!"; - return super.hasPayload(); + BytesRef payload = super.getPayload(); + assert payload == null || payload.length > 0 : "getPayload() returned payload with invalid length!"; + return payload; } }