/*
WARNING:
   TODO The defined hashtable types contain their own key inside. I think that it is not necessary. Review it.
   TODO hOrderTotal and hOrderFifo don't need to be hashtables.
*/
#ifndef HASHTYPES_H
#define HASHTYPES_H

#include "queue.h"
#include "qmqueue.h"

typedef unsigned int NUMERICKEYTYPE;

struct itemhMiddler {
  char key[MAXDBNAME + 1 + MAXTABLENAME + EOF_SIZE/*200*/ /*+ EXTRASIZE - RMEXTRASIZE*/]; // bdname.tablename
  int id_CC;     // id of conflict class
};

struct itemhCC {
  int id_CC;    // id of conflict class
  T_Queue_ptr queue; // queue of conflict class
  int master;   // master of CC
};

typedef struct {
  int tid;
  int idConnection;
  int idPetition;	
  int last_petition;
  int last_received;
  int is_delay_disconnect;
  int isfirst_petition;
  int backup_stp;
  //QM_message discon_msg;
} earlyDisconnect;

typedef struct {
  int id_petition;
  int recordsUpdates;   // number of records updates
  //int state; // 0 = processing, processed
  //int response; // 0 = not response, responsed
  //int queueCC; //The transaction has been inserted in this Queue
  int is_sendtodriver; // is the petition send to driver?
  int is_change_nodelocal_old; //is pending to change node local? 0 = false, 1 = true
  int is_change_nodelocal_new; //is pending to change node local? 0 = false, 1 = true
  long response_length; // length of response
  char *response; //response
} lastPetition;

struct itemhPetition {
  int id; // (num_connection*MAX_PETITION_PER_CLIENT) + num_petition
  int num_connection;   // number of connection
  int num_petition;     // number of petition
  int mclientId;        // id of thread is executing the petition
  
  lastPetition last;
  earlyDisconnect discon;
};

/*struct itemhClient {
  int id; // num_connection
  int mclientId; // id of management client
  char dbname[MAXDBNAME];  
};*/

/*struct itemhQMClient {
  int id; // num_connection
  earlyDisconnect discon;
};*/

struct itemhOrderTotal {
  int tid;
};

struct itemhOrderFifo {
  int tid;
};

struct itemhDBConns {
  char name[MAXDBNAME];
  T_QMQueue_ptr waitingforQM;
  //T_QMQueue_ptr reuseQM;
  T_Dllist_ptr reuseQM;
  //QM_Message connection_msg; //Compound by the way
  int open_conns;
};


/********************************************************************************
Intern Function.
   unsigned int tcl_hash(void *v);
   Purpose: Hash for string.
   Limitations: Nothing
   Returns: return the hash code.
*********************************************************************************/
size_t tcl_hash(void *v);

/********************************************************************************
Intern Function.
   unsigned int numeric_hash(void *v);
   Purpose: Hash for numeric.
   Limitations: Nothing
   Returns: return the hash code.
*********************************************************************************/
size_t numeric_hash(void *v);

size_t petition_hash(void *v);

/********************************************************************************
Intern Function.
   int keys_equal_fn (void *key1, void *key2);
   Purpose: Compare two keys.
   Limitations: Nothing
   Returns: if string1 less than string2 return < 0
      else if string1 identical to string2 return 0
      else if string1 greater than string2 return > 0
*********************************************************************************/
int key_string_compare(void *elem1, void *elem2);

/********************************************************************************
Intern Function.
   int keys_numeric_equal_fn (void *key1, void *key2);
   Purpose: Compare two keys.
   Limitations: Nothing
   Returns: if key1 less than key2 return < 0
      else if key1 identical to key2 return 0
      else if key1 greater than key2 return > 0
*********************************************************************************/
int keys_numeric_compare (void *elem1, void *elem2);

void *key_string_copy(void *elem);
void *key_numeric_copy(void *elem);
void *value_copy(void *elem);
void value_destroy(void *elem);
void key_destroy(void *elem);

void value_destroy_hPetition(void *);

#endif
