Skip to content

Apache Lucene open-source search software

sorted bytes writer

Robert Muira160df3321d7
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
index 3d9981e33aa..4523937fbbc 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSimpleDocValuesFormat.java
@@ -227,8 +227,66 @@ public class SimpleTextSimpleDocValuesFormat extends SimpleDocValuesFormat {
// nocommit
@Override
- public SortedDocValuesConsumer addSortedField(FieldInfo field, int valueCount, boolean fixedLength, int maxLength) throws IOException {
- return null; // nocommit
+ public SortedDocValuesConsumer addSortedField(FieldInfo field, int valueCount, boolean fixedLength, final int maxLength) throws IOException {
+ writeFieldEntry(field);
+ // write numValues
+ SimpleTextUtil.write(data, NUMVALUES);
+ SimpleTextUtil.write(data, Integer.toString(valueCount), scratch);
+ SimpleTextUtil.writeNewline(data);
+
+ // write maxLength
+ SimpleTextUtil.write(data, MAXLENGTH);
+ SimpleTextUtil.write(data, Integer.toString(maxLength), scratch);
+ SimpleTextUtil.writeNewline(data);
+
+ int maxBytesLength = Integer.toString(maxLength).length();
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < maxBytesLength; i++) {
+ sb.append('0');
+ }
+
+ // write our pattern for encoding lengths
+ SimpleTextUtil.write(data, PATTERN);
+ SimpleTextUtil.write(data, sb.toString(), scratch);
+ SimpleTextUtil.writeNewline(data);
+ final DecimalFormat encoder = new DecimalFormat(sb.toString(), new DecimalFormatSymbols(Locale.ROOT));
+
+ int maxOrdBytes = Integer.toString(valueCount).length();
+ sb.setLength(0);
+ for (int i = 0; i < maxOrdBytes; i++) {
+ sb.append('0');
+ }
+
+ // write our pattern for ords
+ SimpleTextUtil.write(data, ORDPATTERN);
+ SimpleTextUtil.write(data, sb.toString(), scratch);
+ SimpleTextUtil.writeNewline(data);
+ final DecimalFormat ordEncoder = new DecimalFormat(sb.toString(), new DecimalFormatSymbols(Locale.ROOT));
+
+ return new SortedDocValuesConsumer() {
+
+ @Override
+ public void addValue(BytesRef value) throws IOException {
+ // write length
+ SimpleTextUtil.write(data, LENGTH);
+ SimpleTextUtil.write(data, encoder.format(value.length), scratch);
+ SimpleTextUtil.writeNewline(data);
+
+ // write bytes
+ SimpleTextUtil.write(data, value);
+ // pad to fit
+ for (int i = value.length; i < maxLength; i++) {
+ data.writeByte((byte)' ');
+ }
+ SimpleTextUtil.writeNewline(data);
+ }
+
+ @Override
+ public void addDoc(int ord) throws IOException {
+ SimpleTextUtil.write(data, encoder.format(ord), scratch);
+ SimpleTextUtil.writeNewline(data);
+ }
+ };
}
/** write the header for this field */
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
index 9c80eb16344..e8b517b7432 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/SortedDocValuesConsumer.java
@@ -17,15 +17,17 @@ package org.apache.lucene.codecs;
* limitations under the License.
*/
+import java.io.IOException;
+
import org.apache.lucene.util.BytesRef;
public abstract class SortedDocValuesConsumer {
/** This is called, in value sort order, once per unique
* value. */
- public abstract void addValue(BytesRef value);
+ public abstract void addValue(BytesRef value) throws IOException;
/** This is called once per document after all values are
* added. */
- public abstract void addDoc(int ord);
+ public abstract void addDoc(int ord) throws IOException;
}