edits
[iotcloud.git] / version2 / src / C / Table.cc
index ee3b8bb0438b1f63f5343b4236ce8b2efda9ef1e..d70a590890b170fac9417fc6ab049437304de286 100644 (file)
@@ -10,7 +10,7 @@
 #include "TransactionStatus.h"
 #include "Transaction.h"
 #include "LastMessage.h"
-#include "Random.h"
+#include "SecureRandom.h"
 #include "ByteBuffer.h"
 #include "Abort.h"
 #include "CommitPart.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),
@@ -149,7 +161,7 @@ Table::Table(CloudComm *_cloud, int64_t _localMachineId) :
  */
 void Table::init() {
        // Init helper objects
-       random = new Random();
+       random = new SecureRandom();
        buffer = new SlotBuffer();
 
        // init data structs
@@ -166,7 +178,7 @@ void Table::init() {
        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 *> >();
+       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>();
@@ -234,7 +246,7 @@ int64_t Table::getArbitrator(IoTString *key) {
 }
 
 void Table::close() {
-       cloud->close();
+       cloud->closeCloud();
 }
 
 IoTString *Table::getCommitted(IoTString *key)  {
@@ -325,7 +337,7 @@ bool Table::update()  {
                updateLiveTransactionsAndStatus();
                return true;
        } catch (Exception *e) {
-               SetIterator<int64_t> *kit = getKeyIterator(localCommunicationTable);
+               SetIterator<int64_t, Pair<IoTString *, int32_t> *> *kit = getKeyIterator(localCommunicationTable);
                while (kit->hasNext()) {
                        int64_t m = kit->next();
                        updateFromLocal(m);
@@ -468,7 +480,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                }
                                        }
 
-                                       SetIterator<Transaction *> *trit = getKeyIterator(lastTransactionPartsSent);
+                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                        while (trit->hasNext()) {
                                                Transaction *transaction = trit->next();
                                                transaction->resetServerFailure();
@@ -527,7 +539,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                        }
                                                }
 
-                                               SetIterator<Transaction *> *trit = getKeyIterator(lastTransactionPartsSent);
+                                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                                while (trit->hasNext()) {
                                                        Transaction *transaction = trit->next();
                                                        transaction->resetServerFailure();
@@ -557,7 +569,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                        }
                                }
 
-                               SetIterator<Transaction *> *trit = getKeyIterator(lastTransactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetServerFailure();
@@ -592,9 +604,9 @@ bool Table::sendToServer(NewKey *newKey) {
                                        // 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);
-                                               
+                                       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())) {
@@ -612,7 +624,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                                }
                                        }
 
