You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by yo...@apache.org on 2010/03/18 05:02:35 UTC

svn commit: r924627 - in /lucene/solr/branches/newtrunk/solr: CHANGES.txt src/java/org/apache/solr/core/RAMDirectoryFactory.java src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java

Author: yonik
Date: Thu Mar 18 04:02:35 2010
New Revision: 924627

URL: http://svn.apache.org/viewvc?rev=924627&view=rev
Log:
SOLR-1379: Add RAMDirectoryFactory

Added:
    lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
    lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java   (with props)
Modified:
    lucene/solr/branches/newtrunk/solr/CHANGES.txt

Modified: lucene/solr/branches/newtrunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/branches/newtrunk/solr/CHANGES.txt?rev=924627&r1=924626&r2=924627&view=diff
==============================================================================
--- lucene/solr/branches/newtrunk/solr/CHANGES.txt (original)
+++ lucene/solr/branches/newtrunk/solr/CHANGES.txt Thu Mar 18 04:02:35 2010
@@ -113,6 +113,9 @@ New Features
 * SOLR-1677: Add support for choosing the Lucene Version for Lucene components within
   Solr. (Uwe Schindler, Mark Miller)
 
+* SOLR-1379: Add RAMDirectoryFactory for non-persistent in memory index storage.
+  (Alex Baranov via yonik)
+
 Optimizations
 ----------------------
 

Added: 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=924627&view=auto
==============================================================================
--- lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java (added)
+++ lucene/solr/branches/newtrunk/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java Thu Mar 18 04:02:35 2010
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.core;
+
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
+
+import java.io.IOException;
+import java.io.File;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Directory provider for using lucene RAMDirectory
+ */
+public class RAMDirectoryFactory extends StandardDirectoryFactory {
+  private Map<String, Directory> directories = new HashMap<String, Directory>();
+
+  @Override
+  public Directory open(String path) throws IOException {
+    synchronized (this) {
+      Directory directory = directories.get(path);
+      if (directory == null) {
+        directory = openNew(path);
+        directories.put(path, directory);
+      }
+
+      return directory;
+    }
+  }
+
+  /**
+   * Non-public for unit-test access only. Do not use directly
+   */
+  Directory openNew(String path) throws IOException {
+    Directory directory;
+    File dirFile = new File(path);
+    boolean indexExists = dirFile.canRead();
+    if (indexExists) {
+      Directory dir = super.open(path);
+      directory = new RAMDirectory(dir);
+    } else {
+      directory = new RAMDirectory();
+    }
+    return directory;
+  }
+}

Added: lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java?rev=924627&view=auto
==============================================================================
--- lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java (added)
+++ lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java Thu Mar 18 04:02:35 2010
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.core;
+
+import junit.framework.TestCase;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
+import org.easymock.EasyMock;
+
+import java.io.IOException;
+import java.io.File;
+
+/**
+ * Test-case for RAMDirectoryFactory
+ */
+public class RAMDirectoryFactoryTest extends TestCase {
+  public void testOpenReturnsTheSameForSamePath() throws IOException {
+    final Directory directory = new RAMDirectory();
+    RAMDirectoryFactory factory = new RAMDirectoryFactory() {
+      @Override
+      Directory openNew(String path) throws IOException {
+        return directory;
+      }
+    };
+    String path = "/fake/path";
+    Directory dir1 = factory.open(path);
+    Directory dir2 = factory.open(path);
+    assertEquals("RAMDirectoryFactory should not create new instance of RAMDirectory " +
+        "every time open() is called for the same path", directory, dir1);
+    assertEquals("RAMDirectoryFactory should not create new instance of RAMDirectory " +
+        "every time open() is called for the same path", directory, dir2);
+  }
+
+  public void testOpenSucceedForEmptyDir() throws IOException {
+    RAMDirectoryFactory factory = new RAMDirectoryFactory();
+    Directory dir = factory.open("/fake/path");
+    assertNotNull("RAMDirectoryFactory should create RAMDirectory even if the path doen't lead " +
+        "to index directory on the file system", dir);
+  }
+}

Propchange: lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/branches/newtrunk/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL