这是使用 neo4j 创建内存数据库的正确方法吗?这样遍历查询将只命中缓存而不是磁盘。
方法 - 1:我试过这个:
package com.test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CreateDBFactory {
private static GraphDatabaseService graphDb = null;
public static final String DB = "test/db";
public static GraphDatabaseService createInMemoryDB() {
System.out.println("- Inside createInMemoryDB() - ");
if (null == graphDb) {
synchronized (GraphDatabaseService.class) {
if (null == graphDb) {
System.out.println(" - Inside if clause -");
final Map<String, String> config = new HashMap<>();
config.put("neostore.nodestore.db.mapped_memory", "50M");
config.put("string_block_size", "60");
config.put("array_block_size", "300");
graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(DB).setConfig(config)
.newGraphDatabase();
registerShutdownHook(graphDb);
}
}
}
return graphDb;
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
public static void clearDb() {
try {
if (graphDb != null) {
graphDb.shutdown();
graphDb = null;
}
FileUtils.deleteRecursively(new File(DB));
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}
方法-2 :使用 Neo4jBasicDocTest 类。
这里new ImpermanentDatabaseBuilder()
不是创建 target/test-data/impermanent-db 文件夹。因此无法测试是否创建了“Nancy”节点。