Search in sources :

Example 1 with MultiMapPartitionContainer

use of com.hazelcast.multimap.impl.MultiMapPartitionContainer in project hazelcast by hazelcast.

the class MultiMapTestUtil method getBackupMultiMap.

/**
 * Returns all backup entries of an {@link MultiMap} by a given map name.
 * <p>
 * Note: This method returns all backups from all nodes and doesn't consider the replica indexes.
 *
 * @param instances    the {@link HazelcastInstance} array to gather the data from
 * @param multiMapName the MultiMap name
 * @param <K>          type of the key
 * @param <V>          type of the value
 * @return a {@link Map} with the backup entries
 */
public static <K, V> Map<K, Collection<V>> getBackupMultiMap(HazelcastInstance[] instances, String multiMapName) {
    Map<K, Collection<V>> map = new HashMap<K, Collection<V>>();
    for (HazelcastInstance instance : instances) {
        NodeEngineImpl nodeEngine = getNodeEngineImpl(instance);
        MultiMapService mapService = nodeEngine.getService(MultiMapService.SERVICE_NAME);
        InternalPartitionService partitionService = nodeEngine.getPartitionService();
        SerializationService serializationService = nodeEngine.getSerializationService();
        for (int partitionId = 0; partitionId < partitionService.getPartitionCount(); partitionId++) {
            if (partitionService.isPartitionOwner(partitionId)) {
                continue;
            }
            MultiMapPartitionContainer partitionContainer = mapService.getPartitionContainer(partitionId);
            MultiMapContainer multiMapContainer = partitionContainer.getMultiMapContainer(multiMapName, false);
            if (multiMapContainer == null) {
                continue;
            }
            for (Map.Entry<Data, MultiMapValue> entry : multiMapContainer.getMultiMapValues().entrySet()) {
                K key = serializationService.toObject(entry.getKey());
                Collection<MultiMapRecord> collection = entry.getValue().getCollection(false);
                Collection<V> values = new ArrayList<V>(collection.size());
                for (MultiMapRecord record : collection) {
                    V value = serializationService.toObject(record.getObject());
                    values.add(value);
                }
                map.put(key, values);
            }
        }
    }
    return map;
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) MultiMapRecord(com.hazelcast.multimap.impl.MultiMapRecord) MultiMapValue(com.hazelcast.multimap.impl.MultiMapValue) HashMap(java.util.HashMap) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) MultiMapPartitionContainer(com.hazelcast.multimap.impl.MultiMapPartitionContainer) MultiMapContainer(com.hazelcast.multimap.impl.MultiMapContainer) MultiMapService(com.hazelcast.multimap.impl.MultiMapService) ArrayList(java.util.ArrayList) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)1 Data (com.hazelcast.internal.serialization.Data)1 SerializationService (com.hazelcast.internal.serialization.SerializationService)1 MultiMapContainer (com.hazelcast.multimap.impl.MultiMapContainer)1 MultiMapPartitionContainer (com.hazelcast.multimap.impl.MultiMapPartitionContainer)1 MultiMapRecord (com.hazelcast.multimap.impl.MultiMapRecord)1 MultiMapService (com.hazelcast.multimap.impl.MultiMapService)1 MultiMapValue (com.hazelcast.multimap.impl.MultiMapValue)1 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)1 Accessors.getNodeEngineImpl (com.hazelcast.test.Accessors.getNodeEngineImpl)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1