Apache Lucene open-source search software
LUCENE-4055: remove .per file, instead use fieldinfo metadata to resolve fieldnames -> posting formats
Robert Muir2ec6fa5d7f1f
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/PostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/PostingsFormat.javaindex e0d8fe88858..c658dc36c4c 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/PostingsFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/PostingsFormat.java@@ -40,6 +40,9 @@ public abstract class PostingsFormat implements NamedSPILoader.NamedSPI {private final String name;protected PostingsFormat(String name) {+ // nocommit: check that name is a-zA-Z0-9 and < some reasonable length+ // also fix this for Codec+ // also make NamedSPILoader's map case-insensitive (like Charset)this.name = name;}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/package.html b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/package.htmlindex 6bde5694e6f..f1413e1aafd 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/package.html+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/package.html@@ -301,11 +301,6 @@ systems that frequently run out of file handles.</td><td>.del</td><td>Info about what files are deleted</td></tr>-<tr>-<td>{@link org.apache.lucene.codecs.perfield.PerFieldPostingsFormat Field formats}</td>-<td>.per</td>-<td>Enables per-field {@link org.apache.lucene.codecs.PostingsFormat PostingsFormat} configuration.</td>-</tr></table></div><a name="Lock_File" id="Lock_File"></a>diff --git a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.javaindex 5df28f4ff8b..7a851f2561f 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/perfield/PerFieldPostingsFormat.java@@ -17,15 +17,11 @@ package org.apache.lucene.codecs.perfield;* limitations under the License.*/-import java.io.Closeable;-import java.io.FileNotFoundException;import java.io.IOException;-import java.util.HashMap;import java.util.IdentityHashMap;import java.util.Iterator;import java.util.Map;import java.util.ServiceLoader; // javadocs-import java.util.Set;import java.util.TreeMap;import org.apache.lucene.codecs.FieldsConsumer;@@ -34,17 +30,9 @@ import org.apache.lucene.codecs.PostingsFormat;import org.apache.lucene.codecs.TermsConsumer;import org.apache.lucene.index.FieldInfo;import org.apache.lucene.index.FieldsEnum;-import org.apache.lucene.index.IndexFileNames;-import org.apache.lucene.index.SegmentInfo;import org.apache.lucene.index.SegmentReadState;import org.apache.lucene.index.SegmentWriteState;import org.apache.lucene.index.Terms;-import org.apache.lucene.store.DataOutput; // javadocs-import org.apache.lucene.store.Directory;-import org.apache.lucene.store.IOContext;-import org.apache.lucene.store.IndexInput;-import org.apache.lucene.store.IndexOutput;-import org.apache.lucene.util.CodecUtil;import org.apache.lucene.util.IOUtils;/**@@ -56,32 +44,17 @@ import org.apache.lucene.util.IOUtils;* This method uses Java's* {@link ServiceLoader Service Provider Interface} to resolve format names.* <p>- * PerFieldFile format:- * <ul>- * <li>PerFieldFile (.per) --> Header, IdToFormat, FieldToFormat</li>- * <li>Header --> {@link CodecUtil#checkHeader CodecHeader}</li>- * <li>IdToFormat,FieldToFormat --> {@link DataOutput#writeStringStringMap(Map) Map<String,String>}</li>- * </ul>- * <p>Notes:</p>- * <ul>- * <li>each format is assigned an id, and files written by that posting format- * have an additional suffix containing the id. For example, in a per-field- * configuration instead of <tt>_1.prx</tt> filenames would look like- * <tt>_1_0.prx</tt>.</li>- * <li>IdToFormat is a mapping between these ids and the available formats.</li>- * <li>FieldToFormat is a mapping between field names and format names.</li>- * </ul>+ * Files written by each posting format have an additional suffix containing the+ * format name. For example, in a per-field configuration instead of <tt>_1.prx</tt>+ * filenames would look like <tt>_1_Lucene40.prx</tt>.* @see ServiceLoader* @lucene.experimental*/public abstract class PerFieldPostingsFormat extends PostingsFormat {-- public static final String PER_FIELD_EXTENSION = "per";public static final String PER_FIELD_NAME = "PerField40";-- public static final int VERSION_START = 0;- public static final int VERSION_LATEST = VERSION_START;++ public static final String PER_FIELD_FORMAT_KEY = PerFieldPostingsFormat.class.getSimpleName() + ".format";public PerFieldPostingsFormat() {super(PER_FIELD_NAME);@@ -92,29 +65,10 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {throws IOException {return new FieldsWriter(state);}-- // NOTE: not private to avoid $accessN at runtime!!- static class FieldsConsumerAndID implements Closeable {- final FieldsConsumer fieldsConsumer;- final String segmentSuffix;-- public FieldsConsumerAndID(FieldsConsumer fieldsConsumer, String segmentSuffix) {- this.fieldsConsumer = fieldsConsumer;- this.segmentSuffix = segmentSuffix;- }-- @Override- public void close() throws IOException {- fieldsConsumer.close();- }- };private class FieldsWriter extends FieldsConsumer {- private final Map<PostingsFormat,FieldsConsumerAndID> formats = new IdentityHashMap<PostingsFormat,FieldsConsumerAndID>();-- /** Records all fields we wrote. */- private final Map<String,PostingsFormat> fieldToFormat = new HashMap<String,PostingsFormat>();+ private final Map<PostingsFormat,FieldsConsumer> formats = new IdentityHashMap<PostingsFormat,FieldsConsumer>();private final SegmentWriteState segmentWriteState;@@ -128,64 +82,30 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {if (format == null) {throw new IllegalStateException("invalid null PostingsFormat for field=\"" + field.name + "\"");}++ String previousValue = field.putAttribute(PER_FIELD_FORMAT_KEY, format.getName());+ assert previousValue == null;- assert !fieldToFormat.containsKey(field.name);- fieldToFormat.put(field.name, format);-- FieldsConsumerAndID consumerAndId = formats.get(format);- if (consumerAndId == null) {- // First time we are seeing this format; assign- // next id and init it:+ FieldsConsumer consumer = formats.get(format);+ if (consumer == null) {+ // First time we are seeing this format; create a new instancefinal String segmentSuffix = getFullSegmentSuffix(field.name,segmentWriteState.segmentSuffix,- ""+formats.size());- consumerAndId = new FieldsConsumerAndID(format.fieldsConsumer(new SegmentWriteState(segmentWriteState, segmentSuffix)),- segmentSuffix);- formats.put(format, consumerAndId);+ format.getName());+ consumer = format.fieldsConsumer(new SegmentWriteState(segmentWriteState, segmentSuffix));+ formats.put(format, consumer);}// nocommit we should only provide the "slice" of FIS// that this PF actually sees ... then stuff like// .hasProx could work correctly?- return consumerAndId.fieldsConsumer.addField(field);+ return consumer.addField(field);}@Overridepublic void close() throws IOException {-// Close all subsIOUtils.close(formats.values());-- // Write _X.per: maps field name -> format name and- // format name -> format id- final String mapFileName = IndexFileNames.segmentFileName(segmentWriteState.segmentName, segmentWriteState.segmentSuffix, PER_FIELD_EXTENSION);- final IndexOutput out = segmentWriteState.directory.createOutput(mapFileName, segmentWriteState.context);- boolean success = false;- try {- CodecUtil.writeHeader(out, PER_FIELD_NAME, VERSION_LATEST);-- // format name -> int id- out.writeVInt(formats.size());- for(Map.Entry<PostingsFormat,FieldsConsumerAndID> ent : formats.entrySet()) {- out.writeString(ent.getValue().segmentSuffix);- out.writeString(ent.getKey().getName());- }-- // field name -> format name- out.writeVInt(fieldToFormat.size());- for(Map.Entry<String,PostingsFormat> ent : fieldToFormat.entrySet()) {- out.writeString(ent.getKey());- out.writeString(ent.getValue().getName());- }-- success = true;- } finally {- if (!success) {- IOUtils.closeWhileHandlingException(out);- } else {- IOUtils.close(out);- }- }}}@@ -210,18 +130,21 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {// Read _X.per and init each format:boolean success = false;try {- new VisitPerFieldFile(readState.dir, readState.segmentInfo.name, readState.segmentSuffix) {- @Override- protected void visitOneFormat(String segmentSuffix, PostingsFormat postingsFormat) throws IOException {- formats.put(postingsFormat, postingsFormat.fieldsProducer(new SegmentReadState(readState, segmentSuffix)));- }-- @Override- protected void visitOneField(String fieldName, PostingsFormat postingsFormat) throws IOException {- assert formats.containsKey(postingsFormat);- fields.put(fieldName, formats.get(postingsFormat));+ // Read field name -> format name+ for (FieldInfo fi : readState.fieldInfos) {+ if (fi.isIndexed()) {+ final String fieldName = fi.name;+ final String formatName = fi.getAttribute(PER_FIELD_FORMAT_KEY);+ if (formatName != null) {+ // null formatName means the field is in fieldInfos, but has no postings!+ PostingsFormat format = PostingsFormat.forName(formatName);+ if (!formats.containsKey(format)) {+ formats.put(format, format.fieldsProducer(new SegmentReadState(readState, formatName)));+ }+ fields.put(fieldName, formats.get(format));+ }}- };+ }success = true;} finally {if (!success) {@@ -283,55 +206,6 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {return new FieldsReader(state);}- private abstract class VisitPerFieldFile {- public VisitPerFieldFile(Directory dir, String segmentName, String outerSegmentSuffix) throws IOException {- final String mapFileName = IndexFileNames.segmentFileName(segmentName, outerSegmentSuffix, PER_FIELD_EXTENSION);- final IndexInput in = dir.openInput(mapFileName, IOContext.READONCE);- boolean success = false;- try {- CodecUtil.checkHeader(in, PER_FIELD_NAME, VERSION_START, VERSION_LATEST);-- // Read format name -> format id- final int formatCount = in.readVInt();- for(int formatIDX=0;formatIDX<formatCount;formatIDX++) {- final String segmentSuffix = in.readString();- final String formatName = in.readString();- PostingsFormat postingsFormat = PostingsFormat.forName(formatName);- //System.out.println("do lookup " + formatName + " -> " + postingsFormat);- if (postingsFormat == null) {- throw new IllegalStateException("unable to lookup PostingsFormat for name=\"" + formatName + "\": got null");- }-- // Better be defined, because it was defined- // during indexing:- visitOneFormat(segmentSuffix, postingsFormat);- }-- // Read field name -> format name- final int fieldCount = in.readVInt();- for(int fieldIDX=0;fieldIDX<fieldCount;fieldIDX++) {- final String fieldName = in.readString();- final String formatName = in.readString();- visitOneField(fieldName, PostingsFormat.forName(formatName));- }-- success = true;- } finally {- if (!success) {- IOUtils.closeWhileHandlingException(in);- } else {- IOUtils.close(in);- }- }- }-- // This is called first, for all formats:- protected abstract void visitOneFormat(String segmentSuffix, PostingsFormat format) throws IOException;-- // ... then this is called, for all fields:- protected abstract void visitOneField(String fieldName, PostingsFormat format) throws IOException;- }-// nocommit simplify now that we don't have files()...?// NOTE: only called during writing; for reading we readdiff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFileNameFilter.java b/lucene/core/src/java/org/apache/lucene/index/IndexFileNameFilter.javaindex 5d9cfff7ff2..9ceba8b0920 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexFileNameFilter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFileNameFilter.java@@ -44,7 +44,7 @@ public class IndexFileNameFilter implements FilenameFilter {// Approximate match for files that seem to be Lucene// index files. This can easily over-match, ie if some// app names a file _foo_bar.go:- private final Pattern luceneFilePattern = Pattern.compile("^_[a-z0-9]+(_[a-z0-9]+)?\\.[a-z0-9]+$");+ private final Pattern luceneFilePattern = Pattern.compile("^_[a-z0-9]+(_[a-zA-Z0-9]+)?\\.[a-z0-9]+$");/* (non-Javadoc)* @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)