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