sorted bytes writer

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1407617 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-11-09 20:27:31 +00:00
parent 75b5ba898a
commit a160df3321
2 changed files with 64 additions and 4 deletions

View File

@ -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 */

View File

@ -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;
}