edits
[iotcloud.git] / version2 / src / C / ArbitrationRound.cc
1 #include "ArbitrationRound.h"
2 #include "Commit.h"
3
4 ArbitrationRound::ArbitrationRound(Commit *_commit, Hashset<Abort *> *_abortsBefore) :
5         abortsBefore(_abortsBefore),
6         parts(new Vector<Entry *>()),
7         commit(_commit),
8         currentSize(0),
9         didSendPart(false),
10         didGenerateParts(false) {
11
12         if (commit != NULL) {
13                 commit->createCommitParts();
14                 currentSize += commit->getNumberOfParts();
15         }
16
17         currentSize += abortsBefore->size();
18 }
19
20 ArbitrationRound::~ArbitrationRound() {
21         delete abortsBefore;
22         delete parts;
23         if (commit != NULL)
24                 delete commit;
25 }
26
27 void ArbitrationRound::generateParts() {
28         if (didGenerateParts) {
29                 return;
30         }
31         parts = new Vector<Entry *>();
32         SetIterator<Abort *, Abort *> *abit = abortsBefore->iterator();
33         while (abit->hasNext())
34                 parts->add((Entry *)abit->next());
35         delete abit;
36         if (commit != NULL) {
37                 Vector<CommitPart *> *cParts = commit->getParts();
38                 uint cPartsSize = cParts->size();
39                 for (uint i = 0; i < cPartsSize; i++) {
40                         parts->add((Entry *)cParts->get(i));
41                 }
42         }
43 }
44
45 Vector<Entry *> *ArbitrationRound::getParts() {
46         return parts;
47 }
48
49 void ArbitrationRound::removeParts(Vector<Entry *> *removeParts) {
50         parts->removeAll(removeParts);
51         didSendPart = true;
52 }
53
54
55 bool ArbitrationRound::isDoneSending() {
56         if ((commit == NULL) && abortsBefore->isEmpty()) {
57                 return true;
58         }
59         return parts->isEmpty();
60 }
61
62 Commit *ArbitrationRound::getCommit() {
63         return commit;
64 }
65
66 void ArbitrationRound::setCommit(Commit *_commit) {
67         if (commit != NULL) {
68                 currentSize -= commit->getNumberOfParts();
69         }
70         commit = _commit;
71
72         if (commit != NULL) {
73                 currentSize += commit->getNumberOfParts();
74         }
75 }
76
77 void ArbitrationRound::addAbort(Abort *abort) {
78         abortsBefore->add(abort);
79         currentSize++;
80 }
81
82 void ArbitrationRound::addAborts(Hashset<Abort *> *aborts) {
83         abortsBefore->addAll(aborts);
84         currentSize += aborts->size();
85 }
86
87 Hashset<Abort *> *ArbitrationRound::getAborts() {
88         return abortsBefore;
89 }
90
91 int ArbitrationRound::getAbortsCount() {
92         return abortsBefore->size();
93 }
94
95 int ArbitrationRound::getCurrentSize() {
96         return currentSize;
97 }
98
99 bool ArbitrationRound::isFull() {
100         return currentSize >= ArbitrationRound_MAX_PARTS;
101 }
102
103 bool ArbitrationRound::getDidSendPart() {
104         return didSendPart;
105 }