Apache Lucene open-source search software
LUCENE-4055: add codec-private metadata to fieldinfos
Robert Muir7f91241b3750
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xFieldInfosReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xFieldInfosReader.javaindex 8a2bb21646c..86f3735bcd8 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xFieldInfosReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xFieldInfosReader.java@@ -100,7 +100,7 @@ class Lucene3xFieldInfosReader extends FieldInfosReader {storePayloads = false;}infos[i] = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector,- omitNorms, storePayloads, indexOptions, null, isIndexed && !omitNorms? Type.FIXED_INTS_8 : null);+ omitNorms, storePayloads, indexOptions, null, isIndexed && !omitNorms? Type.FIXED_INTS_8 : null, null);}if (input.getFilePointer() != input.length()) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosFormat.javaindex 2ef98583761..de203213a38 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosFormat.java@@ -33,13 +33,14 @@ import org.apache.lucene.store.DataOutput; // javadoc* <p>* <p>Field names are stored in the field info file, with suffix <tt>.fnm</tt>.</p>* <p>FieldInfos (.fnm) --> FNMVersion,FieldsCount, <FieldName,FieldNumber,- * FieldBits,DocValuesBits> <sup>FieldsCount</sup></p>+ * FieldBits,DocValuesBits,Attributes> <sup>FieldsCount</sup></p>* <p>Data types:* <ul>* <li>FNMVersion, FieldsCount --> {@link DataOutput#writeVInt VInt}</li>* <li>FieldName --> {@link DataOutput#writeString String}</li>* <li>FieldBits, DocValuesBits --> {@link DataOutput#writeByte Byte}</li>- * <li>FieldNumber --> {@link DataOutput#writeInt Uint32}</li>+ * <li>FieldNumber --> {@link DataOutput#writeInt VInt}</li>+ * <li>Attributes --> {@link DataOutput#writeStringStringMap Map<String,String>}</li>* </ul>* </p>* Field Descriptions:diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.javaindex 1030e2e7528..f79ce9ed72c 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java@@ -1,6 +1,7 @@package org.apache.lucene.codecs.lucene40;import java.io.IOException;+import java.util.Map;import java.util.Set;import org.apache.lucene.codecs.FieldInfosReader;@@ -64,7 +65,7 @@ public class Lucene40FieldInfosReader extends FieldInfosReader {for (int i = 0; i < size; i++) {String name = input.readString();- final int fieldNumber = input.readInt();+ final int fieldNumber = input.readVInt();byte bits = input.readByte();boolean isIndexed = (bits & Lucene40FieldInfosWriter.IS_INDEXED) != 0;boolean storeTermVector = (bits & Lucene40FieldInfosWriter.STORE_TERMVECTOR) != 0;@@ -91,8 +92,9 @@ public class Lucene40FieldInfosReader extends FieldInfosReader {byte val = input.readByte();final DocValues.Type docValuesType = getDocValuesType((byte) (val & 0x0F));final DocValues.Type normsType = getDocValuesType((byte) ((val >>> 4) & 0x0F));+ final Map<String,String> attributes = input.readStringStringMap();infos[i] = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector,- omitNorms, storePayloads, indexOptions, docValuesType, normsType);+ omitNorms, storePayloads, indexOptions, docValuesType, normsType, attributes);}if (input.getFilePointer() != input.length()) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.javaindex 4df940676c4..f5bc7870b3d 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java@@ -76,7 +76,7 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter {bits |= OMIT_POSITIONS;}output.writeString(fi.name);- output.writeInt(fi.number);+ output.writeVInt(fi.number);output.writeByte(bits);// pack the DV types in one byte@@ -85,6 +85,7 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter {assert (dv & (~0xF)) == 0 && (nrm & (~0x0F)) == 0;byte val = (byte) (0xff & ((nrm << 4) | dv));output.writeByte(val);+ output.writeStringStringMap(fi.attributes());}} finally {output.close();diff --git a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.javaindex f720656acc8..f93649d4d7e 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java@@ -18,14 +18,14 @@ package org.apache.lucene.codecs.simpletext;*/import java.io.IOException;-import java.util.Set;+import java.util.HashMap;+import java.util.Map;import org.apache.lucene.codecs.FieldInfosReader;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.FieldInfo;import org.apache.lucene.index.FieldInfos;import org.apache.lucene.index.IndexFileNames;-import org.apache.lucene.index.SegmentInfo;import org.apache.lucene.index.FieldInfo.IndexOptions;import org.apache.lucene.index.DocValues;import org.apache.lucene.store.Directory;@@ -93,14 +93,28 @@ public class SimpleTextFieldInfosReader extends FieldInfosReader {String dvType = readString(DOCVALUES.length, scratch);final DocValues.Type docValuesType = docValuesType(dvType);--SimpleTextUtil.readLine(input, scratch);assert StringHelper.startsWith(scratch, INDEXOPTIONS);IndexOptions indexOptions = IndexOptions.valueOf(readString(INDEXOPTIONS.length, scratch));+ SimpleTextUtil.readLine(input, scratch);+ assert StringHelper.startsWith(scratch, NUM_ATTS);+ int numAtts = Integer.parseInt(readString(NUM_ATTS.length, scratch));+ Map<String,String> atts = new HashMap<String,String>();++ for (int j = 0; j < numAtts; j++) {+ SimpleTextUtil.readLine(input, scratch);+ assert StringHelper.startsWith(scratch, ATT_KEY);+ String key = readString(ATT_KEY.length, scratch);++ SimpleTextUtil.readLine(input, scratch);+ assert StringHelper.startsWith(scratch, ATT_VALUE);+ String value = readString(ATT_VALUE.length, scratch);+ atts.put(key, value);+ }+infos[i] = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector,- omitNorms, storePayloads, indexOptions, docValuesType, normsType);+ omitNorms, storePayloads, indexOptions, docValuesType, normsType, atts);}if (input.getFilePointer() != input.length()) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.javaindex ad83e7c7db4..be618d612f9 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java@@ -17,6 +17,7 @@ package org.apache.lucene.codecs.simpletext;* limitations under the License.*/import java.io.IOException;+import java.util.Map;import org.apache.lucene.codecs.FieldInfosWriter;import org.apache.lucene.index.DocValues;@@ -52,6 +53,9 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter {static final BytesRef NORMS_TYPE = new BytesRef(" norms type ");static final BytesRef DOCVALUES = new BytesRef(" doc values ");static final BytesRef INDEXOPTIONS = new BytesRef(" index options ");+ static final BytesRef NUM_ATTS = new BytesRef(" attributes ");+ final static BytesRef ATT_KEY = new BytesRef(" key ");+ final static BytesRef ATT_VALUE = new BytesRef(" value ");@Overridepublic void write(Directory directory, String segmentName, FieldInfos infos, IOContext context) throws IOException {@@ -101,6 +105,24 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter {SimpleTextUtil.write(out, INDEXOPTIONS);SimpleTextUtil.write(out, fi.getIndexOptions().toString(), scratch);SimpleTextUtil.writeNewline(out);++ Map<String,String> atts = fi.attributes();+ int numAtts = atts == null ? 0 : atts.size();+ SimpleTextUtil.write(out, NUM_ATTS);+ SimpleTextUtil.write(out, Integer.toString(numAtts), scratch);+ SimpleTextUtil.writeNewline(out);++ if (numAtts > 0) {+ for (Map.Entry<String,String> entry : atts.entrySet()) {+ SimpleTextUtil.write(out, ATT_KEY);+ SimpleTextUtil.write(out, entry.getKey(), scratch);+ SimpleTextUtil.writeNewline(out);++ SimpleTextUtil.write(out, ATT_VALUE);+ SimpleTextUtil.write(out, entry.getValue(), scratch);+ SimpleTextUtil.writeNewline(out);+ }+ }}} finally {out.close();diff --git a/lucene/core/src/java/org/apache/lucene/index/DocFieldProcessor.java b/lucene/core/src/java/org/apache/lucene/index/DocFieldProcessor.javaindex e538c2becf1..133692e3962 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocFieldProcessor.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocFieldProcessor.java@@ -83,6 +83,9 @@ final class DocFieldProcessor extends DocConsumer {for (DocValuesConsumerAndDocID consumer : docValues.values()) {consumer.docValuesConsumer.finish(state.numDocs);}++ // close perDocConsumer during flush to ensure all files are flushed due to PerCodec CFS+ IOUtils.close(perDocConsumer);// Important to save after asking consumer to flush so// consumer can alter the FieldInfo* if necessary. EG,@@ -90,9 +93,6 @@ final class DocFieldProcessor extends DocConsumer {// FieldInfo.storePayload.FieldInfosWriter infosWriter = codec.fieldInfosFormat().getFieldInfosWriter();infosWriter.write(state.directory, state.segmentName, state.fieldInfos, IOContext.DEFAULT);-- // close perDocConsumer during flush to ensure all files are flushed due to PerCodec CFS- IOUtils.close(perDocConsumer);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfo.javaindex ab23d0d8727..9a3f8863b7a 100644--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfo.java@@ -17,6 +17,9 @@ package org.apache.lucene.index;* limitations under the License.*/+import java.util.HashMap;+import java.util.Map;+import org.apache.lucene.index.DocValues.Type;/**@@ -42,6 +45,8 @@ public final class FieldInfo {private IndexOptions indexOptions;private boolean storePayloads; // whether this field stores payloads together with term positions+ private Map<String,String> attributes;+/*** Controls how much information is stored in the postings lists.* @lucene.experimental@@ -65,7 +70,7 @@ public final class FieldInfo {* @lucene.experimental*/public FieldInfo(String name, boolean indexed, int number, boolean storeTermVector,- boolean omitNorms, boolean storePayloads, IndexOptions indexOptions, DocValues.Type docValues, DocValues.Type normsType) {+ boolean omitNorms, boolean storePayloads, IndexOptions indexOptions, DocValues.Type docValues, DocValues.Type normsType, Map<String,String> attributes) {this.name = name;this.indexed = indexed;this.number = number;@@ -83,6 +88,7 @@ public final class FieldInfo {this.indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;this.normType = null;}+ this.attributes = attributes;assert checkConsistency();assert indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 || !storePayloads;}@@ -107,13 +113,6 @@ public final class FieldInfo {return true;}-- @Override- public FieldInfo clone() {- return new FieldInfo(name, indexed, number, storeTermVector,- omitNorms, storePayloads, indexOptions, docValueType, normType);- }-// should only be called by FieldInfos#addOrUpdatevoid update(boolean indexed, boolean storeTermVector, boolean omitNorms, boolean storePayloads, IndexOptions indexOptions) {@@ -225,4 +224,39 @@ public final class FieldInfo {public boolean hasVectors() {return storeTermVector;}++ /**+ * Get a codec attribute value, or null if it does not exist+ */+ public String getAttribute(String key) {+ if (attributes == null) {+ return null;+ } else {+ return attributes.get(key);+ }+ }++ /**+ * Puts a codec attribute value.+ * <p>+ * This is a key-value mapping for the field that the codec can use+ * to store additional metadata, and will be available to the codec+ * when reading the segment via {@link #getAttribute(String)}+ * <p>+ * If a value already exists for the field, it will be replaced with+ * the new value.+ */+ public String putAttribute(String key, String value) {+ if (attributes == null) {+ attributes = new HashMap<String,String>();+ }+ return attributes.put(key, value);+ }++ /**+ * @return internal codec attributes map. May be null if no mappings exist.+ */+ public Map<String,String> attributes() {+ return attributes;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.javaindex 3128c956c26..c0085eaaf3f 100644--- a/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/FieldInfos.java@@ -296,7 +296,7 @@ public class FieldInfos implements Iterable<FieldInfo> {boolean storeTermVector, boolean omitNorms, boolean storePayloads,IndexOptions indexOptions, DocValues.Type docValuesType, DocValues.Type normType) {globalFieldNumbers.setIfNotSet(fieldNumber, name);- final FieldInfo fi = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector, omitNorms, storePayloads, indexOptions, docValuesType, normType);+ final FieldInfo fi = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector, omitNorms, storePayloads, indexOptions, docValuesType, normType, null);putInternal(fi);return fi;}@@ -306,12 +306,7 @@ public class FieldInfos implements Iterable<FieldInfo> {}final FieldInfos finish() {- FieldInfo[] cloned = new FieldInfo[byName.size()];- int upto = 0;- for(FieldInfo fieldInfo : byName.values()) {- cloned[upto++] = fieldInfo.clone();- }- return new FieldInfos(cloned);+ return new FieldInfos(byName.values().toArray(new FieldInfo[byName.size()]));}}}diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiFields.java b/lucene/core/src/java/org/apache/lucene/index/MultiFields.javaindex d9a9b5bef4f..5c69731ab9e 100644--- a/lucene/core/src/java/org/apache/lucene/index/MultiFields.java+++ b/lucene/core/src/java/org/apache/lucene/index/MultiFields.java@@ -244,7 +244,13 @@ public final class MultiFields extends Fields {}/** Call this to get the (merged) FieldInfos for a- * composite reader */+ * composite reader.+ * <p>+ * NOTE: the returned field numbers will likely not+ * correspond to the actual field numbers in the underlying+ * readers, and codec metadata ({@link FieldInfo#getAttribute(String)}+ * will be unavailable.+ */public static FieldInfos getMergedFieldInfos(IndexReader reader) {final List<AtomicReader> subReaders = new ArrayList<AtomicReader>();ReaderUtil.gatherSubReaders(subReaders, reader);diff --git a/lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java b/lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.javaindex 8156baacb81..d546c3be341 100644--- a/lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/ParallelAtomicReader.java@@ -205,6 +205,14 @@ public final class ParallelAtomicReader extends AtomicReader {}}+ /**+ * {@inheritDoc}+ * <p>+ * NOTE: the returned field numbers will likely not+ * correspond to the actual field numbers in the underlying+ * readers, and codec metadata ({@link FieldInfo#getAttribute(String)}+ * will be unavailable.+ */@Overridepublic FieldInfos getFieldInfos() {return fieldInfos;diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentMerger.java b/lucene/core/src/java/org/apache/lucene/index/SegmentMerger.javaindex 898eaecb9e5..f65a8668686 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentMerger.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentMerger.java@@ -124,6 +124,10 @@ final class SegmentMerger {numMerged = mergeVectors();assert numMerged == mergeState.mergedDocCount;}++ // write the merged infos+ FieldInfosWriter fieldInfosWriter = codec.fieldInfosFormat().getFieldInfosWriter();+ fieldInfosWriter.write(directory, segment, mergeState.fieldInfos, context);return mergeState;}@@ -190,10 +194,6 @@ final class SegmentMerger {private void mergeFieldInfos() throws IOException {mergeDocValuesAndNormsFieldInfos();- // write the merged infos- FieldInfosWriter fieldInfosWriter = codec.fieldInfosFormat()- .getFieldInfosWriter();- fieldInfosWriter.write(directory, segment, mergeState.fieldInfos, context);}// NOTE: this is actually merging all the fieldinfosdiff --git a/lucene/core/src/test/org/apache/lucene/TestDemo.java b/lucene/core/src/test/org/apache/lucene/TestDemo.javaindex c23df94a206..ebc608c6924 100644--- a/lucene/core/src/test/org/apache/lucene/TestDemo.java+++ b/lucene/core/src/test/org/apache/lucene/TestDemo.java@@ -44,7 +44,7 @@ public class TestDemo extends LuceneTestCase {// Store the index in memory:Directory directory = newDirectory();// To store an index on disk, use this instead:- //Directory directory = FSDirectory.open("/tmp/testindex");+ // Directory directory = FSDirectory.open(new File("/tmp/testindex"));RandomIndexWriter iwriter = new RandomIndexWriter(random(), directory, analyzer);Document doc = new Document();String longTerm = "longtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongtermlongterm";diff --git a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.javaindex 3b65c23b64c..3f8f8ba32df 100644--- a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java+++ b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java@@ -355,7 +355,7 @@ public class MemoryIndex {if (!fieldInfos.containsKey(fieldName)) {fieldInfos.put(fieldName,- new FieldInfo(fieldName, true, fieldInfos.size(), false, false, false, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, null, null));+ new FieldInfo(fieldName, true, fieldInfos.size(), false, false, false, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, null, null, null));}TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosReader.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosReader.javaindex 9ecdbf05144..5918149ad10 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosReader.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosReader.java@@ -92,7 +92,7 @@ class PreFlexRWFieldInfosReader extends FieldInfosReader {}infos[i] = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector,- omitNorms, storePayloads, indexOptions, null, normType);+ omitNorms, storePayloads, indexOptions, null, normType, null);}if (input.getFilePointer() != input.length()) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosWriter.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosWriter.javaindex e4c4e8cf80e..ffa91ce408c 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosWriter.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene3x/PreFlexRWFieldInfosWriter.java@@ -86,6 +86,7 @@ class PreFlexRWFieldInfosWriter extends FieldInfosWriter {// only in RW caseoutput.writeByte((byte) (fi.getNormType() == null ? 0 : 1));}+ assert fi.attributes() == null; // not used or supported}} finally {output.close();