Apache Lucene open-source search software
LUCENE-3143: SegmentMerger should assert delet and separate norms file are not passed to createCompoundFile
Shai Erera1bab6d3212cb
diff --git a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java b/lucene/src/java/org/apache/lucene/index/SegmentMerger.javaindex 701e168a701..db831a62115 100644--- a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java+++ b/lucene/src/java/org/apache/lucene/index/SegmentMerger.java@@ -120,6 +120,12 @@ final class SegmentMerger {return mergedDocs;}+ /**+ * NOTE: this method creates a compound file for all files returned by+ * info.files(). While, generally, this may include separate norms and+ * deletion files, this SegmentInfo must not reference such files when this+ * method is called, because they are not allowed within a compound file.+ */final Collection<String> createCompoundFile(String fileName, final SegmentInfo info)throws IOException {@@ -127,6 +133,10 @@ final class SegmentMerger {Collection<String> files = info.files();CompoundFileWriter cfsWriter = new CompoundFileWriter(directory, fileName, checkAbort);for (String file : files) {+ assert !IndexFileNames.matchesExtension(file, IndexFileNames.DELETES_EXTENSION)+ : ".del file is not allowed in .cfs: " + file;+ assert !file.substring(file.lastIndexOf('.') + 1).startsWith(IndexFileNames.SEPARATE_NORMS_EXTENSION)+ : "separate norms file (.s*) is not allowed in .cfs: " + file;cfsWriter.addFile(file);}diff --git a/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java b/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.javaindex 7961601c013..ac529b7ba07 100644--- a/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java+++ b/lucene/src/test/org/apache/lucene/index/TestSegmentMerger.java@@ -20,7 +20,13 @@ package org.apache.lucene.index;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.store.BufferedIndexInput;import org.apache.lucene.store.Directory;+import org.apache.lucene.analysis.MockAnalyzer;+import org.apache.lucene.analysis.core.WhitespaceAnalyzer;import org.apache.lucene.document.Document;+import org.apache.lucene.document.Field;+import org.apache.lucene.document.Field.Index;+import org.apache.lucene.document.Field.Store;+import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.index.codecs.CodecProvider;import org.apache.lucene.util.BytesRef;@@ -127,4 +133,50 @@ public class TestSegmentMerger extends LuceneTestCase {TestSegmentReader.checkNorms(mergedReader);mergedReader.close();}++ // LUCENE-3143+ public void testInvalidFilesToCreateCompound() throws Exception {+ Directory dir = newDirectory();+ IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));+ IndexWriter w = new IndexWriter(dir, iwc);++ // Create an index w/ .del file+ w.addDocument(new Document());+ Document doc = new Document();+ doc.add(new Field("c", "test", Store.NO, Index.ANALYZED));+ w.addDocument(doc);+ w.commit();+ w.deleteDocuments(new Term("c", "test"));+ w.close();++ // Assert that SM fails if .del exists+ SegmentMerger sm = new SegmentMerger(dir, 1, "a", null, null, null);+ try {+ sm.createCompoundFile("b1", w.segmentInfos.info(0));+ fail("should not have been able to create a .cfs with .del and .s* files");+ } catch (AssertionError e) {+ // expected+ }++ // Create an index w/ .s*+ w = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.CREATE));+ doc = new Document();+ doc.add(new Field("c", "test", Store.NO, Index.ANALYZED));+ w.addDocument(doc);+ w.close();+ IndexReader r = IndexReader.open(dir, false);+ r.setNorm(0, "c", (byte) 1);+ r.close();++ // Assert that SM fails if .s* exists+ try {+ sm.createCompoundFile("b2", w.segmentInfos.info(0));+ fail("should not have been able to create a .cfs with .del and .s* files");+ } catch (AssertionError e) {+ // expected+ }++ dir.close();+ }+}