gpt4 book ai didi

neo4j - 在内存中创建数据库的最佳方法?

转载 作者:行者123 更新时间:2023-12-02 04:47:56 25 4
gpt4 key购买 nike

使用 neo4j 创建内存数据库是否正确?这样遍历查询将只命中缓存而不是磁盘。

approach - 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”节点是否已创建。

最佳答案

Neo4j 没有“内存中”模式,因为所有数据始终存储在内存中,不使用磁盘存储。 ImpermanentGraphDatabase 是您能找到的最接近此类的东西,但它只是随机创建一个数据目录,并在关闭时将其删除。

如果你不介意使用磁盘,你可以使用上面的 ImpermanentGraphDatabase,并将 neo4j 的缓存设置得非常高。这将使一切都存储在内存中,以及磁盘上。

关于neo4j - 在内存中创建数据库的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19435059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com