Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-1379: Add RAMDirectoryFactory

Yonik Seeley2a282e21a168
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index df05a670596..9543e9d4849 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -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
----------------------
diff --git a/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java b/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
new file mode 100644
index 00000000000..ec6d73bf479
--- /dev/null
+++ b/solr/src/java/org/apache/solr/core/RAMDirectoryFactory.java
@@ -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;
+ }
+}
diff --git a/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java b/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java
new file mode 100644
index 00000000000..16ef98a14aa
--- /dev/null
+++ b/solr/src/test/org/apache/solr/core/RAMDirectoryFactoryTest.java
@@ -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);
+ }
+}