edits
[iotcloud.git] / version2 / src / C / Table.cc
index dce3a8f1969e4e960419b080ffe8ec17595c523a..531337cc5336f1c69dac6aed68018b2899355c98 100644 (file)
@@ -46,6 +46,7 @@ Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, i
        oldestLiveSlotSequenceNumver(1),
        localMachineId(_localMachineId),
        sequenceNumber(0),
+       localSequenceNumber(0),
        localTransactionSequenceNumber(0),
        lastTransactionSequenceNumberSpeculatedOn(0),
        oldestTransactionSequenceNumberSpeculatedOn(0),
@@ -108,6 +109,7 @@ Table::Table(CloudComm *_cloud, int64_t _localMachineId) :
        oldestLiveSlotSequenceNumver(1),
        localMachineId(_localMachineId),
        sequenceNumber(0),
+       localSequenceNumber(0),
        localTransactionSequenceNumber(0),
        lastTransactionSequenceNumberSpeculatedOn(0),
        oldestTransactionSequenceNumberSpeculatedOn(0),
@@ -156,6 +158,115 @@ Table::Table(CloudComm *_cloud, int64_t _localMachineId) :
        init();
 }
 
+Table::~Table() {
+       delete cloud;
+       delete random;
+       delete buffer;
+       // init data structs
+       delete committedKeyValueTable;
+       delete speculatedKeyValueTable;
+       delete pendingTransactionSpeculatedKeyValueTable;
+       delete liveNewKeyTable;
+       {
+               SetIterator<int64_t, Pair<int64_t, Liveness *> *> *lmit = getKeyIterator(lastMessageTable);
+               while (lmit->hasNext()) {
+                       Pair<int64_t, Liveness *> * pair = lastMessageTable->get(lmit->next());
+               }
+               delete lmit;
+               delete lastMessageTable;
+       }
+       if (pendingTransactionBuilder != NULL)
+               delete pendingTransactionBuilder;
+       {
+               SetIterator<int64_t, Hashset<RejectedMessage *> *> *rmit = getKeyIterator(rejectedMessageWatchVectorTable);
+               while(rmit->hasNext()) {
+                       int64_t machineid = rmit->next();
+                       Hashset<RejectedMessage *> * rmset = rejectedMessageWatchVectorTable->get(machineid);
+                       SetIterator<RejectedMessage *, RejectedMessage *> * mit = rmset->iterator();
+                       while (mit->hasNext()) {
+                               RejectedMessage * rm = mit->next();
+                               delete rm;
+                       }
+                       delete mit;
+                       delete rmset;
+               }
+               delete rmit;
+               delete rejectedMessageWatchVectorTable;
+       }
+       delete arbitratorTable;
+       delete liveAbortTable;
+       {
+               SetIterator<int64_t, Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newTransactionParts);
+               while (partsit->hasNext()) {
+                       int64_t machineId = partsit->next();
+                       Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId);
+                       delete parts;
+               }
+               delete partsit;
+               delete newTransactionParts;
+       }
+       {
+               SetIterator<int64_t, Hashtable<Pair<int64_t, int32_t> *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newCommitParts);
+               while (partsit->hasNext()) {
+                       int64_t machineId = partsit->next();
+                       Hashtable<Pair<int64_t, int32_t> *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newCommitParts->get(machineId);
+                       delete parts;
+               }
+               delete partsit;
+               delete newCommitParts;
+       }
+       delete lastArbitratedTransactionNumberByArbitratorTable;
+       delete liveTransactionBySequenceNumberTable;
+       delete liveTransactionByTransactionIdTable;
+       {
+               SetIterator<int64_t, Hashtable<int64_t, Commit *> *> *liveit = getKeyIterator(liveCommitsTable);
+               while (liveit->hasNext()) {
+                       int64_t arbitratorId = liveit->next();
+                       
+                       // Get all the commits for a specific arbitrator
+                       Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(arbitratorId);
+                       {
+                               SetIterator<int64_t, Commit *> *clientit = getKeyIterator(commitForClientTable);
+                               while (clientit->hasNext()) {
+                                       int64_t id = clientit->next();
+                                       delete commitForClientTable->get(id);
+                               }
+                               delete clientit;
+                       }
+                       
+                       delete commitForClientTable;
+               }
+               delete liveit;
+               delete liveCommitsTable;
+       }
+       delete liveCommitsByKeyTable;
+       delete lastCommitSeenSequenceNumberByArbitratorTable;
+       delete rejectedSlotVector;
+       {
+               uint size = pendingTransactionQueue->size();
+               for (uint iter = 0; iter < size; iter++) {
+                       delete pendingTransactionQueue->get(iter);
+               }
+               delete pendingTransactionQueue;
+       }
+       delete pendingSendArbitrationEntriesToDelete;
+       delete transactionPartsSent;
+       delete outstandingTransactionStatus;
+       delete liveAbortsGeneratedByLocal;
+       delete offlineTransactionsCommittedAndAtServer;
+       delete localCommunicationTable;
+       delete lastTransactionSeenFromMachineFromServer;
+       {
+               for(uint i = 0; i < pendingSendArbitrationRounds->size(); i++) {
+                       delete pendingSendArbitrationRounds->get(i);
+               }
+               delete pendingSendArbitrationRounds;
+       }
+       if (lastTransactionPartsSent != NULL)
+               delete lastTransactionPartsSent;
+       delete lastArbitrationDataLocalSequenceNumberSeenFromArbitrator;
+}
+
 /**
  * Init all the stuff needed for for table usage
  */
