jankotek / mapdb

MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap-memory. It is a fast and easy to use embedded Java database engine.
https://mapdb.org
Apache License 2.0
4.87k stars 873 forks source link

Use case about Map having Sets as values #1004

Open marco-brandizi opened 2 years ago

marco-brandizi commented 2 years ago

I'm trying to refactor an application that uses a lot of Map<K, Set<V>> data stores in memory, ie, keeps indices of multiple values per key. I'd like to move all those data to disk, but I'm having issues.

Initially, I've tried to DB.hashSet() to get new values for keys that haven't yet, so the attempt was to have a HTreeMap of HTreeSet(s). But I've soon discovered that HTreeMap isn't serialisable (as per #837).

So, now I'm thinking of writing a Map wrapper that is backed by an HTreeSet<String>, where the latter will contain names for HTreeSets kept by MapDB, ie, whenever needed, the implementation would get the name of a set that is associated to a key (assuming such names are created via some 1-1 association to the keys) and use that to fetch the corresponding set from DB.hashSet( <name> ).

Does this make sense? Would it be performant enough?

Moreover, since I'm going to have many mutable keys, I would also need to be able to remove a given MapDB set when the corresponding key is removed. Unfortunately, it seems that there is no way to do so in MapDB anymore, or am I missing something about this?

Thanks in advance for any help.