/*
 * Copyright (c) 2008-2013, 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.map.operation;

import com.hazelcast.map.MapDataSerializerHook;
import com.hazelcast.map.record.Record;
import com.hazelcast.map.record.RecordReplicationInfo;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.BackupOperation;

import java.io.IOException;

public final class PutBackupOperation extends KeyBasedMapOperation implements BackupOperation, IdentifiedDataSerializable {

    // todo unlockKey is a logic just used in transactional put operations. It complicates here there should be another Operation for that logic. e.g. TxnSetBackup
    private boolean unlockKey = false;
    private RecordReplicationInfo replicationInfo;

    public PutBackupOperation(String name, Data dataKey, Data dataValue) {
        super(name, dataKey, dataValue);
    }

    public PutBackupOperation(String name, Data dataKey, Data dataValue, RecordReplicationInfo replicationInfo) {
        super(name, dataKey, dataValue);
        this.replicationInfo = replicationInfo;
    }

    public PutBackupOperation(String name, Data dataKey, Data dataValue, boolean unlockKey) {
        super(name, dataKey, dataValue);
        this.unlockKey = unlockKey;
    }

    public PutBackupOperation() {
    }

    public void run() {
        Record record = recordStore.getRecord(dataKey);
        if (record == null) {
            record = mapService.createRecord(name, dataKey, dataValue, ttl, false);
            updateSizeEstimator(calculateRecordSize(record));
            recordStore.putRecord(dataKey, record);
        } else {
            updateSizeEstimator(-calculateRecordSize(record));
            mapContainer.getRecordFactory().setValue(record, dataValue);
            updateSizeEstimator(calculateRecordSize(record));
        }

        if(replicationInfo != null) {
            mapService.applyRecordReplicationInfo(record, name, replicationInfo);
        }

        if (unlockKey) {
            recordStore.forceUnlock(dataKey);
        }
    }

    @Override
    public Object getResponse() {
        return Boolean.TRUE;
    }

    protected void writeInternal(ObjectDataOutput out) throws IOException {
        super.writeInternal(out);
        out.writeBoolean(unlockKey);
        out.writeObject(replicationInfo);
    }

    protected void readInternal(ObjectDataInput in) throws IOException {
        super.readInternal(in);
        unlockKey = in.readBoolean();
        replicationInfo = in.readObject();
    }

    @Override
    public String toString() {
        return "PutBackupOperation{" + name + "}";
    }

    public int getFactoryId() {
        return MapDataSerializerHook.F_ID;
    }

    public int getId() {
        return MapDataSerializerHook.PUT_BACKUP;
    }

    private void updateSizeEstimator( long recordSize ) {
        recordStore.getSizeEstimator().add( recordSize );
    }

    private long calculateRecordSize( Record record ) {
        return recordStore.getSizeEstimator().getCost(record);
    }

}

@@ -2998,24 +2998,8 @@
     
-out.writeObject(
 repl
@@ -3005,24 +3005,38 @@
 licationInfo
+.writeData(out
 );%0A    %7D%0A%0A  
@@ -3206,22 +3206,72 @@
 o = 
-in.readObject(
+new RecordReplicationInfo();%0A        replicationInfo.readData(in
 );%0A 

check near cache