Change to RefCntRamDirectory rather than RamDirectory

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/branches/newtrunk@925620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2010-03-20 14:57:28 +00:00
parent 7576bfeb27
commit 55f589ae9e
2 changed files with 54 additions and 6 deletions

View File

@ -29,15 +29,17 @@ import java.util.HashMap;
* Directory provider for using lucene RAMDirectory
*/
public class RAMDirectoryFactory extends StandardDirectoryFactory {
private Map<String, Directory> directories = new HashMap<String, Directory>();
private Map<String, RefCntRamDirectory> directories = new HashMap<String, RefCntRamDirectory>();
@Override
public Directory open(String path) throws IOException {
synchronized (this) {
Directory directory = directories.get(path);
if (directory == null) {
directory = openNew(path);
RefCntRamDirectory directory = directories.get(path);
if (directory == null || !directory.isOpen()) {
directory = (RefCntRamDirectory) openNew(path);
directories.put(path, directory);
} else {
directory.incRef();
}
return directory;
@ -53,9 +55,9 @@ public class RAMDirectoryFactory extends StandardDirectoryFactory {
boolean indexExists = dirFile.canRead();
if (indexExists) {
Directory dir = super.open(path);
directory = new RAMDirectory(dir);
directory = new RefCntRamDirectory(dir);
} else {
directory = new RAMDirectory();
directory = new RefCntRamDirectory();
}
return directory;
}

View File

@ -0,0 +1,46 @@
package org.apache.solr.core;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
public class RefCntRamDirectory extends RAMDirectory {
private final AtomicInteger refCount = new AtomicInteger();
public RefCntRamDirectory() {
super();
incRef();
}
public RefCntRamDirectory(Directory dir) throws IOException {
this();
Directory.copy(dir, this, false);
}
public void incRef() {
ensureOpen();
refCount.incrementAndGet();
}
public void decRef() {
ensureOpen();
if (refCount.getAndDecrement() == 1) {
close();
}
}
public final synchronized void close() {
if (isOpen) {
decRef();
super.close();
}
}
public boolean isOpen() {
return isOpen;
}
}