Index: src/java/org/apache/lucene/index/SegmentMerger.java =================================================================== --- src/java/org/apache/lucene/index/SegmentMerger.java (revision 1127458) +++ src/java/org/apache/lucene/index/SegmentMerger.java (working copy) @@ -113,13 +113,22 @@ return mergedDocs; } + /** + * NOTE: this method creates a compound file out of all the files returned by + * info.files(), including the .del and separate norms file (.s*). Those + * should not exist in the result .cfs, and therefore should not be passed to + * this method. + */ final Collection createCompoundFile(String fileName, final SegmentInfo info) throws IOException { - // Now merge all added files Collection 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); } Index: src/test/org/apache/lucene/index/TestSegmentMerger.java =================================================================== --- src/test/org/apache/lucene/index/TestSegmentMerger.java (revision 1127458) +++ src/test/org/apache/lucene/index/TestSegmentMerger.java (working copy) @@ -20,7 +20,12 @@ import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.store.Directory; import org.apache.lucene.store.BufferedIndexInput; +import org.apache.lucene.analysis.MockAnalyzer; 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 java.io.IOException; import java.util.Collection; @@ -123,5 +128,50 @@ TestSegmentReader.checkNorms(mergedReader); mergedReader.close(); - } + } + + 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(); + } + }