-                                       SetIterator<Transaction *> *trit = getKeyIterator(lastTransactionPartsSent);
+                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                        while (trit->hasNext()) {
                                                Transaction *transaction = trit->next();
                                                transaction->resetServerFailure();
@@ -640,7 +652,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                        }
                                        delete trit;
                                } else {
-                                       SetIterator<Transaction *> *trit = getKeyIterator(lastTransactionPartsSent);
+                                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(lastTransactionPartsSent);
                                        while (trit->hasNext()) {
                                                Transaction *transaction = trit->next();
                                                transaction->resetServerFailure();
@@ -691,7 +703,7 @@ bool Table::sendToServer(NewKey *newKey) {
 
                        if (needsResize) {
                                // Reset which transaction to send
-                               SetIterator<Transaction *> *trit = getKeyIterator(transactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
@@ -747,7 +759,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                }
                                pendingSendArbitrationRounds->setSize(oldcount);
 
-                               SetIterator<Transaction *> *trit = getKeyIterator(transactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetServerFailure();
@@ -770,7 +782,7 @@ bool Table::sendToServer(NewKey *newKey) {
                                delete trit;
                        } else {
                                // Reset which transaction to send
-                               SetIterator<Transaction *> *trit = getKeyIterator(transactionPartsSent);
+                               SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
                                while (trit->hasNext()) {
                                        Transaction *transaction = trit->next();
                                        transaction->resetNextPartToSend();
@@ -794,9 +806,9 @@ 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
-                       SetIterator<Transaction *> *trit = getKeyIterator(transactionPartsSent);
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
                        while (trit->hasNext()) {
                                Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
@@ -812,7 +824,7 @@ bool Table::sendToServer(NewKey *newKey) {
                        hadPartialSendToServer = true;
 
                        // Nothing was able to be sent to the server so just clear these data structures
-                       SetIterator<Transaction *> *trit = getKeyIterator(transactionPartsSent);
+                       SetIterator<Transaction *, Vector<int> *> *trit = getKeyIterator(transactionPartsSent);
                        while (trit->hasNext()) {
                                Transaction *transaction = trit->next();
                                transaction->resetNextPartToSend();
@@ -834,13 +846,13 @@ bool Table::updateFromLocal(int64_t machineId) {
        if (!localCommunicationTable->contains(machineId))
                return false;
 
-       Pair<IoTString *, int32_t> * localCommunicationInformation = localCommunicationTable->get(machineId);
+       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);
        }
 
@@ -852,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) {
@@ -883,19 +895,24 @@ bool Table::updateFromLocal(int64_t machineId) {
 Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
 
        // Get the devices local communications
-       if (!localCommunicationTable->contains(machineId))
+       if (!localCommunicationTable->contains(transaction->getArbitrator()))
                return Pair<bool, bool>(true, false);
-       
-       Pair<IoTString *, int32_t> localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator());
+
+       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());
        }
 
@@ -906,13 +923,17 @@ Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
        // 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) {
@@ -946,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 {
@@ -965,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();
@@ -994,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()));
                }
        }
 
@@ -1003,15 +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);
+       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++) {
+       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();
@@ -1020,11 +1048,17 @@ 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);
+               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++) {
+               for (uint clsi = 0; clsi < clsSize; clsi++) {
                        int64_t localSequenceNumber = commitLocalSequenceNumbers->get(clsi);
                        Commit *commit = commitForClientTable->get(localSequenceNumber);
 
@@ -1032,10 +1066,14 @@ Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
                                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();
+                               }
                        }
                }
        }
@@ -1116,8 +1154,8 @@ ThreeTuple<bool, bool, Array<Slot *> *> Table::sendSlotsToServer(Slot *slot, int
                                // 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);
+                               for (uint ei = 0; ei < eSize; ei++) {
+                                       Entry *entry = entries->get(ei);
 
                                        if (entry->getType() == TypeLastMessage) {
                                                LastMessage *lastMessage = (LastMessage *)entry;
@@ -1262,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);
@@ -1310,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;
@@ -1327,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);
@@ -1348,7 +1388,6 @@ void Table::doOptionalRescue(Slot *s, bool seenliveslot, int64_t seqn, bool resi
         * for SKIP_THRESHOLD consecutive entries*/
        int skipcount = 0;
        int64_t newestseqnum = buffer->getNewestSeqNum();
-search:
        for (; seqn <= newestseqnum; seqn++) {
                Slot *prevslot = buffer->getSlot(seqn);
                //Push slot number forward
@@ -1359,7 +1398,9 @@ 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 {
@@ -1369,7 +1410,7 @@ search:
                        }
                }
        }
- donesearch:
+donesearch:
        ;
 }
 
@@ -1398,13 +1439,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
@@ -1413,7 +1463,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
@@ -1427,16 +1477,19 @@ void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal
        commitNewMaxSize();
 
        // Commit new to slots to the local block chain->
