edits
[iotcloud.git] / version2 / src / C / Table.cc
index dcdc99be4159477030ac692aba04206187fb2d8c..344c62ddbcf07ffe09c25d75b53f6718e31783e7 100644 (file)
 #include "ByteBuffer.h"
 #include "Abort.h"
 #include "CommitPart.h"
-
+#include "ArbitrationRound.h"
+#include "TransactionPart.h"
+#include "Commit.h"
+#include "RejectedMessage.h"
+#include "SlotIndexer.h"
+#include <stdlib.h>
+
+int compareInt64(const void * a, const void *b) {
+       const int64_t * pa = (const int64_t *) a;
+       const int64_t * pb = (const int64_t *) b;
+       if (*pa < *pb)
+               return -1;
+       else if (*pa > *pb)
+               return 1;
+       else
+               return 0;
+}
 
 Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, int listeningPort) :
        buffer(NULL),
@@ -153,16 +169,16 @@ void Table::init() {
        speculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
        pendingTransactionSpeculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
        liveNewKeyTable = new Hashtable<IoTString *, NewKey *>();
-       lastMessageTable = new Hashtable<int64_t, Pair<int64_t, Liveness *> >();
+       lastMessageTable = new Hashtable<int64_t, Pair<int64_t, Liveness *> >();
        rejectedMessageWatchVectorTable = new Hashtable<int64_t, Hashset<RejectedMessage *> * >();
        arbitratorTable = new Hashtable<IoTString *, int64_t>();
-       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> *>();
+       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> *>();
        lastArbitratedTransactionNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
        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 *> >();
+       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 *>();
        lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
        rejectedSlotVector = new Vector<int64_t>();
@@ -171,8 +187,8 @@ void Table::init() {
        transactionPartsSent = new Hashtable<Transaction *, Vector<int32_t> *>();
        outstandingTransactionStatus = new Hashtable<int64_t, TransactionStatus *>();
        liveAbortsGeneratedByLocal = new Hashtable<int64_t, Abort *>();
-       offlineTransactionsCommittedAndAtServer = new Hashset<Pair<int64_t, int64_t>, uintptr_t, 0, pairHashFunction, pairEquals>();
-       localCommunicationTable = new Hashtable<int64_t, Pair<IoTString *, int32_t> >();
+       offlineTransactionsCommittedAndAtServer = new Hashset<Pair<int64_t, int64_t> *, uintptr_t, 0, pairHashFunction, pairEquals>();
+       localCommunicationTable = new Hashtable<int64_t, Pair<IoTString *, int32_t> *>();
        lastTransactionSeenFromMachineFromServer = new Hashtable<int64_t, int64_t>();
        pendingSendArbitrationRounds = new Vector<ArbitrationRound *>();
        lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new Hashtable<int64_t, int64_t>();
@@ -222,7 +238,7 @@ void Table::rebuild() {
 }
 
 void Table::addLocalCommunication(int64_t arbitrator, IoTString *hostName, int portNumber) {
-       localCommunicationTable->put(arbitrator, Pair<IoTString *, int32_t>(hostName, portNumber));
+       localCommunicationTable->put(arbitrator, new Pair<IoTString *, int32_t>(hostName, portNumber));
 }
 
 int64_t Table::getArbitrator(IoTString *key) {
@@ -321,9 +337,12 @@ bool Table::update()  {
                updateLiveTransactionsAndStatus();
                return true;
        } catch (Exception *e) {
-               for (int64_t m : localCommunicationTable->keySet()) {
+               SetIterator<int64_t, Pair<IoTString *, int32_t> *> *kit = getKeyIterator(localCommunicationTable);
+               while (kit->hasNext()) {
+                       int64_t m = kit->next();
                        updateFromLocal(m);
                }
+               delete kit;
        }
 
        return false;
@@ -401,10 +420,10 @@ TransactionStatus *Table::commitTransaction() {
                Hashset<int64_t> *arbitratorTriedAndFailed = new Hashset<int64_t>();
                uint size = pendingTransactionQueue->size();
                uint oldindex = 0;
-               for(int iter = 0; iter < size; iter++) {
+               for (int iter = 0; iter < size; iter++) {
                        Transaction *transaction = pendingTransactionQueue->get(iter);
                        pendingTransactionQueue->set(oldindex++, pendingTransactionQueue->get(iter));
-                       
+
                        if (arbitratorTriedAndFailed->contains(transaction->getArbitrator())) {
                                // Already contacted this client so ignore all attempts to contact this client
                                // to preserve ordering for arbitrator
@@ -425,9 +444,9 @@ TransactionStatus *Table::commitTransaction() {
                                }
                        }
                }
+               pendingTransactionQueue->setSize(oldindex);
        }
-       pendingTransactionQueue->setSize(oldindex);
-       
+
        updateLiveStateFromLocal();
 
        return transactionStatus;
@@ -461,7 +480,9 @@ bool Table::sendToServer(NewKey *newKey) {
                                                }
                                        }
 
-                                       for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
+                                       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));
@@ -478,6 +499,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                        pendingTransactionQueue->remove(transaction);
                                                }
                                        }
+                                       delete trit;
                                } else {
                                        newSlots = sendSlotsReturn.getThird();
                                        bool isInserted = false;
@@ -496,7 +518,10 @@ bool Table::sendToServer(NewKey *newKey) {
                                                }
 
                                                // Process each entry in the slot
-                                               for (Entry *entry : s->getEntries()) {
+                                               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())) {
@@ -514,7 +539,9 @@ bool Table::sendToServer(NewKey *newKey) {
                                                        }
                                                }
 
-                                               for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
+                                               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
@@ -538,16 +565,20 @@ bool Table::sendToServer(NewKey *newKey) {
                                                                }
                                                        }
                                                }
+                                               delete trit;
                                        }
                                }
 
-                               for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
+                               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
@@ -571,8 +602,11 @@ bool Table::sendToServer(NewKey *newKey) {
                                        }
 
                                        // Process each entry in the slot
-                                       for (Entry *entry : s->getEntries()) {
-
+                                       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())) {
@@ -590,7 +624,9 @@ bool Table::sendToServer(NewKey *newKey) {
                                                }
                                        }
 