@@ -165,13 +276,13 @@ void Table::init() {
        buffer = new SlotBuffer();
 
        // init data structs
-       committedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
-       speculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
-       pendingTransactionSpeculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
-       liveNewKeyTable = new Hashtable<IoTString *, NewKey *>();
+       committedKeyValueTable = new Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals>();
+       speculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals>();
+       pendingTransactionSpeculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals>();
+       liveNewKeyTable = new Hashtable<IoTString *, NewKey *, uintptr_t, 0, hashString, StringEquals >();
        lastMessageTable = new Hashtable<int64_t, Pair<int64_t, Liveness *> * >();
        rejectedMessageWatchVectorTable = new Hashtable<int64_t, Hashset<RejectedMessage *> * >();
-       arbitratorTable = new Hashtable<IoTString *, int64_t>();
+       arbitratorTable = new Hashtable<IoTString *, int64_t, uintptr_t, 0, hashString, StringEquals>();
        liveAbortTable = new Hashtable<Pair<int64_t, int64_t> *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals>();
        newTransactionParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>();
        newCommitParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t> *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>();
@@ -179,7 +290,7 @@ void Table::init() {
        liveTransactionBySequenceNumberTable = new Hashtable<int64_t, Transaction *>();
        liveTransactionByTransactionIdTable = new Hashtable<Pair<int64_t, int64_t> *, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals>();
        liveCommitsTable = new Hashtable<int64_t, Hashtable<int64_t, Commit *> * >();
-       liveCommitsByKeyTable = new Hashtable<IoTString *, Commit *>();
+       liveCommitsByKeyTable = new Hashtable<IoTString *, Commit *, uintptr_t, 0, hashString, StringEquals>();
        lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
        rejectedSlotVector = new Vector<int64_t>();
        pendingTransactionQueue = new Vector<Transaction *>();
@@ -217,10 +328,13 @@ void Table::initTable() {
                array->set(0, s);
                // update local block chain
                validateAndUpdate(array, true);
+               delete array;
        } else if (array->length() == 1) {
                // in case we did push the slot BUT we failed to init it
                validateAndUpdate(array, true);
+               delete array;
        } else {
+               delete array;
                throw new Error("Error on initialization");
        }
 }
@@ -233,6 +347,7 @@ void Table::rebuild() {
        // Just pull the latest slots from the server
        Array<Slot *> *newslots = cloud->getSlots(sequenceNumber + 1);
        validateAndUpdate(newslots, true);
+       delete newslots;
        sendToServer(NULL);
        updateLiveTransactionsAndStatus();
 }
@@ -253,7 +368,7 @@ IoTString *Table::getCommitted(IoTString *key)  {
        KeyValue *kv = committedKeyValueTable->get(key);
 
        if (kv != NULL) {
-               return kv->getValue();
+               return new IoTString(kv->getValue());
        } else {
                return NULL;
        }
@@ -271,7 +386,7 @@ IoTString *Table::getSpeculative(IoTString *key) {
        }
 
        if (kv != NULL) {
-               return kv->getValue();
+               return new IoTString(kv->getValue());
        } else {
                return NULL;
        }
@@ -292,7 +407,7 @@ IoTString *Table::getCommittedAtomic(IoTString *key) {
 
        if (kv != NULL) {
                pendingTransactionBuilder->addKVGuard(new KeyValue(key, kv->getValue()));
-               return kv->getValue();
+               return new IoTString(kv->getValue());
        } else {
                pendingTransactionBuilder->addKVGuard(new KeyValue(key, NULL));
                return NULL;
@@ -322,7 +437,7 @@ IoTString *Table::getSpeculativeAtomic(IoTString *key) {
 
        if (kv != NULL) {
                pendingTransactionBuilder->addKVGuard(new KeyValue(key, kv->getValue()));
-               return kv->getValue();
+               return new IoTString(kv->getValue());
        } else {
                pendingTransactionBuilder->addKVGuard(new KeyValue(key, NULL));
                return NULL;
@@ -333,6 +448,7 @@ bool Table::update()  {
        try {
                Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
                validateAndUpdate(newSlots, false);
+               delete newSlots;
                sendToServer(NULL);
                updateLiveTransactionsAndStatus();
                return true;
@@ -350,7 +466,7 @@ bool Table::update()  {
 
 bool Table::createNewKey(IoTString *keyName, int64_t machineId) {
        while (true) {
-               if (!arbitratorTable->contains(keyName)) {
+               if (arbitratorTable->contains(keyName)) {
                        // There is already an arbitrator
                        return false;
                }
@@ -365,11 +481,12 @@ bool Table::createNewKey(IoTString *keyName, int64_t machineId) {
 
 void Table::startTransaction() {
        // Create a new transaction, invalidates any old pending transactions.
+       if (pendingTransactionBuilder != NULL)
+               delete pendingTransactionBuilder;
        pendingTransactionBuilder = new PendingTransaction(localMachineId);
 }
 
-void Table::addKV(IoTString *key, IoTString *value) {
-
+void Table::put(IoTString *key, IoTString *value) {
        // Make sure it is a valid key
        if (!arbitratorTable->contains(key)) {
                throw new Error("Key not Found.");
@@ -382,7 +499,7 @@ void Table::addKV(IoTString *key, IoTString *value) {
        }
 
        // Add the key value to this transaction
-       KeyValue *kv = new KeyValue(key, value);
+       KeyValue *kv = new KeyValue(new IoTString(key), new IoTString(value));
        pendingTransactionBuilder->addKV(kv);
 }
 
@@ -408,9 +525,12 @@ TransactionStatus *Table::commitTransaction() {
                pendingTransactionQueue->add(newTransaction);
        } else {
                arbitrateOnLocalTransaction(newTransaction);
+               delete newTransaction;
                updateLiveStateFromLocal();
        }
-
+       if (pendingTransactionBuilder != NULL)
+               delete pendingTransactionBuilder;
+       
        pendingTransactionBuilder = new PendingTransaction(localMachineId);
 
        try {
@@ -440,6 +560,7 @@ TransactionStatus *Table::commitTransaction() {
 
                                if (sendReturn.getSecond()) {
                                        // did arbitrate
+                                       delete transaction;
                                        oldindex--;
                                }
                        }
@@ -464,242 +585,176 @@ int64_t Table::getLocalSequenceNumber() {
        return localSequenceNumber;
 }
 
-bool Table::sendToServer(NewKey *newKey) {
-       bool fromRetry = false;
-       try {
-               if (hadPartialSendToServer) {
-                       Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
-                       if (newSlots->length() == 0) {
-                               fromRetry = true;
-                               ThreeTuple<bool, bool, Array<Slot *> *> sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
-
-                               if (sendSlotsReturn.getFirst()) {
-                                       if (newKey != NULL) {
-                                               if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
-                                                       newKey = NULL;
-                                               }
-                                       }
-
-                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
-                                       while (trit->hasNext()) {
-                                               Transaction *transaction = trit->next();
-                                               transaction->resetServerFailure();
-                                               // Update which transactions parts still need to be sent
-                                               transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
-                                               // Add the transaction status to the outstanding list
-                                               outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
-
-                                               // Update the transaction status
-                                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
-
-                                               // Check if all the transaction parts were successfully
-                                               // sent and if so then remove it from pending
-                                               if (transaction->didSendAllParts()) {
-                                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
-                                                       pendingTransactionQueue->remove(transaction);
-                                               }
-                                       }
-                                       delete trit;
-                               } else {
-                                       newSlots = sendSlotsReturn.getThird();
-                                       bool isInserted = false;
-                                       for (uint si = 0; si < newSlots->length(); si++) {
-                                               Slot *s = newSlots->get(si);
-                                               if ((s->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
-                                                       isInserted = true;
-                                                       break;
-                                               }
-                                       }
-
-                                       for (uint si = 0; si < newSlots->length(); si++) {
-                                               Slot *s = newSlots->get(si);
-                                               if (isInserted) {
-                                                       break;
-                                               }
-
-                                               // Process each entry in the slot
-                                               Vector<Entry *> *ventries = s->getEntries();
-                                               uint vesize = ventries->size();
-                                               for (uint vei = 0; vei < vesize; vei++) {
-                                                       Entry *entry = ventries->get(vei);
-                                                       if (entry->getType() == TypeLastMessage) {
-                                                               LastMessage *lastMessage = (LastMessage *)entry;
-                                                               if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber())) {
-                                                                       isInserted = true;
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-
-                                       if (isInserted) {
-                                               if (newKey != NULL) {
-                                                       if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
-                                                               newKey = NULL;
-                                                       }
-                                               }
-
-                                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
-                                               while (trit->hasNext()) {
-                                                       Transaction *transaction = trit->next();
-                                                       transaction->resetServerFailure();
-
-                                                       // Update which transactions parts still need to be sent
-                                                       transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
-
-                                                       // Add the transaction status to the outstanding list
-                                                       outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
-
-                                                       // Update the transaction status
-                                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
-
-                                                       // Check if all the transaction parts were successfully sent and if so then remove it from pending
-                                                       if (transaction->didSendAllParts()) {
-                                                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
-                                                               pendingTransactionQueue->remove(transaction);
-                                                       } else {
-                                                               transaction->resetServerFailure();
-                                                               // Set the transaction sequence number back to nothing
-                                                               if (!transaction->didSendAPartToServer()) {
-                                                                       transaction->setSequenceNumber(-1);
-                                                               }
-                                                       }
-                                               }
-                                               delete trit;
+NewKey * Table::handlePartialSend(NewKey * newKey) {
+       //Didn't receive acknowledgement for last send
+       //See if the server has received a newer slot
+       
+       Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
+       if (newSlots->length() == 0) {
+               //Retry sending old slot
+               bool wasInserted = false;
+               bool sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey, &wasInserted, &newSlots);
+               
+               if (sendSlotsReturn) {
+                       if (newKey != NULL) {
+                               if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
+                                       newKey = NULL;
+                               }
+                       }
+                       
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+                       while (trit->hasNext()) {
+                               Transaction *transaction = trit->next();
+                               transaction->resetServerFailure();
+                               // Update which transactions parts still need to be sent
+                               transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
+                               // Add the transaction status to the outstanding list
+                               outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
+                               
+                               // Update the transaction status
+                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
+                               
+                               // Check if all the transaction parts were successfully
+                               // sent and if so then remove it from pending
+                               if (transaction->didSendAllParts()) {
+                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
+                                       pendingTransactionQueue->remove(transaction);
+                               }
+                       }
+                       delete trit;
+               } else {
+                       if (checkSend(newSlots, lastSlotAttemptedToSend)) {
+                               if (newKey != NULL) {
+                                       if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
+                                               newKey = NULL;
                                        }
                                }
-
+                               
                                SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetServerFailure();
-                                       // Set the transaction sequence number back to nothing
-                                       if (!transaction->didSendAPartToServer()) {
-                                               transaction->setSequenceNumber(-1);
-                                       }
-                               }
-                               delete trit;
-
-                               if (sendSlotsReturn.getThird()->length() != 0) {
-                                       // insert into the local block chain
-                                       validateAndUpdate(sendSlotsReturn.getThird(), true);
-                               }
-                               // continue;
-                       } else {
-                               bool isInserted = false;
-                               for (uint si = 0; si < newSlots->length(); si++) {
-                                       Slot *s = newSlots->get(si);
-                                       if ((s->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
-                                               isInserted = true;
-                                               break;
-                                       }
-                               }
-
-                               for (uint si = 0; si < newSlots->length(); si++) {
-                                       Slot *s = newSlots->get(si);
-                                       if (isInserted) {
-                                               break;
-                                       }
-
-                                       // Process each entry in the slot
-                                       Vector<Entry *> *entries = s->getEntries();
-                                       uint eSize = entries->size();
-                                       for (uint ei = 0; ei < eSize; ei++) {
-                                               Entry *entry = entries->get(ei);
-
-                                               if (entry->getType() == TypeLastMessage) {
-                                                       LastMessage *lastMessage = (LastMessage *)entry;
-                                                       if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber())) {
-                                                               isInserted = true;
-                                                               break;
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               if (isInserted) {
-                                       if (newKey != NULL) {
-                                               if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
-                                                       newKey = NULL;
-                                               }
-                                       }
-
-                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
-                                       while (trit->hasNext()) {
-                                               Transaction *transaction = trit->next();
-                                               transaction->resetServerFailure();
-
-                                               // Update which transactions parts still need to be sent
-                                               transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
-
-                                               // Add the transaction status to the outstanding list
-                                               outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
-
-                                               // Update the transaction status
-                                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
-
-                                               // Check if all the transaction parts were successfully sent and if so then remove it from pending
-                                               if (transaction->didSendAllParts()) {
-                                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
-                                                       pendingTransactionQueue->remove(transaction);
-                                               } else {
-                                                       transaction->resetServerFailure();
-                                                       // Set the transaction sequence number back to nothing
-                                                       if (!transaction->didSendAPartToServer()) {
-                                                               transaction->setSequenceNumber(-1);
-                                                       }
-                                               }
-                                       }
-                                       delete trit;
-                               } else {
-                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
-                                       while (trit->hasNext()) {
-                                               Transaction *transaction = trit->next();
+                                       
+                                       // Update which transactions parts still need to be sent
+                                       transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
+                                       
+                                       // Add the transaction status to the outstanding list
+                                       outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
+                                       
+                                       // Update the transaction status
+                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
+                                       
+                                       // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                                       if (transaction->didSendAllParts()) {
+                                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
+                                               pendingTransactionQueue->remove(transaction);
+                                       } else {
                                                transaction->resetServerFailure();
                                                // Set the transaction sequence number back to nothing
                                                if (!transaction->didSendAPartToServer()) {
                                                        transaction->setSequenceNumber(-1);
                                                }
                                        }
-                                       delete trit;
                                }
-
-                               // insert into the local block chain
-                               validateAndUpdate(newSlots, true);
+                               delete trit;
                        }
                }
-       } catch (ServerException *e) {
-               throw e;
+               
+               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+               while (trit->hasNext()) {
+                       Transaction *transaction = trit->next();
+                       transaction->resetServerFailure();
+                       // Set the transaction sequence number back to nothing
+                       if (!transaction->didSendAPartToServer()) {
+                               transaction->setSequenceNumber(-1);
+                       }
+               }
+               delete trit;
+               
+               if (newSlots->length() != 0) {
+                       // insert into the local block chain
+                       validateAndUpdate(newSlots, true);
+               }
+       } else {
+               if (checkSend(newSlots, lastSlotAttemptedToSend)) {
+                       if (newKey != NULL) {
+                               if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) {
+                                       newKey = NULL;
+                               }
+                       }
+                       
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+                       while (trit->hasNext()) {
+                               Transaction *transaction = trit->next();
+                               transaction->resetServerFailure();
+                               
+                               // Update which transactions parts still need to be sent
+                               transaction->removeSentParts(lastTransactionPartsSent->get(transaction));
+                               
+                               // Add the transaction status to the outstanding list
+                               outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus());
+                               
+                               // Update the transaction status
+                               transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial);
+                               
+                               // Check if all the transaction parts were successfully sent and if so then remove it from pending
+                               if (transaction->didSendAllParts()) {
+                                       transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully);
+                                       pendingTransactionQueue->remove(transaction);
+                               } else {
+                                       transaction->resetServerFailure();
+                                       // Set the transaction sequence number back to nothing
+                                       if (!transaction->didSendAPartToServer()) {
+                                               transaction->setSequenceNumber(-1);
+                                       }
+                               }
+                       }
+                       delete trit;
+               } else {
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
+                       while (trit->hasNext()) {
+                               Transaction *transaction = trit->next();
+                               transaction->resetServerFailure();
+                               // Set the transaction sequence number back to nothing
+                               if (!transaction->didSendAPartToServer()) {
+                                       transaction->setSequenceNumber(-1);
+                               }
+                       }
+                       delete trit;
+               }
+               
+               // insert into the local block chain
+               validateAndUpdate(newSlots, true);
        }
+       delete newSlots;
+       return newKey;
+}
 
-
+bool Table::sendToServer(NewKey *newKey) {
+       if (hadPartialSendToServer) {
+               newKey = handlePartialSend(newKey);
+       }
 
        try {
                // While we have stuff that needs inserting into the block chain
                while ((pendingTransactionQueue->size() > 0) || (pendingSendArbitrationRounds->size() > 0) || (newKey != NULL)) {
-
-                       fromRetry = false;
-
                        if (hadPartialSendToServer) {
                                throw new Error("Should Be error free");
                        }
-
-
-
+                       
                        // If there is a new key with same name then end
                        if ((newKey != NULL) && arbitratorTable->contains(newKey->getKey())) {
                                return false;
                        }
 
                        // Create the slot
-                       Slot *slot = new Slot(this, sequenceNumber + 1, localMachineId, buffer->getSlot(sequenceNumber)->getHMAC(), localSequenceNumber);
+                       Slot *slot = new Slot(this, sequenceNumber + 1, localMachineId, new Array<char>(buffer->getSlot(sequenceNumber)->getHMAC()), localSequenceNumber);
                        localSequenceNumber++;
 
                        // Try to fill the slot with data
-                       ThreeTuple<bool, int32_t, bool> fillSlotsReturn = fillSlot(slot, false, newKey);
-                       bool needsResize = fillSlotsReturn.getFirst();
-                       int newSize = fillSlotsReturn.getSecond();
-                       bool insertedNewKey = fillSlotsReturn.getThird();
+                       int newSize = 0;
+                       bool insertedNewKey = false;
+                       bool needsResize = fillSlot(slot, false, newKey, newSize, insertedNewKey);
 
                        if (needsResize) {
                                // Reset which transaction to send
@@ -720,7 +775,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                transactionPartsSent->clear();
 
                                // We needed a resize so try again
-                               fillSlot(slot, true, newKey);
+                               fillSlot(slot, true, newKey, newSize, insertedNewKey);
                        }
 
                        lastSlotAttemptedToSend = slot;
@@ -728,18 +783,19 @@ bool Table::sendToServer(NewKey *newKey) {
                        lastInsertedNewKey = insertedNewKey;
                        lastNewSize = newSize;
                        lastNewKey = newKey;
+                       if (lastTransactionPartsSent != NULL)
+                               delete lastTransactionPartsSent;
                        lastTransactionPartsSent = transactionPartsSent->clone();
                        lastPendingSendArbitrationEntriesToDelete = new Vector<Entry *>(pendingSendArbitrationEntriesToDelete);
 
-                       ThreeTuple<bool, bool, Array<Slot *> *> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL);
-
-                       if (sendSlotsReturn.getFirst()) {
+                       Array<Slot *> * newSlots = NULL;
+                       bool wasInserted = false;
+                       bool sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL, &wasInserted, &newSlots);
 
+                       if (sendSlotsReturn) {
                                // Did insert into the block chain
-
                                if (insertedNewKey) {
                                        // This slot was what was inserted not a previous slot
-
                                        // New Key was successfully inserted into the block chain so dont want to insert it again
                                        newKey = NULL;
                                }
@@ -752,10 +808,11 @@ bool Table::sendToServer(NewKey *newKey) {
                                        round->removeParts(pendingSendArbitrationEntriesToDelete);
 
                                        if (!round->isDoneSending()) {
-                                               // Sent all the parts
+                                               //Add part back in
                                                pendingSendArbitrationRounds->set(oldcount++,
                                                                                                                                                                                        pendingSendArbitrationRounds->get(i));
-                                       }
+                                       } else
+                                               delete pendingSendArbitrationRounds->get(i);
                                }
                                pendingSendArbitrationRounds->setSize(oldcount);
 
@@ -799,12 +856,12 @@ bool Table::sendToServer(NewKey *newKey) {
                        pendingSendArbitrationEntriesToDelete->clear();
                        transactionPartsSent->clear();
 
-                       if (sendSlotsReturn.getThird()->length() != 0) {
+                       if (newSlots->length() != 0) {
                                // insert into the local block chain
-                               validateAndUpdate(sendSlotsReturn.getThird(), true);
+                               validateAndUpdate(newSlots, true);
                        }
+                       delete newSlots;
                }
-
        } catch (ServerException *e) {
                if (e->getType() != ServerException_TypeInputTimeout) {
                        // Nothing was able to be sent to the server so just clear these data structures
@@ -1114,80 +1171,82 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
        return returnData;
 }
 
-ThreeTuple<bool, bool, Array<Slot *> *> Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey) {
-       bool attemptedToSendToServerTmp = attemptedToSendToServer;
-       attemptedToSendToServer = true;
-
-       bool inserted = false;
-       bool lastTryInserted = false;
+/** Checks whether a given slot was sent using new slots in
+               array. Returns true if sent and false otherwise.  */
 
-       Array<Slot *> *array = cloud->putSlot(slot, newSize);
-       if (array == NULL) {
-               array = new Array<Slot *>();
-               array->set(0, slot);
-               rejectedSlotVector->clear();
-               inserted = true;
-       } else {
-               if (array->length() == 0) {
-                       throw new Error("Server Error: Did not send any slots");
+bool Table::checkSend(Array<Slot *> * array, Slot *checkSlot) {
+       uint size = array->length();
+       for (uint i = 0; i < size; i++) {
+               Slot *s = array->get(i);
+               if ((s->getSequenceNumber() == checkSlot->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
+                       return true;
                }
-
-               // if (attemptedToSendToServerTmp) {
-               if (hadPartialSendToServer) {
-
-                       bool isInserted = false;
-                       uint size = array->length();
-                       for (uint i = 0; i < size; i++) {
-                               Slot *s = array->get(i);
-                               if ((s->getSequenceNumber() == slot->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
-                                       isInserted = true;
-                                       break;
+       }
+       
+       //Also need to see if other machines acknowledged our message
+       for (uint i = 0; i < size; i++) {
+               Slot *s = array->get(i);
+               
+               // Process each entry in the slot
+               Vector<Entry *> *entries = s->getEntries();
+               uint eSize = entries->size();
+               for (uint ei = 0; ei < eSize; ei++) {
+                       Entry *entry = entries->get(ei);
+                       
+                       if (entry->getType() == TypeLastMessage) {
+                               LastMessage *lastMessage = (LastMessage *)entry;
+                               
+                               if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == checkSlot->getSequenceNumber())) {
+                                       return true;
                                }
                        }
+               }
+       }
+       //Not found
+       return false;
+}
 
-                       for (uint i = 0; i < size; i++) {
-                               Slot *s = array->get(i);
-                               if (isInserted) {
-                                       break;
-                               }
+/** Method tries to send slot to server.  Returns status in tuple.
+               isInserted returns whether last un-acked send (if any) was
+               successful.  Returns whether send was confirmed.x
+ */
 
-                               // Process each entry in the slot
-                               Vector<Entry *> *entries = s->getEntries();
-                               uint eSize = entries->size();
-                               for (uint ei = 0; ei < eSize; ei++) {
-                                       Entry *entry = entries->get(ei);
+bool Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey, bool *isInserted, Array<Slot *> **array) {
+       attemptedToSendToServer = true;
 
-                                       if (entry->getType() == TypeLastMessage) {
-                                               LastMessage *lastMessage = (LastMessage *)entry;
+       *array = cloud->putSlot(slot, newSize);
+       if (*array == NULL) {
+               *array = new Array<Slot *>(1);
+               (*array)->set(0, slot);
+               rejectedSlotVector->clear();
+               *isInserted = false;
+               return true;
+       } else {
+               if ((*array)->length() == 0) {
+                       throw new Error("Server Error: Did not send any slots");
+               }
 
-                                               if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == slot->getSequenceNumber())) {
-                                                       isInserted = true;
-                                                       break;
-                                               }
-                                       }
-                               }
-                       }
+               if (hadPartialSendToServer) {
+                       *isInserted = checkSend(*array, slot);
 
-                       if (!isInserted) {
+                       if (!(*isInserted)) {
                                rejectedSlotVector->add(slot->getSequenceNumber());
-                               lastTryInserted = false;
-                       } else {
-                               lastTryInserted = true;
                        }
+                       
+                       return false;
                } else {
                        rejectedSlotVector->add(slot->getSequenceNumber());
-                       lastTryInserted = false;
+                       *isInserted = false;
+                       return false;
                }
        }
-
-       return ThreeTuple<bool, bool, Array<Slot *> *>(inserted, lastTryInserted, array);
 }
 
 /**
- * Returns false if a resize was needed
+ * Returns true if a resize was needed but not done.
  */
-ThreeTuple<bool, int32_t, bool> Table::fillSlot(Slot *slot, bool resize, NewKey *newKeyEntry) {
-       int newSize = 0;
+bool Table::fillSlot(Slot *slot, bool resize, NewKey *newKeyEntry, int & newSize, bool & insertedKey) {
+       newSize = 0;//special value to indicate no resize
        if (liveSlotCount > bufferResizeThreshold) {
                resize = true;//Resize is forced
        }
@@ -1210,16 +1269,16 @@ ThreeTuple<bool, int32_t, bool> Table::fillSlot(Slot *slot, bool resize, NewKey
        int64_t currentRescueSequenceNumber = mandatoryRescueReturn.getThird();
 
        if (needsResize && !resize) {
-               // We need to resize but we are not resizing so return false
-               return ThreeTuple<bool, int32_t, bool>(true, NULL, NULL);
+               // We need to resize but we are not resizing so return true to force on retry
+               return true;
        }
 
-       bool inserted = false;
+       insertedKey = false;
        if (newKeyEntry != NULL) {
                newKeyEntry->setSlot(slot);
                if (slot->hasSpace(newKeyEntry)) {
                        slot->addEntry(newKeyEntry);
-                       inserted = true;
+                       insertedKey = true;
                }
        }
 
@@ -1291,7 +1350,7 @@ ThreeTuple<bool, int32_t, bool> Table::fillSlot(Slot *slot, bool resize, NewKey
        // Fill the remainder of the slot with rescue data
        doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize);
 
-       return ThreeTuple<bool, int32_t, bool>(false, newSize, inserted);
+       return false;
 }
 
 void Table::doRejectedMessages(Slot *s) {
@@ -1405,10 +1464,13 @@ void Table::doOptionalRescue(Slot *s, bool seenliveslot, int64_t seqn, bool resi
                                s->addEntry(liveentry);
                        else {
                                skipcount++;
-                               if (skipcount > Table_SKIP_THRESHOLD)
+                               if (skipcount > Table_SKIP_THRESHOLD) {
+                                       delete liveentries;
                                        goto donesearch;
+                               }
                        }
                }
+               delete liveentries;
        }
 donesearch:
        ;
@@ -1456,7 +1518,8 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
                        updateExpectedSize();
                }
        }
-
+       delete indexer;
+       
        // If there is a gap, check to see if the server sent us
        // everything->
        if (firstSeqNum != (sequenceNumber + 1)) {
@@ -1469,10 +1532,11 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
                // must have a last message message-> If not then the server is
                // hiding slots
                if (!machineSet->isEmpty()) {
+                       delete machineSet;
                        throw new Error("Missing record for machines: ");
                }
        }
-
+       delete machineSet;
        // Update the size of our local block chain->
        commitNewMaxSize();
 
@@ -1623,25 +1687,33 @@ void Table::processNewTransactionParts() {
                        if (transaction == NULL) {
                                // This is a new transaction that we dont have so make a new one
                                transaction = new Transaction();
+                               
+                               // Add that part to the transaction
+                               transaction->addPartDecode(part);
 
                                // Insert this new transaction into the live tables
                                liveTransactionBySequenceNumberTable->put(part->getSequenceNumber(), transaction);
-                               liveTransactionByTransactionIdTable->put(new Pair<int64_t, int64_t>(part->getTransactionId()), transaction);
+                               liveTransactionByTransactionIdTable->put(transaction->getId(), transaction);
                        }
-
-                       // Add that part to the transaction
-                       transaction->addPartDecode(part);
                }
                delete ptit;
        }
        delete tpit;
        // Clear all the new transaction parts in preparation for the next
        // time the server sends slots
-       newTransactionParts->clear();
+       {
+               SetIterator<int64_t, Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newTransactionParts);
+               while (partsit->hasNext()) {
+                       int64_t machineId = partsit->next();
+                       Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId);
+                       delete parts;
+               }
+               delete partsit;
+               newTransactionParts->clear();
+       }
 }
 
 void Table::arbitrateFromServer() {
-
        if (liveTransactionBySequenceNumberTable->size() == 0) {
                // Nothing to arbitrate on so move on
                return;
@@ -1658,7 +1730,7 @@ void Table::arbitrateFromServer() {
        qsort(transactionSequenceNumbers->expose(), transactionSequenceNumbers->size(), sizeof(int64_t), compareInt64);
 
        // Collection of key value pairs that are
-       Hashtable<IoTString *, KeyValue *> *speculativeTableTmp = new Hashtable<IoTString *, KeyValue *>();
+       Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals> *speculativeTableTmp = new Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals>();
 
        // The last transaction arbitrated on
        int64_t lastTransactionCommitted = -1;
@@ -1683,14 +1755,12 @@ void Table::arbitrateFromServer() {
                        continue;
                }
 
-
                if (!transaction->isComplete()) {
                        // Will arbitrate in incorrect order if we continue so just break
                        // Most likely this
                        break;
                }
 
-
                // update the largest transaction seen by arbitrator from server
                if (!lastTransactionSeenFromMachineFromServer->contains(transaction->getMachineId())) {
                        lastTransactionSeenFromMachineFromServer->put(transaction->getMachineId(), transaction->getClientLocalSequenceNumber());
@@ -1703,7 +1773,6 @@ void Table::arbitrateFromServer() {
 
                if (transaction->evaluateGuard(committedKeyValueTable, speculativeTableTmp, NULL)) {
                        // Guard evaluated as true
-
                        // Update the local changes so we can make the commit
                        SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
                        while (kvit->hasNext()) {
@@ -1742,14 +1811,14 @@ void Table::arbitrateFromServer() {
                localArbitrationSequenceNumber++;
 
                // Add all the new keys to the commit
-               SetIterator<IoTString *, KeyValue *> *spit = getKeyIterator(speculativeTableTmp);
+               SetIterator<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals> *spit = getKeyIterator(speculativeTableTmp);
                while (spit->hasNext()) {
                        IoTString *string = spit->next();
                        KeyValue *kv = speculativeTableTmp->get(string);
                        newCommit->addKV(kv);
                }
                delete spit;
-
+               
                // create the commit parts
                newCommit->createCommitParts();
 
@@ -1763,6 +1832,7 @@ void Table::arbitrateFromServer() {
                        processEntry(commitPart);
                }
        }
+       delete speculativeTableTmp;
 
        if ((newCommit != NULL) || (generatedAborts->size() > 0)) {
                ArbitrationRound *arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
@@ -1899,7 +1969,7 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
 }
 
 /**
- * Compacts the arbitration data my merging commits and aggregating
+ * Compacts the arbitration data by merging commits and aggregating
  * aborts so that a single large push of commits can be done instead
  * of many small updates
  */
@@ -1918,6 +1988,7 @@ bool Table::compactArbitrationData() {
        bool gotNewCommit = false;
 
        uint numberToDelete = 1;
+       
        while (numberToDelete < pendingSendArbitrationRounds->size()) {
                ArbitrationRound *round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1);
 
@@ -1949,11 +2020,23 @@ bool Table::compactArbitrationData() {
                        newSize += round->getAbortsCount();
 
                        if (newSize > ArbitrationRound_MAX_PARTS) {
-                               // Cant compact since it would be too large
+                               // Can't compact since it would be too large
+                               if (lastRound->getCommit() != newCommit &&
+                                               round->getCommit() != newCommit)
+                                       delete newCommit;
                                break;
                        }
-
                        // Set the new compacted part
+                       if (lastRound->getCommit() == newCommit)
+                               lastRound->setCommit(NULL);
+                       if (round->getCommit() == newCommit)
+                               round->setCommit(NULL);
+                       
+                       if (lastRound->getCommit() != NULL) {
+                               Commit * oldcommit = lastRound->getCommit();
+                               lastRound->setCommit(NULL);
+                               delete oldcommit;
+                       }
                        lastRound->setCommit(newCommit);
                        lastRound->addAborts(round->getAborts());
                        gotNewCommit = true;
@@ -1965,15 +2048,11 @@ bool Table::compactArbitrationData() {
        if (numberToDelete != 1) {
                // If there is a compaction
                // Delete the previous pieces that are now in the new compacted piece
-               if (numberToDelete == pendingSendArbitrationRounds->size()) {
-                       pendingSendArbitrationRounds->clear();
-               } else {
-                       for (uint i = 0; i < numberToDelete; i++) {
-                               pendingSendArbitrationRounds->removeIndex(pendingSendArbitrationRounds->size() - 1);
-                       }
+               for (uint i = 2; i <= numberToDelete; i++) {
+                       delete pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size()-i);
                }
+               pendingSendArbitrationRounds->setSize(pendingSendArbitrationRounds->size() - numberToDelete);
 
-               // Add the new compacted into the pending to send list
                pendingSendArbitrationRounds->add(lastRound);
 
                // Should reinsert into the commit processor
@@ -1990,7 +2069,6 @@ bool Table::compactArbitrationData() {
  * transactions
  */
 bool Table::updateCommittedTable() {
-
        if (newCommitParts->size() == 0) {
                // Nothing new to process
                return false;
@@ -2031,6 +2109,7 @@ bool Table::updateCommittedTable() {
                        commit->addPartDecode(part);
                }
                delete pairit;
+               delete parts;
        }
        delete partsit;
 
@@ -2045,7 +2124,6 @@ bool Table::updateCommittedTable() {
        SetIterator<int64_t, Hashtable<int64_t, Commit *> *> *liveit = getKeyIterator(liveCommitsTable);
        while (liveit->hasNext()) {
                int64_t arbitratorId = liveit->next();
-
                // Get all the commits for a specific arbitrator
                Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(arbitratorId);
 
@@ -2070,7 +2148,6 @@ bool Table::updateCommittedTable() {
                for (uint i = 0; i < commitSequenceNumbers->size(); i++) {
                        int64_t commitSequenceNumber = commitSequenceNumbers->get(i);
                        Commit *commit = commitForClientTable->get(commitSequenceNumber);
-
                        // Special processing if a commit is not complete
                        if (!commit->isComplete()) {
                                if (i == (commitSequenceNumbers->size() - 1)) {
@@ -2084,6 +2161,7 @@ bool Table::updateCommittedTable() {
                                        // Delete it and move on
                                        commit->setDead();
                                        commitForClientTable->remove(commit->getSequenceNumber());
+                                       delete commit;
                                        continue;
                                }
                        }
@@ -2111,7 +2189,6 @@ bool Table::updateCommittedTable() {
                        // We have already seen this commit before so need to do the
                        // full processing on this commit
                        if (commit->getSequenceNumber() <= lastCommitSeenSequenceNumber) {
-
                                // Update the last transaction that was updated if we can
                                if (commit->getTransactionSequenceNumber() != -1) {
                                        int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId());
@@ -2120,7 +2197,6 @@ bool Table::updateCommittedTable() {
                                                lastArbitratedTransactionNumberByArbitratorTable->put(commit->getMachineId(), commit->getTransactionSequenceNumber());
                                        }
                                }
-
                                continue;
                        }
 
@@ -2130,7 +2206,7 @@ bool Table::updateCommittedTable() {
                        // have live values for their keys
                        Hashset<Commit *> *commitsToEdit = new Hashset<Commit *>();
                        {
-                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0> *kvit = commit->getKeyValueUpdateSet()->iterator();
                                while (kvit->hasNext()) {
                                        KeyValue *kv = kvit->next();
                                        Commit *commit = liveCommitsByKeyTable->get(kv->getKey());
@@ -2150,7 +2226,7 @@ bool Table::updateCommittedTable() {
 
                                        // Update which keys in the old commits are still live
                                        {
-                                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0> *kvit = commit->getKeyValueUpdateSet()->iterator();
                                                while (kvit->hasNext()) {
                                                        KeyValue *kv = kvit->next();
                                                        previousCommit->invalidateKey(kv->getKey());
@@ -2161,10 +2237,12 @@ bool Table::updateCommittedTable() {
                                        // if the commit is now dead then remove it
                                        if (!previousCommit->isLive()) {
                                                commitForClientTable->remove(previousCommit->getSequenceNumber());
+                                               delete previousCommit;
                                        }
                                }
                        }
                        delete commitit;
+                       delete commitsToEdit;
 
                        // Update the last seen sequence number from this arbitrator
                        if (lastCommitSeenSequenceNumberByArbitratorTable->contains(commit->getMachineId())) {
@@ -2180,7 +2258,7 @@ bool Table::updateCommittedTable() {
 
                        // Update the committed table of keys and which commit is using which key
                        {
-                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0> *kvit = commit->getKeyValueUpdateSet()->iterator();
                                while (kvit->hasNext()) {
                                        KeyValue *kv = kvit->next();
                                        committedKeyValueTable->put(kv->getKey(), kv);
@@ -2352,13 +2430,15 @@ void Table::updateLiveTransactionsAndStatus() {
                        Transaction *transaction = liveTransactionBySequenceNumberTable->get(key);
 
                        // Check if the transaction is dead
-                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(transaction->getArbitrator()) && lastArbitratedTransactionNumberByArbitratorTable->get(transaction->getArbitrator() >= transaction->getSequenceNumber())) {
+                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(transaction->getArbitrator())
+                                       && lastArbitratedTransactionNumberByArbitratorTable->get(transaction->getArbitrator()) >= transaction->getSequenceNumber()) {
                                // Set dead the transaction
                                transaction->setDead();
 
                                // Remove the transaction from the live table
                                iter->remove();
                                liveTransactionByTransactionIdTable->remove(transaction->getId());
+                               delete transaction;
                        }
                }
                delete iter;
@@ -2372,8 +2452,8 @@ void Table::updateLiveTransactionsAndStatus() {
                        TransactionStatus *status = outstandingTransactionStatus->get(key);
 
                        // Check if the transaction is dead
-                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(status->getTransactionArbitrator()) && (lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator()) >= status->getTransactionSequenceNumber())) {
-
+                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(status->getTransactionArbitrator())
+                                       && (lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator()) >= status->getTransactionSequenceNumber())) {
                                // Set committed
                                status->setStatus(TransactionStatus_StatusCommitted);
 
@@ -2529,6 +2609,7 @@ void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
        if (deviceWatchSet->isEmpty()) {
                // This rejected message has been seen by all the clients so
                entry->setDead();
+               delete deviceWatchSet;
        } else {
                // We need to watch this rejected message
                entry->setWatchSet(deviceWatchSet);
@@ -2639,7 +2720,8 @@ void Table::processEntry(TransactionPart *entry) {
 void Table::processEntry(CommitPart *entry) {
        // Update the last transaction that was updated if we can
        if (entry->getTransactionSequenceNumber() != -1) {
-               if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getMachineId() || lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId()) < entry->getTransactionSequenceNumber())) {
+               if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getMachineId()) ||
+                               lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId()) < entry->getTransactionSequenceNumber()) {
                        lastArbitratedTransactionNumberByArbitratorTable->put(entry->getMachineId(), entry->getTransactionSequenceNumber());
                }
        }
@@ -2652,7 +2734,7 @@ void Table::processEntry(CommitPart *entry) {
        }
        // Update the part and set dead ones we have already seen (got a
        // rescued version)
-       CommitPart *previouslySeenPart = commitPart->put(new Pair<int64_t, int32_t>(entry->getPartId()), entry);
+       CommitPart *previouslySeenPart = commitPart->put(entry->getPartId(), entry);
        if (previouslySeenPart != NULL) {
                previouslySeenPart->setDead();
        }