/* This file is part of VoltDB.
 * Copyright (C) 2008-2014 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.messaging;

import com.google_voltpatches.common.collect.ImmutableSet;
import com.google_voltpatches.common.collect.Sets;
import org.voltcore.messaging.VoltMessage;
import org.voltdb.StoredProcedureInvocation;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Set;

/**
 * A message sent from the involved partitions to the
 * multipart replayer during command log replay.
 */
public class MpReplayMessage extends VoltMessage {
    long m_txnId;
    long m_uniqueId;
    int m_partitionId;
    Set<Integer> m_involvedPartitions;
    StoredProcedureInvocation m_invocation;

    /** Empty constructor for de-serialization */
    MpReplayMessage() {
        super();
    }

    public MpReplayMessage(long txnId, long uniqueId, int partitionId, Collection<Integer> involvedPartitions,
                           StoredProcedureInvocation invocation)
    {
        super();

        m_txnId = txnId;
        m_uniqueId = uniqueId;
        m_partitionId = partitionId;
        m_involvedPartitions = ImmutableSet.copyOf(involvedPartitions);
        m_invocation = invocation;
    }

    public long getTxnId()
    {
        return m_txnId;
    }

    public long getUniqueId()
    {
        return m_uniqueId;
    }

    public int getPartitionId()
    {
        return m_partitionId;
    }

    public Set<Integer> getInvolvedPartitions()
    {
        return m_involvedPartitions;
    }

    public StoredProcedureInvocation getInvocation()
    {
        return m_invocation;
    }

    @Override
    public int getSerializedSize()
    {
        int size = super.getSerializedSize();
        size +=   8 // m_txnId
                + 8 // m_uniqueId
                + 4 // m_partitionId
                + 4 // m_involvedPartitions.size()
                + 4 * m_involvedPartitions.size()
                + m_invocation.getSerializedSize();
        return size;
    }

    @Override
    protected void initFromBuffer(ByteBuffer buf) throws IOException
    {
        m_txnId = buf.getLong();
        m_uniqueId = buf.getLong();
        m_partitionId = buf.getInt();
        int partitionCount = buf.getInt();
        m_involvedPartitions = Sets.newHashSet();
        for (int i = 0; i < partitionCount; i++) {
            m_involvedPartitions.add(buf.getInt());
        }
        m_invocation = new StoredProcedureInvocation();
        m_invocation.initFromBuffer(buf);
    }

    @Override
    public void flattenToBuffer(ByteBuffer buf) throws IOException
    {
        buf.put(VoltDbMessageFactory.MP_REPLAY_ID);
        buf.putLong(m_txnId);
        buf.putLong(m_uniqueId);
        buf.putInt(m_partitionId);
        buf.putInt(m_involvedPartitions.size());
        for (int pid : m_involvedPartitions) {
            buf.putInt(pid);
        }
        m_invocation.flattenToBuffer(buf);

        assert(buf.capacity() == buf.position());
        buf.limit(buf.position());
    }
}

@@ -2581,32 +2581,34 @@
 artitions.size()
+;%0A
 %0A               
@@ -2600,25 +2600,63 @@
         
-        +
+if (m_invocation != null) %7B%0A            size +=
  m_invoc
@@ -2674,32 +2674,43 @@
 rializedSize();%0A
+        %7D%0A%0A
         return s
@@ -3115,32 +3115,72 @@
 t());%0A        %7D%0A
+%0A        if (buf.remaining() %3E 0) %7B%0A    
         m_invoca
@@ -3219,32 +3219,36 @@
 tion();%0A        
+    
 m_invocation.ini
@@ -3265,16 +3265,76 @@
 r(buf);%0A
+        %7D else %7B%0A            m_invocation = null;%0A        %7D%0A
     %7D%0A%0A 
@@ -3700,24 +3700,65 @@
 ;%0A        %7D%0A
+%0A        if (m_invocation != null) %7B%0A    
         m_in
@@ -3787,16 +3787,26 @@
 er(buf);
+%0A        %7D
 %0A%0A      

Allow null invocation in MpReplayMessage.