1 #include "Transaction.h"
2 #include "TransactionPart.h"
4 #include "ByteBuffer.h"
6 #include "TransactionStatus.h"
8 Transaction::Transaction() :
9 parts(new Vector<TransactionPart *>()),
12 partsPendingSend(new Vector<int32_t>()),
15 keyValueGuardSet(new Hashset<KeyValue *>()),
16 keyValueUpdateSet(new Hashset<KeyValue *>()),
19 clientLocalSequenceNumber(-1),
22 transactionId(Pair<int64_t, int64_t>(0,0)),
24 flddidSendAPartToServer(false),
25 transactionStatus(NULL),
26 hadServerFailure(false) {
29 Transaction::~Transaction() {
31 delete keyValueGuardSet;
32 delete keyValueUpdateSet;
35 void Transaction::addPartEncode(TransactionPart *newPart) {
36 TransactionPart *old = parts->setExpand(newPart->getPartNumber(), newPart);
39 partsPendingSend->add(newPart->getPartNumber());
41 sequenceNumber = newPart->getSequenceNumber();
42 arbitratorId = newPart->getArbitratorId();
43 transactionId = newPart->getTransactionId();
44 clientLocalSequenceNumber = newPart->getClientLocalSequenceNumber();
45 machineId = newPart->getMachineId();
49 void Transaction::addPartDecode(TransactionPart *newPart) {
51 // If dead then just kill this part and move on
56 sequenceNumber = newPart->getSequenceNumber();
57 arbitratorId = newPart->getArbitratorId();
58 transactionId = newPart->getTransactionId();
59 clientLocalSequenceNumber = newPart->getClientLocalSequenceNumber();
60 machineId = newPart->getMachineId();
62 TransactionPart *previouslySeenPart = parts->setExpand(newPart->getPartNumber(), newPart);
63 if (previouslySeenPart == NULL)
66 if (previouslySeenPart != NULL) {
67 // Set dead the old one since the new one is a rescued version of this part
68 previouslySeenPart->setDead();
69 } else if (newPart->isLastPart()) {
70 missingParts = new Hashset<int32_t>();
73 for (int i = 0; i < newPart->getPartNumber(); i++) {
74 if (parts->get(i) == NULL) {
80 if (!fldisComplete && hasLastPart) {
82 // We have seen this part so remove it from the set of missing parts
83 missingParts->remove(newPart->getPartNumber());
85 // Check if all the parts have been seen
86 if (missingParts->size() == 0) {
88 // We have all the parts
91 // Decode all the parts and create the key value guard and update sets
92 decodeTransactionData();
97 void Transaction::addUpdateKV(KeyValue *kv) {
98 keyValueUpdateSet->add(kv);
101 void Transaction::addGuardKV(KeyValue *kv) {
102 keyValueGuardSet->add(kv);
106 int64_t Transaction::getSequenceNumber() {
107 return sequenceNumber;
110 void Transaction::setSequenceNumber(int64_t _sequenceNumber) {
111 sequenceNumber = _sequenceNumber;
113 for (uint32_t i = 0; i < parts->size(); i++) {
114 TransactionPart *tp = parts->get(i);
116 tp->setSequenceNumber(sequenceNumber);
120 int64_t Transaction::getClientLocalSequenceNumber() {
121 return clientLocalSequenceNumber;
124 Vector<TransactionPart *> *Transaction::getParts() {
128 bool Transaction::didSendAPartToServer() {
129 return flddidSendAPartToServer;
132 void Transaction::resetNextPartToSend() {
136 TransactionPart *Transaction::getNextPartToSend() {
137 if ((partsPendingSend->size() == 0) || (partsPendingSend->size() == nextPartToSend)) {
140 TransactionPart *part = parts->get(partsPendingSend->get(nextPartToSend));
146 void Transaction::setServerFailure() {
147 hadServerFailure = true;
150 bool Transaction::getServerFailure() {
151 return hadServerFailure;
155 void Transaction::resetServerFailure() {
156 hadServerFailure = false;
160 void Transaction::setTransactionStatus(TransactionStatus *_transactionStatus) {
161 transactionStatus = _transactionStatus;
164 TransactionStatus *Transaction::getTransactionStatus() {
165 return transactionStatus;
168 void Transaction::removeSentParts(Vector<int32_t> *sentParts) {
170 bool changed = false;
171 uint lastusedindex = 0;
172 for (uint i = 0; i < partsPendingSend->size(); i++) {
173 int32_t parti = partsPendingSend->get(i);
174 for (uint j = 0; j < sentParts->size(); j++) {
175 int32_t partj = sentParts->get(j);
176 if (parti == partj) {
181 partsPendingSend->set(lastusedindex++, parti);
186 partsPendingSend->setSize(lastusedindex);
187 flddidSendAPartToServer = true;
188 transactionStatus->setTransactionSequenceNumber(sequenceNumber);
192 bool Transaction::didSendAllParts() {
193 return partsPendingSend->isEmpty();
196 Hashset<KeyValue *> *Transaction::getKeyValueUpdateSet() {
197 return keyValueUpdateSet;
200 int Transaction::getNumberOfParts() {
204 int64_t Transaction::getMachineId() {
208 int64_t Transaction::getArbitrator() {
212 bool Transaction::isComplete() {
213 return fldisComplete;
216 Pair<int64_t, int64_t> *Transaction::getId() {
217 return &transactionId;
220 void Transaction::setDead() {
224 // Make all the parts of this transaction dead
225 for (uint32_t partNumber = 0; partNumber < parts->size(); partNumber++) {
226 TransactionPart *part = parts->get(partNumber);
233 TransactionPart *Transaction::getPart(int index) {
234 return parts->get(index);
237 void Transaction::decodeTransactionData() {
238 // Calculate the size of the data section
240 for (uint i = 0; i < parts->size(); i++) {
241 TransactionPart *tp = parts->get(i);
242 dataSize += tp->getDataSize();
245 Array<char> *combinedData = new Array<char>(dataSize);
246 int currentPosition = 0;
248 // Stitch all the data sections together
249 for (uint i = 0; i < parts->size(); i++) {
250 TransactionPart *tp = parts->get(i);
251 System_arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize());
252 currentPosition += tp->getDataSize();
256 ByteBuffer *bbDecode = ByteBuffer_wrap(combinedData);
258 // Decode how many key value pairs need to be decoded
259 int numberOfKVGuards = bbDecode->getInt();
260 int numberOfKVUpdates = bbDecode->getInt();
262 // Decode all the guard key values
263 for (int i = 0; i < numberOfKVGuards; i++) {
264 KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode);
265 keyValueGuardSet->add(kv);
268 // Decode all the updates key values
269 for (int i = 0; i < numberOfKVUpdates; i++) {
270 KeyValue *kv = (KeyValue *)KeyValue_decode(bbDecode);
271 keyValueUpdateSet->add(kv);
275 bool Transaction::evaluateGuard(Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals> *committedKeyValueTable, Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals> *speculatedKeyValueTable, Hashtable<IoTString *, KeyValue *, uintptr_t, 0, hashString, StringEquals> *pendingTransactionSpeculatedKeyValueTable) {
276 SetIterator<KeyValue *, KeyValue *> *kvit = keyValueGuardSet->iterator();
277 while (kvit->hasNext()) {
278 KeyValue *kvGuard = kvit->next();
279 // First check if the key is in the speculative table, this is the value of the latest assumption
282 // If we have a speculation table then use it first
283 if (pendingTransactionSpeculatedKeyValueTable != NULL) {
284 kv = pendingTransactionSpeculatedKeyValueTable->get(kvGuard->getKey());
287 // If we have a speculation table then use it first
288 if ((kv == NULL) && (speculatedKeyValueTable != NULL)) {
289 kv = speculatedKeyValueTable->get(kvGuard->getKey());
293 // if it is not in the speculative table then check the committed table and use that
294 // value as our latest assumption
295 kv = committedKeyValueTable->get(kvGuard->getKey());
298 if (kvGuard->getValue() != NULL) {
299 if ((kv == NULL) || (!kvGuard->getValue()->equals(kv->getValue()))) {
303 printf("%s %s\n", kvGuard->getKey()->internalBytes()->internalArray(), kv->getValue()->internalBytes()->internalArray());
305 printf("%s null\n", kvGuard->getValue()->internalBytes()->internalArray());