Java 为什么赢了';重新打开数据库时MapDB是否工作?
因此,我创建了一个数据库,其工作方式如下:Java 为什么赢了';重新打开数据库时MapDB是否工作?,java,mapdb,Java,Mapdb,因此,我创建了一个数据库,其工作方式如下: static class Record implements Serializable { final String action; final String categoryOfAction; final String personWhoPerformedAction; final Long timeOfOccurrence; public record(String actn, String cat, St
static class Record implements Serializable
{
final String action;
final String categoryOfAction;
final String personWhoPerformedAction;
final Long timeOfOccurrence;
public record(String actn, String cat, String person, Long time)
{
action = actn;
categoryOfAction = cat;
personWhoPerformedAction = person;
timeOfOccurence = time;
}
}
static void main(String[] args)
{
DB thedb = DBMaker.newFileDB(new File("D:\\thedb.db")
.compressionEnable()
.closeOnJvmShutdown()
.mmapFileEnableIfSupported()
.transactionDisable()
.asyncWriteEnable()
.make();
//primaryMap maps each record to a unique ID
BTreeMap<Integer,Record> primaryMap = thedb.createTreeMap("pri")
.keySerializer(BTreeKeySerializer.INTEGER)
.makeOrGet();;
//this map holds the unique ID of every record in primaryMap with a common action
NavigableSet<Object[]> map_commonAction = thedb.createTreeSet("com_a")
.comparator(Fun.COMPARABLE_ARRAY_COMPARATOR)
.makeOrGet();
//this map holds the unique ID of every record in primaryMap with a common person
NavigableSet<Object[]> map_commonPerson = thedb.createTreeSet("com_p")
.comparator(Fun.COMPARABLE_ARRAY_COMPARATOR)
.makeOrGet();
//binding map_commonAction to primaryMap so it is updated with primary
Bind.secondaryKey(primaryMap, map_commonAction, new Fun.Function2<String, Integer, Record>() {
@Override
public String run(Integer recordID, Record r) {
return r.action;
}
});
//binding map_commonPerson to primaryMap so it is updated with primary
Bind.secondaryKey(primaryMap, map_commonPerson, new Fun.Function2<String, Integer, Record>() {
@Override
public String run(Integer recordID, Record r) {
return r.personWhoPerformedAction;
}
});
//method used to attain all records with some action
for (Object[] k : Fun.filter(map_commonAction, "someAction"))
{
Record obtainedRecord = primary.get(k[1]);
}
//method used to attain all records with some person
for (Object[] k : Fun.filter(map_commonPerson, "somePerson"))
{
Record obtainedRecord = primary.get(k[1]);
}
}
然后我检查了primaryMap是否工作,检查了几个int值,它返回了一条应该返回的记录
Record r1 = primaryMap.get(1);
Record r2 = primaryMap.get(2);
System.out.println(r1.toString());
System.out.println(r2.toString());
只有当我尝试遍历Fun.filter(map_common*,“something”)给出的内容时,它才会失败,但调用它的行为并不会使它失败,只是尝试遍历它。我是这样测试的:
//this method fails and causes and exception to be thrown
for (Object[] k : Fun.filter(map_commonPerson, "person"))
{
System.out.println(primaryMap.get(k[1]).toString());
}
//this method doesn't cause an exception to be thrown
Iterable<Object[]> x = Fun.filter(map_commonPerson, "person");
//此方法失败并导致引发异常
for(Object[]k:Fun.filter(映射普通人,“person”))
{
System.out.println(primaryMap.get(k[1]).toString());
}
//此方法不会导致引发异常
Iterable x=有趣的过滤器(映射普通人(“人”);
所以现在我被卡住了,我不知道我的地图怎么了。一旦我创建了一个新的数据库并插入了190亿项,它就可以完美地工作,但是一旦我关闭它并尝试重新打开它以进行更多的读取,它就会失败
有人能帮忙吗?谢谢。我找到了一个解决方案,它包括将map_commonPerson和map_commonAction组合到同一个map中,并使用Bind.secondaryKeys()而不是Bind.secondaryKey()函数。为什么会这样?每个映射只能安装一个修改侦听器吗 以下是关闭后正确恢复的工作数据库的代码:
static class Record implements Serializable
{
final String action;
final String categoryOfAction;
final String personWhoPerformedAction;
final Long timeOfOccurrence;
public record(String actn, String cat, String person, Long time)
{
action = actn;
categoryOfAction = cat;
personWhoPerformedAction = person;
timeOfOccurence = time;
}
}
static void main(String[] args)
{
DB thedb = DBMaker.newFileDB(new File("D:\\thedb.db")
.compressionEnable()
.closeOnJvmShutdown()
.mmapFileEnableIfSupported()
.transactionDisable()
.asyncWriteEnable()
.make();
//primaryMap maps each record to a unique ID
BTreeMap<Integer,Record> primaryMap = thedb.createTreeMap("pri")
.keySerializer(BTreeKeySerializer.INTEGER)
.makeOrGet();;
//this map holds the unique ID of every record with the same person or action
NavigableSet<Object[]> map_commonAttributes = thedb.createTreeSet("com_a")
.comparator(Fun.COMPARABLE_ARRAY_COMPARATOR)
.makeOrGet();
//binding map_commonAction to primaryMap so it is updated with primary
Bind.secondaryKeys(primaryMap, map_commonAttributes, new Fun.Function2<String[], Integer, Record>() {
@Override
public String[] run(Integer recordID, Record r) {
return new String[]{record.action, record.personWhoPerformedAction};
}
});
//method used to attain all records with same person or action
for (Object[] k : Fun.filter(map_commonAttribute, "somePersonOrAction"))
{
Record obtainedRecord = primary.get(k[1]);
}
静态类记录实现可序列化
{
最终串动作;
最终字符串分类;
执行动作的最后一个串人;
最后长时间发生;
公共记录(字符串actn、字符串cat、字符串人、长时间)
{
动作=actn;
类别=猫;
执行行动的人=人;
发生时间=时间;
}
}
静态void main(字符串[]参数)
{
DB thedb=DBMaker.newFileDB(新文件(“D:\\thedb.DB”)
.compressionEnable()
.closeOnJvmShutdown()
.mmapFileEnableIfSupported()
.transactionDisable()
.asyncWriteEnable()
.make();
//primaryMap将每个记录映射到一个唯一的ID
BTreeMap primaryMap=thedb.createTreeMap(“pri”)
.keySerializer(BTreeKeySerializer.INTEGER)
.makeOrGet();;
//此映射保存具有相同人员或操作的每个记录的唯一ID
NavigableSet map_commonAttributes=thedb.createTreeSet(“com_a”)
.比较器(有趣的.可比数组比较器)
.makeOrGet();
//将map_commonAction绑定到primaryMap,以便使用primary更新它
Bind.secondaryKeys(primaryMap、map\u commonAttributes、new-Fun.Function2(){
@凌驾
公共字符串[]运行(整数记录ID,记录r){
返回新字符串[]{record.action,record.personWhoPerformedAction};
}
});
//用同一人或同一动作获取所有记录的方法
for(对象[]k:Fun.filter(映射公共属性,“SomePersonAction”))
{
Record AcquiredRecord=primary.get(k[1]);
}
//this method fails and causes and exception to be thrown
for (Object[] k : Fun.filter(map_commonPerson, "person"))
{
System.out.println(primaryMap.get(k[1]).toString());
}
//this method doesn't cause an exception to be thrown
Iterable<Object[]> x = Fun.filter(map_commonPerson, "person");
static class Record implements Serializable
{
final String action;
final String categoryOfAction;
final String personWhoPerformedAction;
final Long timeOfOccurrence;
public record(String actn, String cat, String person, Long time)
{
action = actn;
categoryOfAction = cat;
personWhoPerformedAction = person;
timeOfOccurence = time;
}
}
static void main(String[] args)
{
DB thedb = DBMaker.newFileDB(new File("D:\\thedb.db")
.compressionEnable()
.closeOnJvmShutdown()
.mmapFileEnableIfSupported()
.transactionDisable()
.asyncWriteEnable()
.make();
//primaryMap maps each record to a unique ID
BTreeMap<Integer,Record> primaryMap = thedb.createTreeMap("pri")
.keySerializer(BTreeKeySerializer.INTEGER)
.makeOrGet();;
//this map holds the unique ID of every record with the same person or action
NavigableSet<Object[]> map_commonAttributes = thedb.createTreeSet("com_a")
.comparator(Fun.COMPARABLE_ARRAY_COMPARATOR)
.makeOrGet();
//binding map_commonAction to primaryMap so it is updated with primary
Bind.secondaryKeys(primaryMap, map_commonAttributes, new Fun.Function2<String[], Integer, Record>() {
@Override
public String[] run(Integer recordID, Record r) {
return new String[]{record.action, record.personWhoPerformedAction};
}
});
//method used to attain all records with same person or action
for (Object[] k : Fun.filter(map_commonAttribute, "somePersonOrAction"))
{
Record obtainedRecord = primary.get(k[1]);
}