-                                       for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
+                                       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
@@ -614,14 +650,18 @@ bool Table::sendToServer(NewKey *newKey) {
                                                        }
                                                }
                                        }
+                                       delete trit;
                                } else {
-                                       for (Transaction *transaction : lastTransactionPartsSent->keySet()) {
+                                       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
@@ -663,7 +703,9 @@ bool Table::sendToServer(NewKey *newKey) {
 
                        if (needsResize) {
                                // Reset which transaction to send
-                               for (Transaction *transaction : transactionPartsSent->keySet()) {
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                               while (trit->hasNext()) {
+                                       Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
 
                                        // Set the transaction sequence number back to nothing
@@ -671,6 +713,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                transaction->setSequenceNumber(-1);
                                        }
                                }
+                               delete trit;
 
                                // Clear the sent data since we are trying again
                                pendingSendArbitrationEntriesToDelete->clear();
@@ -685,10 +728,9 @@ bool Table::sendToServer(NewKey *newKey) {
                        lastInsertedNewKey = insertedNewKey;
                        lastNewSize = newSize;
                        lastNewKey = newKey;
-                       lastTransactionPartsSent = new Hashtable<Transaction *, Vector<int32_t> * >(transactionPartsSent);
+                       lastTransactionPartsSent = transactionPartsSent->clone();
                        lastPendingSendArbitrationEntriesToDelete = new Vector<Entry *>(pendingSendArbitrationEntriesToDelete);
 
-
                        ThreeTuple<bool, bool, Array<Slot *> *> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL);
 
                        if (sendSlotsReturn.getFirst()) {
@@ -703,17 +745,23 @@ bool Table::sendToServer(NewKey *newKey) {
                                }
 
                                // Remove the aborts and commit parts that were sent from the pending to send queue
-                               for (Iterator<ArbitrationRound *> *iter = pendingSendArbitrationRounds->iterator(); iter->hasNext(); ) {
-                                       ArbitrationRound *round = iter->next();
+                               uint size = pendingSendArbitrationRounds->size();
+                               uint oldcount = 0;
+                               for (uint i = 0; i < size; i++) {
+                                       ArbitrationRound *round = pendingSendArbitrationRounds->get(i);
                                        round->removeParts(pendingSendArbitrationEntriesToDelete);
 
-                                       if (round->isDoneSending()) {
+                                       if (!round->isDoneSending()) {
                                                // Sent all the parts
-                                               iter->remove();
+                                               pendingSendArbitrationRounds->set(oldcount++,
+                                                                                                                                                                                       pendingSendArbitrationRounds->get(i));
                                        }
                                }
+                               pendingSendArbitrationRounds->setSize(oldcount);
 
-                               for (Transaction *transaction : transactionPartsSent->keySet()) {
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                               while (trit->hasNext()) {
+                                       Transaction *transaction = trit->next();
                                        transaction->resetServerFailure();
 
                                        // Update which transactions parts still need to be sent
@@ -731,9 +779,12 @@ bool Table::sendToServer(NewKey *newKey) {
                                                pendingTransactionQueue->remove(transaction);
                                        }
                                }
+                               delete trit;
                        } else {
                                // Reset which transaction to send
-                               for (Transaction *transaction : transactionPartsSent->keySet()) {
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                               while (trit->hasNext()) {
+                                       Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
 
                                        // Set the transaction sequence number back to nothing
@@ -741,6 +792,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                transaction->setSequenceNumber(-1);
                                        }
                                }
+                               delete trit;
                        }
 
                        // Clear the sent data in preparation for next send
@@ -754,10 +806,11 @@ bool Table::sendToServer(NewKey *newKey) {
                }
 
        } catch (ServerException *e) {
-
-               if (e->getType() != ServerException->TypeInputTimeout) {
+               if (e->getType() != ServerException_TypeInputTimeout) {
                        // Nothing was able to be sent to the server so just clear these data structures
-                       for (Transaction *transaction : transactionPartsSent->keySet()) {
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                       while (trit->hasNext()) {
+                               Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
 
                                // Set the transaction sequence number back to nothing
@@ -765,15 +818,19 @@ bool Table::sendToServer(NewKey *newKey) {
                                        transaction->setSequenceNumber(-1);
                                }
                        }
+                       delete trit;
                } else {
                        // There was a partial send to the server
                        hadPartialSendToServer = true;
 
                        // Nothing was able to be sent to the server so just clear these data structures
-                       for (Transaction *transaction : transactionPartsSent->keySet()) {
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
+                       while (trit->hasNext()) {
+                               Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
                                transaction->setServerFailure();
                        }
+                       delete trit;
                }
 
                pendingSendArbitrationEntriesToDelete->clear();
@@ -786,17 +843,16 @@ bool Table::sendToServer(NewKey *newKey) {
 }
 
 bool Table::updateFromLocal(int64_t machineId) {
-       Pair<IoTString *, int32_t> localCommunicationInformation = localCommunicationTable->get(machineId);
-       if (localCommunicationInformation == NULL) {
-               // Cant talk to that device locally so do nothing
+       if (!localCommunicationTable->contains(machineId))
                return false;
-       }
+
+       Pair<IoTString *, int32_t> * localCommunicationInformation = localCommunicationTable->get(machineId);
 
        // Get the size of the send data
        int sendDataSize = sizeof(int32_t) + sizeof(int64_t);
 
        int64_t lastArbitrationDataLocalSequenceNumber = (int64_t) -1;
-       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(machineId) != NULL) {
+       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(machineId)) {
                lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(machineId);
        }
 
@@ -808,7 +864,7 @@ bool Table::updateFromLocal(int64_t machineId) {
        bbEncode->putInt(0);
 
        // Send by local
-       Array<char> *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
+       Array<char> *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation->getFirst(), localCommunicationInformation->getSecond());
        localSequenceNumber++;
 
        if (returnData == NULL) {
@@ -823,10 +879,10 @@ bool Table::updateFromLocal(int64_t machineId) {
        for (int i = 0; i < numberOfEntries; i++) {
                char type = bbDecode->get();
                if (type == TypeAbort) {
-                       Abort *abort = (Abort*)Abort_decode(NULL, bbDecode);
+                       Abort *abort = (Abort *)Abort_decode(NULL, bbDecode);
                        processEntry(abort);
                } else if (type == TypeCommitPart) {
-                       CommitPart *commitPart = (CommitPart*)CommitPart_decode(NULL, bbDecode);
+                       CommitPart *commitPart = (CommitPart *)CommitPart_decode(NULL, bbDecode);
                        processEntry(commitPart);
                }
        }
@@ -839,38 +895,45 @@ bool Table::updateFromLocal(int64_t machineId) {
 Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
 
        // Get the devices local communications
-       Pair<IoTString *, int32_t> localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator());
-
-       if (localCommunicationInformation == NULL) {
-               // Cant talk to that device locally so do nothing
+       if (!localCommunicationTable->contains(transaction->getArbitrator()))
                return Pair<bool, bool>(true, false);
-       }
+       
+       Pair<IoTString *, int32_t> * localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator());
 
        // Get the size of the send data
        int sendDataSize = sizeof(int32_t) + sizeof(int64_t);
-       for (TransactionPart *part : transaction->getParts()->values()) {
-               sendDataSize += part->getSize();
+       {
+               Vector<TransactionPart *> * tParts = transaction->getParts();
+               uint tPartsSize = tParts->size();
+               for (uint i = 0; i < tPartsSize; i++) {
+                       TransactionPart * part = tParts->get(i);
+                       sendDataSize += part->getSize();
+               }
        }
 
        int64_t lastArbitrationDataLocalSequenceNumber = (int64_t) -1;
-       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(transaction->getArbitrator()) != NULL) {
+       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(transaction->getArbitrator())) {
                lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(transaction->getArbitrator());
        }
 
        // Make the send data size
        Array<char> *sendData = new Array<char>(sendDataSize);
-       ByteBuffer *bbEncode = ByteBuffer.wrap(sendData);
+       ByteBuffer *bbEncode = ByteBuffer_wrap(sendData);
 
        // Encode the data
        bbEncode->putLong(lastArbitrationDataLocalSequenceNumber);
        bbEncode->putInt(transaction->getParts()->size());
-       for (TransactionPart *part : transaction->getParts()->values()) {
-               part->encode(bbEncode);
+       {
+               Vector<TransactionPart *> * tParts = transaction->getParts();
+               uint tPartsSize = tParts->size();
+               for (uint i = 0; i < tPartsSize; i++) {
+                       TransactionPart * part = tParts->get(i);
+                       part->encode(bbEncode);
+               }
        }
 
-
        // Send by local
-       Array<char> *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
+       Array<char> *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation->getFirst(), localCommunicationInformation->getSecond());
        localSequenceNumber++;
 
        if (returnData == NULL) {
@@ -888,7 +951,7 @@ Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
        for (int i = 0; i < numberOfEntries; i++) {
                char type = bbDecode->get();
                if (type == TypeAbort) {
-                       Abort *abort = (Abort*)Abort_decode(NULL, bbDecode);
+                       Abort *abort = (Abort *)Abort_decode(NULL, bbDecode);
 
                        if ((abort->getTransactionMachineId() == localMachineId) && (abort->getTransactionClientLocalSequenceNumber() == transaction->getClientLocalSequenceNumber())) {
                                foundAbort = true;
@@ -896,7 +959,7 @@ Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
 
                        processEntry(abort);
                } else if (type == TypeCommitPart) {
-                       CommitPart *commitPart = (CommitPart*)CommitPart_decode(NULL, bbDecode);
+                       CommitPart *commitPart = (CommitPart *)CommitPart_decode(NULL, bbDecode);
                        processEntry(commitPart);
                }
        }
@@ -904,14 +967,14 @@ Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
        updateLiveStateFromLocal();
 
        if (couldArbitrate) {
-               TransactionStatus status =  transaction->getTransactionStatus();
+               TransactionStatus status =  transaction->getTransactionStatus();
                if (didCommit) {
                        status->setStatus(TransactionStatus_StatusCommitted);
                } else {
                        status->setStatus(TransactionStatus_StatusAborted);
                }
        } else {
-               TransactionStatus status =  transaction->getTransactionStatus();
+               TransactionStatus status =  transaction->getTransactionStatus();
                if (foundAbort) {
                        status->setStatus(TransactionStatus_StatusAborted);
                } else {
@@ -923,7 +986,6 @@ Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
 }
 
 Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
-
        // Decode the data
        ByteBuffer *bbDecode = ByteBuffer_wrap(data);
        int64_t lastArbitratedSequenceNumberSeen = bbDecode->getLong();
@@ -939,7 +1001,7 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
                Transaction *transaction = new Transaction();
                for (int i = 0; i < numberOfParts; i++) {
                        bbDecode->get();
-                       TransactionPart *newPart = (TransactionPart)TransactionPart.decode(NULL, bbDecode);
+                       TransactionPart *newPart = (TransactionPart *)TransactionPart_decode(NULL, bbDecode);
                        transaction->addPartDecode(newPart);
                }
 
@@ -952,7 +1014,7 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
 
                // Transaction was sent to the server so keep track of it to prevent double commit
                if (transaction->getSequenceNumber() != -1) {
-                       offlineTransactionsCommittedAndAtServer->add(transaction->getId());
+                       offlineTransactionsCommittedAndAtServer->add(new Pair<int64_t, int64_t>(transaction->getId()));
                }
        }
 
@@ -961,13 +1023,23 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
        Vector<Entry *> *unseenArbitrations = new Vector<Entry *>();
 
        // Get the aborts to send back
-       Vector<int64_t> *abortLocalSequenceNumbers = new Vector<int64_t>(liveAbortsGeneratedByLocal->keySet());
-       Collections->sort(abortLocalSequenceNumbers);
-       for (int64_t localSequenceNumber : abortLocalSequenceNumbers) {
+       Vector<int64_t> *abortLocalSequenceNumbers = new Vector<int64_t>();
+       {
+               SetIterator<int64_t, Abort *> *abortit = getKeyIterator(liveAbortsGeneratedByLocal);
+               while(abortit->hasNext())
+                       abortLocalSequenceNumbers->add(abortit->next());
+               delete abortit;
+       }
+       
+       qsort(abortLocalSequenceNumbers->expose(), abortLocalSequenceNumbers->size(), sizeof(int64_t), compareInt64);
+
+       uint asize = abortLocalSequenceNumbers->size();
+       for(uint i=0; i<asize; i++) {
+               int64_t localSequenceNumber = abortLocalSequenceNumbers->get(i);
                if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
                        continue;
                }
-
+               
                Abort *abort = liveAbortsGeneratedByLocal->get(localSequenceNumber);
                unseenArbitrations->add(abort);
                returnDataSize += abort->getSize();
@@ -976,20 +1048,32 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
        // Get the commits to send back
        Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(localMachineId);
        if (commitForClientTable != NULL) {
-               Vector<int64_t> *commitLocalSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
-               Collections->sort(commitLocalSequenceNumbers);
-
-               for (int64_t localSequenceNumber : commitLocalSequenceNumbers) {
+               Vector<int64_t> *commitLocalSequenceNumbers = new Vector<int64_t>();
+               {
+                       SetIterator<int64_t, Commit *> *commitit = getKeyIterator(commitForClientTable);
+                       while(commitit->hasNext())
+                               commitLocalSequenceNumbers->add(commitit->next());
+                       delete commitit;
+               }
+               qsort(commitLocalSequenceNumbers->expose(), commitLocalSequenceNumbers->size(), sizeof(int64_t), compareInt64);
+
+               uint clsSize = commitLocalSequenceNumbers->size();
+               for(uint clsi = 0; clsi < clsSize; clsi++) {
+                       int64_t localSequenceNumber = commitLocalSequenceNumbers->get(clsi);
                        Commit *commit = commitForClientTable->get(localSequenceNumber);
 
                        if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
                                continue;
                        }
 
-                       unseenArbitrations->addAll(commit->getParts()->values());
-
-                       for (CommitPart *commitPart : commit->getParts()->values()) {
-                               returnDataSize += commitPart->getSize();
+                       {
+                               Vector<CommitPart *> * parts = commit->getParts();
+                               uint nParts = parts->size();
+                               for(uint i=0; i<nParts; i++) {
+                                       CommitPart * commitPart = parts->get(i);
+                                       unseenArbitrations->add(commitPart);
+                                       returnDataSize += commitPart->getSize();
+                               }
                        }
                }
        }
@@ -1021,8 +1105,8 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
 
        bbEncode->putInt(unseenArbitrations->size());
        uint size = unseenArbitrations->size();
-       for(uint i = 0; i< size; i++) {
-               Entry * entry = unseenArbitrations->get(i);
+       for (uint i = 0; i < size; i++) {
+               Entry *entry = unseenArbitrations->get(i);
                entry->encode(bbEncode);
        }
 
@@ -1052,8 +1136,8 @@ ThreeTuple<bool, bool, Array<Slot *> *> Table::sendSlotsToServer(Slot *slot, int
                if (hadPartialSendToServer) {
 
                        bool isInserted = false;
-                       uint size = s->size();
-                       for(uint i=0; i < size; i++) {
+                       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;
@@ -1061,14 +1145,17 @@ ThreeTuple<bool, bool, Array<Slot *> *> Table::sendSlotsToServer(Slot *slot, int
                                }
                        }
 
-                       for(uint i=0; i < size; i++) {
+                       for (uint i = 0; i < size; i++) {
                                Slot *s = array->get(i);
                                if (isInserted) {
                                        break;
                                }
 
                                // Process each entry in the slot
-                               for (Entry *entry : s->getEntries()) {
+                               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;
@@ -1102,7 +1189,7 @@ ThreeTuple<bool, bool, Array<Slot *> *> Table::sendSlotsToServer(Slot *slot, int
 ThreeTuple<bool, int32_t, bool> Table::fillSlot(Slot *slot, bool resize, NewKey *newKeyEntry) {
        int newSize = 0;
        if (liveSlotCount > bufferResizeThreshold) {
-               resize = true;  //Resize is forced
+               resize = true;//Resize is forced
        }
 
        if (resize) {
@@ -1139,17 +1226,20 @@ ThreeTuple<bool, int32_t, bool> Table::fillSlot(Slot *slot, bool resize, NewKey
        // Clear the transactions, aborts and commits that were sent previously
        transactionPartsSent->clear();
        pendingSendArbitrationEntriesToDelete->clear();
-
-       for (ArbitrationRound *round : pendingSendArbitrationRounds) {
+       uint size = pendingSendArbitrationRounds->size();
+       for (uint i = 0; i < size; i++) {
+               ArbitrationRound *round = pendingSendArbitrationRounds->get(i);
                bool isFull = false;
                round->generateParts();
                Vector<Entry *> *parts = round->getParts();
 
                // Insert pending arbitration data
-               for (Entry *arbitrationData : parts) {
+               uint vsize = parts->size();
+               for (uint vi = 0; vi < vsize; vi++) {
+                       Entry *arbitrationData = parts->get(vi);
 
                        // If it is an abort then we need to set some information
-                       if (arbitrationData instanceof Abort) {
+                       if (arbitrationData->getType() == TypeAbort) {
                                ((Abort *)arbitrationData)->setSequenceNumber(slot->getSequenceNumber());
                        }
 
@@ -1210,7 +1300,7 @@ void Table::doRejectedMessages(Slot *s) {
                 * there is already a sufficient entry in the queue (e->g->,
                 * equalsto value of true and same sequence number)->  */
 
-               int64_t old_seqn = rejectedSlotVector->firstElement();
+               int64_t old_seqn = rejectedSlotVector->get(0);
                if (rejectedSlotVector->size() > Table_REJECTED_THRESHOLD) {
                        int64_t new_seqn = rejectedSlotVector->lastElement();
                        RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, new_seqn, false);
@@ -1258,7 +1348,7 @@ ThreeTuple<bool, bool, int64_t> Table::doMandatoryResuce(Slot *slot, bool resize
 
        // Mandatory Rescue
        for (; currentSequenceNumber < threshold; currentSequenceNumber++) {
-               Slot previousSlot = buffer->getSlot(currentSequenceNumber);
+               Slot previousSlot = buffer->getSlot(currentSequenceNumber);
                // Push slot number forward
                if (!seenLiveSlot) {
                        oldestLiveSlotSequenceNumver = currentSequenceNumber;
@@ -1275,7 +1365,9 @@ ThreeTuple<bool, bool, int64_t> Table::doMandatoryResuce(Slot *slot, bool resize
                Vector<Entry *> *liveEntries = previousSlot->getLiveEntries(resize);
 
                // Iterate over all the live entries and try to rescue them
-               for (Entry *liveEntry : liveEntries) {
+               uint lESize = liveEntries->size();
+               for (uint i=0; i< lESize; i++) {
+                       Entry * liveEntry = liveEntries->get(i);
                        if (slot->hasSpace(liveEntry)) {
                                // Enough space to rescue the entry
                                slot->addEntry(liveEntry);
@@ -1307,16 +1399,20 @@ search:
                        continue;
                seenliveslot = true;
                Vector<Entry *> *liveentries = prevslot->getLiveEntries(resize);
-               for (Entry *liveentry : liveentries) {
+               uint lESize = liveentries->size();
+               for (uint i=0; i< lESize; i++) {
+                       Entry * liveentry = liveentries->get(i);
                        if (s->hasSpace(liveentry))
                                s->addEntry(liveentry);
                        else {
                                skipcount++;
                                if (skipcount > Table_SKIP_THRESHOLD)
-                                       break search;
+                                       goto donesearch;
                        }
                }
        }
+ donesearch:
+       ;
 }
 
 /**
@@ -1344,13 +1440,22 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
        checkHMACChain(indexer, newSlots);
 
        // Set to keep track of messages from clients
-       Hashset<int64_t> *machineSet = new Hashset<int64_t>(lastMessageTable->keySet());
+       Hashset<int64_t> *machineSet = new Hashset<int64_t>();
+       {
+               SetIterator<int64_t, Pair<int64_t, Liveness *> *> * lmit=getKeyIterator(lastMessageTable);
+               while(lmit->hasNext())
+                       machineSet->add(lmit->next());
+               delete lmit;
+       }
 
        // Process each slots data
-       for (Slot *slot : newSlots) {
-               processSlot(indexer, slot, acceptUpdatesToLocal, machineSet);
-
-               updateExpectedSize();
+       {
+               uint numSlots = newSlots->length();
+               for(uint i=0; i<numSlots; i++) {
+                       Slot *slot = newSlots->get(i);
+                       processSlot(indexer, slot, acceptUpdatesToLocal, machineSet);
+                       updateExpectedSize();
+               }
        }
 
        // If there is a gap, check to see if the server sent us
@@ -1359,7 +1464,7 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
 
                // Check the size of the slots that were sent down by the server->
                // Can only check the size if there was a gap
-               checkNumSlots(newSlots->length);
+               checkNumSlots(newSlots->length());
 
                // Since there was a gap every machine must have pushed a slot or
                // must have a last message message-> If not then the server is
@@ -1373,16 +1478,19 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
        commitNewMaxSize();
 
        // Commit new to slots to the local block chain->
-       for (Slot *slot : newSlots) {
-
-               // Insert this slot into our local block chain copy->
-               buffer->putSlot(slot);
-
-               // Keep track of how many slots are currently live (have live data
-               // in them)->
-               liveSlotCount++;
+       {
+               uint numSlots = newSlots->length();
+               for(uint i=0; i<numSlots; i++) {
+                       Slot *slot = newSlots->get(i);
+                       
+                       // Insert this slot into our local block chain copy->
+                       buffer->putSlot(slot);
+                       
+                       // Keep track of how many slots are currently live (have live data
+                       // in them)->
+                       liveSlotCount++;
+               }
        }
-
        // Get the sequence number of the latest slot in the system
        sequenceNumber = newSlots->get(newSlots->length() - 1)->getSequenceNumber();
        updateLiveStateFromServer();
@@ -1458,11 +1566,6 @@ void Table::checkNumSlots(int numberOfSlots) {
        }
 }
 
-void Table::updateCurrMaxSize(int newmaxsize) {
-       currMaxSize = newmaxsize;
-}
-
-
 /**
  * Update the size of of the local buffer if it is needed->
  */
@@ -1495,20 +1598,26 @@ void Table::processNewTransactionParts() {
 
        // Iterate through all the machine Ids that we received new parts
        // for
-       for (int64_t machineId : newTransactionParts->keySet()) {
-               Hashtable<Pair<int64_t int32_t>, TransactionPart *> *parts = newTransactionParts->get(machineId);
+       SetIterator<int64_t, Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> * tpit= getKeyIterator(newTransactionParts);
+       while(tpit->hasNext()) {
+               int64_t machineId = tpit->next();
+               Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId);
 
+               SetIterator<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *ptit = getKeyIterator(parts);
                // Iterate through all the parts for that machine Id
-               for (Pair<int64_t, int32_t> partId : parts->keySet()) {
+               while(ptit->hasNext()) {
+                       Pair<int64_t, int32_t> * partId = ptit->next();
                        TransactionPart *part = parts->get(partId);
 
-                       int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(part->getArbitratorId());
-                       if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= part->getSequenceNumber())) {
-                               // Set dead the transaction part
-                               part->setDead();
-                               continue;
+                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(part->getArbitratorId())) {
+                               int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(part->getArbitratorId());
+                               if (lastTransactionNumber >= part->getSequenceNumber()) {
+                                       // Set dead the transaction part
+                                       part->setDead();
+                                       continue;
+                               }
                        }
-
+                       
                        // Get the transaction object for that sequence number
                        Transaction *transaction = liveTransactionBySequenceNumberTable->get(part->getSequenceNumber());
 
@@ -1518,14 +1627,15 @@ void Table::processNewTransactionParts() {
 
                                // Insert this new transaction into the live tables
                                liveTransactionBySequenceNumberTable->put(part->getSequenceNumber(), transaction);
-                               liveTransactionByTransactionIdTable->put(part->getTransactionId(), transaction);
+                               liveTransactionByTransactionIdTable->put(new Pair<int64_t, int64_t>(part->getTransactionId()), 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();
@@ -1539,21 +1649,26 @@ void Table::arbitrateFromServer() {
        }
 
        // Get the transaction sequence numbers and sort from oldest to newest
-       Vector<int64_t> *transactionSequenceNumbers = new Vector<int64_t>(liveTransactionBySequenceNumberTable->keySet());
-       Collections->sort(transactionSequenceNumbers);
+       Vector<int64_t> *transactionSequenceNumbers = new Vector<int64_t>();
+       {
+               SetIterator<int64_t, Transaction *> * trit = getKeyIterator(liveTransactionBySequenceNumberTable);
+               while(trit->hasNext())
+                       transactionSequenceNumbers->add(trit->next());
+               delete trit;
+       }
+       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 *> speculativeTableTmp = new Hashtable<IoTString *, KeyValue *>();
 
        // The last transaction arbitrated on
        int64_t lastTransactionCommitted = -1;
        Hashset<Abort *> *generatedAborts = new Hashset<Abort *>();
-
-       for (int64_t transactionSequenceNumber : transactionSequenceNumbers) {
+       uint tsnSize = transactionSequenceNumbers->size();
+       for(uint i=0; i<tsnSize; i++) {
+               int64_t transactionSequenceNumber = transactionSequenceNumbers->get(i);
                Transaction *transaction = liveTransactionBySequenceNumberTable->get(transactionSequenceNumber);
 
-
-
                // Check if this machine arbitrates for this transaction if not
                // then we cant arbitrate this transaction
                if (transaction->getArbitrator() != localMachineId) {
@@ -1578,7 +1693,7 @@ void Table::arbitrateFromServer() {
 
 
                // update the largest transaction seen by arbitrator from server
-               if (lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId()) == NULL) {
+               if (!lastTransactionSeenFromMachineFromServer->contains(transaction->getMachineId())) {
                        lastTransactionSeenFromMachineFromServer->put(transaction->getMachineId(), transaction->getClientLocalSequenceNumber());
                } else {
                        int64_t lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId());
@@ -1591,10 +1706,13 @@ void Table::arbitrateFromServer() {
                        // Guard evaluated as true
 
                        // Update the local changes so we can make the commit
-                       for (KeyValue *kv : transaction->getKeyValueUpdateSet()) {
+                       SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
+                       while (kvit->hasNext()) {
+                               KeyValue *kv = kvit->next();
                                speculativeTableTmp->put(kv->getKey(), kv);
                        }
-
+                       delete kvit;
+                       
                        // Update what the last transaction committed was for use in batch commit
                        lastTransactionCommitted = transactionSequenceNumber;
                } else {
@@ -1686,10 +1804,13 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
                localArbitrationSequenceNumber++;
 
                // Update the local changes so we can make the commit
-               for (KeyValue *kv : transaction->getKeyValueUpdateSet()) {
+               SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
+               while (kvit->hasNext()) {
+                       KeyValue *kv = kvit->next();
                        newCommit->addKV(kv);
                }
-
+               delete kvit;
+               
                // create the commit parts
                newCommit->createCommitParts();
 
@@ -1723,7 +1844,7 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
                if (transaction->getMachineId() == localMachineId) {
                        // For locally created messages update the status
                        // Guard evaluated was false so create abort
-                       TransactionStatus status = transaction->getTransactionStatus();
+                       TransactionStatus status = transaction->getTransactionStatus();
                        if (status != NULL) {
                                status->setStatus(TransactionStatus_StatusAborted);
                        }
@@ -1770,7 +1891,7 @@ bool Table::compactArbitrationData() {
        }
 
        ArbitrationRound *lastRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
-       if (lastRound->didSendPart()) {
+       if (lastRound->getDidSendPart()) {
                return false;
        }
 
@@ -1779,9 +1900,9 @@ bool Table::compactArbitrationData() {
 
        int numberToDelete = 1;
        while (numberToDelete < pendingSendArbitrationRounds->size()) {
-               ArbitrationRound *xs round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1);
+               ArbitrationRound *round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1);
 
-               if (round->isFull() || round->didSendPart()) {
+               if (round->isFull() || round->getDidSendPart()) {
                        // Stop since there is a part that cannot be compacted and we
                        // need to compact in order
                        break;
@@ -1790,14 +1911,14 @@ bool Table::compactArbitrationData() {
                if (round->getCommit() == NULL) {
                        // Try compacting aborts only
                        int newSize = round->getCurrentSize() + lastRound->getAbortsCount();
-                       if (newSize > ArbitrationRound->MAX_PARTS) {
+                       if (newSize > ArbitrationRound_MAX_PARTS) {
                                // Cant compact since it would be too large
                                break;
                        }
                        lastRound->addAborts(round->getAborts());
                } else {
                        // Create a new larger commit
-                       Commit newCommit = Commit->merge(lastRound->getCommit(), round->getCommit(), localArbitrationSequenceNumber);
+                       Commit * newCommit = Commit_merge(lastRound->getCommit(), round->getCommit(), localArbitrationSequenceNumber);
                        localArbitrationSequenceNumber++;
 
                        // Create the commit parts so that we can count them
@@ -1808,7 +1929,7 @@ bool Table::compactArbitrationData() {
                        newSize += lastRound->getAbortsCount();
                        newSize += round->getAbortsCount();
 
-                       if (newSize > ArbitrationRound->MAX_PARTS) {
+                       if (newSize > ArbitrationRound_MAX_PARTS) {
                                // Cant compact since it would be too large
                                break;
                        }
@@ -1829,7 +1950,7 @@ bool Table::compactArbitrationData() {
                        pendingSendArbitrationRounds->clear();
                } else {
                        for (int i = 0; i < numberToDelete; i++) {
-                               pendingSendArbitrationRounds->remove(pendingSendArbitrationRounds->size() - 1);
+                               pendingSendArbitrationRounds->removeIndex(pendingSendArbitrationRounds->size() - 1);
                        }
                }
 
@@ -1858,7 +1979,7 @@ bool Table::updateCommittedTable() {
 
        // Iterate through all the machine Ids that we received new parts for
        for (int64_t machineId : newCommitParts->keySet()) {
-               Hashtable<Pair<int64_t, int32_t>, CommitPart *> *parts = newCommitParts->get(machineId);
+               Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *parts = newCommitParts->get(machineId);
 
                // Iterate through all the parts for that machine Id
                for (Pair<int64_t, int32_t> partId : parts->keySet()) {
@@ -1903,7 +2024,7 @@ bool Table::updateCommittedTable() {
 
                // Sort the commits in order
                Vector<int64_t> *commitSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
-               Collections->sort(commitSequenceNumbers);
+               qsort(commitSequenceNumbers->expose(), commitSequenceNumbers->size(), sizeof(int64_t), compareInt64);
 
                // Get the last commit seen from this arbitrator
                int64_t lastCommitSeenSequenceNumber = -1;
@@ -1944,8 +2065,7 @@ bool Table::updateCommittedTable() {
                        }
 
                        // Update the last arbitration data that we have seen so far
-                       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(commit->getMachineId()) != NULL) {
-
+                       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(commit->getMachineId())) {
                                int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(commit->getMachineId());
                                if (commit->getSequenceNumber() > lastArbitrationSequenceNumber) {
                                        // Is larger
@@ -1978,8 +2098,13 @@ bool Table::updateCommittedTable() {
                        // Get what commits should be edited, these are the commits that
                        // have live values for their keys
                        Hashset<Commit *> *commitsToEdit = new Hashset<Commit *>();
-                       for (KeyValue *kv : commit->getKeyValueUpdateSet()) {
-                               commitsToEdit->add(liveCommitsByKeyTable->get(kv->getKey()));
+                       {
+                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                               while (kvit->hasNext()) {
+                                       KeyValue *kv = kvit->next();
+                                       commitsToEdit->add(liveCommitsByKeyTable->get(kv->getKey()));
+                               }
+                               delete kvit;
                        }
                        commitsToEdit->remove(NULL);            // remove NULL since it could be in this set
 
@@ -1990,10 +2115,15 @@ bool Table::updateCommittedTable() {
                                if (previousCommit->isLive()) {
 
                                        // Update which keys in the old commits are still live
-                                       for (KeyValue *kv : commit->getKeyValueUpdateSet()) {
-                                               previousCommit->invalidateKey(kv->getKey());
+                                       {
+                                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                                               while (kvit->hasNext()) {
+                                                       KeyValue *kv = kvit->next();
+                                                       previousCommit->invalidateKey(kv->getKey());
+                                               }
+                                               delete kvit;
                                        }
-
+                                       
                                        // if the commit is now dead then remove it
                                        if (!previousCommit->isLive()) {
                                                commitForClientTable->remove(previousCommit);
@@ -2014,9 +2144,14 @@ bool Table::updateCommittedTable() {
                        didProcessANewCommit = true;
 
                        // Update the committed table of keys and which commit is using which key
-                       for (KeyValue *kv : commit->getKeyValueUpdateSet()) {
-                               committedKeyValueTable->put(kv->getKey(), kv);
-                               liveCommitsByKeyTable->put(kv->getKey(), commit);
+                       {
+                               SetIterator<KeyValue *, KeyValue *, uintptr_t, 0, hashKeyValue, equalsKeyValue> *kvit = commit->getKeyValueUpdateSet()->iterator();
+                               while (kvit->hasNext()) {
+                                       KeyValue *kv = kvit->next();
+                                       committedKeyValueTable->put(kv->getKey(), kv);
+                                       liveCommitsByKeyTable->put(kv->getKey(), commit);
+                               }
+                               delete kvit;
                        }
                }
        }
@@ -2037,7 +2172,7 @@ bool Table::updateSpeculativeTable(bool didProcessNewCommits) {
        // Create a list of the transaction sequence numbers and sort them
        // from oldest to newest
        Vector<int64_t> *transactionSequenceNumbersSorted = new Vector<int64_t>(liveTransactionBySequenceNumberTable->keySet());
-       Collections->sort(transactionSequenceNumbersSorted);
+       qsort(transactionSequenceNumberSorted->expose(), transactionSequenceNumbersSorted->size(), sizeof(int64_t), compareInt64);
 
        bool hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted->get(0) != oldestTransactionSequenceNumberSpeculatedOn;
 
@@ -2090,8 +2225,13 @@ bool Table::updateSpeculativeTable(bool didProcessNewCommits) {
 
                if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, NULL)) {
                        // Guard evaluated to true so update the speculative table
-                       for (KeyValue *kv : transaction->getKeyValueUpdateSet()) {
-                               speculatedKeyValueTable->put(kv->getKey(), kv);
+                       {
+                               SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
+                               while (kvit->hasNext()) {
+                                       KeyValue *kv = kvit->next();
+                                       speculatedKeyValueTable->put(kv->getKey(), kv);
+                               }
+                               delete kvit;
                        }
                }
        }
@@ -2138,9 +2278,12 @@ void Table::updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOr
 
                if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) {
                        // Guard evaluated to true so update the speculative table
-                       for (KeyValue *kv : transaction->getKeyValueUpdateSet()) {
+                       SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
+                       while (kvit->hasNext()) {
+                               KeyValue *kv = kvit->next();
                                pendingTransactionSpeculatedKeyValueTable->put(kv->getKey(), kv);
                        }
+                       delete kvit;
                }
        }
 }
@@ -2194,7 +2337,10 @@ void Table::processSlot(SlotIndexer *indexer, Slot *slot, bool acceptUpdatesToLo
        updateLastMessage(slot->getMachineID(), slot->getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet);
 
        // Process each entry in the slot
-       for (Entry *entry : slot->getEntries()) {
+       Vector<Entry *> *entries = slot->getEntries();
+       uint eSize = entries->size();
+       for(uint ei=0; ei < eSize; ei++) {
+               Entry * entry = entries->get(ei);
                switch (entry->getType()) {
                case TypeCommitPart:
                        processEntry((CommitPart *)entry);
@@ -2253,7 +2399,7 @@ void Table::processEntry(NewKey *entry) {
  * seen in this current round of updating the local copy of the block
  * chain
  */
-void Table::processEntry(TableStatus entry, int64_t seq) {
+void Table::processEntry(TableStatus entry, int64_t seq) {
        int newNumSlots = entry->getMaxSlots();
        updateCurrMaxSize(newNumSlots);
        initExpectedSize(seq, newNumSlots);
@@ -2346,7 +2492,7 @@ void Table::processEntry(Abort *entry) {
        // keep track of it
        Abort *previouslySeenAbort = liveAbortTable->put(entry->getAbortId(), entry);
        if (previouslySeenAbort != NULL) {
-               previouslySeenAbort->setDead(); // Delete old version of the abort since we got a rescued newer version
+               previouslySeenAbort->setDead();         // Delete old version of the abort since we got a rescued newer version
        }
 
        if (entry->getTransactionArbitrator() == localMachineId) {
@@ -2356,7 +2502,7 @@ void Table::processEntry(Abort *entry) {
        if ((entry->getSequenceNumber() != -1) && (lastMessageTable->get(entry->getTransactionMachineId()).getFirst() >= entry->getSequenceNumber())) {
                // The machine already saw this so it is dead
                entry->setDead();
-               liveAbortTable->remove(entry->getAbortId());
+               liveAbortTable->remove(&entry->getAbortId());
 
                if (entry->getTransactionArbitrator() == localMachineId) {
                        liveAbortsGeneratedByLocal->remove(entry->getArbitratorLocalSequenceNumber());
@@ -2365,7 +2511,7 @@ void Table::processEntry(Abort *entry) {
        }
 
        // Update the last arbitration data that we have seen so far
-       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(entry->getTransactionArbitrator()) != NULL) {
+       if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(entry->getTransactionArbitrator())) {
                int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(entry->getTransactionArbitrator());
                if (entry->getSequenceNumber() > lastArbitrationSequenceNumber) {
                        // Is larger
@@ -2408,11 +2554,11 @@ void Table::processEntry(TransactionPart *entry) {
        }
 
        // This part is still alive
-       Hashtable<Pair<int64_t, int32_t>, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *transactionPart = newTransactionParts->get(entry->getMachineId());
+       Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *transactionPart = newTransactionParts->get(entry->getMachineId());
 
        if (transactionPart == NULL) {
                // Dont have a table for this machine Id yet so make one
-               transactionPart = new Hashtable<Pair<int64_t, int32_t>, TransactionPart *>();
+               transactionPart = new Hashtable<Pair<int64_t, int32_t> *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals>();
                newTransactionParts->put(entry->getMachineId(), transactionPart);
        }
 
@@ -2438,10 +2584,10 @@ void Table::processEntry(CommitPart *entry) {
                }
        }
 
-       Hashtable<Pair<int64_t, int32_t>, CommitPart *> *commitPart = newCommitParts->get(entry->getMachineId());
+       Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *commitPart = newCommitParts->get(entry->getMachineId());
        if (commitPart == NULL) {
                // Don't have a table for this machine Id yet so make one
-               commitPart = new Hashtable<Pair<int64_t, int32_t>, CommitPart *>();
+               commitPart = new Hashtable<Pair<int64_t, int32_t> *, CommitPart *>();
                newCommitParts->put(entry->getMachineId(), commitPart);
        }
        // Update the part and set dead ones we have already seen (got a
@@ -2460,7 +2606,7 @@ void Table::processEntry(CommitPart *entry) {
  * our own last message or that other clients have not had a rollback
  * on the last message->
  */
-void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liveness, bool acceptUpdatesToLocal, Hashset<intr64_t> *machineSet) {
+void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liveness, bool acceptUpdatesToLocal, Hashset<int64_t> *machineSet) {
        // We have seen this machine ID
        machineSet->remove(machineId);
 
@@ -2470,7 +2616,9 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
        if (watchset != NULL) {
                // Go through each rejected message that this machine Id has not
                // seen yet
-               for (Iterator<RejectedMessage *> *rmit = watchset->iterator(); rmit->hasNext(); ) {
+
+               SetIterator<RejectedMessage *, RejectedMessage *> *rmit = watchset->iterator();
+               while(rmit->hasNext()) {
                        RejectedMessage *rm = rmit->next();
                        // If this machine Id has seen this rejected message->->->
                        if (rm->getSequenceNumber() <= seqNum) {
@@ -2480,6 +2628,7 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
                                rm->removeWatcher(machineId);
                        }
                }
+               delete rmit;
        }
 
        // Set dead the abort
@@ -2495,29 +2644,33 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
        }
        if (machineId == localMachineId) {
                // Our own messages are immediately dead->
-               if (liveness instanceof LastMessage) {
+               char livenessType = liveness->getType();
+               if (livenessType==TypeLastMessage) {
                        ((LastMessage *)liveness)->setDead();
-               } else if (liveness instanceof Slot) {
+               } else if (livenessType == TypeSlot) {
                        ((Slot *)liveness)->setDead();
                } else {
                        throw new Error("Unrecognized type");
                }
        }
        // Get the old last message for this device
-       Pair<int64_t, Liveness *> lastMessageEntry = lastMessageTable->put(machineId, Pair<int64_t, Liveness *>(seqNum, liveness));
+       Pair<int64_t, Liveness *> * lastMessageEntry = lastMessageTable->put(machineId, new Pair<int64_t, Liveness *>(seqNum, liveness));
        if (lastMessageEntry == NULL) {
                // If no last message then there is nothing else to process
                return;
        }
 
-       int64_t lastMessageSeqNum = lastMessageEntry.getFirst();
-       Liveness *lastEntry = lastMessageEntry.getSecond();
-
+       int64_t lastMessageSeqNum = lastMessageEntry->getFirst();
+       Liveness *lastEntry = lastMessageEntry->getSecond();
+       delete lastMessageEntry;
+       
        // If it is not our machine Id since we already set ours to dead
        if (machineId != localMachineId) {
-               if (lastEntry instanceof LastMessage) {
+               char lastEntryType = lastEntry->getType();
+               
+               if (lastEntryType == TypeLastMessage) {
                        ((LastMessage *)lastEntry)->setDead();
-               } else if (lastEntry instanceof Slot) {
+               } else if (lastEntryType == TypeSlot) {
                        ((Slot *)lastEntry)->setDead();
                } else {
                        throw new Error("Unrecognized type");