IMap.keySet().removeAll(collection) behaviour

624 views
Skip to first unread message

Joan Balagueró

unread,
Jan 19, 2017, 2:40:22 PM1/19/17
to Hazelcast
Hello,

I have an IMap and I want to remove a set of keys using IMap.keySet().removeAll(collection<String>).

What is better?

1. Perform IMap.keySet().removeAll(collection<String>) in one node. 

2. Distribute a execution to all nodes of "IMap.localKeySet().removeAll(local_collection<String>)", so the collection contains those strings that are local to each node.


I was wondering if Hazelcast, internally, performs the point 1 using the system explained in point 2.

Thanks,

Joan.

Vassilis Bekiaris

unread,
Jan 19, 2017, 4:45:36 PM1/19/17
to Hazelcast
Hello Joan,

both keySet and localKeySet indicate in their javadoc that "The set is NOT backed by the map, so changes to the map are NOT reflected in the set, and vice-versa." so I am not sure the two methods above will help you achieve what you want.
Instead, if you are already aware of a specific collection of keys you want to remove, you could use executeOnKeys(Set<K> keys, EntryProcessor entryProcessor) with an entry processor that removes the entry by executing entry.setValue(null) in its process method. In a relevant side-note, in 3.8 a new method removeAll(Predicate<K, V> predicate) has been added in IMap interface which could also help in your use case.

Cheers!
Vassilis

Joan Balagueró

unread,
Jan 19, 2017, 6:23:21 PM1/19/17
to Hazelcast
Hello Vassillis,

Thanks. If I use IMap.removeAll(predicate) I should use the "in" predicate:
this.cache.removeAll(new SqlPredicate("__key IN (v1,v2,v3)"));

I should generate the string "v1,v2,v3", but I have two questions:

1. Is this suitable if I have thousands of keys to remove?
2. My values can contains commas, so the string I'm going to generate could not be correct.

Any solution for this? Or my only choice is the EntryProcessor?

Thanks,

Joan.

Vassilis Bekiaris

unread,
Jan 20, 2017, 2:05:20 AM1/20/17
to haze...@googlegroups.com
Hi Joan,

instead of SqlPredicate you can use the built-in InPredicate which fits nicely your use case:
new com.hazelcast.query.impl.predicates.InPredicate("__key", key1, key2, key3);
or if you need to implement some more custom processing logic you can always implement your own Predicate.

Best,
Vassilis

Joan Balagueró

unread,
Jan 20, 2017, 2:42:34 AM1/20/17
to Hazelcast
Hi Vassilis,

Ok, thanks. Not sure how to use the "In" predicate in my case, because I have a collection of Strings, and with the "In" predicate one must pass the keys one by one as arguments.

I have thought this solution:

  EntryObject e = new PredicateBuilder().getEntryObject();
  PredicateBuilder predicate = e.key().equal("");
  for (String cacheId : cacheIds) predicate = e.key().equal(cacheId).or(predicate);

Looks fine?

Joan.

Vassilis Bekiaris

unread,
Jan 20, 2017, 3:19:20 AM1/20/17
to haze...@googlegroups.com
Hi Joan,

you can transform your collection to an array so that it is suitable to be used as a varargs argument in InPredicate's constructor:

new InPredicate("__key", yourCollectionOfStrings.toArray(new String[0]));

This way you provide an optimized predicate, instead of chaining several OR predicates and relying on internal optimizations to detect this and flatten the predicate to you to a single IN (key1, key2, key3,...) predicate.

Cheers!
Vassilis



--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+unsubscribe@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/4dcaabfb-4b09-461a-88bd-d4d5ec6d80e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vishalpat...@gmail.com

unread,
Jul 15, 2020, 3:25:49 AM7/15/20
to Hazelcast
This is better? - loadedMap.removeAll(mapEntry -> loadedMap.localKeySet().contains(mapEntry.getKey()));
Reply all
Reply to author
Forward
0 new messages