Index: lucene/src/java/org/apache/lucene/store/RAMDirectory.java =================================================================== --- lucene/src/java/org/apache/lucene/store/RAMDirectory.java (revision 1039835) +++ lucene/src/java/org/apache/lucene/store/RAMDirectory.java (working copy) @@ -20,8 +20,9 @@ import java.io.IOException; import java.io.FileNotFoundException; import java.io.Serializable; -import java.util.HashMap; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import org.apache.lucene.util.ThreadInterruptedException; @@ -35,7 +36,7 @@ private static final long serialVersionUID = 1l; - protected HashMap fileMap = new HashMap(); + protected Map fileMap = new ConcurrentHashMap(); protected final AtomicLong sizeInBytes = new AtomicLong(); // ***** @@ -78,7 +79,7 @@ } @Override - public synchronized final String[] listAll() { + public final String[] listAll() { ensureOpen(); Set fileNames = fileMap.keySet(); String[] result = new String[fileNames.size()]; @@ -92,11 +93,7 @@ @Override public final boolean fileExists(String name) { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - return file != null; + return fileMap.containsKey(name); } /** Returns the time the named file was last modified. @@ -105,12 +102,10 @@ @Override public final long fileModified(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return file.getLastModified(); } @@ -120,12 +115,10 @@ @Override public void touchFile(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } long ts2, ts1 = System.currentTimeMillis(); do { @@ -146,19 +139,17 @@ @Override public final long fileLength(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file==null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return file.getLength(); } /** Return total size in bytes of all files in this * directory. This is currently quantized to * RAMOutputStream.BUFFER_SIZE. */ - public synchronized final long sizeInBytes() { + public final long sizeInBytes() { ensureOpen(); return sizeInBytes.get(); } @@ -167,29 +158,28 @@ * @throws IOException if the file does not exist */ @Override - public synchronized void deleteFile(String name) throws IOException { + public void deleteFile(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.remove(name); - if (file!=null) { + if (file != null) { file.directory = null; sizeInBytes.addAndGet(-file.sizeInBytes); - } else + } else { throw new FileNotFoundException(name); + } } /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */ @Override public IndexOutput createOutput(String name) throws IOException { ensureOpen(); - RAMFile file = newRAMFile(); - synchronized (this) { - RAMFile existing = fileMap.get(name); - if (existing!=null) { - sizeInBytes.addAndGet(-existing.sizeInBytes); - existing.directory = null; - } - fileMap.put(name, file); + RAMFile existing = fileMap.get(name); + if (existing != null) { + sizeInBytes.addAndGet(-existing.sizeInBytes); + existing.directory = null; } + RAMFile file = newRAMFile(); + fileMap.put(name, file); return new RAMOutputStream(file); } @@ -206,12 +196,10 @@ @Override public IndexInput openInput(String name) throws IOException { ensureOpen(); - RAMFile file; - synchronized (this) { - file = fileMap.get(name); - } - if (file == null) + RAMFile file = fileMap.get(name); + if (file == null) { throw new FileNotFoundException(name); + } return new RAMInputStream(file); }