We have a current java application built with Vert.x using simple local HashMaps. In order to distribute our application on several nodes, we would like to replace local HashMaps by a single Hazelcast IMap.
Basically we have our 2 HashMaps :
Map<String, Group> // <groupId - group>
Map<String, Set<String> // <memberId - set of groupIds>
We have 2 HashMaps in order to query easily our groups by their ids and to know to which groups belong our members.
However, with IMap, according to documentation, it should be possible to create only one map :
IMap<String, Group> // <groupId - group>
class Group {
String id;
Set<Member> members;
}
class Member {
String id;
// some data
}
and we should be able to perform :
Collection<Group> groups = imap.values(Predicates.in("members", varargs of member ids))
So, I have 4 questions :
imap.values contain duplicates (same groupId) ? Do I have to perform a kind of distinct operation ?StreamStage like that :StreamStage.flatMap(memberIds -> Traversers.traverseIterable(imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0])))
Thanks a lot !
yes
it should not contain duplicates if your original map doesn't contain them and the IMap isn't concurrently updated
depends on your use case, I can't tell in general
This is not good as the imap.values call is blocking and the function in flatMap must not block. By blocking I mean it does blocking IO operations. Also you can't capture the imap instance from a local variable because it's not serializable and Jet can't send it to the member. But you can use mapUsingService and use ServiceFactories.imapService like this:
.mapUsingService(
ServiceFactories.iMapService("my_map").toNonCooperative(),
(imap, memberIds) ->
imap.values(Predicates.in("memberIds", memberIds.toArray(new String[0]))))