LUCENE-4130: fix CompoundFileDirectory.listAll when the .cfs has a segment suffix

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1348952 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-06-11 18:11:53 +00:00
parent b232abb645
commit bc14cec669
4 changed files with 79 additions and 12 deletions

View File

@ -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) {

View File

@ -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];
}

View File

@ -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)) {

View File

@ -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 LuceneTestCase
cfr.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);
}
}
}
}
}