/*
   Copyright (C) 2003, 2005, 2006 MySQL AB, 2008 Sun Microsystems, Inc.
    All rights reserved. Use is subject to license terms.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#ifndef HUGO_ASYNCH_TRANSACTIONS_HPP
#define HUGO_ASYNCH_TRANSACTIONS_HPP


#include <NDBT.hpp>
#include <HugoCalculator.hpp>
#include <HugoTransactions.hpp>

class HugoAsynchTransactions : private HugoTransactions {
public:
  HugoAsynchTransactions(const NdbDictionary::Table&);
  ~HugoAsynchTransactions();
  int loadTableAsynch(Ndb*, 
		      int records = 0,
		      int batch = 1,
		      int trans = 1,
		      int operations = 1);
  int pkReadRecordsAsynch(Ndb*, 
			  int records = 0,
			  int batch= 1,
			  int trans = 1,
			  int operations = 1);
  int pkUpdateRecordsAsynch(Ndb*, 
			    int records = 0,
			    int batch= 1,
			    int trans = 1,
			    int operations = 1);
  int pkDelRecordsAsynch(Ndb*, 
			 int records = 0,
			 int batch = 1,
			 int trans = 1,
			 int operations = 1);

private:  
  enum NDB_OPERATION {NO_INSERT, NO_UPDATE, NO_READ, NO_DELETE};

  long transactionsCompleted;

  struct TransactionInfo
  {
    HugoAsynchTransactions* hugoP;
    NdbConnection* transaction;
    int startRecordId;
    int numRecords;
    int resultRowStartIndex;
    int retries;
    NDB_OPERATION opType;
  };

  TransactionInfo* transInfo;
  Ndb* theNdb;

  /* Work description */
  int totalLoops;
  int recordsPerLoop;
  int maxOpsPerTrans;
  NDB_OPERATION operationType;
  ExecType execType;

  /* Progress description */
  int nextUnProcessedRecord;
  int loopNum;
  int totalCompletedRecords;
  int maxUsedRetries;
  bool finished;
  int testResult;

  void allocTransactions(int trans, int maxOpsPerTrans);
  void deallocTransactions();

  int getNextWorkTask(int* startRecordId, int* numRecords);

  int defineUpdateOpsForTask(TransactionInfo* tInfo);
  int defineTransactionForTask(TransactionInfo* tInfo, ExecType taskExecType);

  int beginNewTask(TransactionInfo* tInfo);
  static void callbackFunc(int result, NdbConnection* trans, void* anObject);
  void callback(int result, NdbConnection* trans, TransactionInfo* tInfo);

  int executeAsynchOperation(Ndb*,		      
                             int records,
                             int batch,
                             int trans,
                             int operations,
                             NDB_OPERATION theOperation,
                             ExecType theType = Commit);
};



#endif

