All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.multimap.impl.operations.MultiMapMigrationOperation Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.multimap.impl.operations;

import com.hazelcast.config.MultiMapConfig;
import com.hazelcast.multimap.impl.MultiMapRecord;
import com.hazelcast.multimap.impl.MultiMapService;
import com.hazelcast.multimap.impl.MultiMapValue;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.AbstractOperation;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MultiMapMigrationOperation extends AbstractOperation {

    Map map;

    public MultiMapMigrationOperation() {
    }

    public MultiMapMigrationOperation(Map map) {
        this.map = map;
    }

    @Override
    public void run() throws Exception {
        MultiMapService service = getService();
        service.insertMigratedData(getPartitionId(), map);
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        out.writeInt(map.size());
        for (Map.Entry entry : map.entrySet()) {
            String name = entry.getKey();
            out.writeUTF(name);

            Map collections = entry.getValue();
            out.writeInt(collections.size());
            for (Map.Entry collectionEntry : collections.entrySet()) {
                Data key = collectionEntry.getKey();
                out.writeData(key);
                MultiMapValue multiMapValue = collectionEntry.getValue();
                Collection coll = multiMapValue.getCollection(false);
                out.writeInt(coll.size());
                String collectionType = MultiMapConfig.ValueCollectionType.SET.name();
                if (coll instanceof List) {
                    collectionType = MultiMapConfig.ValueCollectionType.LIST.name();
                }
                out.writeUTF(collectionType);
                for (MultiMapRecord record : coll) {
                    record.writeData(out);
                }
            }
        }
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        int mapSize = in.readInt();
        map = new HashMap(mapSize);
        for (int i = 0; i < mapSize; i++) {
            String name = in.readUTF();
            int collectionSize = in.readInt();
            Map collections = new HashMap();
            for (int j = 0; j < collectionSize; j++) {
                Data key = in.readData();
                int collSize = in.readInt();
                String collectionType = in.readUTF();
                Collection coll;
                if (collectionType.equals(MultiMapConfig.ValueCollectionType.SET.name())) {
                    coll = new HashSet();
                } else {
                    coll = new LinkedList();
                }
                for (int k = 0; k < collSize; k++) {
                    MultiMapRecord record = new MultiMapRecord();
                    record.readData(in);
                    coll.add(record);
                }
                collections.put(key, new MultiMapValue(coll));

            }
            map.put(name, collections);
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy