diff --git a/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java index eb3fa0f6f24..db9f08542d9 100644 --- a/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/FileSwitchDirectory.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.NoSuchFileException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -118,7 +119,9 @@ public class FileSwitchDirectory extends Directory { if (exc != null && files.isEmpty()) { throw exc; } - return files.toArray(new String[files.size()]); + String[] result = files.toArray(new String[files.size()]); + Arrays.sort(result); + return result; } /** Utility method to return a file's extension. */ diff --git a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java index 4f3b25422cc..ef7b895af76 100644 --- a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java @@ -106,7 +106,9 @@ public class NRTCachingDirectory extends FilterDirectory implements Accountable "cache=" + Arrays.toString(cache.listAll()) + ",delegate=" + Arrays.toString(in.listAll())); } } - return files.toArray(new String[files.size()]); + String[] result = files.toArray(new String[files.size()]); + Arrays.sort(result); + return result; } @Override diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java index c2e199698dd..3d9c20dd46f 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java @@ -1295,4 +1295,30 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase { assertTrue(Arrays.asList(fsDir.listAll()).contains(fileName)); } } + + public void testListAllIsSorted() throws IOException { + try (Directory dir = getDirectory(createTempDir())) { + int count = atLeast(20); + Set names = new HashSet<>(); + while(names.size() < count) { + String name = TestUtil.randomSimpleString(random()); + if (name.length() == 0) { + continue; + } + if (random().nextInt(5) == 1) { + IndexOutput out = dir.createTempOutput(name, "foo", IOContext.DEFAULT); + names.add(out.getName()); + out.close(); + } else if (names.contains(name) == false) { + IndexOutput out = dir.createOutput(name, IOContext.DEFAULT); + names.add(out.getName()); + out.close(); + } + } + String[] actual = dir.listAll(); + String[] expected = actual.clone(); + Arrays.sort(expected); + assertEquals(expected, actual); + } + } }