/*
 * 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.map.impl.operation;

import com.hazelcast.core.EntryEventType;
import com.hazelcast.core.EntryView;
import com.hazelcast.map.impl.record.Record;
import com.hazelcast.map.impl.record.RecordInfo;
import com.hazelcast.map.impl.record.Records;
import com.hazelcast.map.merge.MapMergePolicy;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.Operation;

import java.io.IOException;

public class MergeOperation extends BasePutOperation {

    private MapMergePolicy mergePolicy;
    private EntryView<Data, Data> mergingEntry;
    private boolean merged;
    private Data mergingValue;
    private boolean disableWanReplicationEvent;

    public MergeOperation(String name, Data dataKey, EntryView<Data, Data> entryView,
                          MapMergePolicy policy) {
        this(name, dataKey, entryView, policy, false);
    }

    public MergeOperation(String name, Data dataKey, EntryView<Data, Data> entryView,
                          MapMergePolicy policy, boolean disableWanReplicationEvent) {
        super(name, dataKey, null);
        this.mergingEntry = entryView;
        this.mergePolicy = policy;
        this.disableWanReplicationEvent = disableWanReplicationEvent;
    }

    public MergeOperation() {
    }

    @Override
    public void run() {
        Record oldRecord = recordStore.getRecord(dataKey);
        if (oldRecord != null) {
            dataOldValue = mapServiceContext.toData(oldRecord.getValue());
        }
        merged = recordStore.merge(dataKey, mergingEntry, mergePolicy);
        if (merged) {
            Record record = recordStore.getRecord(dataKey);
            if (record != null) {
                dataValue = mapServiceContext.toData(record.getValue());
                mergingValue = mapServiceContext.toData(mergingEntry.getValue());
            }
        }
    }

    @Override
    public Object getResponse() {
        return merged;
    }

    @Override
    public boolean shouldBackup() {
        return merged;
    }

    @Override
    public void afterRun() {
        if (merged) {
            mapServiceContext.interceptAfterPut(name, dataValue);
            mapEventPublisher.publishEvent(getCallerAddress(), name, EntryEventType.MERGED, dataKey, dataOldValue,
                    dataValue, mergingValue);
            invalidateNearCache(dataKey);
            evict(dataKey);
        }
    }

    @Override
    public Operation getBackupOperation() {
        if (dataValue == null) {
            return new RemoveBackupOperation(name, dataKey, false, disableWanReplicationEvent);
        } else {
            final Record record = recordStore.getRecord(dataKey);
            final RecordInfo replicationInfo = Records.buildRecordInfo(record);
            return new PutBackupOperation(name, dataKey, dataValue, replicationInfo, false, false, disableWanReplicationEvent);
        }
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        super.writeInternal(out);
        out.writeObject(mergingEntry);
        out.writeObject(mergePolicy);
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        super.readInternal(in);
        mergingEntry = in.readObject();
        mergePolicy = in.readObject();
    }
}

@@ -1181,16 +1181,472 @@
 tion %7B%0A%0A
+    /**%0A     * This is a flag to understand if the MergeOperation is created by a WAN event and setting%0A     * %7B@link #disableWanReplicationEvent%7D.%0A     * It's needed to not to break backward compatibility by adding %7B@link #disableWanReplicationEvent%7D%0A     * to %7B@link #writeInternal(ObjectDataOutput)%7D and %7B@link #readInternal(ObjectDataInput)%7D methods.%0A     */%0A    private static final int WAN_TTL = -999;%0A    private static final int DEFAULT_TTL = -1;%0A%0A
     priv
@@ -4132,32 +4132,111 @@
 s IOException %7B%0A
+        if (disableWanReplicationEvent) %7B%0A            ttl = WAN_TTL;%0A        %7D%0A
         super.wr
@@ -4531,20 +4531,183 @@
 adObject();%0A
+        if (ttl == WAN_TTL) %7B%0A            disableWanReplicationEvent = true;%0A            //reset ttl to its default value%0A            ttl = DEFAULT_TTL;%0A        %7D%0A
     %7D%0A%7D%0A

Use ttl field as disableWanReplicationEvent flag