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

com.hazelcast.crdt.AbstractCRDTReplicationOperation Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2018, 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.crdt;

import com.hazelcast.internal.partition.MigrationCycleOperation;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.Operation;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import static com.hazelcast.util.MapUtil.createHashMap;

/**
 * Base class for CRDT replication operations. It supports replicating a
 * map of {@link IdentifiedDataSerializable} values. In the case of CRDT
 * replication, this will be a map from map name to CRDT state.
 * Each concrete implementation of this class should be responsible for a
 * single CRDT type and the replication map should only contain CRDT
 * states for this CRDT type.
 *
 * @param  the CRDT type
 */
public abstract class AbstractCRDTReplicationOperation
        extends Operation implements IdentifiedDataSerializable, MigrationCycleOperation {
    /** The map from CRDT name to CRDT state */
    private Map replicationData;

    protected AbstractCRDTReplicationOperation() {
    }

    /**
     * Constructs the replication operation.
     *
     * @param replicationData the map of CRDT states to replicate
     */
    public AbstractCRDTReplicationOperation(Map replicationData) {
        this.replicationData = replicationData;
    }

    @Override
    public void run() throws Exception {
        final CRDTReplicationAwareService service = getService();
        for (Map.Entry entry : replicationData.entrySet()) {
            service.merge(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public void afterRun() throws Exception {
        super.afterRun();
        final CRDTReplicationMigrationService replicationMigrationService =
                getNodeEngine().getService(CRDTReplicationMigrationService.SERVICE_NAME);
        replicationMigrationService.scheduleMigrationTask(0);
    }

    @Override
    public int getFactoryId() {
        return CRDTDataSerializerHook.F_ID;
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        out.writeInt(replicationData.size());
        for (Entry entry : replicationData.entrySet()) {
            out.writeUTF(entry.getKey());
            out.writeObject(entry.getValue());
        }
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        final int mapSize = in.readInt();
        replicationData = createHashMap(mapSize);
        for (int i = 0; i < mapSize; i++) {
            final String name = in.readUTF();
            final T crdt = in.readObject();
            replicationData.put(name, crdt);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy