Apache Lucene open-source search software
LUCENE-4130: fix CompoundFileDirectory.listAll when the .cfs has a segment suffix
Robert Muirbc14cec66942
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFileNames.java b/lucene/core/src/java/org/apache/lucene/index/IndexFileNames.javaindex 98452d336c9..39e3a8e4f7a 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexFileNames.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFileNames.java@@ -140,6 +140,17 @@ public final class IndexFileNames {return filename.endsWith("." + ext);}+ /** locates the boundary of the segment name, or -1 */+ private static int indexOfSegmentName(String filename) {+ // If it is a .del file, there's an '_' after the first character+ int idx = filename.indexOf('_', 1);+ if (idx == -1) {+ // If it's not, strip everything that's before the '.'+ idx = filename.indexOf('.');+ }+ return idx;+ }+/*** Strips the segment name out of the given file name. If you used* {@link #segmentFileName} or {@link #fileNameFromGeneration} to create your@@ -150,18 +161,27 @@ public final class IndexFileNames {* if it does not contain a '.' and '_'.*/public static String stripSegmentName(String filename) {- // If it is a .del file, there's an '_' after the first character- int idx = filename.indexOf('_', 1);- if (idx == -1) {- // If it's not, strip everything that's before the '.'- idx = filename.indexOf('.');- }+ int idx = indexOfSegmentName(filename);if (idx != -1) {filename = filename.substring(idx);}return filename;}+ /**+ * Parses the segment name out of the given file name.+ *+ * @return the segment name only, or filename+ * if it does not contain a '.' and '_'.+ */+ public static String parseSegmentName(String filename) {+ int idx = indexOfSegmentName(filename);+ if (idx != -1) {+ filename = filename.substring(0, idx);+ }+ return filename;+ }+public static String stripExtension(String filename) {int idx = filename.indexOf('.');if (idx != -1) {diff --git a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.javaindex b9e1b1399f0..fbbabc58d12 100644--- a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java@@ -204,7 +204,7 @@ public final class CompoundFileDirectory extends Directory {} else {res = entries.keySet().toArray(new String[entries.size()]);// Add the segment name- String seg = fileName.substring(0, fileName.indexOf('.'));+ String seg = IndexFileNames.parseSegmentName(fileName);for (int i = 0; i < res.length; i++) {res[i] = seg + res[i];}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestAllFilesHaveCodecHeader.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestAllFilesHaveCodecHeader.javaindex c8d90d9ac96..cd141d1ee8c 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestAllFilesHaveCodecHeader.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestAllFilesHaveCodecHeader.java@@ -69,11 +69,9 @@ public class TestAllFilesHaveCodecHeader extends LuceneTestCase {continue; // segments.gen has no header, thats ok}if (file.endsWith(IndexFileNames.COMPOUND_FILE_EXTENSION)) {- /* TODO: enable this after resolving LUCENE-4130- * CompoundFileDirectory cfsDir = new CompoundFileDirectory(dir, file, newIOContext(random()), false);- * checkHeaders(cfsDir); // recurse into cfs- * cfsDir.close();- */+ CompoundFileDirectory cfsDir = new CompoundFileDirectory(dir, file, newIOContext(random()), false);+ checkHeaders(cfsDir); // recurse into cfs+ cfsDir.close();continue; // .cfs has its own header... would be nice to fix}if (file.endsWith(IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION)) {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.javaindex 44eb4916e48..06a13e33ef9 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java@@ -20,8 +20,11 @@ package org.apache.lucene.index;import java.io.IOException;import java.io.File;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.document.Document;+import org.apache.lucene.document.Field;import org.apache.lucene.store.CompoundFileDirectory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexOutput;@@ -804,4 +807,50 @@ public class TestCompoundFile extends LuceneTestCasecfr.close();d.close();}++ public void testListAll() throws Exception {+ Directory dir = newDirectory();+ // riw should sometimes create docvalues fields, etc+ RandomIndexWriter riw = new RandomIndexWriter(random(), dir);+ Document doc = new Document();+ // these fields should sometimes get term vectors, etc+ Field idField = newStringField("id", "", Field.Store.NO);+ Field bodyField = newTextField("body", "", Field.Store.NO);+ doc.add(idField);+ doc.add(bodyField);+ for (int i = 0; i < 100; i++) {+ idField.setStringValue(Integer.toString(i));+ bodyField.setStringValue(_TestUtil.randomUnicodeString(random()));+ riw.addDocument(doc);+ if (random().nextInt(7) == 0) {+ riw.commit();+ }+ }+ riw.close();+ checkFiles(dir);+ dir.close();+ }++ // checks that we can open all files returned by listAll!+ private void checkFiles(Directory dir) throws IOException {+ for (String file : dir.listAll()) {+ if (file.endsWith(IndexFileNames.COMPOUND_FILE_EXTENSION)) {+ CompoundFileDirectory cfsDir = new CompoundFileDirectory(dir, file, newIOContext(random()), false);+ checkFiles(cfsDir); // recurse into cfs+ cfsDir.close();+ }+ IndexInput in = null;+ boolean success = false;+ try {+ in = dir.openInput(file, newIOContext(random()));+ success = true;+ } finally {+ if (success) {+ IOUtils.close(in);+ } else {+ IOUtils.closeWhileHandlingException(in);+ }+ }+ }+ }}