IMap has no deleteAll(Set<k> keys) ? ... but has getAll() and putAll()

369 views
Skip to first unread message

Rob Bygrave

unread,
Feb 6, 2018, 4:55:59 PM2/6/18
to Hazelcast
So from a performance perspective I'd like to see and use a IMap.deleteAll( Set<k> keys).

It seems like an odd omission from the IMap API given that there is a getAll() and putAll().  So the fallback it to iterate the keys and call delete() on each one.  I see this was raised a few years ago: https://groups.google.com/forum/#!searchin/hazelcast/IMAP$20deleteall|sort:date/hazelcast/RC087UTMeq8/qM_k66-MO3AJ

Any reason why there is no deleteAll() ?


Thanks, Rob.

Ahmet Mircik

unread,
Feb 7, 2018, 4:53:48 AM2/7/18
to Hazelcast

Hi,

You can do deletes in batches without a deleteAll method. Below is a simple example to demonstrate 2 ways of doing delete-all. I'm using version 3.9.2.


import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.query.EntryObject;
import com.hazelcast.query.PredicateBuilder;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

public class DeleteAll {

    public static void main(String[] args) {
        HazelcastInstance node = Hazelcast.newHazelcastInstance();
        IMap<Integer, Integer> map = node.getMap("test");

        map.put(1, 1);
        map.put(2, 2);
        map.put(3, 3);

        deleteAllWithEntryProcessor(map);
        // deleteAllWithPredicate(map);

        int size = map.size();
        if (size != 0) {
            throw new AssertionError("Expected size is zero but found :" + size);
        }

        System.out.println("Map size is :" + size);

    }

    private static void deleteAllWithEntryProcessor(IMap<Integer, Integer> map) {
        map.executeOnKeys(new HashSet<Integer>(asList(1, 2, 3)), new DeleteKeys());
    }

    public static final class DeleteKeys extends AbstractEntryProcessor<Integer, Integer> {
        @Override
        public Object process(Map.Entry<Integer, Integer> entry) {
            return entry.setValue(null);
        }
    }

    private static void deleteAllWithPredicate(IMap<Integer, Integer> map) {
        PredicateBuilder predicateBuilder = new PredicateBuilder();
        EntryObject entryObject = predicateBuilder.getEntryObject();

        List<Integer> keysToRemove = asList(1, 2, 3);
        Iterator<Integer> iterator = keysToRemove.iterator();

        boolean isFirstKey = true;
        while (iterator.hasNext()) {
            Integer key = iterator.next();

            if (isFirstKey) {
                isFirstKey = false;
                predicateBuilder = entryObject.key().equal(key);
            } else {
                predicateBuilder.or(entryObject.key().equal(key));
            }
        }

        map.removeAll(predicateBuilder);
    }
}

--
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/cf7b6c01-f1fe-49dd-b0e1-9844c437ab1f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Bygrave

unread,
Feb 12, 2018, 7:43:11 PM2/12/18
to haze...@googlegroups.com
Thanks !!

--
You received this message because you are subscribed to a topic in the Google Groups "Hazelcast" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hazelcast/IxCeRprNVtw/unsubscribe.
To unsubscribe from this group and all its topics, 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.
Reply all
Reply to author
Forward
0 new messages