Apache Solr open-source search software
var straight bytes 4.0 impl
Robert Muirfff871c2454a
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesFormat.javaindex 2eaac5935bf..6c5c417683e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesFormat.java@@ -69,4 +69,10 @@ public class Lucene40DocValuesFormat extends DocValuesFormat {static final String BYTES_FIXED_STRAIGHT_CODEC_NAME = "FixedStraightBytes";static final int BYTES_FIXED_STRAIGHT_VERSION_START = 0;static final int BYTES_FIXED_STRAIGHT_VERSION_CURRENT = BYTES_FIXED_STRAIGHT_VERSION_START;++ // constants for BYTES_VAR_STRAIGHT+ static final String BYTES_VAR_STRAIGHT_CODEC_NAME_IDX = "VarStraightBytesIdx";+ static final String BYTES_VAR_STRAIGHT_CODEC_NAME_DAT = "VarStraightBytesDat";+ static final int BYTES_VAR_STRAIGHT_VERSION_START = 0;+ static final int BYTES_VAR_STRAIGHT_VERSION_CURRENT = BYTES_VAR_STRAIGHT_VERSION_START;}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesReader.javaindex b2352ff06cf..f6016f6dc77 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesReader.java@@ -272,6 +272,9 @@ class Lucene40DocValuesReader extends DocValuesProducer {case BYTES_FIXED_STRAIGHT:instance = loadBytesFixedStraight(field);break;+ case BYTES_VAR_STRAIGHT:+ instance = loadBytesVarStraight(field);+ break;default:throw new AssertionError();}@@ -292,6 +295,7 @@ class Lucene40DocValuesReader extends DocValuesProducer {// nocommit? can the current impl even handle > 2G?final byte bytes[] = new byte[state.segmentInfo.getDocCount() * fixedLength];input.readBytes(bytes, 0, bytes.length);+ success = true;return new BinaryDocValues() {@Overridepublic void get(int docID, BytesRef result) {@@ -308,6 +312,46 @@ class Lucene40DocValuesReader extends DocValuesProducer {}}}++ private BinaryDocValues loadBytesVarStraight(FieldInfo field) throws IOException {+ String dataName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "dat");+ String indexName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "idx");+ IndexInput data = null;+ IndexInput index = null;+ boolean success = false;+ try {+ data = dir.openInput(dataName, state.context);+ CodecUtil.checkHeader(data, Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_CODEC_NAME_DAT,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_START,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_CURRENT);+ index = dir.openInput(indexName, state.context);+ CodecUtil.checkHeader(index, Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_CODEC_NAME_IDX,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_START,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_CURRENT);+ // nocommit? can the current impl even handle > 2G?+ long totalBytes = index.readVLong();+ final byte bytes[] = new byte[(int)totalBytes];+ data.readBytes(bytes, 0, bytes.length);+ final PackedInts.Reader reader = PackedInts.getReader(index);+ success = true;+ return new BinaryDocValues() {+ @Override+ public void get(int docID, BytesRef result) {+ long startAddress = reader.get(docID);+ long endAddress = reader.get(docID+1);+ result.bytes = bytes;+ result.offset = (int)startAddress;+ result.length = (int)(endAddress - startAddress);+ }+ };+ } finally {+ if (success) {+ IOUtils.close(data, index);+ } else {+ IOUtils.closeWhileHandlingException(data, index);+ }+ }+ }@Overridepublic synchronized SortedDocValues getSorted(FieldInfo field) throws IOException {diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.javaindex 7f60763e68e..7d8bd1729dc 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesWriter.java@@ -149,6 +149,7 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {@Overridepublic void addBinaryField(FieldInfo field, Iterable<BytesRef> values) throws IOException {// examine the values to determine best type to use+ // TODO: would be cool to write the deref types in this impersonator tooint minLength = Integer.MAX_VALUE;int maxLength = Integer.MIN_VALUE;for (BytesRef b : values) {@@ -172,8 +173,24 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {}}} else {- // not yet- assert false;+ // variable byte[]+ boolean success = false;+ IndexOutput data = null;+ IndexOutput index = null;+ String dataName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "dat");+ String indexName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "idx");+ try {+ data = dir.createOutput(dataName, state.context);+ index = dir.createOutput(indexName, state.context);+ addVarStraightBytesField(field, data, index, values);+ success = true;+ } finally {+ if (success) {+ IOUtils.close(data, index);+ } else {+ IOUtils.closeWhileHandlingException(data, index);+ }+ }}}@@ -189,6 +206,42 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {output.writeBytes(v.bytes, v.offset, v.length);}}++ // NOTE: 4.0 file format docs are crazy/wrong here...+ private void addVarStraightBytesField(FieldInfo field, IndexOutput data, IndexOutput index, Iterable<BytesRef> values) throws IOException {+ field.putAttribute(legacyKey, LegacyDocValuesType.BYTES_VAR_STRAIGHT.name());++ CodecUtil.writeHeader(data,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_CODEC_NAME_DAT,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_CURRENT);++ CodecUtil.writeHeader(index,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_CODEC_NAME_IDX,+ Lucene40DocValuesFormat.BYTES_VAR_STRAIGHT_VERSION_CURRENT);++ final long startPos = data.getFilePointer();++ for (BytesRef v : values) {+ data.writeBytes(v.bytes, v.offset, v.length);+ }++ final long maxAddress = data.getFilePointer() - startPos;+ index.writeVLong(maxAddress);++ final int maxDoc = state.segmentInfo.getDocCount();+ assert maxDoc != Integer.MAX_VALUE; // unsupported by the 4.0 impl++ final PackedInts.Writer w = PackedInts.getWriter(index, maxDoc+1, PackedInts.bitsRequired(maxAddress), PackedInts.DEFAULT);+ long currentPosition = 0;+ for (BytesRef v : values) {+ w.add(currentPosition);+ currentPosition += v.length;+ }+ // write sentinel+ assert currentPosition == maxAddress;+ w.add(currentPosition);+ w.finish();+ }@Overridepublic void addSortedField(FieldInfo field, Iterable<BytesRef> values, Iterable<Number> docToOrd) throws IOException {diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.javaindex 8678531afd7..2a62a58b948 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java@@ -33,6 +33,7 @@ public final class Lucene40RWCodec extends Lucene40Codec {}};+ //private final DocValuesFormat docValues = new Lucene40RWDocValuesFormat();private final DocValuesFormat docValues = new Lucene40LyingRWDocValuesFormat();private final NormsFormat norms = new Lucene40RWNormsFormat();