/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package txnIdSelfCheck.procedures;

import org.voltdb.SQLStmt;
import org.voltdb.VoltProcedure;
import org.voltdb.VoltTable;
import org.voltdb.VoltTable.ColumnInfo;
import org.voltdb.VoltType;

public class Summarize extends VoltProcedure {

    final SQLStmt dateSqlP = new SQLStmt("select ts from partitioned order by ts desc limit 1;");
    final SQLStmt dateSqlR = new SQLStmt("select ts from partitioned order by ts desc limit 1;");
    final SQLStmt countSqlP = new SQLStmt("select sum(cnt) from partitioned;");
    final SQLStmt countSqlR = new SQLStmt("select sum(cnt) from partitioned;");

    public VoltTable run() {
        voltQueueSQL(dateSqlP, EXPECT_ZERO_OR_ONE_ROW);
        voltQueueSQL(dateSqlR, EXPECT_ZERO_OR_ONE_ROW);
        voltQueueSQL(countSqlP, EXPECT_ZERO_OR_ONE_ROW);
        voltQueueSQL(countSqlR, EXPECT_ZERO_OR_ONE_ROW);
        VoltTable[] results = voltExecuteSQL(true);

        long latest1 = results[0].getRowCount() > 0 ?
                results[0].asScalarLong() : 0;
        long latest2 = results[1].getRowCount() > 0 ?
                results[1].asScalarLong() : 0;

        long count1 = results[2].getRowCount() > 0 ?
                results[2].asScalarLong() : 0;
        long count2 = results[3].getRowCount() > 0 ?
                results[3].asScalarLong() : 0;

        long latest = Math.max(latest1, latest2);
        long sum = count1 + count2;

        VoltTable t = new VoltTable(
                new ColumnInfo("ts", VoltType.BIGINT),
                new ColumnInfo("count", VoltType.BIGINT));

        t.addRow(latest, sum);
        return t;
    }
}

@@ -1519,33 +1519,32 @@
 ect ts from 
-partition
+replicat
 ed order by 
@@ -1609,32 +1609,27 @@
 tmt(%22select 
-sum(
 cnt
-)
  from partit
@@ -1629,24 +1629,64 @@
  partitioned
+ where cid = ? order by cnt desc limit 1
 ;%22);%0A    fin
@@ -1732,179 +1732,111 @@
 ect 
-sum(
 cnt
-)
  from 
-partitioned;%22);%0A%0A    public VoltTable run() %7B%0A        voltQueueSQL(dateSqlP, EXPECT_ZERO_OR_ONE_ROW);%0A  
+replicated where cid = ? order by cnt desc limit 1;%22);%0A%0A
     
-  voltQueueSQL(dateSqlR, EXPECT_ZERO_OR_ONE_ROW);
+public long countForCid(int cid) %7B
 %0A   
@@ -1878,32 +1878,37 @@
 _ZERO_OR_ONE_ROW
+, cid
 );%0A        voltQ
@@ -1948,16 +1948,21 @@
 _ONE_ROW
+, cid
 );%0A     
@@ -2005,12 +2005,8 @@
 SQL(
-true
 );%0A%0A
@@ -2018,21 +2018,20 @@
    long 
-lates
+coun
 t1 = res
@@ -2118,21 +2118,20 @@
    long 
-lates
+coun
 t2 = res
@@ -2210,33 +2210,527 @@
 0;%0A%0A        
-long coun
+if ((count1 %3E 0) && (count2 %3E 0)) %7B%0A            if (count1 != count2) %7B%0A                throw new VoltAbortException(%0A                        %22hybrid updates left partitioned and replicated tables in a different state.%22);%0A            %7D%0A        %7D%0A%0A        return Math.max(count1, count2);%0A    %7D%0A%0A    public VoltTable run() %7B%0A        voltQueueSQL(dateSqlP, EXPECT_ZERO_OR_ONE_ROW);%0A        voltQueueSQL(dateSqlR, EXPECT_ZERO_OR_ONE_ROW);%0A        VoltTable%5B%5D results = voltExecuteSQL();%0A%0A        long lates
 t1 = results
@@ -2730,17 +2730,17 @@
 results%5B
-2
+0
 %5D.getRow
@@ -2777,17 +2777,17 @@
 results%5B
-2
+0
 %5D.asScal
@@ -2805,36 +2805,37 @@
 0;%0A        long 
-coun
+lates
 t2 = results%5B3%5D.
@@ -2831,17 +2831,17 @@
 results%5B
-3
+1
 %5D.getRow
@@ -2882,9 +2882,9 @@
 lts%5B
-3
+1
 %5D.as
@@ -2948,24 +2948,25 @@
 , latest2);%0A
+%0A
         long
@@ -2976,24 +2976,107 @@
 m = 
-count1 + count2;
+0;%0A        for (int i = 0; i %3C= Byte.MAX_VALUE; i++) %7B%0A            sum += countForCid(i);%0A        %7D
 %0A%0A  

Fix the math for the count logging on startup.