gpt4 book ai didi

java - 将其存储在mapdb中的最佳方法?

转载 作者:行者123 更新时间:2023-12-02 05:07:52 26 4
gpt4 key购买 nike

所以我试图用 MapDB 做一些事情,但我遇到了困难。我会尽力描述它:

我有四条数据,我们会说它是这样的:

1) String action; //the name of the action itself
2) String categoryOfAction; //the category of the action
3) Integer personWhoPerformedAction; //the person that did the action
4) Long timeOfOccurrence; //the time the action was done

同一操作可以在此数据库中由不同的人在不同的时间以及在不同的类别中执行多次。我想要三个单独的 map ,每个 map 都将数据组织成如下所示:

String[] actionOccurances = map1.get(action); //returns every occurrence of that action (possibly in an array), including who did that occurrence, time the occurrence occurred, and the category of that occurrence

Long latestOccurance = map2.get(action); //returns the latest occurrence of that action

String[] actionsPerformedByPerson = map3.get(personWhoPerformedAction); //returns every action that this person has done, including the category of that action, the time they performed that action, and the name of the action itself

所以,我想尽可能高效地做到这一点。我知道我可以做这样的事情:

DB thedb = DBMaker.newTempFileDB().make();

NavigableSet<Object[]> map1 = thedb.createTreeSet("actionOccurences").comparator(Fun.COMPARABLE_ARRAY_COMPARATOR).make();

HTreeMap<String, Long> map2 = thedb.getHashMap("lastOccurrence");

NavigableSet<Object[]> map3 = thedb.createTreeSet("actionsPerformedByPerson").comparator(Fun.COMPARABLE_ARRAY_COMPARATOR).make();

但我觉得这是错误的。一定有一种更有效的方法,我不必多次存储相同的数据,是吗?

我对 Bind 类及其函数( secondaryValues、mapInverse 等)进行了相当多的研究,但我似乎找不到一种方法来将这组数据映射成我想要的样子.

有什么帮助吗?谢谢。

最佳答案

啊!经过一段时间的研究,我找到了解决方案。我基本上为每条记录分配一个唯一的 ID,然后使用 MapDB 的 secondaryKey 绑定(bind)。事情是这样的:

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.newTempFileDB().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;
}
});


primaryMap.put(1, new Record("a", "abc", "person1", 123434L));
primaryMap.put(2, new Record("a", "abc", "person2", 322443L));
primaryMap.put(3, new Record("b", "def", "person2", 124243L));
primaryMap.put(4, new Record("b", "abc", "person1", 983243L));
primaryMap.put(5, new Record("c", "def", "person2", 999993L));


//this is how we attain all records with some action
for (Object[] k : Fun.filter(map_commonAction, "someAction"))
{
Record obtainedRecord = primary.get(k[1]);

}

//this is how we attain all records with some person
for (Object[] k : Fun.filter(map_commonPerson, "somePerson"))
{
Record obtainedRecord = primary.get(k[1]);

}

}

但是,如果您认为可以改进此解决方案,请仍然插话。谢谢!

关于java - 将其存储在mapdb中的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27609990/

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