1 and carries no payload
+ */
+ public static class TermFreqPayloadIteratorWrapper implements TermFreqPayloadIterator {
+ private final BytesRefIterator wrapped;
+
+ /**
+ * Creates a new wrapper, wrapping the specified iterator and
+ * specifying a weight value of 1 for all terms
+ * and nullifies associated payloads.
+ */
+ public TermFreqPayloadIteratorWrapper(BytesRefIterator wrapped) {
+ this.wrapped = wrapped;
+ }
+
+ @Override
+ public long weight() {
+ return 1;
+ }
+
+ @Override
+ public BytesRef next() throws IOException {
+ return wrapped.next();
+ }
+
+ @Override
+ public BytesRef payload() {
+ return null;
+ }
+
+ @Override
+ public boolean hasPayloads() {
+ return false;
+ }
+ }
}
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/BufferingTermFreqIteratorWrapper.java lucene/suggest/src/java/org/apache/lucene/search/suggest/BufferingTermFreqIteratorWrapper.java
index 6228667..c1c16ae 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/BufferingTermFreqIteratorWrapper.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/BufferingTermFreqIteratorWrapper.java
@@ -18,7 +18,8 @@ package org.apache.lucene.search.suggest;
*/
import java.io.IOException;
-import org.apache.lucene.search.spell.TermFreqIterator;
+
+import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Counter;
@@ -27,22 +28,30 @@ import org.apache.lucene.util.Counter;
* This wrapper buffers incoming elements.
* @lucene.experimental
*/
-public class BufferingTermFreqIteratorWrapper implements TermFreqIterator {
+public class BufferingTermFreqIteratorWrapper implements TermFreqPayloadIterator {
// TODO keep this for now
/** buffered term entries */
protected BytesRefArray entries = new BytesRefArray(Counter.newCounter());
+ /** buffered payload entries */
+ protected BytesRefArray payloads = new BytesRefArray(Counter.newCounter());
/** current buffer position */
protected int curPos = -1;
/** buffered weights, parallel with {@link #entries} */
protected long[] freqs = new long[1];
private final BytesRef spare = new BytesRef();
+ private final BytesRef payloadSpare = new BytesRef();
+ private final boolean hasPayloads;
/** Creates a new iterator, buffering entries from the specified iterator */
- public BufferingTermFreqIteratorWrapper(TermFreqIterator source) throws IOException {
+ public BufferingTermFreqIteratorWrapper(TermFreqPayloadIterator source) throws IOException {
BytesRef spare;
int freqIndex = 0;
+ hasPayloads = source.hasPayloads();
while((spare = source.next()) != null) {
entries.append(spare);
+ if (hasPayloads) {
+ payloads.append(source.payload());
+ }
if (freqIndex >= freqs.length) {
freqs = ArrayUtil.grow(freqs, freqs.length+1);
}
@@ -64,4 +73,17 @@ public class BufferingTermFreqIteratorWrapper implements TermFreqIterator {
}
return null;
}
+
+ @Override
+ public BytesRef payload() {
+ if (hasPayloads && curPos < payloads.size()) {
+ return payloads.get(payloadSpare, curPos);
+ }
+ return null;
+ }
+
+ @Override
+ public boolean hasPayloads() {
+ return hasPayloads;
+ }
}
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
index 3519961..3a430c7 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/DocumentDictionary.java
@@ -169,6 +169,11 @@ public class DocumentDictionary implements Dictionary {
public BytesRef payload() {
return currentPayload;
}
+
+ @Override
+ public boolean hasPayloads() {
+ return withPayload;
+ }
}
}
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
index fa242ef..b03033b 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/FileDictionary.java
@@ -21,7 +21,7 @@ package org.apache.lucene.search.suggest;
import java.io.*;
import org.apache.lucene.search.spell.Dictionary;
-import org.apache.lucene.search.spell.TermFreqIterator;
+import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
@@ -57,11 +57,11 @@ public class FileDictionary implements Dictionary {
}
@Override
- public TermFreqIterator getWordsIterator() {
+ public TermFreqPayloadIterator getWordsIterator() {
return new FileIterator();
}
- final class FileIterator implements TermFreqIterator {
+ final class FileIterator implements TermFreqPayloadIterator {
private long curFreq;
private final BytesRef spare = new BytesRef();
@@ -98,5 +98,15 @@ public class FileDictionary implements Dictionary {
return null;
}
}
+
+ @Override
+ public BytesRef payload() {
+ return null;
+ }
+
+ @Override
+ public boolean hasPayloads() {
+ return false;
+ }
}
}
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java lucene/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java
index a1c64d3..2cd5377 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/Lookup.java
@@ -24,7 +24,7 @@ import java.util.Comparator;
import java.util.List;
import org.apache.lucene.search.spell.Dictionary;
-import org.apache.lucene.search.spell.TermFreqIterator;
+import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.PriorityQueue;
@@ -159,20 +159,20 @@ public abstract class Lookup {
*/
public void build(Dictionary dict) throws IOException {
BytesRefIterator it = dict.getWordsIterator();
- TermFreqIterator tfit;
- if (it instanceof TermFreqIterator) {
- tfit = (TermFreqIterator)it;
+ TermFreqPayloadIterator tfit;
+ if (it instanceof TermFreqPayloadIterator) {
+ tfit = (TermFreqPayloadIterator)it;
} else {
- tfit = new TermFreqIterator.TermFreqIteratorWrapper(it);
+ tfit = new TermFreqPayloadIterator.TermFreqPayloadIteratorWrapper(it);
}
build(tfit);
}
/**
- * Builds up a new internal {@link Lookup} representation based on the given {@link TermFreqIterator}.
+ * Builds up a new internal {@link Lookup} representation based on the given {@link TermFreqPayloadIterator}.
* The implementation might re-sort the data internally.
*/
- public abstract void build(TermFreqIterator tfit) throws IOException;
+ public abstract void build(TermFreqPayloadIterator tfit) throws IOException;
/**
* Look up a key and return possible completion for this key.
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java
index 53c4212..bf6207f 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedTermFreqIteratorWrapper.java
@@ -21,7 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Comparator;
-import org.apache.lucene.search.spell.TermFreqIterator;
+import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.search.suggest.Sort.ByteSequencesReader;
import org.apache.lucene.search.suggest.Sort.ByteSequencesWriter;
import org.apache.lucene.store.ByteArrayDataInput;
@@ -34,9 +34,9 @@ import org.apache.lucene.util.IOUtils;
* This wrapper buffers incoming elements and makes sure they are sorted based on given comparator.
* @lucene.experimental
*/
-public class SortedTermFreqIteratorWrapper implements TermFreqIterator {
+public class SortedTermFreqIteratorWrapper implements TermFreqPayloadIterator {
- private final TermFreqIterator source;
+ private final TermFreqPayloadIterator source;
private File tempInput;
private File tempSorted;
private final ByteSequencesReader reader;
@@ -50,7 +50,7 @@ public class SortedTermFreqIteratorWrapper implements TermFreqIterator {
* Creates a new sorted wrapper, using {@link
* BytesRef#getUTF8SortedAsUnicodeComparator} for
* sorting. */
- public SortedTermFreqIteratorWrapper(TermFreqIterator source) throws IOException {
+ public SortedTermFreqIteratorWrapper(TermFreqPayloadIterator source) throws IOException {
this(source, BytesRef.getUTF8SortedAsUnicodeComparator());
}
@@ -58,7 +58,10 @@ public class SortedTermFreqIteratorWrapper implements TermFreqIterator {
* Creates a new sorted wrapper, sorting by BytesRef
* (ascending) then cost (ascending).
*/
- public SortedTermFreqIteratorWrapper(TermFreqIterator source, ComparatorThis adapter differs from {@link FSTCompletion} in that it attempts
- * to discretize any "weights" as passed from in {@link TermFreqIterator#weight()}
+ * to discretize any "weights" as passed from in {@link TermFreqPayloadIterator#weight()}
* to match the number of buckets. For the rationale for bucketing, see
* {@link FSTCompletion}.
*
@@ -96,7 +96,7 @@ public class FSTCompletionLookup extends Lookup {
/**
* This constructor prepares for creating a suggested FST using the
- * {@link #build(TermFreqIterator)} method. The number of weight
+ * {@link #build(TermFreqPayloadIterator)} method. The number of weight
* discretization buckets is set to {@link FSTCompletion#DEFAULT_BUCKETS} and
* exact matches are promoted to the top of the suggestions list.
*/
@@ -106,7 +106,7 @@ public class FSTCompletionLookup extends Lookup {
/**
* This constructor prepares for creating a suggested FST using the
- * {@link #build(TermFreqIterator)} method.
+ * {@link #build(TermFreqPayloadIterator)} method.
*
* @param buckets
* The number of weight discretization buckets (see
@@ -141,8 +141,8 @@ public class FSTCompletionLookup extends Lookup {
}
@Override
- public void build(TermFreqIterator tfit) throws IOException {
- if (tfit instanceof TermFreqPayloadIterator) {
+ public void build(TermFreqPayloadIterator tfit) throws IOException {
+ if (tfit.hasPayloads()) {
throw new IllegalArgumentException("this suggester doesn't support payloads");
}
File tempInput = File.createTempFile(
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java
index f634bee..6c252dc 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/WFSTCompletionLookup.java
@@ -25,7 +25,6 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
-import org.apache.lucene.search.spell.TermFreqIterator;
import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.search.suggest.Lookup;
import org.apache.lucene.search.suggest.Sort.ByteSequencesWriter;
@@ -93,12 +92,12 @@ public class WFSTCompletionLookup extends Lookup {
}
@Override
- public void build(TermFreqIterator iterator) throws IOException {
- if (iterator instanceof TermFreqPayloadIterator) {
+ public void build(TermFreqPayloadIterator iterator) throws IOException {
+ if (iterator.hasPayloads()) {
throw new IllegalArgumentException("this suggester doesn't support payloads");
}
BytesRef scratch = new BytesRef();
- TermFreqIterator iter = new WFSTTermFreqIteratorWrapper(iterator);
+ TermFreqPayloadIterator iter = new WFSTTermFreqIteratorWrapper(iterator);
IntsRef scratchInts = new IntsRef();
BytesRef previous = null;
PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton();
@@ -257,7 +256,7 @@ public class WFSTCompletionLookup extends Lookup {
private final class WFSTTermFreqIteratorWrapper extends SortedTermFreqIteratorWrapper {
- WFSTTermFreqIteratorWrapper(TermFreqIterator source) throws IOException {
+ WFSTTermFreqIteratorWrapper(TermFreqPayloadIterator source) throws IOException {
super(source);
}
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java lucene/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java
index 558e115..6ec5f9b 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/jaspell/JaspellLookup.java
@@ -47,13 +47,13 @@ public class JaspellLookup extends Lookup {
/**
* Creates a new empty trie
- * @see #build(TermFreqIterator)
+ * @see #build(TermFreqPayloadIterator)
* */
public JaspellLookup() {}
@Override
- public void build(TermFreqIterator tfit) throws IOException {
- if (tfit instanceof TermFreqPayloadIterator) {
+ public void build(TermFreqPayloadIterator tfit) throws IOException {
+ if (tfit.hasPayloads()) {
throw new IllegalArgumentException("this suggester doesn't support payloads");
}
trie = new JaspellTernarySearchTrie();
diff --git lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
index 852ebb5..b9cc6fb 100644
--- lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
+++ lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java
@@ -46,13 +46,13 @@ public class TSTLookup extends Lookup {
/**
* Creates a new TSTLookup with an empty Ternary Search Tree.
- * @see #build(TermFreqIterator)
+ * @see #build(TermFreqPayloadIterator)
*/
public TSTLookup() {}
@Override
- public void build(TermFreqIterator tfit) throws IOException {
- if (tfit instanceof TermFreqPayloadIterator) {
+ public void build(TermFreqPayloadIterator tfit) throws IOException {
+ if (tfit.hasPayloads()) {
throw new IllegalArgumentException("this suggester doesn't support payloads");
}
root = new TernaryTreeNode();
diff --git lucene/suggest/src/test/org/apache/lucene/search/suggest/TermFreqArrayIterator.java lucene/suggest/src/test/org/apache/lucene/search/suggest/TermFreqArrayIterator.java
index d77fa5c..2845da1 100644
--- lucene/suggest/src/test/org/apache/lucene/search/suggest/TermFreqArrayIterator.java
+++ lucene/suggest/src/test/org/apache/lucene/search/suggest/TermFreqArrayIterator.java
@@ -21,12 +21,13 @@ import java.util.Arrays;
import java.util.Iterator;
import org.apache.lucene.search.spell.TermFreqIterator;
+import org.apache.lucene.search.spell.TermFreqPayloadIterator;
import org.apache.lucene.util.BytesRef;
/**
- * A {@link TermFreqIterator} over a sequence of {@link TermFreq}s.
+ * A {@link TermFreqPayloadIterator} over a sequence of {@link TermFreq}s.
*/
-public final class TermFreqArrayIterator implements TermFreqIterator {
+public final class TermFreqArrayIterator implements TermFreqPayloadIterator {
private final Iterator