LUCENE-2779: Use ConcurrentHashMap in RAMDirectory

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1040138 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2010-11-29 15:07:41 +00:00
parent 4c724b958b
commit 7f443f67ea
1 changed files with 34 additions and 51 deletions

View File

@ -20,8 +20,8 @@ package org.apache.lucene.store;
import java.io.IOException; import java.io.IOException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.Serializable; 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 java.util.concurrent.atomic.AtomicLong;
import org.apache.lucene.util.ThreadInterruptedException; import org.apache.lucene.util.ThreadInterruptedException;
@ -35,7 +35,7 @@ public class RAMDirectory extends Directory implements Serializable {
private static final long serialVersionUID = 1l; private static final long serialVersionUID = 1l;
protected HashMap<String,RAMFile> fileMap = new HashMap<String,RAMFile>(); protected Map<String,RAMFile> fileMap = new ConcurrentHashMap<String,RAMFile>();
protected final AtomicLong sizeInBytes = new AtomicLong(); protected final AtomicLong sizeInBytes = new AtomicLong();
// ***** // *****
@ -78,25 +78,16 @@ public class RAMDirectory extends Directory implements Serializable {
} }
@Override @Override
public synchronized final String[] listAll() { public final String[] listAll() {
ensureOpen(); ensureOpen();
Set<String> fileNames = fileMap.keySet(); return fileMap.keySet().toArray(new String[0]);
String[] result = new String[fileNames.size()];
int i = 0;
for(final String fileName: fileNames)
result[i++] = fileName;
return result;
} }
/** Returns true iff the named file exists in this directory. */ /** Returns true iff the named file exists in this directory. */
@Override @Override
public final boolean fileExists(String name) { public final boolean fileExists(String name) {
ensureOpen(); ensureOpen();
RAMFile file; return fileMap.containsKey(name);
synchronized (this) {
file = fileMap.get(name);
}
return file != null;
} }
/** Returns the time the named file was last modified. /** Returns the time the named file was last modified.
@ -105,12 +96,10 @@ public class RAMDirectory extends Directory implements Serializable {
@Override @Override
public final long fileModified(String name) throws IOException { public final long fileModified(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file; RAMFile file = fileMap.get(name);
synchronized (this) { if (file == null) {
file = fileMap.get(name);
}
if (file==null)
throw new FileNotFoundException(name); throw new FileNotFoundException(name);
}
return file.getLastModified(); return file.getLastModified();
} }
@ -120,12 +109,10 @@ public class RAMDirectory extends Directory implements Serializable {
@Override @Override
public void touchFile(String name) throws IOException { public void touchFile(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file; RAMFile file = fileMap.get(name);
synchronized (this) { if (file == null) {
file = fileMap.get(name);
}
if (file==null)
throw new FileNotFoundException(name); throw new FileNotFoundException(name);
}
long ts2, ts1 = System.currentTimeMillis(); long ts2, ts1 = System.currentTimeMillis();
do { do {
@ -146,19 +133,18 @@ public class RAMDirectory extends Directory implements Serializable {
@Override @Override
public final long fileLength(String name) throws IOException { public final long fileLength(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file; RAMFile file = fileMap.get(name);
synchronized (this) { if (file == null) {
file = fileMap.get(name);
}
if (file==null)
throw new FileNotFoundException(name); throw new FileNotFoundException(name);
}
return file.getLength(); return file.getLength();
} }
/** Return total size in bytes of all files in this /**
* directory. This is currently quantized to * Return total size in bytes of all files in this directory. This is
* RAMOutputStream.BUFFER_SIZE. */ * currently quantized to RAMOutputStream.BUFFER_SIZE.
public synchronized final long sizeInBytes() { */
public final long sizeInBytes() {
ensureOpen(); ensureOpen();
return sizeInBytes.get(); return sizeInBytes.get();
} }
@ -167,29 +153,28 @@ public class RAMDirectory extends Directory implements Serializable {
* @throws IOException if the file does not exist * @throws IOException if the file does not exist
*/ */
@Override @Override
public synchronized void deleteFile(String name) throws IOException { public void deleteFile(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file = fileMap.remove(name); RAMFile file = fileMap.remove(name);
if (file!=null) { if (file != null) {
file.directory = null; file.directory = null;
sizeInBytes.addAndGet(-file.sizeInBytes); sizeInBytes.addAndGet(-file.sizeInBytes);
} else } else {
throw new FileNotFoundException(name); throw new FileNotFoundException(name);
} }
}
/** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */ /** Creates a new, empty file in the directory with the given name. Returns a stream writing this file. */
@Override @Override
public IndexOutput createOutput(String name) throws IOException { public IndexOutput createOutput(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file = newRAMFile(); RAMFile file = newRAMFile();
synchronized (this) { RAMFile existing = fileMap.remove(name);
RAMFile existing = fileMap.get(name); if (existing != null) {
if (existing!=null) {
sizeInBytes.addAndGet(-existing.sizeInBytes); sizeInBytes.addAndGet(-existing.sizeInBytes);
existing.directory = null; existing.directory = null;
} }
fileMap.put(name, file); fileMap.put(name, file);
}
return new RAMOutputStream(file); return new RAMOutputStream(file);
} }
@ -206,12 +191,10 @@ public class RAMDirectory extends Directory implements Serializable {
@Override @Override
public IndexInput openInput(String name) throws IOException { public IndexInput openInput(String name) throws IOException {
ensureOpen(); ensureOpen();
RAMFile file; RAMFile file = fileMap.get(name);
synchronized (this) { if (file == null) {
file = fileMap.get(name);
}
if (file == null)
throw new FileNotFoundException(name); throw new FileNotFoundException(name);
}
return new RAMInputStream(file); return new RAMInputStream(file);
} }