Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-3279: Allow CFS to be empty

Simon Willnauerb448fc6ef71f
diff --git a/lucene/src/java/org/apache/lucene/store/CompoundFileDirectory.java b/lucene/src/java/org/apache/lucene/store/CompoundFileDirectory.java
index 0e8458ccebf..9693bdd18af 100644
--- a/lucene/src/java/org/apache/lucene/store/CompoundFileDirectory.java
+++ b/lucene/src/java/org/apache/lucene/store/CompoundFileDirectory.java
@@ -78,6 +78,7 @@ public abstract class CompoundFileDirectory extends Directory {
this.entries = SENTINEL;
this.openForWrite = true;
this.isOpen = true;
+ writer = new CompoundFileWriter(directory, fileName);
}
/** Helper method that reads CFS entries from an input stream */
@@ -269,7 +270,6 @@ public abstract class CompoundFileDirectory extends Directory {
@Override
public IndexOutput createOutput(String name) throws IOException {
ensureOpen();
- initWriter();
return writer.createOutput(name);
}
@@ -302,12 +302,4 @@ public abstract class CompoundFileDirectory extends Directory {
throw new UnsupportedOperationException();
}
- private final void initWriter() {
- assert openForWrite;
- assert entries == SENTINEL;
- if (writer == null) {
- writer = new CompoundFileWriter(directory, fileName);
- }
- }
-
}
diff --git a/lucene/src/java/org/apache/lucene/store/CompoundFileWriter.java b/lucene/src/java/org/apache/lucene/store/CompoundFileWriter.java
index 748a1787eda..4a8714b8cf6 100644
--- a/lucene/src/java/org/apache/lucene/store/CompoundFileWriter.java
+++ b/lucene/src/java/org/apache/lucene/store/CompoundFileWriter.java
@@ -135,10 +135,7 @@ final class CompoundFileWriter {
IOException priorException = null;
IndexOutput entryTableOut = null;
try {
- if (entries.isEmpty()) {
- throw new IllegalStateException("CFS has no entries");
- }
-
+ initDataOut();
if (!pendingEntries.isEmpty() || outputTaken.get()) {
throw new IllegalStateException("CFS has pending open files");
}
diff --git a/lucene/src/test-framework/org/apache/lucene/store/MockCompoundFileDirectoryWrapper.java b/lucene/src/test-framework/org/apache/lucene/store/MockCompoundFileDirectoryWrapper.java
index cd0f498e78f..5271ad35428 100644
--- a/lucene/src/test-framework/org/apache/lucene/store/MockCompoundFileDirectoryWrapper.java
+++ b/lucene/src/test-framework/org/apache/lucene/store/MockCompoundFileDirectoryWrapper.java
@@ -19,7 +19,6 @@ package org.apache.lucene.store;
import java.io.IOException;
import java.util.Collection;
-import java.util.Collections;
public class MockCompoundFileDirectoryWrapper extends CompoundFileDirectory {
private final MockDirectoryWrapper parent;
@@ -31,11 +30,7 @@ public class MockCompoundFileDirectoryWrapper extends CompoundFileDirectory {
this.name = name;
this.parent = parent;
this.delegate = delegate;
- if (forWrite) {
- super.initForWrite();
- } else {
- super.initForRead(Collections.<String,FileEntry>emptyMap());
- }
+ // don't initialize here since we delegate everything - if not initialized a direct call will cause an assert to fail!
parent.addFileHandle(this, name, !forWrite);
}
@@ -51,12 +46,8 @@ public class MockCompoundFileDirectoryWrapper extends CompoundFileDirectory {
@Override
public synchronized void close() throws IOException {
- try {
- delegate.close();
- parent.removeOpenFile(this, name);
- } finally {
- super.close();
- }
+ delegate.close();
+ parent.removeOpenFile(this, name);
}
@Override
diff --git a/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java b/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java
index 8b2deed6858..5c0db606597 100644
--- a/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java
+++ b/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java
@@ -21,13 +21,13 @@ import java.io.IOException;
import java.io.File;
import org.apache.lucene.util.LuceneTestCase;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
import org.apache.lucene.store.CompoundFileDirectory;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.MockDirectoryWrapper;
+import org.apache.lucene.store.MockDirectoryWrapper.Failure;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.store._TestHelper;
import org.apache.lucene.util._TestUtil;
@@ -35,27 +35,8 @@ import org.apache.lucene.util._TestUtil;
public class TestCompoundFile extends LuceneTestCase
{
- /** Main for running test case by itself. */
- public static void main(String args[]) {
- TestRunner.run (new TestSuite(TestCompoundFile.class));
-// TestRunner.run (new TestCompoundFile("testSingleFile"));
-// TestRunner.run (new TestCompoundFile("testTwoFiles"));
-// TestRunner.run (new TestCompoundFile("testRandomFiles"));
-// TestRunner.run (new TestCompoundFile("testClonedStreamsClosing"));
-// TestRunner.run (new TestCompoundFile("testReadAfterClose"));
-// TestRunner.run (new TestCompoundFile("testRandomAccess"));
-// TestRunner.run (new TestCompoundFile("testRandomAccessClones"));
-// TestRunner.run (new TestCompoundFile("testFileNotFound"));
-// TestRunner.run (new TestCompoundFile("testReadPastEOF"));
-
-// TestRunner.run (new TestCompoundFile("testIWCreate"));
-
- }
-
-
private Directory dir;
-
@Override
public void setUp() throws Exception {
super.setUp();
@@ -717,5 +698,16 @@ public class TestCompoundFile extends LuceneTestCase
cfr.close();
newDir.close();
}
+
+ public void testEmptyCFS() throws IOException {
+ Directory newDir = newDirectory();
+ CompoundFileDirectory csw = newDir.createCompoundOutput("d.cfs");
+ csw.close();
+ CompoundFileDirectory csr = newDir.openCompoundInput("d.cfs", 1024);
+ assertEquals(0, csr.listAll().length);
+ csr.close();
+
+ newDir.close();
+ }
}