Nested Maps

557 views
Skip to first unread message

Kelly Davis

unread,
Nov 15, 2012, 4:51:44 PM11/15/12
to jd...@googlegroups.com
Is it possible to create a nested map using mapdb:

Map<String,Map<Long,Map<String,Long>>> myMap = db.getTreeMap("myMap");

Jan Kotek

unread,
Nov 15, 2012, 4:59:44 PM11/15/12
to jd...@googlegroups.com
Nested map will be stored as normal java object.
So it is not very effective, but it is possible.

I would recommend using tuples with comparator. This is not yet added to
MapDB, bat can be handcoded

Map<Tuple3<String,Long,Long>,Long> map = db.getTreeMap("myMap");

Kelly Davis

unread,
Nov 15, 2012, 5:11:24 PM11/15/12
to jd...@googlegroups.com
Thanks for your reply.

When you say it is possible, do you mean that it will work, but it is not efficient or ? In my testing with the current snapshot, it doesn't bark when I construct the nested map, but when I retrieve one of the nested maps (in the same thread that I created them), they are empty - the values are not getting stored.

I was thinking of creating a compound key to collapse the nested maps, so that I would just have Map<String,Long>, similar to using a Tuple3. The problem is that I really need a Map<String,Map<String,Long>> because for each key in the top-level map I will insert all the values into a single row in a relational database table (where each key in the nested map maps to a column in the row). Without the nesting I think I would have to iterate over all the keys and essentially recreate the nested map before I could do the row inserts. It would probably make more sense to use some kind of embedded db or kv store.

Jan Kotek

unread,
Nov 15, 2012, 6:37:11 PM11/15/12
to jd...@googlegroups.com
>, but when I retrieve one of the nested maps (in the same thread that
I created them), they are empty
I guess you should put(store) submap AFTER you update it not BEFORE.

> Without the nesting I think I would have to iterate over all the keys
Use SortedMap submaps (tailMap,headMap) to only query interval you are
interested in.

> It would probably make more sense to use some kind of embedded db or
kv store.
Give a try to H2 database. It is surprisingly easy and intuitive.

Jan

Kelly Davis

unread,
Nov 16, 2012, 9:32:15 AM11/16/12
to jd...@googlegroups.com


On Thursday, November 15, 2012 6:37:16 PM UTC-5, Jan Kotek wrote:
>, but when I retrieve one of the nested maps (in the same thread that
I created them), they are empty
I guess you should put(store) submap AFTER you update it not BEFORE.

This seems a little unexpected to me, as it differs from how java.util.HashMap would behave. Would this occur if I was using the transaction functionality?
 

 > Without the nesting I think I would have to iterate over all the keys
Use SortedMap submaps (tailMap,headMap)  to only query interval you are
interested in.

good to know, I wasn't even aware of the tailMap, headMap methods :) I assume that this would require the key structure to be regular in order to use this, though. In my case, my key tuple would consist of Tuple<String,Long,Long,String>, where the last String would map to a column in the row that will be created. I may not have values in the map for each column (in which case I am currently inserting a zero in the column), so in order to use the SortedMap, I think I would need to have these populated with zero values, so that I could consistently iterate over all of the column keys.
 

 > It would probably make more sense to use some kind of embedded db or
kv store.
Give a try to H2 database. It is surprisingly easy and intuitive.

Thanks for the tip, that is exactly what I am going to try

Jan Kotek

unread,
Nov 16, 2012, 12:55:35 PM11/16/12
to jd...@googlegroups.com
>I guess you should put(store) submap AFTER you update it not BEFORE.

> This seems a little unexpected to me, as it differs from how
java.util.HashMap would behave. Would this occur if I was using the
transaction functionality?

Transactions wont change it. MapDB needs to know that you modified to
record.

Jan
> <javascript:>
Reply all
Reply to author
Forward
0 new messages