LUCENE-5555: Fix SortedInputIterator to correctly encode/decode contexts in presence of payload

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1582473 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Areek Zillur 2014-03-27 21:00:40 +00:00
parent 6afdd45556
commit 580ef3bb4e
3 changed files with 25 additions and 5 deletions

View File

@ -210,6 +210,8 @@ Bug fixes
* LUCENE-5111: Fix WordDelimiterFilter to return offsets in correct order. (Robert Muir)
* LUCENE-5555: Fix SortedInputIterator to correctly encode/decode contexts in presence of payload (Areek Zillur)
Test Framework
* LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _.

View File

@ -209,7 +209,7 @@ public class SortedInputIterator implements InputIterator {
}
}
/** encodes an entry (bytes+(payload)+(contexts)+weight) to the provided writer */
/** encodes an entry (bytes+(contexts)+(payload)+weight) to the provided writer */
protected void encode(ByteSequencesWriter writer, ByteArrayDataOutput output, byte[] buffer, BytesRef spare, BytesRef payload, Set<BytesRef> contexts, long weight) throws IOException {
int requiredLength = spare.length + 8 + ((hasPayloads) ? 2 + payload.length : 0);
if (hasContexts) {
@ -223,10 +223,6 @@ public class SortedInputIterator implements InputIterator {
}
output.reset(buffer);
output.writeBytes(spare.bytes, spare.offset, spare.length);
if (hasPayloads) {
output.writeBytes(payload.bytes, payload.offset, payload.length);
output.writeShort((short) payload.length);
}
if (hasContexts) {
for (BytesRef ctx : contexts) {
output.writeBytes(ctx.bytes, ctx.offset, ctx.length);
@ -234,6 +230,10 @@ public class SortedInputIterator implements InputIterator {
}
output.writeShort((short) contexts.size());
}
if (hasPayloads) {
output.writeBytes(payload.bytes, payload.offset, payload.length);
output.writeShort((short) payload.length);
}
output.writeLong(weight);
writer.write(buffer, 0, output.getPosition());
}

View File

@ -48,9 +48,11 @@ public class TestInputIterator extends LuceneTestCase {
TreeMap<BytesRef, SimpleEntry<Long, BytesRef>> sorted = new TreeMap<>(comparator);
TreeMap<BytesRef, Long> sortedWithoutPayload = new TreeMap<>(comparator);
TreeMap<BytesRef, SimpleEntry<Long, Set<BytesRef>>> sortedWithContext = new TreeMap<>(comparator);
TreeMap<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>> sortedWithPayloadAndContext = new TreeMap<>(comparator);
Input[] unsorted = new Input[num];
Input[] unsortedWithoutPayload = new Input[num];
Input[] unsortedWithContexts = new Input[num];
Input[] unsortedWithPayloadAndContext = new Input[num];
Set<BytesRef> ctxs;
for (int i = 0; i < num; i++) {
BytesRef key;
@ -67,9 +69,11 @@ public class TestInputIterator extends LuceneTestCase {
sortedWithoutPayload.put(key, value);
sorted.put(key, new SimpleEntry<>(value, payload));
sortedWithContext.put(key, new SimpleEntry<>(value, ctxs));
sortedWithPayloadAndContext.put(key, new SimpleEntry<>(value, new SimpleEntry<>(payload, ctxs)));
unsorted[i] = new Input(key, value, payload);
unsortedWithoutPayload[i] = new Input(key, value);
unsortedWithContexts[i] = new Input(key, value, ctxs);
unsortedWithPayloadAndContext[i] = new Input(key, value, payload, ctxs);
}
// test the sorted iterator wrapper with payloads
@ -96,6 +100,20 @@ public class TestInputIterator extends LuceneTestCase {
}
assertNull(wrapper.next());
// test the sorted iterator wrapper with contexts and payload
wrapper = new SortedInputIterator(new InputArrayIterator(unsortedWithPayloadAndContext), comparator);
Iterator<Map.Entry<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>>> expectedPayloadContextEntries = sortedWithPayloadAndContext.entrySet().iterator();
while (expectedPayloadContextEntries.hasNext()) {
Map.Entry<BytesRef, SimpleEntry<Long, SimpleEntry<BytesRef, Set<BytesRef>>>> entry = expectedPayloadContextEntries.next();
assertEquals(entry.getKey(), wrapper.next());
assertEquals(entry.getValue().getKey().longValue(), wrapper.weight());
Set<BytesRef> actualCtxs = entry.getValue().getValue().getValue();
assertEquals(actualCtxs, wrapper.contexts());
BytesRef actualPayload = entry.getValue().getValue().getKey();
assertEquals(actualPayload, wrapper.payload());
}
assertNull(wrapper.next());
// test the unsorted iterator wrapper with payloads
wrapper = new UnsortedInputIterator(new InputArrayIterator(unsorted));
TreeMap<BytesRef, SimpleEntry<Long, BytesRef>> actual = new TreeMap<>();