From: bdemsky Date: Mon, 12 Feb 2018 19:37:26 +0000 (-0800) Subject: Edits X-Git-Url: http://demsky.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=34ca5d4113dbbd0b5342abd280e2f9e11e7cd3b7 Edits --- diff --git a/version2/src/C/CloudComm.cc b/version2/src/C/CloudComm.cc index edc0706..c6c0e75 100644 --- a/version2/src/C/CloudComm.cc +++ b/version2/src/C/CloudComm.cc @@ -101,10 +101,10 @@ void CloudComm::initCrypt() { */ URL *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) { const char *reqstring = isput ? "req=putslot" : "req=getslot"; - char * buffer = (char *) malloc(baseurl->length() + 200); + char *buffer = (char *) malloc(baseurl->length() + 200); memcpy(buffer, baseurl->internalBytes(), baseurl->length()); int offset = baseurl->length(); - offset+=sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber); + offset += sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber); if (maxentries != 0) sprintf(&buffer[offset], "&max=%" PRId64, maxentries); IoTString *urlstr = new IoTString(buffer); @@ -117,23 +117,23 @@ void CloudComm::setSalt() { // Salt already sent to server so don't set it again return; } - + try { Array *saltTmp = new Array(CloudComm_SALT_SIZE); random->nextBytes(saltTmp); - char * buffer = (char *) malloc(baseurl->length() + 100); + char *buffer = (char *) malloc(baseurl->length() + 100); memcpy(buffer, baseurl->internalBytes(), baseurl->length()); int offset = baseurl->length(); - offset+=sprintf(&buffer[offset], "?req=setsalt"); + offset += sprintf(&buffer[offset], "?req=setsalt"); IoTString *urlstr = new IoTString(buffer); free(buffer); - + URL *url = new URL(urlstr); timer->startTime(); URLConnection *con = url->openConnection(); HttpURLConnection *http = (HttpURLConnection *) con; - + http->setRequestMethod("POST"); http->setFixedLengthStreamingMode(saltTmp->length()); http->setDoOutput(true); @@ -163,10 +163,10 @@ bool CloudComm::getSalt() { HttpURLConnection *http = NULL; try { - char * buffer = (char *) malloc(baseurl->length() + 100); + char *buffer = (char *) malloc(baseurl->length() + 100); memcpy(buffer, baseurl->internalBytes(), baseurl->length()); int offset = baseurl->length(); - offset+=sprintf(&buffer[offset], "?req=getsalt"); + offset += sprintf(&buffer[offset], "?req=getsalt"); IoTString *urlstr = new IoTString(buffer); free(buffer); diff --git a/version2/src/C/Commit.cc b/version2/src/C/Commit.cc index fb09a94..ec4b39a 100644 --- a/version2/src/C/Commit.cc +++ b/version2/src/C/Commit.cc @@ -39,9 +39,9 @@ void Commit::addPartDecode(CommitPart *newPart) { } CommitPart *previouslySeenPart = parts->setExpand(newPart->getPartNumber(), newPart); - if(previouslySeenPart == NULL) + if (previouslySeenPart == NULL) partCount++; - + if (previouslySeenPart != NULL) { // Set dead the old one since the new one is a rescued version of this part previouslySeenPart->setDead(); @@ -115,9 +115,9 @@ void Commit::setDead() { if (!isDead) { isDead = true; // Make all the parts of this transaction dead - for (int32_t partNumber = 0; partNumber < parts->size(); partNumber ++) { + for (int32_t partNumber = 0; partNumber < parts->size(); partNumber++) { CommitPart *part = parts->get(partNumber); - if (parts!=NULL) + if (parts != NULL) part->setDead(); } } @@ -150,7 +150,7 @@ void Commit::createCommitParts() { Array *partData = new Array(copySize); System_arraycopy(charData, currentPosition, partData, 0, copySize); - CommitPart* part = new CommitPart(NULL, machineId, sequenceNumber, transactionSequenceNumber, commitPartCount, partData, isLastPart); + CommitPart *part = new CommitPart(NULL, machineId, sequenceNumber, transactionSequenceNumber, commitPartCount, partData, isLastPart); parts->setExpand(part->getPartNumber(), part); // Update position, count and remaining @@ -165,7 +165,7 @@ void Commit::decodeCommitData() { int dataSize = 0; for (int i = 0; i < parts->size(); i++) { CommitPart *tp = parts->get(i); - if (tp!=NULL) + if (tp != NULL) dataSize += tp->getDataSize(); } @@ -175,7 +175,7 @@ void Commit::decodeCommitData() { // Stitch all the data sections together for (int i = 0; i < parts->size(); i++) { CommitPart *tp = parts->get(i); - if (tp!=NULL) { + if (tp != NULL) { System_arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize()); currentPosition += tp->getDataSize(); } @@ -198,13 +198,13 @@ void Commit::decodeCommitData() { Array *Commit::convertDataToBytes() { // Calculate the size of the data int sizeOfData = sizeof(int32_t); // Number of Update KV's - SetIterator * kvit = keyValueUpdateSet->iterator(); - while(kvit->hasNext()) { - KeyValue * kv = kvit->next(); + SetIterator *kvit = keyValueUpdateSet->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); sizeOfData += kv->getSize(); } delete kvit; - + // Data handlers and storage Array *dataArray = new Array(sizeOfData); ByteBuffer *bbEncode = ByteBuffer_wrap(dataArray); @@ -214,12 +214,12 @@ Array *Commit::convertDataToBytes() { // Encode all the updates kvit = keyValueUpdateSet->iterator(); - while(kvit->hasNext()) { - KeyValue * kv = kvit->next(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); kv->encode(bbEncode); } delete kvit; - + return bbEncode->array(); } @@ -227,8 +227,8 @@ void Commit::setKVsMap(Hashsetclear(); keyValueUpdateSet->addAll(newKVs); liveKeys->clear(); - SetIterator *kvit = newKVs->iterator(); - while(kvit->hasNext()) { + SetIterator *kvit = newKVs->iterator(); + while (kvit->hasNext()) { liveKeys->add(kvit->next()->getKey()); } delete kvit; @@ -242,18 +242,18 @@ Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber) { } Hashset *kvSet = new Hashset(); SetIterator *kvit = older->getKeyValueUpdateSet()->iterator(); - while(kvit->hasNext()) { - KeyValue* kv=kvit->next(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); kvSet->add(kv); } delete kvit; kvit = newer->getKeyValueUpdateSet()->iterator(); - while(kvit->hasNext()) { - KeyValue* kv=kvit->next(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); kvSet->add(kv); } delete kvit; - + int64_t transactionSequenceNumber = newer->getTransactionSequenceNumber(); if (transactionSequenceNumber == -1) { transactionSequenceNumber = older->getTransactionSequenceNumber(); diff --git a/version2/src/C/Entry.h b/version2/src/C/Entry.h index e006b4a..acdd04a 100644 --- a/version2/src/C/Entry.h +++ b/version2/src/C/Entry.h @@ -17,13 +17,14 @@ #define TypeLastMessage 5 #define TypeRejectedMessage 6 #define TypeTableStatus 7 +#define TypeSlot 8 class Entry : public Liveness { /* Records whether the information is still live or has been superceded by a newer update. */ private: bool islive; - protected: +protected: Slot *parentslot; public: @@ -52,12 +53,6 @@ public: virtual int getSize() = 0; - /** - * Returns a char encoding the type of the entry object. - */ - virtual char getType() = 0; - - /** * Returns a copy of the Entry that can be added to a different slot. */ diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index f72dfa8..bd838bb 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -9,10 +9,10 @@ * @version 1.0 */ -inline int hashCharArray(Array * array) { +inline int hashCharArray(Array *array) { uint len = array->length(); - int hash=0; - for(uint i=0; i get(i); } return hash; @@ -29,8 +29,8 @@ private: */ public: - IoTString(Array *_array) : - array(new Array(_array)), + IoTString(Array *_array) : + array(new Array(_array)), hashvalue(hashCharArray(array)) { } @@ -38,11 +38,11 @@ public: int32_t len = strlen(_array); array = new Array(len); strcpy(array->internalArray(), _array); - hashvalue=hashCharArray(array); + hashvalue = hashCharArray(array); } - IoTString(IoTString *string) : - array(new Array(string->array)), + IoTString(IoTString *string) : + array(new Array(string->array)), hashvalue(hashCharArray(array)) { } diff --git a/version2/src/C/KeyValue.h b/version2/src/C/KeyValue.h index 2493bb2..9234b2d 100644 --- a/version2/src/C/KeyValue.h +++ b/version2/src/C/KeyValue.h @@ -8,7 +8,7 @@ * @version 1.0 */ -class KeyValue { /*extends Entry */ +class KeyValue {/*extends Entry */ private: IoTString *key; IoTString *value; @@ -30,4 +30,4 @@ public: KeyValue *KeyValue_decode(ByteBuffer *bb); unsigned int hashKeyValue(KeyValue *kv); bool equalsKeyValue(KeyValue *a, KeyValue *b); -#endif +#endif diff --git a/version2/src/C/Liveness.h b/version2/src/C/Liveness.h index ad56cb9..1f92b99 100644 --- a/version2/src/C/Liveness.h +++ b/version2/src/C/Liveness.h @@ -2,6 +2,10 @@ #define LIVENESS_H class Liveness { + /** + * Returns a char encoding the type of the entry object. + */ + virtual char getType() = 0; }; #endif diff --git a/version2/src/C/Pair.h b/version2/src/C/Pair.h index 4da3bf7..5506eae 100644 --- a/version2/src/C/Pair.h +++ b/version2/src/C/Pair.h @@ -23,13 +23,13 @@ public: }; template -inline unsigned int pairHashFunction(Pair p) { - return (p.getFirst() << 1) ^ p.getSecond(); +inline unsigned int pairHashFunction(Pair * p) { + return (p->getFirst() << 1) ^ p->getSecond(); } template -inline bool pairEquals(Pair a, Pair b) { - return ( a.getFirst() == b.getFirst() ) && (a.getSecond() == b.getSecond()); +inline bool pairEquals(Pair *a, Pair *b) { + return (a->getFirst() == b->getFirst() ) && (a->getSecond() == b->getSecond()); } inline unsigned int pairHashFunction(Pair p) { @@ -37,6 +37,6 @@ inline unsigned int pairHashFunction(Pair p) { } inline bool pairEquals(Pair a, Pair b) { - return ( a.getFirst() == b.getFirst() ) && (a.getSecond() == b.getSecond()); + return (a.getFirst() == b.getFirst() ) && (a.getSecond() == b.getSecond()); } #endif diff --git a/version2/src/C/Slot.cc b/version2/src/C/Slot.cc index f4247e4..10c92de 100644 --- a/version2/src/C/Slot.cc +++ b/version2/src/C/Slot.cc @@ -97,6 +97,10 @@ Slot *Slot_decode(Table *table, Array *array, Mac *mac) { return slot; } +char Slot::getType() { + return TypeSlot; +} + Array *Slot::encode(Mac *mac) { Array *array = new Array(SLOT_SIZE); ByteBuffer *bb = ByteBuffer_wrap(array); diff --git a/version2/src/C/Slot.h b/version2/src/C/Slot.h index c91aac1..d6e7575 100644 --- a/version2/src/C/Slot.h +++ b/version2/src/C/Slot.h @@ -52,6 +52,7 @@ public: bool isLive() { return livecount > 0; } Array *getSlotCryptIV(); friend Slot *Slot_decode(Table *table, Array *array, Mac *mac); + char getType(); }; Slot *Slot_decode(Table *table, Array *array, Mac *mac); diff --git a/version2/src/C/Table.cc b/version2/src/C/Table.cc index 5f93fe8..ee3b8bb 100644 --- a/version2/src/C/Table.cc +++ b/version2/src/C/Table.cc @@ -14,7 +14,11 @@ #include "ByteBuffer.h" #include "Abort.h" #include "CommitPart.h" - +#include "ArbitrationRound.h" +#include "TransactionPart.h" +#include "Commit.h" +#include "RejectedMessage.h" +#include "SlotIndexer.h" Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, int listeningPort) : buffer(NULL), @@ -153,15 +157,15 @@ void Table::init() { speculatedKeyValueTable = new Hashtable(); pendingTransactionSpeculatedKeyValueTable = new Hashtable(); liveNewKeyTable = new Hashtable(); - lastMessageTable = new Hashtable >(); + lastMessageTable = new Hashtable * >(); rejectedMessageWatchVectorTable = new Hashtable * >(); arbitratorTable = new Hashtable(); - liveAbortTable = new Hashtable, Abort *, uintptr_t, 0, pairHashFunction, pairEquals>(); - newTransactionParts = new Hashtable, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); - newCommitParts = new Hashtable, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); + liveAbortTable = new Hashtable *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals>(); + newTransactionParts = new Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); + newCommitParts = new Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); lastArbitratedTransactionNumberByArbitratorTable = new Hashtable(); liveTransactionBySequenceNumberTable = new Hashtable(); - liveTransactionByTransactionIdTable = new Hashtable, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals>(); + liveTransactionByTransactionIdTable = new Hashtable *, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals>(); liveCommitsTable = new Hashtable >(); liveCommitsByKeyTable = new Hashtable(); lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable(); @@ -171,8 +175,8 @@ void Table::init() { transactionPartsSent = new Hashtable *>(); outstandingTransactionStatus = new Hashtable(); liveAbortsGeneratedByLocal = new Hashtable(); - offlineTransactionsCommittedAndAtServer = new Hashset, uintptr_t, 0, pairHashFunction, pairEquals>(); - localCommunicationTable = new Hashtable >(); + offlineTransactionsCommittedAndAtServer = new Hashset *, uintptr_t, 0, pairHashFunction, pairEquals>(); + localCommunicationTable = new Hashtable *>(); lastTransactionSeenFromMachineFromServer = new Hashtable(); pendingSendArbitrationRounds = new Vector(); lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new Hashtable(); @@ -222,7 +226,7 @@ void Table::rebuild() { } void Table::addLocalCommunication(int64_t arbitrator, IoTString *hostName, int portNumber) { - localCommunicationTable->put(arbitrator, Pair(hostName, portNumber)); + localCommunicationTable->put(arbitrator, new Pair(hostName, portNumber)); } int64_t Table::getArbitrator(IoTString *key) { @@ -321,8 +325,8 @@ bool Table::update() { updateLiveTransactionsAndStatus(); return true; } catch (Exception *e) { - SetIterator * kit = getKeyIterator(localCommunicationTable); - while(kit->hasNext()) { + SetIterator *kit = getKeyIterator(localCommunicationTable); + while (kit->hasNext()) { int64_t m = kit->next(); updateFromLocal(m); } @@ -404,10 +408,10 @@ TransactionStatus *Table::commitTransaction() { Hashset *arbitratorTriedAndFailed = new Hashset(); 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 @@ -430,7 +434,7 @@ TransactionStatus *Table::commitTransaction() { } pendingTransactionQueue->setSize(oldindex); } - + updateLiveStateFromLocal(); return transactionStatus; @@ -464,8 +468,8 @@ bool Table::sendToServer(NewKey *newKey) { } } - SetIterator * trit = getKeyIterator(lastTransactionPartsSent); - while(trit->hasNext()) { + SetIterator *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { Transaction *transaction = trit->next(); transaction->resetServerFailure(); // Update which transactions parts still need to be sent @@ -502,9 +506,9 @@ bool Table::sendToServer(NewKey *newKey) { } // Process each entry in the slot - Vector * ventries=s->getEntries(); + Vector *ventries = s->getEntries(); uint vesize = ventries->size(); - for(uint vei = 0; vei < vesize; vei++) { + for (uint vei = 0; vei < vesize; vei++) { Entry *entry = ventries->get(vei); if (entry->getType() == TypeLastMessage) { LastMessage *lastMessage = (LastMessage *)entry; @@ -523,7 +527,9 @@ bool Table::sendToServer(NewKey *newKey) { } } - for (Transaction *transaction : lastTransactionPartsSent->keySet()) { + SetIterator *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetServerFailure(); // Update which transactions parts still need to be sent @@ -547,16 +553,20 @@ bool Table::sendToServer(NewKey *newKey) { } } } + delete trit; } } - for (Transaction *transaction : lastTransactionPartsSent->keySet()) { + SetIterator *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 @@ -580,8 +590,11 @@ bool Table::sendToServer(NewKey *newKey) { } // Process each entry in the slot - for (Entry *entry : s->getEntries()) { - + Vector *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())) { @@ -599,7 +612,9 @@ bool Table::sendToServer(NewKey *newKey) { } } - for (Transaction *transaction : lastTransactionPartsSent->keySet()) { + SetIterator *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetServerFailure(); // Update which transactions parts still need to be sent @@ -623,14 +638,18 @@ bool Table::sendToServer(NewKey *newKey) { } } } + delete trit; } else { - for (Transaction *transaction : lastTransactionPartsSent->keySet()) { + SetIterator *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 @@ -672,7 +691,9 @@ bool Table::sendToServer(NewKey *newKey) { if (needsResize) { // Reset which transaction to send - for (Transaction *transaction : transactionPartsSent->keySet()) { + SetIterator *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetNextPartToSend(); // Set the transaction sequence number back to nothing @@ -680,6 +701,7 @@ bool Table::sendToServer(NewKey *newKey) { transaction->setSequenceNumber(-1); } } + delete trit; // Clear the sent data since we are trying again pendingSendArbitrationEntriesToDelete->clear(); @@ -694,10 +716,9 @@ bool Table::sendToServer(NewKey *newKey) { lastInsertedNewKey = insertedNewKey; lastNewSize = newSize; lastNewKey = newKey; - lastTransactionPartsSent = new Hashtable * >(transactionPartsSent); + lastTransactionPartsSent = transactionPartsSent->clone(); lastPendingSendArbitrationEntriesToDelete = new Vector(pendingSendArbitrationEntriesToDelete); - ThreeTuple *> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL); if (sendSlotsReturn.getFirst()) { @@ -714,19 +735,21 @@ bool Table::sendToServer(NewKey *newKey) { // Remove the aborts and commit parts that were sent from the pending to send queue uint size = pendingSendArbitrationRounds->size(); uint oldcount = 0; - for (uint i=0; i < size; i++) - ArbitrationRound *round = pendingSendArbitrartionRounds->get(i); + for (uint i = 0; i < size; i++) { + ArbitrationRound *round = pendingSendArbitrationRounds->get(i); round->removeParts(pendingSendArbitrationEntriesToDelete); if (!round->isDoneSending()) { // Sent all the parts - pendingSendArbitrartionRounds->set(oldcount++, - pendingSendArbitrartionRounds->get(i)); + pendingSendArbitrationRounds->set(oldcount++, + pendingSendArbitrationRounds->get(i)); } } - pendingSendArbitrationRounds->setSize(oldcount); + pendingSendArbitrationRounds->setSize(oldcount); - for (Transaction *transaction : transactionPartsSent->keySet()) { + SetIterator *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetServerFailure(); // Update which transactions parts still need to be sent @@ -744,9 +767,12 @@ bool Table::sendToServer(NewKey *newKey) { pendingTransactionQueue->remove(transaction); } } + delete trit; } else { // Reset which transaction to send - for (Transaction *transaction : transactionPartsSent->keySet()) { + SetIterator *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetNextPartToSend(); // Set the transaction sequence number back to nothing @@ -754,6 +780,7 @@ bool Table::sendToServer(NewKey *newKey) { transaction->setSequenceNumber(-1); } } + delete trit; } // Clear the sent data in preparation for next send @@ -767,10 +794,11 @@ bool Table::sendToServer(NewKey *newKey) { } } catch (ServerException *e) { - 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 *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetNextPartToSend(); // Set the transaction sequence number back to nothing @@ -778,15 +806,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 *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); transaction->resetNextPartToSend(); transaction->setServerFailure(); } + delete trit; } pendingSendArbitrationEntriesToDelete->clear(); @@ -799,11 +831,10 @@ bool Table::sendToServer(NewKey *newKey) { } bool Table::updateFromLocal(int64_t machineId) { - Pair localCommunicationInformation = localCommunicationTable->get(machineId); - if (localCommunicationInformation == NULL) { - // Cant talk to that device locally so do nothing + if (!localCommunicationTable->contains(machineId)) return false; - } + + Pair * localCommunicationInformation = localCommunicationTable->get(machineId); // Get the size of the send data int sendDataSize = sizeof(int32_t) + sizeof(int64_t); @@ -836,10 +867,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); } } @@ -852,12 +883,10 @@ bool Table::updateFromLocal(int64_t machineId) { Pair Table::sendTransactionToLocal(Transaction *transaction) { // Get the devices local communications - Pair localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator()); - - if (localCommunicationInformation == NULL) { - // Cant talk to that device locally so do nothing + if (!localCommunicationTable->contains(machineId)) return Pair(true, false); - } + + Pair localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator()); // Get the size of the send data int sendDataSize = sizeof(int32_t) + sizeof(int64_t); @@ -872,7 +901,7 @@ Pair Table::sendTransactionToLocal(Transaction *transaction) { // Make the send data size Array *sendData = new Array(sendDataSize); - ByteBuffer *bbEncode = ByteBuffer.wrap(sendData); + ByteBuffer *bbEncode = ByteBuffer_wrap(sendData); // Encode the data bbEncode->putLong(lastArbitrationDataLocalSequenceNumber); @@ -901,7 +930,7 @@ Pair 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; @@ -909,7 +938,7 @@ Pair 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); } } @@ -917,14 +946,14 @@ Pair 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 { @@ -952,7 +981,7 @@ Array *Table::acceptDataFromLocal(Array *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); } @@ -976,11 +1005,13 @@ Array *Table::acceptDataFromLocal(Array *data) { // Get the aborts to send back Vector *abortLocalSequenceNumbers = new Vector(liveAbortsGeneratedByLocal->keySet()); Collections->sort(abortLocalSequenceNumbers); - for (int64_t localSequenceNumber : abortLocalSequenceNumbers) { + uint asize = abortLocalSequenceNumbers->size(); + for(uint i=0; iget(i); if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) { continue; } - + Abort *abort = liveAbortsGeneratedByLocal->get(localSequenceNumber); unseenArbitrations->add(abort); returnDataSize += abort->getSize(); @@ -992,7 +1023,9 @@ Array *Table::acceptDataFromLocal(Array *data) { Vector *commitLocalSequenceNumbers = new Vector(commitForClientTable->keySet()); Collections->sort(commitLocalSequenceNumbers); - for (int64_t localSequenceNumber : commitLocalSequenceNumbers) { + 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) { @@ -1034,8 +1067,8 @@ Array *Table::acceptDataFromLocal(Array *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); } @@ -1065,8 +1098,8 @@ ThreeTuple *> Table::sendSlotsToServer(Slot *slot, int if (hadPartialSendToServer) { bool isInserted = false; - uint size = array->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; @@ -1074,14 +1107,17 @@ ThreeTuple *> 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 *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; @@ -1115,7 +1151,7 @@ ThreeTuple *> Table::sendSlotsToServer(Slot *slot, int ThreeTuple 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) { @@ -1153,17 +1189,17 @@ ThreeTuple Table::fillSlot(Slot *slot, bool resize, NewKey transactionPartsSent->clear(); pendingSendArbitrationEntriesToDelete->clear(); uint size = pendingSendArbitrationRounds->size(); - for (uint i=0; iget(i); + for (uint i = 0; i < size; i++) { + ArbitrationRound *round = pendingSendArbitrationRounds->get(i); bool isFull = false; round->generateParts(); Vector *parts = round->getParts(); // Insert pending arbitration data uint vsize = parts->size(); - for (uint vi=0; viget(vi); - + // If it is an abort then we need to set some information if (arbitrationData->getType() == TypeAbort) { ((Abort *)arbitrationData)->setSequenceNumber(slot->getSequenceNumber()); @@ -1274,7 +1310,7 @@ ThreeTuple 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; @@ -1329,10 +1365,12 @@ search: else { skipcount++; if (skipcount > Table_SKIP_THRESHOLD) - break search; + goto donesearch; } } } + donesearch: + ; } /** @@ -1512,7 +1550,7 @@ void Table::processNewTransactionParts() { // Iterate through all the machine Ids that we received new parts // for for (int64_t machineId : newTransactionParts->keySet()) { - Hashtable, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId); + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId); // Iterate through all the parts for that machine Id for (Pair partId : parts->keySet()) { @@ -1559,7 +1597,7 @@ void Table::arbitrateFromServer() { Collections->sort(transactionSequenceNumbers); // Collection of key value pairs that are - Hashtable speculativeTableTmp = new Hashtable(); + Hashtable * speculativeTableTmp = new Hashtable(); // The last transaction arbitrated on int64_t lastTransactionCommitted = -1; @@ -1607,10 +1645,13 @@ void Table::arbitrateFromServer() { // Guard evaluated as true // Update the local changes so we can make the commit - for (KeyValue *kv : transaction->getKeyValueUpdateSet()) { + SetIterator *kvit = getKeyIterator(transaction->getKeyValueUpdateSet()); + 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 { @@ -1702,10 +1743,13 @@ Pair Table::arbitrateOnLocalTransaction(Transaction *transaction) { localArbitrationSequenceNumber++; // Update the local changes so we can make the commit - for (KeyValue *kv : transaction->getKeyValueUpdateSet()) { + SetIterator *kvit = getKeyIterator(transaction->getKeyValueUpdateSet()); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); newCommit->addKV(kv); } - + delete kvit; + // create the commit parts newCommit->createCommitParts(); @@ -1739,7 +1783,7 @@ Pair 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); } @@ -1786,7 +1830,7 @@ bool Table::compactArbitrationData() { } ArbitrationRound *lastRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1); - if (lastRound->didSendPart()) { + if (lastRound->getDidSendPart()) { return false; } @@ -1797,7 +1841,7 @@ bool Table::compactArbitrationData() { while (numberToDelete < pendingSendArbitrationRounds->size()) { ArbitrationRound *xs 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; @@ -1874,7 +1918,7 @@ bool Table::updateCommittedTable() { // Iterate through all the machine Ids that we received new parts for for (int64_t machineId : newCommitParts->keySet()) { - Hashtable, CommitPart *> *parts = newCommitParts->get(machineId); + Hashtable *, CommitPart *> *parts = newCommitParts->get(machineId); // Iterate through all the parts for that machine Id for (Pair partId : parts->keySet()) { @@ -1960,8 +2004,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 @@ -1994,9 +2037,12 @@ bool Table::updateCommittedTable() { // Get what commits should be edited, these are the commits that // have live values for their keys Hashset *commitsToEdit = new Hashset(); - for (KeyValue *kv : commit->getKeyValueUpdateSet()) { + SetIterator *kvit = getKeyIterator(commit->getKeyValueUpdateSet()); + 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 // Update each previous commit that needs to be updated @@ -2006,10 +2052,13 @@ bool Table::updateCommittedTable() { if (previousCommit->isLive()) { // Update which keys in the old commits are still live - for (KeyValue *kv : commit->getKeyValueUpdateSet()) { + SetIterator *kvit = getKeyIterator(commit->getKeyValueUpdateSet()); + 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); @@ -2030,10 +2079,13 @@ bool Table::updateCommittedTable() { didProcessANewCommit = true; // Update the committed table of keys and which commit is using which key - for (KeyValue *kv : commit->getKeyValueUpdateSet()) { + SetIterator *kvit = getKeyIterator(commit->getKeyValueUpdateSet()); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); committedKeyValueTable->put(kv->getKey(), kv); liveCommitsByKeyTable->put(kv->getKey(), commit); } + delete kvit; } } @@ -2106,9 +2158,12 @@ 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()) { + SetIterator *kvit = getKeyIterator(commit->getKeyValueUpdateSet()); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); speculatedKeyValueTable->put(kv->getKey(), kv); } + delete kvit; } } @@ -2154,9 +2209,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 *kvit = getKeyIterator(commit->getKeyValueUpdateSet()); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); pendingTransactionSpeculatedKeyValueTable->put(kv->getKey(), kv); } + delete kvit; } } } @@ -2210,7 +2268,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 *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); @@ -2362,7 +2423,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) { @@ -2424,11 +2485,11 @@ void Table::processEntry(TransactionPart *entry) { } // This part is still alive - Hashtable, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *transactionPart = newTransactionParts->get(entry->getMachineId()); + Hashtable *, 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, TransactionPart *>(); + transactionPart = new Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals>(); newTransactionParts->put(entry->getMachineId(), transactionPart); } @@ -2454,10 +2515,10 @@ void Table::processEntry(CommitPart *entry) { } } - Hashtable, CommitPart *> *commitPart = newCommitParts->get(entry->getMachineId()); + Hashtable *, 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, CommitPart *>(); + commitPart = new Hashtable *, CommitPart *>(); newCommitParts->put(entry->getMachineId(), commitPart); } // Update the part and set dead ones we have already seen (got a diff --git a/version2/src/C/Table.h b/version2/src/C/Table.h index 7cc98ab..eb9faca 100644 --- a/version2/src/C/Table.h +++ b/version2/src/C/Table.h @@ -62,15 +62,15 @@ private: Hashtable *speculatedKeyValueTable; // Table of speculated key value pairs, if there is a speculative value Hashtable *pendingTransactionSpeculatedKeyValueTable; // Table of speculated key value pairs, if there is a speculative value from the pending transactions Hashtable *liveNewKeyTable; // Table of live new keys - Hashtable > *lastMessageTable; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage); + Hashtable *> *lastMessageTable; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage); Hashtable *> *rejectedMessageWatchVectorTable; // Table of machine Ids and the set of rejected messages they have not seen yet Hashtable *arbitratorTable;// Table of keys and their arbitrators - Hashtable, Abort *, uintptr_t, 0, pairHashFunction, pairEquals> *liveAbortTable;// Table live abort messages - Hashtable, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newTransactionParts; // transaction parts that are seen in this latest round of slots from the server - Hashtable, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newCommitParts; // commit parts that are seen in this latest round of slots from the server + Hashtable *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals> *liveAbortTable;// Table live abort messages + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newTransactionParts; // transaction parts that are seen in this latest round of slots from the server + Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newCommitParts; // commit parts that are seen in this latest round of slots from the server Hashtable *lastArbitratedTransactionNumberByArbitratorTable; // Last transaction sequence number that an arbitrator arbitrated on Hashtable *liveTransactionBySequenceNumberTable; // live transaction grouped by the sequence number - Hashtable, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals> *liveTransactionByTransactionIdTable; // live transaction grouped by the transaction ID + Hashtable *, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals> *liveTransactionByTransactionIdTable; // live transaction grouped by the transaction ID Hashtable > *liveCommitsTable; Hashtable *liveCommitsByKeyTable; Hashtable *lastCommitSeenSequenceNumberByArbitratorTable; @@ -81,8 +81,8 @@ private: Hashtable *> *transactionPartsSent; Hashtable *outstandingTransactionStatus; Hashtable *liveAbortsGeneratedByLocal; - Hashset, uintptr_t, 0, pairHashFunction, pairEquals> *offlineTransactionsCommittedAndAtServer; - Hashtable > *localCommunicationTable; + Hashset *, uintptr_t, 0, pairHashFunction, pairEquals> *offlineTransactionsCommittedAndAtServer; + Hashtable *> *localCommunicationTable; Hashtable *lastTransactionSeenFromMachineFromServer; Hashtable *lastArbitrationDataLocalSequenceNumberSeenFromArbitrator; bool lastInsertedNewKey; diff --git a/version2/src/C/Transaction.cc b/version2/src/C/Transaction.cc index 51fcbf6..77c3590 100644 --- a/version2/src/C/Transaction.cc +++ b/version2/src/C/Transaction.cc @@ -24,7 +24,7 @@ Transaction::Transaction() : } void Transaction::addPartEncode(TransactionPart *newPart) { - TransactionPart * old=parts->setExpand(newPart->getPartNumber(), newPart); + TransactionPart *old = parts->setExpand(newPart->getPartNumber(), newPart); if (old == NULL) partCount++; partsPendingSend->add(newPart->getPartNumber()); @@ -54,7 +54,7 @@ void Transaction::addPartDecode(TransactionPart *newPart) { TransactionPart *previouslySeenPart = parts->setExpand(newPart->getPartNumber(), newPart); if (previouslySeenPart == NULL) partCount++; - + if (previouslySeenPart != NULL) { // Set dead the old one since the new one is a rescued version of this part previouslySeenPart->setDead(); @@ -101,10 +101,10 @@ int64_t Transaction::getSequenceNumber() { void Transaction::setSequenceNumber(int64_t _sequenceNumber) { sequenceNumber = _sequenceNumber; - + for (int32_t i = 0; i < parts->size(); i++) { - TransactionPart * tp = parts->get(i); - if (tp !=NULL) + TransactionPart *tp = parts->get(i); + if (tp != NULL) tp->setSequenceNumber(sequenceNumber); } } @@ -161,9 +161,9 @@ void Transaction::removeSentParts(Vector *sentParts) { nextPartToSend = 0; bool changed = false; uint lastusedindex = 0; - for(uint i=0; i < partsPendingSend->size(); i++) { + for (uint i = 0; i < partsPendingSend->size(); i++) { int32_t parti = partsPendingSend->get(i); - for(uint j=0; j < sentParts->size(); j++) { + for (uint j = 0; j < sentParts->size(); j++) { int32_t partj = sentParts->get(j); if (parti == partj) { changed = true; @@ -171,7 +171,7 @@ void Transaction::removeSentParts(Vector *sentParts) { } } partsPendingSend->set(lastusedindex++, parti); - NextElement: +NextElement: ; } if (changed) { @@ -214,7 +214,7 @@ void Transaction::setDead() { // Set dead isDead = true; // Make all the parts of this transaction dead - for (int32_t partNumber = 0; partNumber < parts->size(); partNumber ++) { + for (int32_t partNumber = 0; partNumber < parts->size(); partNumber++) { TransactionPart *part = parts->get(partNumber); if (part != NULL) part->setDead(); @@ -265,8 +265,8 @@ void Transaction::decodeTransactionData() { } bool Transaction::evaluateGuard(Hashtable *committedKeyValueTable, Hashtable *speculatedKeyValueTable, Hashtable *pendingTransactionSpeculatedKeyValueTable) { - SetIterator* kvit=keyValueGuardSet->iterator(); - while(kvit->hasNext()) { + SetIterator *kvit = keyValueGuardSet->iterator(); + while (kvit->hasNext()) { KeyValue *kvGuard = kvit->next(); // First check if the key is in the speculative table, this is the value of the latest assumption KeyValue *kv = NULL; diff --git a/version2/src/C/URL.h b/version2/src/C/URL.h index 72139c3..0b20bc2 100644 --- a/version2/src/C/URL.h +++ b/version2/src/C/URL.h @@ -2,7 +2,7 @@ #define URL_H #include "common.h" class URL { - public: +public: URL(IoTString *string); }; #endif diff --git a/version2/src/C/hashset.h b/version2/src/C/hashset.h index d0b01f6..55ae1fb 100644 --- a/version2/src/C/hashset.h +++ b/version2/src/C/hashset.h @@ -17,9 +17,9 @@ class Hashset; template, bool (*equals)(_Key, _Key) = defaultEquals<_Key> > class SetIterator { public: - SetIterator(Hashlistnode<_Key, _Key> *_curr, Hashtable <_Key, _Key, _KeyInt, _Shift, hash_function, equals> *_table) : -curr(_curr), -table(_table) + SetIterator(Hashlistnode<_Key, _Key> *_curr, Hashtable <_Key, _Key, _KeyInt, _Shift, hash_function, equals> *_table) : + curr(_curr), + table(_table) { } @@ -64,16 +64,16 @@ table(_table) } private: -Hashlistnode<_Key,_Key> *curr; -Hashlistnode<_Key, _Key> *last; -Hashtable <_Key, _Key, _KeyInt, _Shift, hash_function, equals> *table; + Hashlistnode<_Key,_Key> *curr; + Hashlistnode<_Key, _Key> *last; + Hashtable <_Key, _Key, _KeyInt, _Shift, hash_function, equals> *table; }; template, bool (*equals)(_Key, _Key) = defaultEquals<_Key> > class Hashset { public: Hashset(unsigned int initialcapacity = 16, double factor = 0.5) : -table(new Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(initialcapacity, factor)) + table(new Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(initialcapacity, factor)) { } @@ -174,7 +174,7 @@ private: }; template - SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> * getKeyIterator(Hashtable<_Key,_Val,_KeyInt,_Shift,hash_function,equals> *table) { +SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *getKeyIterator(Hashtable<_Key,_Val,_KeyInt,_Shift,hash_function,equals> *table) { return new SetIterator<_Key, _KeyInt, _Shift, hash_function, equals>(table->list, table); } #endif diff --git a/version2/src/C/hashtable.h b/version2/src/C/hashtable.h index 3ded5b5..aca3361 100644 --- a/version2/src/C/hashtable.h +++ b/version2/src/C/hashtable.h @@ -31,8 +31,8 @@ struct Hashlistnode { _Key key; _Val val; uint hashcode; - struct Hashlistnode<_Key, _Val> * next; - struct Hashlistnode<_Key, _Val> * prev; + struct Hashlistnode<_Key, _Val> *next; + struct Hashlistnode<_Key, _Val> *prev; }; template @@ -59,6 +59,7 @@ inline bool defaultEquals(_Key key1, _Key key2) { * manipulation and storage. * @tparam _Shift Logical shift to apply to all keys. Default 0. */ + template, bool (*equals)(_Key, _Key) = defaultEquals<_Key> > class Hashtable { public: @@ -82,6 +83,17 @@ public: tail = list = NULL; } +Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> * clone() { + Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> * ctable = new Hashtable<_Key, _Val, _KeyInt, _Shift, hash_function, equals> (capacity, loadfactor); + struct Hashlistnode<_Key, _Val> * ptr = list; + while (ptr != NULL) { + ctable->put(ptr->key, ptr->val); + ptr = ptr->next; + } + return ctable; +} + + /** @brief Hash table destructor */ ~Hashtable() { ourfree(table); @@ -302,16 +314,16 @@ public: return (_Val)0; } else { _Val v = zero->val; - if (zero -> next != NULL) - zero -> next -> prev = zero ->prev; + if (zero->next != NULL) + zero->next->prev = zero->prev; else - tail = zero -> prev; + tail = zero->prev; - if (zero -> prev != NULL) - zero -> prev -> next = zero -> next; + if (zero->prev != NULL) + zero->prev->next = zero->next; else list = zero->next; - + ourfree(zero); zero = NULL; Size--; @@ -335,17 +347,17 @@ public: //empty out this bin search->val = (_Val) 1; search->key = 0; - - if (search -> next != NULL) - search -> next -> prev = search ->prev; + + if (search->next != NULL) + search->next->prev = search->prev; else - tail = search -> prev; - - if (search -> prev != NULL) - search -> prev -> next = search -> next; + tail = search->prev; + + if (search->prev != NULL) + search->prev->next = search->next; else list = search->next; - + Size--; return v; } @@ -428,9 +440,9 @@ public: if (tail == NULL) tail = search; - search -> next = list; + search->next = list; if (list != NULL) - list -> prev = search; + list->prev = search; list = search; search->hashcode = hashcode; search->key = key; @@ -443,9 +455,9 @@ public: unsigned int getCapacity() {return capacity;} struct Hashlistnode<_Key, _Val> *table; struct Hashlistnode<_Key, _Val> *zero; - struct Hashlistnode<_Key, _Val> * list; - struct Hashlistnode<_Key, _Val> * tail; - unsigned int capacity; + struct Hashlistnode<_Key, _Val> *list; + struct Hashlistnode<_Key, _Val> *tail; + unsigned int capacity; unsigned int Size; private: unsigned int capacitymask;