4 parts(new Hashtable<int32_t, CommitPart *>()),
8 keyValueUpdateSet(new HashSet<KeyValue *>()),
12 transactionSequenceNumber(-1),
13 liveKeys(new Hashset<IoTString *>) {
17 Commit::Commit(int64_t _sequenceNumber, int64_t _machineId, int64_t _transactionSequenceNumber) :
18 parts(new Hashtable<int32_t, CommitPart *>()),
22 keyValueUpdateSet(new HashSet<KeyValue *>()),
24 sequenceNumber(_sequenceNumber),
25 machineId(_machineId),
26 transactionSequenceNumber(_transactionSequenceNumber),
27 liveKeys(new Hashset<IoTString *>) {
30 void Commit::addPartDecode(CommitPart newPart) {
33 // If dead then just kill this part and move on
38 CommitPart *previoslySeenPart = parts->put(newPart->getPartNumber(), newPart);
40 if (previoslySeenPart != NULL) {
41 // Set dead the old one since the new one is a rescued version of this part
42 previoslySeenPart->setDead();
43 } else if (newPart->isLastPart()) {
44 missingParts = new HashSet<int32_t>();
47 for (int i = 0; i < newPart->getPartNumber(); i++) {
48 if (parts->get(i) == NULL) {
54 if (!fldisComplete && hasLastPart) {
56 // We have seen this part so remove it from the set of missing parts
57 missingParts->remove(newPart->getPartNumber());
59 // Check if all the parts have been seen
60 if (missingParts->size() == 0) {
62 // We have all the parts
65 // Decode all the parts and create the key value guard and update sets
68 // Get the sequence number and arbitrator of this transaction
69 sequenceNumber = parts->get(0)->getSequenceNumber();
70 machineId = parts->get(0)->getMachineId();
71 transactionSequenceNumber = parts->get(0)->getTransactionSequenceNumber();
76 int64_t Commit::getSequenceNumber() {
77 return sequenceNumber;
80 int64_t Commit::getTransactionSequenceNumber() {
81 return transactionSequenceNumber;
84 Hashtable<int32_t, CommitPart *> *Commit::getParts() {
88 void Commit::addKV(KeyValue *kv) {
89 keyValueUpdateSet->add(kv);
90 liveKeys->add(kv->getKey());
93 void Commit::invalidateKey(IoTString *key) {
94 liveKeys->remove(key);
96 if (liveKeys->size() == 0) {
101 Hashset<KeyValue *> *Commit::getKeyValueUpdateSet() {
102 return keyValueUpdateSet;
105 int32_t Commit::getNumberOfParts() {
106 return parts->size();
109 void Commit::setDead() {
118 // Make all the parts of this transaction dead
119 for (int32_t partNumber : parts->keySet()) {
120 CommitPart part = parts->get(partNumber);
125 CommitPart *Commit::getPart(int index) {
126 return parts->get(index);
129 void Commit::createCommitParts() {
133 Array<char> *charData = convertDataToBytes();
136 int commitPartCount = 0;
137 int currentPosition = 0;
138 int remaining = charData->length();
140 while (remaining > 0) {
142 bool isLastPart = false;
143 // determine how much to copy
144 int copySize = CommitPart_MAX_NON_HEADER_SIZE;
145 if (remaining <= CommitPart_MAX_NON_HEADER_SIZE) {
146 copySize = remaining;
147 isLastPart = true;// last bit of data so last part
150 // Copy to a smaller version
151 Array<char> *partData = new Array<char>(copySize);
152 System->arraycopy(charData, currentPosition, partData, 0, copySize);
154 CommitPart part = new CommitPart(NULL, machineId, sequenceNumber, transactionSequenceNumber, commitPartCount, partData, isLastPart);
155 parts->put(part->getPartNumber(), part);
157 // Update position, count and remaining
158 currentPosition += copySize;
160 remaining -= copySize;
164 void Commit::decodeCommitData() {
166 // Calculate the size of the data section
168 for (int i = 0; i < parts->keySet()->size(); i++) {
169 CommitPart *tp = parts->get(i);
170 dataSize += tp->getDataSize();
173 Array<char> *combinedData = new Array<char>(dataSize);
174 int currentPosition = 0;
176 // Stitch all the data sections together
177 for (int i = 0; i < parts->keySet()->size(); i++) {
178 CommitPart *tp = parts->get(i);
179 System->arraycopy(tp->getData(), 0, combinedData, currentPosition, tp->getDataSize());
180 currentPosition += tp->getDataSize();
184 ByteBuffer *bbDecode = ByteBuffer_wrap(combinedData);
186 // Decode how many key value pairs need to be decoded
187 int numberOfKVUpdates = bbDecode->getInt();
189 // Decode all the updates key values
190 for (int i = 0; i < numberOfKVUpdates; i++) {
191 KeyValue *kv = (KeyValue *)KeyValue->decode(bbDecode);
192 keyValueUpdateSet->add(kv);
193 liveKeys->add(kv->getKey());
197 Array<char> *convertDataToBytes() {
199 // Calculate the size of the data
200 int sizeOfData = sizeof(int32_t); // Number of Update KV's
201 for (KeyValue *kv : keyValueUpdateSet) {
202 sizeOfData += kv->getSize();
205 // Data handlers and storage
206 Array<char> *dataArray = new Array<char>(sizeOfData);
207 ByteBuffer *bbEncode = ByteBuffer_wrap(dataArray);
209 // Encode the size of the updates and guard sets
210 bbEncode->putInt(keyValueUpdateSet->size());
212 // Encode all the updates
213 for (KeyValue *kv : keyValueUpdateSet) {
214 kv->encode(bbEncode);
217 return bbEncode->array();
220 void Commit::setKVsMap(Hashtable<IoTString *, KeyValue *> *newKVs) {
221 keyValueUpdateSet->clear();
224 keyValueUpdateSet->addAll(newKVs->values());
225 liveKeys->addAll(newKVs->keySet());
230 Commit *Commit_merge(Commit *newer, Commit *older, int64_t newSequenceNumber) {
234 } else if (newer == NULL) {
238 Hashtable<IoTString *, KeyValue *> *kvSet = new Hashtable<IoTString *, KeyValue *>();
239 for (KeyValue *kv : older->getKeyValueUpdateSet()) {
240 kvSet->put(kv->getKey(), kv);
243 for (KeyValue *kv : newer->getKeyValueUpdateSet()) {
244 kvSet->put(kv->getKey(), kv);
247 int64_t transactionSequenceNumber = newer->getTransactionSequenceNumber();
249 if (transactionSequenceNumber == -1) {
250 transactionSequenceNumber = older->getTransactionSequenceNumber();
253 Commit *newCommit = new Commit(newSequenceNumber, newer->getMachineId(), transactionSequenceNumber);
255 newCommit->setKVsMap(kvSet);