+#include "Transaction.h"
+#include "TransactionPart.h"
+#include "KeyValue.h"
+#include "ByteBuffer.h"
+#include "IoTString.h"
+#include "TransactionStatus.h"
+
+Transaction::Transaction() :
+ parts(new Hashtable<int32_t, TransactionPart *>()),
+ missingParts(NULL),
+ partsPendingSend(new Vector<int32_t>()),
+ fldisComplete(false),
+ hasLastPart(false),
+ keyValueGuardSet(new Hashset<KeyValue *>()),
+ keyValueUpdateSet(new Hashset<KeyValue *>()),
+ isDead(false),
+ sequenceNumber(-1),
+ clientLocalSequenceNumber(-1),
+ arbitratorId(-1),
+ machineId(-1),
+ transactionId(NULL),
+ hadServerFailure(false) {
+}
+
+void Transaction::addPartEncode(TransactionPart *newPart) {
+ parts->put(newPart->getPartNumber(), newPart);
+ partsPendingSend->add(newPart->getPartNumber());
+ sequenceNumber = newPart->getSequenceNumber();
+ arbitratorId = newPart->getArbitratorId();
+ transactionId = newPart->getTransactionId();
+ clientLocalSequenceNumber = newPart->getClientLocalSequenceNumber();
+ machineId = newPart->getMachineId();
-class Transaction {
+ fldisComplete = true;
+}
- Hashtable<int32_t, TransactionPart> parts = NULL;
- Set<int32_t> missingParts = NULL;
- Vector<int32_t> partsPendingSend = NULL;
- bool isComplete = false;
- bool hasLastPart = false;
- Set<KeyValue> keyValueGuardSet = NULL;
- Set<KeyValue> keyValueUpdateSet = NULL;
- bool isDead = false;
- int64_t sequenceNumber = -1;
- int64_t clientLocalSequenceNumber = -1;
- int64_t arbitratorId = -1;
- int64_t machineId = -1;
- Pair<int64_t, int64_t> transactionId = NULL;
+void Transaction::addPartDecode(TransactionPart *newPart) {
+ if (isDead) {
+ // If dead then just kill this part and move on
+ newPart->setDead();
+ return;
+ }
- int nextPartToSend = 0;
- bool didSendAPartToServer = false;
+ sequenceNumber = newPart->getSequenceNumber();
+ arbitratorId = newPart->getArbitratorId();
+ transactionId = newPart->getTransactionId();
+ clientLocalSequenceNumber = newPart->getClientLocalSequenceNumber();
+ machineId = newPart->getMachineId();
- TransactionStatus transactionStatus = NULL;
+ TransactionPart *previoslySeenPart = parts->put(newPart->getPartNumber(), newPart);
- bool hadServerFailure = false;
+ if (previoslySeenPart != NULL) {
+ // Set dead the old one since the new one is a rescued version of this part
+ previoslySeenPart->setDead();
+ } else if (newPart->isLastPart()) {
+ missingParts = new Hashset<int32_t>();
+ hasLastPart = true;
- Transaction() {
- parts = new Hashtable<int32_t, TransactionPart>();
- keyValueGuardSet = new HashSet<KeyValue>();
- keyValueUpdateSet = new HashSet<KeyValue>();
- partsPendingSend = new Vector<int32_t>();
+ for (int i = 0; i < newPart->getPartNumber(); i++) {
+ if (parts->get(i) == NULL) {
+ missingParts->add(i);
+ }
+ }
}
- void addPartEncode(TransactionPart newPart) {
- parts.put(newPart.getPartNumber(), newPart);
- partsPendingSend.add(newPart.getPartNumber());
+ if (!fldisComplete && hasLastPart) {
- sequenceNumber = newPart.getSequenceNumber();
- arbitratorId = newPart.getArbitratorId();
- transactionId = newPart.getTransactionId();
- clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
- machineId = newPart.getMachineId();
+ // We have seen this part so remove it from the set of missing parts
+ missingParts->remove(newPart->getPartNumber());
- isComplete = true;
- }
+ // Check if all the parts have been seen
+ if (missingParts->size() == 0) {
- void addPartDecode(TransactionPart newPart) {
+ // We have all the parts
+ fldisComplete = true;
- if (isDead) {
- // If dead then just kill this part and move on
- newPart.setDead();
- return;
+ // Decode all the parts and create the key value guard and update sets
+ decodeTransactionData();
}
+ }
+}
- sequenceNumber = newPart.getSequenceNumber();
- arbitratorId = newPart.getArbitratorId();
- transactionId = newPart.getTransactionId();
- clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
- machineId = newPart.getMachineId();
+void Transaction::addUpdateKV(KeyValue *kv) {
+ keyValueUpdateSet->add(kv);
+}
- TransactionPart previoslySeenPart = parts.put(newPart.getPartNumber(), newPart);
+void Transaction::addGuardKV(KeyValue *kv) {
+ keyValueGuardSet->add(kv);
+}
- if (previoslySeenPart != NULL) {
- // Set dead the old one since the new one is a rescued version of this part
- previoslySeenPart.setDead();
- } else if (newPart.isLastPart()) {
- missingParts = new HashSet<int32_t>();
- hasLastPart = true;
- for (int i = 0; i < newPart.getPartNumber(); i++) {
- if (parts.get(i) == NULL) {
- missingParts.add(i);
- }
- }
- }
+int64_t Transaction::getSequenceNumber() {
+ return sequenceNumber;
+}
- if (!isComplete && hasLastPart) {
+void Transaction::setSequenceNumber(int64_t _sequenceNumber) {
+ sequenceNumber = _sequenceNumber;
- // We have seen this part so remove it from the set of missing parts
- missingParts.remove(newPart.getPartNumber());
+ for (int32_t i : parts->keySet()) {
+ parts->get(i)->setSequenceNumber(sequenceNumber);
+ }
+}
- // Check if all the parts have been seen
- if (missingParts.size() == 0) {
+int64_t Transaction::getClientLocalSequenceNumber() {
+ return clientLocalSequenceNumber;
+}
- // We have all the parts
- isComplete = true;
+Hashtable<int32_t, TransactionPart *> *Transaction::getParts() {
+ return parts;
+}
- // Decode all the parts and create the key value guard and update sets
- decodeTransactionData();
- }
- }
- }
+bool Transaction::didSendAPartToServer() {
+ return flddidSendAPartToServer;
+}
- void addUpdateKV(KeyValue kv) {
- keyValueUpdateSet.add(kv);
- }
+void Transaction::resetNextPartToSend() {
+ nextPartToSend = 0;
+}
- void addGuardKV(KeyValue kv) {
- keyValueGuardSet.add(kv);
+TransactionPart *Transaction::getNextPartToSend() {
+ if ((partsPendingSend->size() == 0) || (partsPendingSend->size() == nextPartToSend)) {
+ return NULL;
}
+ TransactionPart *part = parts->get(partsPendingSend->get(nextPartToSend));
+ nextPartToSend++;
+ return part;
+}
- int64_t getSequenceNumber() {
- return sequenceNumber;
- }
+void Transaction::setServerFailure() {
+ hadServerFailure = true;
+}
- void setSequenceNumber(int64_t _sequenceNumber) {
- sequenceNumber = _sequenceNumber;
+bool Transaction::getServerFailure() {
+ return hadServerFailure;
+}
- for (int32_t i : parts.keySet()) {
- parts.get(i).setSequenceNumber(sequenceNumber);
- }
- }
- int64_t getClientLocalSequenceNumber() {
- return clientLocalSequenceNumber;
- }
+void Transaction::resetServerFailure() {
+ hadServerFailure = false;
+}
- Hashtable<int32_t, TransactionPart> getParts() {
- return parts;
- }
- bool didSendAPartToServer() {
- return didSendAPartToServer;
- }
+void Transaction::setTransactionStatus(TransactionStatus *_transactionStatus) {
+ transactionStatus = _transactionStatus;
+}
- void resetNextPartToSend() {
- nextPartToSend = 0;
- }
+TransactionStatus *Transaction::getTransactionStatus() {
+ return transactionStatus;
+}
- TransactionPart getNextPartToSend() {
- if ((partsPendingSend.size() == 0) || (partsPendingSend.size() == nextPartToSend)) {
- return NULL;
- }
- TransactionPart part = parts.get(partsPendingSend.get(nextPartToSend));
- nextPartToSend++;
- return part;
+void Transaction::removeSentParts(Vector<int32_t> *sentParts) {
+ nextPartToSend = 0;
+ if (partsPendingSend->removeAll(sentParts))
+ {
+ flddidSendAPartToServer = true;
+ transactionStatus->setTransactionSequenceNumber(sequenceNumber);
}
+}
+bool Transaction::didSendAllParts() {
+ return partsPendingSend->isEmpty();
+}
- void setServerFailure() {
- hadServerFailure = true;
- }
-
- bool getServerFailure() {
- return hadServerFailure;
- }
+Hashset<KeyValue *> *Transaction::getKeyValueUpdateSet() {
+ return keyValueUpdateSet;
+}
+int Transaction::getNumberOfParts() {
+ return parts->size();
+}
- void resetServerFailure() {
- hadServerFailure = false;
- }
+int64_t Transaction::getMachineId() {
+ return machineId;
+}
+int64_t Transaction::getArbitrator() {
+ return arbitratorId;
+}
- void setTransactionStatus(TransactionStatus _transactionStatus) {
- transactionStatus = _transactionStatus;
- }
+bool Transaction::isComplete() {
+ return fldisComplete;
+}
- TransactionStatus getTransactionStatus() {
- return transactionStatus;
- }
+Pair<int64_t, int64_t> Transaction::getId() {
+ return transactionId;
+}
- void removeSentParts(Vector<int32_t> sentParts) {
- nextPartToSend = 0;
- if (partsPendingSend.removeAll(sentParts))
- {
- didSendAPartToServer = true;
- transactionStatus.setTransactionSequenceNumber(sequenceNumber);
- }
+void Transaction::setDead() {
+ if (isDead) {
+ // Already dead
+ return;
}
- bool didSendAllParts() {
- return partsPendingSend.isEmpty();
- }
+ // Set dead
+ isDead = true;
- Set<KeyValue> getKeyValueUpdateSet() {
- return keyValueUpdateSet;
+ // Make all the parts of this transaction dead
+ for (int32_t partNumber : parts->keySet()) {
+ TransactionPart *part = parts->get(partNumber);
+ part->setDead();
}
+}
- int getNumberOfParts() {
- return parts.size();
- }
+TransactionPart *Transaction::getPart(int index) {
+ return parts->get(index);
+}
- int64_t getMachineId() {
- return machineId;
- }
+void Transaction::decodeTransactionData() {
- int64_t getArbitrator() {
- return arbitratorId;
+ // Calculate the size of the data section
+ int dataSize = 0;
+ for (int i = 0; i < parts->keySet()->size(); i++) {
+ TransactionPart *tp = parts->get(i);
+ dataSize += tp->getDataSize();
}
- bool isComplete() {
- return isComplete;
- }
+ Array<char> *combinedData = new Array<char>(dataSize);
+ int currentPosition = 0;
- Pair<int64_t, int64_t> getId() {
- return transactionId;
+ // Stitch all the data sections together
+ for (int i = 0; i < parts->keySet()->size(); i++) {
+ TransactionPart *tp = parts->get(i);
+ System_arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize());
+ currentPosition += tp->getDataSize();
}
- void setDead() {
- if (isDead) {
- // Already dead
- return;
- }
+ // Decoder Object
+ ByteBuffer *bbDecode = ByteBuffer_wrap(combinedData);
- // Set dead
- isDead = true;
+ // Decode how many key value pairs need to be decoded
+ int numberOfKVGuards = bbDecode->getInt();
+ int numberOfKVUpdates = bbDecode->getInt();
- // Make all the parts of this transaction dead
- for (int32_t partNumber : parts.keySet()) {
- TransactionPart part = parts.get(partNumber);
- part.setDead();
- }
+ // Decode all the guard key values
+ for (int i = 0; i < numberOfKVGuards; i++) {
+ KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode);
+ keyValueGuardSet->add(kv);
}
- TransactionPart getPart(int index) {
- return parts.get(index);
+ // Decode all the updates key values
+ for (int i = 0; i < numberOfKVUpdates; i++) {
+ KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode);
+ keyValueUpdateSet->add(kv);
}
+}
- void decodeTransactionData() {
+bool Transaction::evaluateGuard(Hashtable<IoTString *, KeyValue *> *committedKeyValueTable, Hashtable<IoTString *, KeyValue *> *speculatedKeyValueTable, Hashtable<IoTString *, KeyValue *> *pendingTransactionSpeculatedKeyValueTable) {
+ for (KeyValue *kvGuard : keyValueGuardSet) {
- // Calculate the size of the data section
- int dataSize = 0;
- for (int i = 0; i < parts.keySet().size(); i++) {
- TransactionPart tp = parts.get(i);
- dataSize += tp.getDataSize();
- }
+ // First check if the key is in the speculative table, this is the value of the latest assumption
+ KeyValue *kv = NULL;
- Array<char> *combinedData = new char[dataSize];
- int currentPosition = 0;
-
- // Stitch all the data sections together
- for (int i = 0; i < parts.keySet().size(); i++) {
- TransactionPart tp = parts.get(i);
- System.arraycopy(tp.getData(), 0, combinedData, currentPosition, tp.getDataSize());
- currentPosition += tp.getDataSize();
+ // If we have a speculation table then use it first
+ if (pendingTransactionSpeculatedKeyValueTable != NULL) {
+ kv = pendingTransactionSpeculatedKeyValueTable->get(kvGuard->getKey());
}
- // Decoder Object
- ByteBuffer bbDecode = ByteBuffer.wrap(combinedData);
-
- // Decode how many key value pairs need to be decoded
- int numberOfKVGuards = bbDecode.getInt();
- int numberOfKVUpdates = bbDecode.getInt();
-
- // Decode all the guard key values
- for (int i = 0; i < numberOfKVGuards; i++) {
- KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
- keyValueGuardSet.add(kv);
+ // If we have a speculation table then use it first
+ if ((kv == NULL) && (speculatedKeyValueTable != NULL)) {
+ kv = speculatedKeyValueTable->get(kvGuard->getKey());
}
- // Decode all the updates key values
- for (int i = 0; i < numberOfKVUpdates; i++) {
- KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
- keyValueUpdateSet.add(kv);
+ 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 = committedKeyValueTable->get(kvGuard->getKey());
}
- }
-
- bool evaluateGuard(Hashtable<IoTString, KeyValue> committedKeyValueTable, Hashtable<IoTString, KeyValue> speculatedKeyValueTable, Hashtable<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable) {
- for (KeyValue kvGuard : keyValueGuardSet) {
-
- // First check if the key is in the speculative table, this is the value of the latest assumption
- KeyValue kv = NULL;
-
- // If we have a speculation table then use it first
- if (pendingTransactionSpeculatedKeyValueTable != NULL) {
- kv = pendingTransactionSpeculatedKeyValueTable.get(kvGuard.getKey());
- }
-
- // If we have a speculation table then use it first
- if ((kv == NULL) && (speculatedKeyValueTable != NULL)) {
- kv = speculatedKeyValueTable.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 = committedKeyValueTable.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()))) {
- if (kv != NULL) {
- System.out.println(kvGuard.getValue() + " " + kv.getValue());
- } else {
- System.out.println(kvGuard.getValue() + " " + kv);
- }
-
- return false;
- }
- } else {
if (kv != NULL) {
- return false;
+ printf("%s %s\n", kvGuard->getKey()->internalBytes()->internalArray(), kv->getValue()->internalBytes()->internalArray());
+ } else {
+ printf("%s null\n", kvGuard->getValue()->internalBytes()->internalArray());
}
+
+ return false;
+ }
+ } else {
+ if (kv != NULL) {
+ return false;
}
}
- return true;
}
+ return true;
}
+