From a578eebaebcddd7f6b2a1b2edeed739bef657d5a Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 19 Jan 2018 14:44:59 -0800 Subject: [PATCH] edits --- version2/src/C/ByteBuffer.h | 2 + version2/src/C/IoTString.h | 10 +++ version2/src/C/PendingTransaction.cc | 100 +++++++++++++++++---------- version2/src/C/PendingTransaction.h | 20 +++--- version2/src/C/Transaction.cc | 32 ++++++--- version2/src/C/Transaction.h | 35 +++++----- version2/src/C/array.h | 10 +++ version2/src/C/common.h | 17 ++--- version2/src/C/hashset.h | 2 +- 9 files changed, 143 insertions(+), 85 deletions(-) diff --git a/version2/src/C/ByteBuffer.h b/version2/src/C/ByteBuffer.h index a89ca24..086571b 100644 --- a/version2/src/C/ByteBuffer.h +++ b/version2/src/C/ByteBuffer.h @@ -12,6 +12,8 @@ public: int32_t getInt(); char get(); void get(Array * array); + Array * array(); private: }; +ByteBuffer * ByteBuffer_wrap(Array * array); #endif diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index f22324b..c90d46d 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -40,6 +40,16 @@ public: * Returns the length in chars of the IoTString. */ + bool equals(IoTString * str) { + uint strlength = str->array->length(); + uint thislength = array->length(); + if (strlength != thislength) + return false; + + int result = memcmp(str->array->internalArray(), array->internalArray(), strlength); + return result == 0; + } + int length() { return array->length(); } friend IoTString *IoTString_shallow(Array *_array); }; diff --git a/version2/src/C/PendingTransaction.cc b/version2/src/C/PendingTransaction.cc index b3da754..84bef5e 100644 --- a/version2/src/C/PendingTransaction.cc +++ b/version2/src/C/PendingTransaction.cc @@ -1,8 +1,13 @@ #include "PendingTransaction.h" +#include "KeyValue.h" +#include "IoTString.h" +#include "Transaction.h" +#include "TransactionPart.h" +#include "ByteBuffer.h" PendingTransaction::PendingTransaction(int64_t _machineId) : keyValueUpdateSet(new Hashset()), - keyValueGuardSet(new HashSet()), + keyValueGuardSet(new Hashset()), arbitrator(-1), clientLocalSequenceNumber(-1), machineId(_machineId), @@ -15,27 +20,30 @@ PendingTransaction::PendingTransaction(int64_t _machineId) : */ void PendingTransaction::addKV(KeyValue *newKV) { - KeyValue rmKV = NULL; + KeyValue * rmKV = NULL; // Make sure there are no duplicates - for (KeyValue kv : keyValueUpdateSet) { - if (kv.getKey().equals(newKV.getKey())) { + SetIterator * kvit = keyValueUpdateSet->iterator(); + while(kvit->hasNext()) { + KeyValue *kv = kvit->next(); + if (kv->getKey()->equals(newKV->getKey())) { // Remove key if we are adding a newer version of the same key rmKV = kv; break; } } - + delete kvit; + // Remove key if we are adding a newer version of the same key if (rmKV != NULL) { - keyValueUpdateSet.remove(rmKV); - currentDataSize -= rmKV.getSize(); + keyValueUpdateSet->remove(rmKV); + currentDataSize -= rmKV->getSize(); } // Add the key to the hash set - keyValueUpdateSet.add(newKV); - currentDataSize += newKV.getSize(); + keyValueUpdateSet->add(newKV); + currentDataSize += newKV->getSize(); } /** @@ -44,8 +52,8 @@ void PendingTransaction::addKV(KeyValue *newKV) { */ void PendingTransaction::addKVGuard(KeyValue *newKV) { // Add the key to the hash set - keyValueGuardSet.add(newKV); - currentDataSize += newKV.getSize(); + keyValueGuardSet->add(newKV); + currentDataSize += newKV->getSize(); } /** @@ -60,36 +68,41 @@ bool PendingTransaction::checkArbitrator(int64_t arb) { return arb == arbitrator; } -bool PendingTransaction::evaluateGuard(Hashtable keyValTableCommitted, Hashtable keyValTableSpeculative, Hashtable keyValTablePendingTransSpeculative) { - for (KeyValue kvGuard : keyValueGuardSet) { +bool PendingTransaction::evaluateGuard(Hashtable * keyValTableCommitted, Hashtable * keyValTableSpeculative, Hashtable * keyValTablePendingTransSpeculative) { + 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 = keyValTablePendingTransSpeculative.get(kvGuard.getKey()); + KeyValue * kv = keyValTablePendingTransSpeculative->get(kvGuard->getKey()); if (kv == NULL) { // if it is not in the pending trans table then check the speculative table and use that // value as our latest assumption - kv = keyValTableSpeculative.get(kvGuard.getKey()); + kv = keyValTableSpeculative->get(kvGuard->getKey()); } if (kv == NULL) { // if it is not in the speculative table then check the committed table and use that // value as our latest assumption - kv = keyValTableCommitted.get(kvGuard.getKey()); + kv = keyValTableCommitted->get(kvGuard->getKey()); } - if (kvGuard.getValue() != NULL) { - if ((kv == NULL) || (!kvGuard.getValue().equals(kv.getValue()))) { + if (kvGuard->getValue() != NULL) { + if ((kv == NULL) || (!kvGuard->getValue()->equals(kv->getValue()))) { + delete kvit; return false; } } else { if (kv != NULL) { + delete kvit; return false; } } } + delete kvit; return true; } @@ -101,24 +114,24 @@ Transaction *PendingTransaction::createTransaction() { Array *charData = convertDataToBytes(); int currentPosition = 0; - int remaining = charData.length; + int remaining = charData->length(); while (remaining > 0) { bool isLastPart = false; // determine how much to copy - int copySize = TransactionPart.MAX_NON_HEADER_SIZE; - if (remaining <= TransactionPart.MAX_NON_HEADER_SIZE) { + int copySize = TransactionPart_MAX_NON_HEADER_SIZE; + if (remaining <= TransactionPart_MAX_NON_HEADER_SIZE) { copySize = remaining; isLastPart = true;// last bit of data so last part } // Copy to a smaller version - Array *partData = new char[copySize]; - System.arraycopy(charData, currentPosition, partData, 0, copySize); + Array *partData = new Array(copySize); + System_arraycopy(charData, currentPosition, partData, 0, copySize); - TransactionPart part = new TransactionPart(NULL, machineId, arbitrator, clientLocalSequenceNumber, transactionPartCount, partData, isLastPart); - newTransaction.addPartEncode(part); + TransactionPart * part = new TransactionPart(NULL, machineId, arbitrator, clientLocalSequenceNumber, transactionPartCount, partData, isLastPart); + newTransaction->addPartEncode(part); // Update position, count and remaining currentPosition += copySize; @@ -127,19 +140,24 @@ Transaction *PendingTransaction::createTransaction() { } // Add the Guard Conditions - for (KeyValue kv : keyValueGuardSet) { - newTransaction.addGuardKV(kv); + SetIterator * kvit = keyValueGuardSet->iterator(); + while(kvit->hasNext()) { + KeyValue *kv = kvit->next(); + newTransaction->addGuardKV(kv); } - + delete kvit; + // Add the updates - for (KeyValue kv : keyValueUpdateSet) { - newTransaction.addUpdateKV(kv); + kvit = keyValueUpdateSet->iterator(); + while(kvit->hasNext()) { + KeyValue *kv = kvit->next(); + newTransaction->addUpdateKV(kv); } - + delete kvit; return newTransaction; } -Arrar *PendingTransaction::convertDataToBytes() { +Array *PendingTransaction::convertDataToBytes() { // Calculate the size of the data int sizeOfData = 2 * sizeof(int32_t); // Number of Update KV's and Guard KV's sizeOfData += currentDataSize; @@ -149,19 +167,25 @@ Arrar *PendingTransaction::convertDataToBytes() { ByteBuffer *bbEncode = ByteBuffer_wrap(dataArray); // Encode the size of the updates and guard sets - bbEncode->putInt(keyValueGuardSet.size()); - bbEncode->putInt(keyValueUpdateSet.size()); + bbEncode->putInt(keyValueGuardSet->size()); + bbEncode->putInt(keyValueUpdateSet->size()); // Encode all the guard conditions - for (KeyValue kv : keyValueGuardSet) { + SetIterator * kvit = keyValueGuardSet->iterator(); + while(kvit->hasNext()) { + KeyValue *kv = kvit->next(); kv->encode(bbEncode); } - + delete kvit; + // Encode all the updates - for (KeyValue kv : keyValueUpdateSet) { + kvit = keyValueUpdateSet->iterator(); + while(kvit->hasNext()) { + KeyValue *kv = kvit->next(); kv->encode(bbEncode); } - + delete kvit; + return bbEncode->array(); } diff --git a/version2/src/C/PendingTransaction.h b/version2/src/C/PendingTransaction.h index e919ca8..09689d4 100644 --- a/version2/src/C/PendingTransaction.h +++ b/version2/src/C/PendingTransaction.h @@ -5,12 +5,12 @@ class PendingTransaction { private: - Hashset *keyValueUpdateSet = NULL; - Hashset *keyValueGuardSet = NULL; - int64_t arbitrator = -1; - int64_t clientLocalSequenceNumber = -1; - int64_t machineId = -1; - int32_T currentDataSize = 0; + Hashset *keyValueUpdateSet; + Hashset *keyValueGuardSet; + int64_t arbitrator; + int64_t clientLocalSequenceNumber; + int64_t machineId; + int32_t currentDataSize; public: PendingTransaction(int64_t _machineId); @@ -18,12 +18,12 @@ public: * Add a new key value to the updates * */ - void addKV(KeyValue newKV); + void addKV(KeyValue * newKV); /** * Add a new key value to the guard set * */ - void addKVGuard(KeyValue newKV); + void addKVGuard(KeyValue * newKV); /** * Checks if the arbitrator is the same */ @@ -40,7 +40,7 @@ public: /** * Get the key value update set */ - public Hashset *getKVGuard() { return keyValueGuardSet; } + Hashset *getKVGuard() { return keyValueGuardSet; } void setClientLocalSequenceNumber(int64_t _clientLocalSequenceNumber) { clientLocalSequenceNumber = _clientLocalSequenceNumber; } @@ -48,7 +48,7 @@ public: int64_t getMachineId() { return machineId; } - bool evaluateGuard(Hashtable keyValTableCommitted, Hashtable keyValTableSpeculative, Hashtable keyValTablePendingTransSpeculative); + bool evaluateGuard(Hashtable * keyValTableCommitted, Hashtable * keyValTableSpeculative, Hashtable * keyValTablePendingTransSpeculative); Transaction *createTransaction(); diff --git a/version2/src/C/Transaction.cc b/version2/src/C/Transaction.cc index 526c0ba..aad62e5 100644 --- a/version2/src/C/Transaction.cc +++ b/version2/src/C/Transaction.cc @@ -1,10 +1,20 @@ #include "Transaction.h" -Transaction::Transaction() { - parts = new Hashtable(); - keyValueGuardSet = new HashSet(); - keyValueUpdateSet = new HashSet(); - partsPendingSend = new Vector(); +Transaction::Transaction() : + parts(new Hashtable()), + missingParts(NULL), + partsPendingSend(new Vector()), + fldisComplete(false), + hasLastPart(false), + keyValueGuardSet(new HashSet()), + keyValueUpdateSet(new HashSet()), + isDead(false), + sequenceNumber(-1), + clientLocalSequenceNumber(-1), + arbitratorId(-1), + machineId(-1), + transactionId(NULL), + hadServerFailure(false) { } void Transaction::addPartEncode(TransactionPart *newPart) { @@ -17,7 +27,7 @@ void Transaction::addPartEncode(TransactionPart *newPart) { clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber(); machineId = newPart.getMachineId(); - isComplete = true; + fldisComplete = true; } void Transaction::addPartDecode(TransactionPart *newPart) { @@ -49,7 +59,7 @@ void Transaction::addPartDecode(TransactionPart *newPart) { } } - if (!isComplete && hasLastPart) { + if (!fldisComplete && hasLastPart) { // We have seen this part so remove it from the set of missing parts missingParts.remove(newPart.getPartNumber()); @@ -58,7 +68,7 @@ void Transaction::addPartDecode(TransactionPart *newPart) { if (missingParts.size() == 0) { // We have all the parts - isComplete = true; + fldisComplete = true; // Decode all the parts and create the key value guard and update sets decodeTransactionData(); @@ -96,7 +106,7 @@ Hashtable *Transaction::getParts() { } bool Transaction::didSendAPartToServer() { - return didSendAPartToServer; + return flddidSendAPartToServer; } void Transaction::resetNextPartToSend() { @@ -139,7 +149,7 @@ void Transaction::removeSentParts(Vector *sentParts) { nextPartToSend = 0; if (partsPendingSend.removeAll(sentParts)) { - didSendAPartToServer = true; + flddidSendAPartToServer = true; transactionStatus.setTransactionSequenceNumber(sequenceNumber); } } @@ -165,7 +175,7 @@ int64_t Transaction::getArbitrator() { } bool Transaction::isComplete() { - return isComplete; + return fldisComplete; } Pair *Transaction::getId() { diff --git a/version2/src/C/Transaction.h b/version2/src/C/Transaction.h index aa62661..2f0e3dd 100644 --- a/version2/src/C/Transaction.h +++ b/version2/src/C/Transaction.h @@ -1,26 +1,27 @@ #ifndef TRANSACTION_H #define TRANSACTION_H #include "common.h" +#include "Pair.h" class Transaction { private: - Hashtable *parts = NULL; - Hashset *missingParts = NULL; - Vector *partsPendingSend = NULL; - bool isComplete = false; - bool hasLastPart = false; - Hashset *keyValueGuardSet = NULL; - Hashset *keyValueUpdateSet = NULL; - bool isDead = false; - int64_t sequenceNumber = -1; - int64_t clientLocalSequenceNumber = -1; - int64_t arbitratorId = -1; - int64_t machineId = -1; - Pair *transactionId = NULL; - int nextPartToSend = 0; - bool didSendAPartToServer = false; - TransactionStatus *transactionStatus = NULL; - bool hadServerFailure = false; + Hashtable *parts; + Hashset *missingParts; + Vector *partsPendingSend; + bool fldisComplete; + bool hasLastPart; + Hashset *keyValueGuardSet; + Hashset *keyValueUpdateSet; + bool isDead; + int64_t sequenceNumber; + int64_t clientLocalSequenceNumber; + int64_t arbitratorId; + int64_t machineId; + Pair *transactionId; + int nextPartToSend; + bool flddidSendAPartToServer; + TransactionStatus *transactionStatus; + bool hadServerFailure; void decodeTransactionData(); public: diff --git a/version2/src/C/array.h b/version2/src/C/array.h index a456fef..b9d7196 100644 --- a/version2/src/C/array.h +++ b/version2/src/C/array.h @@ -1,6 +1,7 @@ #ifndef ARRAY_H #define ARRAY_H #include +#include "common.h" typedef uint32_t uint; @@ -72,4 +73,13 @@ private: type *array; uint size; }; + +template +void System_arraycopy(Array * src, int32_t srcPos, Array *dst, int32_t dstPos, int32_t len) { + if (srcPos + len > src->length() || + dstPos + len > dst->length()) + ASSERT(0); + uint bytesToCopy = len * sizeof(type); + memcpy(&dst->internalArray()[dstPos], &src->internalArray()[srcPos], bytesToCopy); +} #endif diff --git a/version2/src/C/common.h b/version2/src/C/common.h index 5ce508e..7973b6b 100644 --- a/version2/src/C/common.h +++ b/version2/src/C/common.h @@ -4,6 +4,14 @@ typedef uint32_t uint; #define CMEMALLOC ; #define model_print printf +#define ASSERT(expr) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \ + /* print_trace(); // Trace printing may cause dynamic memory allocation */ \ + exit(EXIT_FAILURE); \ + } \ + } while (0) #include "hashset.h" #include "vector.h" @@ -39,13 +47,6 @@ class TransactionStatus; class Mac; class Error; -#define ASSERT(expr) \ - do { \ - if (!(expr)) { \ - fprintf(stderr, "Error: assertion failed in %s at line %d\n", __FILE__, __LINE__); \ - /* print_trace(); // Trace printing may cause dynamic memory allocation */ \ - exit(EXIT_FAILURE); \ - } \ - } while (0) + #endif diff --git a/version2/src/C/hashset.h b/version2/src/C/hashset.h index ab096fb..a518de6 100644 --- a/version2/src/C/hashset.h +++ b/version2/src/C/hashset.h @@ -21,7 +21,7 @@ struct Linknode { template class Hashset; -template, bool (*equals)(_Key, _Key) = defaultEquals<_Key> > +template, bool (*equals)(_Key, _Key) = defaultEquals<_Key> > class SetIterator { public: SetIterator(Linknode<_Key> *_curr, Hashset <_Key, _KeyInt, _Shift, hash_function, equals> *_set) : -- 2.34.1