Apache Solr open-source search software
add 4.0 FIXED_DEREF
Robert Muirb8c2238a46ff
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 0142bf68f14..63fc60fb7f2 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@@ -75,6 +75,18 @@ public class Lucene40DocValuesFormat extends DocValuesFormat {static final int BYTES_VAR_STRAIGHT_VERSION_START = 0;static final int BYTES_VAR_STRAIGHT_VERSION_CURRENT = BYTES_VAR_STRAIGHT_VERSION_START;+ // constants for BYTES_FIXED_DEREF+ static final String BYTES_FIXED_DEREF_CODEC_NAME_IDX = "FixedDerefBytesIdx";+ static final String BYTES_FIXED_DEREF_CODEC_NAME_DAT = "FixedDerefBytesDat";+ static final int BYTES_FIXED_DEREF_VERSION_START = 0;+ static final int BYTES_FIXED_DEREF_VERSION_CURRENT = BYTES_FIXED_DEREF_VERSION_START;++ // constants for BYTES_VAR_DEREF+ static final String BYTES_VAR_DEREF_CODEC_NAME_IDX = "VarDerefBytesIdx";+ static final String BYTES_VAR_DEREF_CODEC_NAME_DAT = "VarDerefBytesDat";+ static final int BYTES_VAR_DEREF_VERSION_START = 0;+ static final int BYTES_VAR_DEREF_VERSION_CURRENT = BYTES_VAR_DEREF_VERSION_START;+// constants for BYTES_FIXED_SORTEDstatic final String BYTES_FIXED_SORTED_CODEC_NAME_IDX = "FixedSortedBytesIdx";static final String BYTES_FIXED_SORTED_CODEC_NAME_DAT = "FixedSortedBytesDat";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 ee9de30c640..3e2c2e1f506 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@@ -275,8 +275,14 @@ class Lucene40DocValuesReader extends DocValuesProducer {case BYTES_VAR_STRAIGHT:instance = loadBytesVarStraight(field);break;+ case BYTES_FIXED_DEREF:+ instance = loadBytesFixedDeref(field);+ break;+ case BYTES_VAR_DEREF:+ instance = loadBytesVarDeref(field);+ break;default:- throw new AssertionError(); // nocommit+ throw new AssertionError();}binaryInstances.put(field.number, instance);}@@ -352,6 +358,51 @@ class Lucene40DocValuesReader extends DocValuesProducer {}}}++ private BinaryDocValues loadBytesFixedDeref(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_FIXED_DEREF_CODEC_NAME_DAT,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_START,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT);+ index = dir.openInput(indexName, state.context);+ CodecUtil.checkHeader(index, Lucene40DocValuesFormat.BYTES_FIXED_DEREF_CODEC_NAME_IDX,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_START,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT);++ final int fixedLength = data.readInt();+ final int valueCount = index.readInt();+ // nocommit? can the current impl even handle > 2G?+ final byte bytes[] = new byte[fixedLength * valueCount];+ 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) {+ int ord = (int)reader.get(docID);+ result.bytes = bytes;+ result.offset = ord * fixedLength;+ result.length = fixedLength;+ }+ };+ } finally {+ if (success) {+ IOUtils.close(data, index);+ } else {+ IOUtils.closeWhileHandlingException(data, index);+ }+ }+ }++ private BinaryDocValues loadBytesVarDeref(FieldInfo field) throws IOException {+ throw new AssertionError(); // nocommit+ }@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 a2f0891cd35..1ddc752da03 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@@ -18,6 +18,8 @@ package org.apache.lucene.codecs.lucene40;*/import java.io.IOException;+import java.util.HashSet;+import java.util.TreeSet;import org.apache.lucene.codecs.CodecUtil;import org.apache.lucene.codecs.DocValuesConsumer;@@ -149,31 +151,28 @@ 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 too+ HashSet<BytesRef> uniqueValues = new HashSet<BytesRef>();int minLength = Integer.MAX_VALUE;int maxLength = Integer.MIN_VALUE;for (BytesRef b : values) {minLength = Math.min(minLength, b.length);maxLength = Math.max(maxLength, b.length);- }-- if (minLength == maxLength) {- // fixed byte[]- String fileName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "dat");- IndexOutput data = dir.createOutput(fileName, state.context);- boolean success = false;- try {- addFixedStraightBytesField(field, data, values, minLength);- success = true;- } finally {- if (success) {- IOUtils.close(data);- } else {- IOUtils.closeWhileHandlingException(data);+ if (uniqueValues != null) {+ if (uniqueValues.add(BytesRef.deepCopyOf(b))) {+ if (uniqueValues.size() > 256) {+ uniqueValues = null;+ }}}- } else {- // variable byte[]+ }++ int maxDoc = state.segmentInfo.getDocCount();+ final boolean fixed = minLength == maxLength;+ // nocommit+ final boolean dedup = fixed && (uniqueValues != null && uniqueValues.size() * 2 < maxDoc);++ if (dedup) {+ // we will deduplicate and deref valuesboolean success = false;IndexOutput data = null;IndexOutput index = null;@@ -182,7 +181,11 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {try {data = dir.createOutput(dataName, state.context);index = dir.createOutput(indexName, state.context);- addVarStraightBytesField(field, data, index, values);+ if (fixed) {+ addFixedDerefBytesField(field, data, index, values, minLength);+ } else {+ assert false; // nocommit+ }success = true;} finally {if (success) {@@ -191,6 +194,43 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {IOUtils.closeWhileHandlingException(data, index);}}+ } else {+ // we dont deduplicate, just write values straight+ if (fixed) {+ // fixed byte[]+ String fileName = IndexFileNames.segmentFileName(state.segmentInfo.name, Integer.toString(field.number), "dat");+ IndexOutput data = dir.createOutput(fileName, state.context);+ boolean success = false;+ try {+ addFixedStraightBytesField(field, data, values, minLength);+ success = true;+ } finally {+ if (success) {+ IOUtils.close(data);+ } else {+ IOUtils.closeWhileHandlingException(data);+ }+ }+ } else {+ // 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);+ }+ }+ }}}@@ -246,6 +286,43 @@ class Lucene40DocValuesWriter extends DocValuesConsumer {w.add(currentPosition);w.finish();}++ private void addFixedDerefBytesField(FieldInfo field, IndexOutput data, IndexOutput index, Iterable<BytesRef> values, int length) throws IOException {+ field.putAttribute(legacyKey, LegacyDocValuesType.BYTES_FIXED_DEREF.name());++ CodecUtil.writeHeader(data,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_CODEC_NAME_DAT,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT);++ CodecUtil.writeHeader(index,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_CODEC_NAME_IDX,+ Lucene40DocValuesFormat.BYTES_FIXED_DEREF_VERSION_CURRENT);++ // deduplicate+ TreeSet<BytesRef> dictionary = new TreeSet<BytesRef>();+ for (BytesRef v : values) {+ dictionary.add(BytesRef.deepCopyOf(v));+ }++ /* values */+ data.writeInt(length);+ for (BytesRef v : dictionary) {+ data.writeBytes(v.bytes, v.offset, v.length);+ }++ /* ordinals */+ int valueCount = dictionary.size();+ assert valueCount > 0;+ index.writeInt(valueCount);+ final int maxDoc = state.segmentInfo.getDocCount();+ final PackedInts.Writer w = PackedInts.getWriter(index, maxDoc, PackedInts.bitsRequired(valueCount-1), PackedInts.DEFAULT);++ for (BytesRef v : values) {+ int ord = dictionary.headSet(v).size();+ w.add(ord);+ }+ w.finish();+ }@Overridepublic void addSortedField(FieldInfo field, Iterable<BytesRef> values, Iterable<Number> docToOrd) throws IOException {