From 04e297adee35105e5ac5204e3cc4799613ba02fa Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 13 Feb 2015 17:30:54 +0000 Subject: [PATCH] LUCENE-6241: FSDirectory.listAll doesnt filter out subdirectories anymore git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1659621 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 +++++ .../src/java/org/apache/lucene/store/Directory.java | 2 +- .../java/org/apache/lucene/store/FSDirectory.java | 12 ++---------- .../java/org/apache/lucene/store/RAMDirectory.java | 9 ++++++--- .../test/org/apache/lucene/index/TestAddIndexes.java | 6 +++--- .../apache/lucene/index/TestIndexWriterDelete.java | 3 +-- .../lucene/index/TestIndexWriterExceptions.java | 2 +- .../lucene/index/TestIndexWriterOnDiskFull.java | 2 +- .../apache/lucene/index/TestIndexWriterReader.java | 4 ++-- .../apache/lucene/index/TestTermVectorsWriter.java | 4 ++-- .../test/org/apache/lucene/search/TestBoolean2.java | 6 ++---- .../test/org/apache/lucene/store/TestDirectory.java | 2 +- .../org/apache/lucene/store/TestRAMDirectory.java | 8 ++++---- .../src/java/org/apache/lucene/util/TestUtil.java | 12 ++++++++++++ .../java/org/apache/solr/core/DirectoryFactory.java | 3 ++- .../org/apache/solr/store/hdfs/HdfsDirectory.java | 6 ++---- 16 files changed, 47 insertions(+), 39 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 2081af93ee6..d12c73c00ca 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -86,6 +86,11 @@ Optimizations positions lazily if the phrase query is in a conjunction with other queries. (Robert Muir, Adrien Grand) +* LUCENE-6241: FSDirectory.listAll() doesnt filter out subdirectories anymore, + for faster performance. Subdirectories don't matter to Lucene. If you need to + filter out non-index files with some custom usage, you may want to look at + the IndexFileNames class. (Robert Muir) + API Changes * LUCENE-6204, LUCENE-6208: Simplify CompoundFormat: remove files() diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.java index 2060e17da0e..3ab6dbfda49 100644 --- a/lucene/core/src/java/org/apache/lucene/store/Directory.java +++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java @@ -43,7 +43,7 @@ import org.apache.lucene.util.IOUtils; public abstract class Directory implements Closeable { /** - * Returns an array of strings, one for each file in the directory. + * Returns an array of strings, one for each entry in the directory. * * @throws IOException in case of IO error */ diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java index 4b7a96a7ad4..629e467d924 100644 --- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java @@ -166,19 +166,14 @@ public abstract class FSDirectory extends BaseDirectory { } } - /** Lists all files (not subdirectories) in the + /** Lists all files (including subdirectories) in the * directory. * * @throws IOException if there was an I/O error during listing */ public static String[] listAll(Path dir) throws IOException { List entries = new ArrayList<>(); - try (DirectoryStream stream = Files.newDirectoryStream(dir, new DirectoryStream.Filter() { - @Override - public boolean accept(Path entry) throws IOException { - return !Files.isDirectory(entry); // filter out entries that are definitely directories. - } - })) { + try (DirectoryStream stream = Files.newDirectoryStream(dir)) { for (Path path : stream) { entries.add(path.getFileName().toString()); } @@ -187,9 +182,6 @@ public abstract class FSDirectory extends BaseDirectory { return entries.toArray(new String[entries.size()]); } - /** Lists all files (not subdirectories) in the - * directory. - * @see #listAll(Path) */ @Override public String[] listAll() throws IOException { ensureOpen(); diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java index e7c4e5e40e3..660229faddb 100644 --- a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java @@ -19,6 +19,7 @@ package org.apache.lucene.store; import java.io.IOException; import java.io.FileNotFoundException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -87,14 +88,16 @@ public class RAMDirectory extends BaseDirectory implements Accountable { * @param dir a Directory value * @exception IOException if an error occurs */ - public RAMDirectory(Directory dir, IOContext context) throws IOException { + public RAMDirectory(FSDirectory dir, IOContext context) throws IOException { this(dir, false, context); } - private RAMDirectory(Directory dir, boolean closeDir, IOContext context) throws IOException { + private RAMDirectory(FSDirectory dir, boolean closeDir, IOContext context) throws IOException { this(); for (String file : dir.listAll()) { - copyFrom(dir, file, file, context); + if (!Files.isDirectory(dir.getDirectory().resolve(file))) { + copyFrom(dir, file, file, context); + } } if (closeDir) { dir.close(); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java b/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java index 9d4313c97bb..ad9e7625a69 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestAddIndexes.java @@ -394,7 +394,7 @@ public class TestAddIndexes extends LuceneTestCase { setMergePolicy(newLogMergePolicy(4)) ); - writer.addIndexes(aux, new MockDirectoryWrapper(random(), new RAMDirectory(aux, newIOContext(random())))); + writer.addIndexes(aux, new MockDirectoryWrapper(random(), TestUtil.ramCopyOf(aux))); assertEquals(1060, writer.maxDoc()); assertEquals(1000, writer.getDocCount(0)); writer.close(); @@ -436,7 +436,7 @@ public class TestAddIndexes extends LuceneTestCase { if (VERBOSE) { System.out.println("\nTEST: now addIndexes"); } - writer.addIndexes(aux, new MockDirectoryWrapper(random(), new RAMDirectory(aux, newIOContext(random())))); + writer.addIndexes(aux, new MockDirectoryWrapper(random(), TestUtil.ramCopyOf(aux))); assertEquals(1020, writer.maxDoc()); assertEquals(1000, writer.getDocCount(0)); writer.close(); @@ -686,7 +686,7 @@ public class TestAddIndexes extends LuceneTestCase { final Directory[] dirs = new Directory[NUM_COPY]; for(int k=0;k