-       for (Slot *slot : newSlots) {
+       {
+               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);
+                       // 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++;
+                       // 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();
@@ -1512,11 +1565,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->
  */
@@ -1549,18 +1597,24 @@ void Table::processNewTransactionParts() {
 
        // Iterate through all the machine Ids that we received new parts
        // for
-       for (int64_t machineId : newTransactionParts->keySet()) {
+       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
@@ -1572,14 +1626,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();
@@ -1593,21 +1648,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) {
@@ -1632,7 +1692,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());
@@ -1645,13 +1705,13 @@ void Table::arbitrateFromServer() {
                        // Guard evaluated as true
 
                        // Update the local changes so we can make the commit
-                       SetIterator<KeyValue *> *kvit = getKeyIterator(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 {
@@ -1682,9 +1742,13 @@ void Table::arbitrateFromServer() {
                localArbitrationSequenceNumber++;
 
                // Add all the new keys to the commit
-               for (KeyValue *kv : speculativeTableTmp->values()) {
+               SetIterator<IoTString *, KeyValue *> *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();
@@ -1692,7 +1756,10 @@ void Table::arbitrateFromServer() {
                // Append all the commit parts to the end of the pending queue
                // waiting for sending to the server
                // Insert the commit so we can process it
-               for (CommitPart *commitPart : newCommit->getParts()->values()) {
+               Vector<CommitPart *> *parts = newCommit->getParts();
+               uint partsSize = parts->size();
+               for (uint i = 0; i < partsSize; i++) {
+                       CommitPart *commitPart = parts->get(i);
                        processEntry(commitPart);
                }
        }
@@ -1704,7 +1771,10 @@ void Table::arbitrateFromServer() {
                if (compactArbitrationData()) {
                        ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
                        if (newArbitrationRound->getCommit() != NULL) {
-                               for (CommitPart *commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+                               Vector<CommitPart *> *parts = newArbitrationRound->getCommit()->getParts();
+                               uint partsSize = parts->size();
+                               for (uint i = 0; i < partsSize; i++) {
+                                       CommitPart *commitPart = parts->get(i);
                                        processEntry(commitPart);
                                }
                        }
@@ -1728,7 +1798,7 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
 
        if (transaction->getMachineId() != localMachineId) {
                // dont do this check for local transactions
-               if (lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId()) != NULL) {
+               if (lastTransactionSeenFromMachineFromServer->contains(transaction->getMachineId())) {
                        if (lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId()) > transaction->getClientLocalSequenceNumber()) {
                                // We've have already seen this from the server
                                return Pair<bool, bool>(false, false);
@@ -1743,13 +1813,13 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
                localArbitrationSequenceNumber++;
 
                // Update the local changes so we can make the commit
-               SetIterator<KeyValue *> *kvit = getKeyIterator(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();
 
@@ -1760,12 +1830,18 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
 
                if (compactArbitrationData()) {
                        ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
-                       for (CommitPart *commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+                       Vector<CommitPart *> *parts = newArbitrationRound->getCommit()->getParts();
+                       uint partsSize = parts->size();
+                       for (uint i = 0; i < partsSize; i++) {
+                               CommitPart *commitPart = parts->get(i);
                                processEntry(commitPart);
                        }
                } else {
                        // Insert the commit so we can process it
-                       for (CommitPart *commitPart : newCommit->getParts()->values()) {
+                       Vector<CommitPart *> *parts = newCommit->getParts();
+                       uint partsSize = parts->size();
+                       for (uint i = 0; i < partsSize; i++) {
+                               CommitPart *commitPart = parts->get(i);
                                processEntry(commitPart);
                        }
                }
@@ -1783,7 +1859,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);
                        }
@@ -1807,7 +1883,11 @@ Pair<bool, bool> Table::arbitrateOnLocalTransaction(Transaction *transaction) {
 
                        if (compactArbitrationData()) {
                                ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
-                               for (CommitPart *commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+
+                               Vector<CommitPart *> *parts = newArbitrationRound->getCommit()->getParts();
+                               uint partsSize = parts->size();
+                               for (uint i = 0; i < partsSize; i++) {
+                                       CommitPart *commitPart = parts->get(i);
                                        processEntry(commitPart);
                                }
                        }
@@ -1839,7 +1919,7 @@ 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->getDidSendPart()) {
                        // Stop since there is a part that cannot be compacted and we
@@ -1850,14 +1930,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
@@ -1868,7 +1948,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;
                        }
@@ -1889,7 +1969,7 @@ bool Table::compactArbitrationData() {
                        pendingSendArbitrationRounds->clear();
                } else {
                        for (int i = 0; i < numberToDelete; i++) {
-                               pendingSendArbitrationRounds->remove(pendingSendArbitrationRounds->size() - 1);
+                               pendingSendArbitrationRounds->removeIndex(pendingSendArbitrationRounds->size() - 1);
                        }
                }
 
@@ -1917,11 +1997,15 @@ 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);
+       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);
 
                // Iterate through all the parts for that machine Id
-               for (Pair<int64_t, int32_t> partId : parts->keySet()) {
+               SetIterator<Pair<int64_t, int32_t> *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *pairit = getKeyIterator(parts);
+               while (pairit->hasNext()) {
+                       Pair<int64_t, int32_t> *partId = pairit->next();
                        CommitPart *part = parts->get(partId);
 
                        // Get the transaction object for that sequence number
@@ -1946,7 +2030,9 @@ bool Table::updateCommittedTable() {
                        // Add that part to the commit
                        commit->addPartDecode(part);
                }
+               delete pairit;
        }
+       delete partsit;
 
        // Clear all the new commits parts in preparation for the next time
        // the server sends slots
@@ -1956,18 +2042,27 @@ bool Table::updateCommittedTable() {
        bool didProcessANewCommit = false;
 
        // Process the commits one by one
-       for (int64_T arbitratorId : liveCommitsTable->keySet()) {
+       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);
 
                // Sort the commits in order
-               Vector<int64_t> *commitSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
-               Collections->sort(commitSequenceNumbers);
+               Vector<int64_t> *commitSequenceNumbers = new Vector<int64_t>();
+               {
+                       SetIterator<int64_t, Commit *> *clientit = getKeyIterator(commitForClientTable);
+                       while (clientit->hasNext())
+                               commitSequenceNumbers->add(clientit->next());
+                       delete clientit;
+               }
+
+               qsort(commitSequenceNumbers->expose(), commitSequenceNumbers->size(), sizeof(int64_t), compareInt64);
 
                // Get the last commit seen from this arbitrator
                int64_t lastCommitSeenSequenceNumber = -1;
-               if (lastCommitSeenSequenceNumberByArbitratorTable->get(arbitratorId) != NULL) {
+               if (lastCommitSeenSequenceNumberByArbitratorTable->contains(arbitratorId)) {
                        lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable->get(arbitratorId);
                }
 
@@ -1995,10 +2090,8 @@ bool Table::updateCommittedTable() {
 
                        // Update the last transaction that was updated if we can
                        if (commit->getTransactionSequenceNumber() != -1) {
-                               int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId());
-
-                               // Update the last transaction sequence number that the arbitrator arbitrated on
-                               if ((lastTransactionNumber == NULL) || (lastTransactionNumber < commit->getTransactionSequenceNumber())) {
+                               // Update the last transaction sequence number that the arbitrator arbitrated on1
+                               if (!lastArbitratedTransactionNumberByArbitratorTable->contains(commit->getMachineId()) || lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId()) < commit->getTransactionSequenceNumber()) {
                                        lastArbitratedTransactionNumberByArbitratorTable->put(commit->getMachineId(), commit->getTransactionSequenceNumber());
                                }
                        }
@@ -2022,9 +2115,8 @@ bool Table::updateCommittedTable() {
                                // Update the last transaction that was updated if we can
                                if (commit->getTransactionSequenceNumber() != -1) {
                                        int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId());
-
-                                       // Update the last transaction sequence number that the arbitrator arbitrated on
-                                       if ((lastTransactionNumber == NULL) || (lastTransactionNumber < commit->getTransactionSequenceNumber())) {
+                                       if (!lastArbitratedTransactionNumberByArbitratorTable->contains(commit->getMachineId()) ||
+                                                       lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId()) < commit->getTransactionSequenceNumber()) {
                                                lastArbitratedTransactionNumberByArbitratorTable->put(commit->getMachineId(), commit->getTransactionSequenceNumber());
                                        }
                                }
@@ -2037,37 +2129,45 @@ 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 *>();
-                       SetIterator<KeyValue *> *kvit = getKeyIterator(commit->getKeyValueUpdateSet());
-                       while (kvit->hasNext()) {
-                               KeyValue *kv = kvit->next();
-                               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();
+                                       Commit *commit = liveCommitsByKeyTable->get(kv->getKey());
+                                       if (commit != NULL)
+                                               commitsToEdit->add(commit);
+                               }
+                               delete kvit;
                        }
-                       delete kvit;
-                       commitsToEdit->remove(NULL);            // remove NULL since it could be in this set
 
                        // Update each previous commit that needs to be updated
-                       for (Commit *previousCommit : commitsToEdit) {
+                       SetIterator<Commit *, Commit *> *commitit = commitsToEdit->iterator();
+                       while (commitit->hasNext()) {
+                               Commit *previousCommit = commitit->next();
 
                                // Only bother with live commits (TODO: Maybe remove this check)
                                if (previousCommit->isLive()) {
 
                                        // Update which keys in the old commits are still live
-                                       SetIterator<KeyValue *> *kvit = getKeyIterator(commit->getKeyValueUpdateSet());
-                                       while (kvit->hasNext()) {
-                                               KeyValue *kv = kvit->next();
-                                               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;
                                        }
-                                       delete kvit;
-                                       
+
                                        // if the commit is now dead then remove it
                                        if (!previousCommit->isLive()) {
-                                               commitForClientTable->remove(previousCommit);
+                                               commitForClientTable->remove(previousCommit->getSequenceNumber());
                                        }
                                }
                        }
+                       delete commitit;
 
                        // Update the last seen sequence number from this arbitrator
-                       if (lastCommitSeenSequenceNumberByArbitratorTable->get(commit->getMachineId()) != NULL) {
+                       if (lastCommitSeenSequenceNumberByArbitratorTable->contains(commit->getMachineId())) {
                                if (commit->getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable->get(commit->getMachineId())) {
                                        lastCommitSeenSequenceNumberByArbitratorTable->put(commit->getMachineId(), commit->getSequenceNumber());
                                }
@@ -2079,15 +2179,18 @@ bool Table::updateCommittedTable() {
                        didProcessANewCommit = true;
 
                        // Update the committed table of keys and which commit is using which key
-                       SetIterator<KeyValue *> *kvit = getKeyIterator(commit->getKeyValueUpdateSet());
-                       while (kvit->hasNext()) {
-                               KeyValue *kv = kvit->next();
-                               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;
                        }
-                       delete kvit;
                }
        }
+       delete liveit;
 
        return didProcessANewCommit;
 }
@@ -2097,15 +2200,22 @@ bool Table::updateCommittedTable() {
  * and have come from the cloud
  */
 bool Table::updateSpeculativeTable(bool didProcessNewCommits) {
-       if (liveTransactionBySequenceNumberTable->keySet()->size() == 0) {
+       if (liveTransactionBySequenceNumberTable->size() == 0) {
                // There is nothing to speculate on
                return false;
        }
 
        // 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);
+       Vector<int64_t> *transactionSequenceNumbersSorted = new Vector<int64_t>();
+       {
+               SetIterator<int64_t, Transaction *> *trit = getKeyIterator(liveTransactionBySequenceNumberTable);
+               while (trit->hasNext())
+                       transactionSequenceNumbersSorted->add(trit->next());
+               delete trit;
+       }
+
+       qsort(transactionSequenceNumbersSorted->expose(), transactionSequenceNumbersSorted->size(), sizeof(int64_t), compareInt64);
 
        bool hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted->get(0) != oldestTransactionSequenceNumberSpeculatedOn;
 
@@ -2120,14 +2230,18 @@ bool Table::updateSpeculativeTable(bool didProcessNewCommits) {
                speculatedKeyValueTable->clear();
                lastTransactionSequenceNumberSpeculatedOn = -1;
                oldestTransactionSequenceNumberSpeculatedOn = -1;
-
        }
 
        // Remember the front of the transaction list
        oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted->get(0);
 
        // Find where to start arbitration from
-       int startIndex = transactionSequenceNumbersSorted->indexOf(lastTransactionSequenceNumberSpeculatedOn) + 1;
+       int startIndex = 0;
+
+       for (; startIndex < transactionSequenceNumbersSorted->size(); startIndex++)
+               if (transactionSequenceNumbersSorted->get(startIndex) == lastTransactionSequenceNumberSpeculatedOn)
+                       break;
+       startIndex++;
 
        if (startIndex >= transactionSequenceNumbersSorted->size()) {
                // Make sure we are not out of bounds
@@ -2158,12 +2272,14 @@ bool Table::updateSpeculativeTable(bool didProcessNewCommits) {
 
                if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, NULL)) {
                        // Guard evaluated to true so update the speculative table
-                       SetIterator<KeyValue *> *kvit = getKeyIterator(commit->getKeyValueUpdateSet());
-                       while (kvit->hasNext()) {
-                               KeyValue *kv = kvit->next();
-                               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;
                        }
-                       delete kvit;
                }
        }
 
@@ -2195,7 +2311,11 @@ void Table::updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOr
        }
 
        // Find where to start arbitration from
-       int startIndex = pendingTransactionQueue->indexOf(firstPendingTransaction) + 1;
+       int startIndex = 0;
+
+       for (; startIndex < pendingTransactionQueue->size(); startIndex++)
+               if (pendingTransactionQueue->get(startIndex) == firstPendingTransaction)
+                       break;
 
        if (startIndex >= pendingTransactionQueue->size()) {
                // Make sure we are not out of bounds
@@ -2209,7 +2329,7 @@ void Table::updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOr
 
                if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) {
                        // Guard evaluated to true so update the speculative table
-                       SetIterator<KeyValue *> *kvit = getKeyIterator(commit->getKeyValueUpdateSet());
+                       SetIterator<KeyValue *, KeyValue *> *kvit = transaction->getKeyValueUpdateSet()->iterator();
                        while (kvit->hasNext()) {
                                KeyValue *kv = kvit->next();
                                pendingTransactionSpeculatedKeyValueTable->put(kv->getKey(), kv);
@@ -2224,38 +2344,44 @@ void Table::updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOr
  * transactions that are dead
  */
 void Table::updateLiveTransactionsAndStatus() {
-
        // Go through each of the transactions
-       for (Iterator<Map->Entry<int64_t, Transaction> > *iter = liveTransactionBySequenceNumberTable->entrySet()->iterator(); iter->hasNext();) {
-               Transaction *transaction = iter->next()->getValue();
-
-               // Check if the transaction is dead
-               int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(transaction->getArbitrator());
-               if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= transaction->getSequenceNumber())) {
-
-                       // Set dead the transaction
-                       transaction->setDead();
-
-                       // Remove the transaction from the live table
-                       iter->remove();
-                       liveTransactionByTransactionIdTable->remove(transaction->getId());
+       {
+               SetIterator<int64_t, Transaction *> *iter = getKeyIterator(liveTransactionBySequenceNumberTable);
+               while (iter->hasNext()) {
+                       int64_t key = iter->next();
+                       Transaction *transaction = liveTransactionBySequenceNumberTable->get(key);
+
+                       // Check if the transaction is dead
+                       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 iter;
        }
 
        // Go through each of the transactions
-       for (Iterator<Map->Entry<int64_t, TransactionStatus *> > *iter = outstandingTransactionStatus->entrySet()->iterator(); iter->hasNext();) {
-               TransactionStatus *status = iter->next()->getValue();
+       {
+               SetIterator<int64_t, TransactionStatus *> *iter = getKeyIterator(outstandingTransactionStatus);
+               while (iter->hasNext()) {
+                       int64_t key = iter->next();
+                       TransactionStatus *status = outstandingTransactionStatus->get(key);
 
-               // Check if the transaction is dead
-               int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator());
-               if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= status->getTransactionSequenceNumber())) {
+                       // Check if the transaction is dead
+                       if (lastArbitratedTransactionNumberByArbitratorTable->contains(status->getTransactionArbitrator()) && (lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator()) >= status->getTransactionSequenceNumber())) {
 
-                       // Set committed
-                       status->setStatus(TransactionStatus_StatusCommitted);
+                               // Set committed
+                               status->setStatus(TransactionStatus_StatusCommitted);
 
-                       // Remove
-                       iter->remove();
+                               // Remove
+                               iter->remove();
+                       }
                }
+               delete iter;
        }
 }
 
@@ -2270,8 +2396,8 @@ void Table::processSlot(SlotIndexer *indexer, Slot *slot, bool acceptUpdatesToLo
        // Process each entry in the slot
        Vector<Entry *> *entries = slot->getEntries();
        uint eSize = entries->size();
-       for(uint ei=0; ei < eSize; ei++) {
-               Entry * entry = entries->get(ei);
+       for (uint ei = 0; ei < eSize; ei++) {
+               Entry *entry = entries->get(ei);
                switch (entry->getType()) {
                case TypeCommitPart:
                        processEntry((CommitPart *)entry);
@@ -2330,7 +2456,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);
@@ -2375,9 +2501,10 @@ void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
        // Create a list of clients to watch until they see this rejected
        // message entry->
        Hashset<int64_t> *deviceWatchSet = new Hashset<int64_t>();
-       for (Map->Entry<int64_t, Pair<int64_t, Liveness *> > *lastMessageEntry : lastMessageTable->entrySet()) {
+       SetIterator<int64_t, Pair<int64_t, Liveness *> *> *iter = getKeyIterator(lastMessageTable);
+       while (iter->hasNext()) {
                // Machine ID for the last message entry
-               int64_t lastMessageEntryMachineId = lastMessageEntry->getKey();
+               int64_t lastMessageEntryMachineId = iter->next();
 
                // We've seen it, don't need to continue to watch->  Our next
                // message will implicitly acknowledge it->
@@ -2385,8 +2512,8 @@ void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
                        continue;
                }
 
-               Pair<int64_t, Liveness *> lastMessageValue = lastMessageEntry->getValue();
-               int64_t entrySequenceNumber = lastMessageValue.getFirst();
+               Pair<int64_t, Liveness *> *lastMessageValue = lastMessageTable->get(lastMessageEntryMachineId);
+               int64_t entrySequenceNumber = lastMessageValue->getFirst();
 
                if (entrySequenceNumber < seq) {
                        // Add this rejected message to the set of messages that this
@@ -2397,6 +2524,8 @@ void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
                        deviceWatchSet->add(lastMessageEntryMachineId);
                }
        }
+       delete iter;
+
        if (deviceWatchSet->isEmpty()) {
                // This rejected message has been seen by all the clients so
                entry->setDead();
@@ -2421,7 +2550,8 @@ void Table::processEntry(Abort *entry) {
 
        // Abort has not been seen by the client it is for yet so we need to
        // keep track of it
-       Abort *previouslySeenAbort = liveAbortTable->put(entry->getAbortId(), entry);
+
+       Abort *previouslySeenAbort = liveAbortTable->put(new Pair<int64_t, int64_t>(entry->getAbortId()), entry);
        if (previouslySeenAbort != NULL) {
                previouslySeenAbort->setDead();         // Delete old version of the abort since we got a rescued newer version
        }
@@ -2430,10 +2560,11 @@ void Table::processEntry(Abort *entry) {
                liveAbortsGeneratedByLocal->put(entry->getArbitratorLocalSequenceNumber(), entry);
        }
 
-       if ((entry->getSequenceNumber() != -1) && (lastMessageTable->get(entry->getTransactionMachineId()).getFirst() >= entry->getSequenceNumber())) {
+       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());
+               Pair<int64_t, int64_t> abortid = entry->getAbortId();
+               liveAbortTable->remove(&abortid);
 
                if (entry->getTransactionArbitrator() == localMachineId) {
                        liveAbortsGeneratedByLocal->remove(entry->getArbitratorLocalSequenceNumber());
@@ -2442,7 +2573,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
@@ -2454,15 +2585,17 @@ void Table::processEntry(Abort *entry) {
        }
 
        // Set dead a transaction if we can
-       Transaction *transactionToSetDead = liveTransactionByTransactionIdTable->remove(Pair<int64_t, int64_t>(entry->getTransactionMachineId(), entry->getTransactionClientLocalSequenceNumber()));
+       Pair<int64_t, int64_t> deadPair = Pair<int64_t, int64_t>(entry->getTransactionMachineId(), entry->getTransactionClientLocalSequenceNumber());
+
+       Transaction *transactionToSetDead = liveTransactionByTransactionIdTable->remove(&deadPair);
        if (transactionToSetDead != NULL) {
                liveTransactionBySequenceNumberTable->remove(transactionToSetDead->getSequenceNumber());
        }
 
        // Update the last transaction sequence number that the arbitrator
        // arbitrated on
-       int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(entry->getTransactionArbitrator());
-       if ((lastTransactionNumber == NULL) || (lastTransactionNumber < entry->getTransactionSequenceNumber())) {
+       if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getTransactionArbitrator()) ||
+                       (lastArbitratedTransactionNumberByArbitratorTable->get(entry->getTransactionArbitrator()) < entry->getTransactionSequenceNumber())) {
                // Is a valid one
                if (entry->getTransactionSequenceNumber() != -1) {
                        lastArbitratedTransactionNumberByArbitratorTable->put(entry->getTransactionArbitrator(), entry->getTransactionSequenceNumber());
@@ -2477,8 +2610,7 @@ void Table::processEntry(Abort *entry) {
 void Table::processEntry(TransactionPart *entry) {
        // Check if we have already seen this transaction and set it dead OR
        // if it is not alive
-       int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(entry->getArbitratorId());
-       if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= entry->getSequenceNumber())) {
+       if (lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getArbitratorId()) && (lastArbitratedTransactionNumberByArbitratorTable->get(entry->getArbitratorId()) >= entry->getSequenceNumber())) {
                // This transaction is dead, it was already committed or aborted
                entry->setDead();
                return;
@@ -2495,7 +2627,7 @@ void Table::processEntry(TransactionPart *entry) {
 
        // Update the part and set dead ones we have already seen (got a
        // rescued version)
-       TransactionPart *previouslySeenPart = transactionPart->put(entry->getPartId(), entry);
+       TransactionPart *previouslySeenPart = transactionPart->put(new Pair<int64_t, int32_t>(entry->getPartId()), entry);
        if (previouslySeenPart != NULL) {
                previouslySeenPart->setDead();
        }
@@ -2507,23 +2639,20 @@ void Table::processEntry(TransactionPart *entry) {
 void Table::processEntry(CommitPart *entry) {
        // Update the last transaction that was updated if we can
        if (entry->getTransactionSequenceNumber() != -1) {
-               int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId());
-               // Update the last transaction sequence number that the arbitrator
-               // arbitrated on
-               if ((lastTransactionNumber == NULL) || (lastTransactionNumber < entry->getTransactionSequenceNumber())) {
+               if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getMachineId() || lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId()) < entry->getTransactionSequenceNumber())) {
                        lastArbitratedTransactionNumberByArbitratorTable->put(entry->getMachineId(), entry->getTransactionSequenceNumber());
                }
        }
 
-       Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *commitPart = newCommitParts->get(entry->getMachineId());
+       Hashtable<Pair<int64_t, int32_t> *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *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 *, uintptr_t, 0, pairHashFunction, pairEquals>();
                newCommitParts->put(entry->getMachineId(), commitPart);
        }
        // Update the part and set dead ones we have already seen (got a
        // rescued version)
-       CommitPart *previouslySeenPart = commitPart->put(entry->getPartId(), entry);
+       CommitPart *previouslySeenPart = commitPart->put(new Pair<int64_t, int32_t>(entry->getPartId()), entry);
        if (previouslySeenPart != NULL) {
                previouslySeenPart->setDead();
        }
@@ -2537,7 +2666,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);
 
@@ -2547,7 +2676,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) {
@@ -2557,44 +2688,53 @@ void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liven
                                rm->removeWatcher(machineId);
                        }
                }
+               delete rmit;
        }
 
        // Set dead the abort
-       for (Iterator<Map->Entry<Pair<int64_t, int64_t>, Abort *> > i = liveAbortTable->entrySet()->iterator(); i->hasNext();) {
-               Abort *abort = i->next()->getValue();
+       SetIterator<Pair<int64_t, int64_t> *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals> *abortit = getKeyIterator(liveAbortTable);
+
+       while (abortit->hasNext()) {
+               Pair<int64_t, int64_t> *key = abortit->next();
+               Abort *abort = liveAbortTable->get(key);
                if ((abort->getTransactionMachineId() == machineId) && (abort->getSequenceNumber() <= seqNum)) {
                        abort->setDead();
-                       i->remove();
+                       abortit->remove();
                        if (abort->getTransactionArbitrator() == localMachineId) {
                                liveAbortsGeneratedByLocal->remove(abort->getArbitratorLocalSequenceNumber());
                        }
                }
        }
+       delete abortit;
        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");