Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Change to RefCntRamDirectory rather than RamDirectory

Mark Robert Miller55f589ae9e6d
diff --git a/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java b/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
index ec6d73bf479..8ae4bb0ecbe 100644
--- a/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
+++ b/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
@@ -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;
}
diff --git a/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java b/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
new file mode 100644
index 00000000000..16332b1862b
--- /dev/null
+++ b/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
@@ -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;
+ }
+
+}