lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r925620 - in /lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core: RAMDirectoryFactory.java RefCntRamDirectory.java
Date Sat, 20 Mar 2010 14:57:29 GMT
Author: markrmiller
Date: Sat Mar 20 14:57:28 2010
New Revision: 925620

URL: http://svn.apache.org/viewvc?rev=925620&view=rev
Log:
Change to RefCntRamDirectory rather than RamDirectory

Added:
    lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
Modified:
    lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java

Modified: lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java?rev=925620&r1=925619&r2=925620&view=diff
==============================================================================
--- lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
(original)
+++ lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
Sat Mar 20 14:57:28 2010
@@ -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
     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;
   }

Added: lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java?rev=925620&view=auto
==============================================================================
--- lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
(added)
+++ lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RefCntRamDirectory.java
Sat Mar 20 14:57:28 2010
@@ -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;
+  }
+
+}



Mime
View raw message