From 49508c277bf2f7bc434d1b2ef48895f7ad41ec75 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Wed, 1 Dec 2010 15:03:27 +0000 Subject: [PATCH] LUCENE-2779: fix listAll() git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1041039 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/store/RAMDirectory.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/src/java/org/apache/lucene/store/RAMDirectory.java index 8be9dddac1d..cd8641e3260 100644 --- a/lucene/src/java/org/apache/lucene/store/RAMDirectory.java +++ b/lucene/src/java/org/apache/lucene/store/RAMDirectory.java @@ -20,8 +20,11 @@ package org.apache.lucene.store; import java.io.IOException; import java.io.FileNotFoundException; import java.io.Serializable; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; @@ -36,7 +39,7 @@ public class RAMDirectory extends Directory implements Serializable { private static final long serialVersionUID = 1l; - protected Map fileMap = new ConcurrentHashMap(); + protected final Map fileMap = new ConcurrentHashMap(); protected final AtomicLong sizeInBytes = new AtomicLong(); // ***** @@ -81,7 +84,12 @@ public class RAMDirectory extends Directory implements Serializable { @Override public final String[] listAll() { ensureOpen(); - return fileMap.keySet().toArray(new String[0]); + // NOTE: fileMap.keySet().toArray(new String[0]) is broken in non Sun JDKs, + // and the code below is resilient to map changes during the array population. + Set fileNames = fileMap.keySet(); + List names = new ArrayList(fileNames.size()); + for (String name : fileNames) names.add(name); + return names.toArray(new String[names.size()]); } /** Returns true iff the named file exists in this directory. */ @@ -188,6 +196,7 @@ public class RAMDirectory extends Directory implements Serializable { return new RAMFile(this); } + @Override public void sync(Collection names) throws IOException { } @@ -206,6 +215,6 @@ public class RAMDirectory extends Directory implements Serializable { @Override public void close() { isOpen = false; - fileMap = null; + fileMap.clear(); } }