SOLR-1379: Add RAMDirectoryFactory

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/branches/newtrunk@924627 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-03-18 04:02:35 +00:00
parent 96c5dfd904
commit 2a282e21a1
3 changed files with 120 additions and 0 deletions

View File

@ -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
----------------------

View File

@ -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;
}
}

View File

@ -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);
}
}