-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIReplicationModel.h
More file actions
236 lines (220 loc) · 8.55 KB
/
IReplicationModel.h
File metadata and controls
236 lines (220 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**The MIT License (MIT)
Copyright (c) 2018 by AleksanderSergeevich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef REPLICATIONMODEL_H
#define REPLICATIONMODEL_H
#pragma once
#include <QtCore>
#include "objectmanager.h"
/*This file contains description of tree data structure used for representation of replication model and model itself*/
template<class T = Idata_wraper>
class ReplicaDescriptor {
public:
virtual ~ReplicaDescriptor() {
reset();
}
virtual void reset() {
if(!replica_ptr.isNull()) replica_ptr.reset();
}
private:
QDateTime creation_time;
QString replica_id;
unsigned int version_number;
QSharedPointer<T> replica_ptr;
};
class LeafReplicaHolder {
public:
virtual ~LeafReplicaHolder() {
reset();
}
virtual void reset() {
if(replica_descriptors.isEmpty()) return;
foreach(ReplicaDescriptor<> item, replica_descriptors) {
item.reset();
}
replica_descriptors.clear();
}
bool is_contains_replica_version(const unsigned int& replica_version) {
if(!replica_descriptors.contains(replica_version)) return false;
return true;
}
bool get_replica_desc_ptr(const unsigned int& replica_version, QSharedPointer<ReplicaDescriptor<> >& rep_desc_ptr) {
if(!replica_descriptors.contains(replica_version)) return false;
rep_desc_ptr.reset(&replica_descriptors[replica_version]);
return true;
}
void set_replica_desc(const unsigned int replica_version, ReplicaDescriptor<>& replica_desc) {
replica_descriptors.insert(replica_version, replica_desc);
}
bool delete_replica_desc(const unsigned int replica_version) {
if(!replica_descriptors.contains(replica_version)) return false;
replica_descriptors[replica_version].reset();
replica_descriptors.remove(replica_version);
return true;
}
bool get_versions_list(QList<unsigned int>& versions_list) {
if(replica_descriptors.isEmpty()) return false;
if(!versions_list.isEmpty()) versions_list.clear();
versions_list.append(replica_descriptors.keys());
return true;
}
void set_node_holder_id(QString node_holder_id_) {
node_holder_id = node_holder_id_;
}
const QString get_node_holder_id() const {
return node_holder_id;
}
private:
QString node_holder_id;
QMap<unsigned int, ReplicaDescriptor<> > replica_descriptors;
};
struct SiblingNodeDesc {
virtual ~SiblingNodeDesc() {}
virtual void reset() {
node_holder_url.clear();
if(!available_versions.isEmpty()) available_versions.clear();
}
QString node_holder_id;
QUrl node_holder_url;
/*Available versions of replica and count of defined version replica in branch of replica tree*/
QMap<unsigned int, unsigned int> available_versions;
};
class InternalReplicaHolder : public LeafReplicaHolder {
public:
virtual ~InternalReplicaHolder() {
reset();
}
virtual void reset() {
LeafReplicaHolder::reset();
if(replica_child_nodes_desc.isEmpty()) return;
foreach(SiblingNodeDesc item, replica_child_nodes_desc) {
item.reset();
}
replica_child_nodes_desc.clear();
}
bool get_sibling_node_desc(const QString node_id, QSharedPointer<SiblingNodeDesc>& sibling_node_desc_ptr) {
if(!replica_child_nodes_desc.contains(node_id)) return false;
sibling_node_desc_ptr.reset(&replica_child_nodes_desc[node_id]);
return true;
}
void set_sibling_node_desc(const QString node_id, SiblingNodeDesc& sibling_node_desc) {
replica_child_nodes_desc.insert(node_id, sibling_node_desc);
}
bool delete_sibling_node_desc(const QString node_id) {
if(!replica_child_nodes_desc.contains(node_id)) return false;
replica_child_nodes_desc[node_id].reset();
replica_child_nodes_desc.remove(node_id);
return true;
}
bool get_child_nodes_id(QList<QString>& nodes_id_list) {
if(replica_child_nodes_desc.isEmpty()) return false;
if(!nodes_id_list.isEmpty()) nodes_id_list.clear();
nodes_id_list.append(replica_child_nodes_desc.keys());
return true;
}
bool is_branch_contains_rep_version(const unsigned int replica_version, QList<QString>& nodes_id_list) {
if(replica_child_nodes_desc.isEmpty()) return false;
if(!nodes_id_list.isEmpty()) nodes_id_list.clear();
QMapIterator<QString, SiblingNodeDesc> desc_iter(replica_child_nodes_desc);
while(desc_iter.hasNext()) {
desc_iter.next();
if(!desc_iter.value().available_versions.contains(replica_version)) continue;
nodes_id_list.append(desc_iter.key());
}
return true;
}
unsigned int count_rep_version(const unsigned int replica_version) {
unsigned int total_count = 0;
if(is_contains_replica_version(replica_version)) ++total_count;
if(replica_child_nodes_desc.isEmpty()) return total_count;
QMapIterator<QString, SiblingNodeDesc> desc_iter(replica_child_nodes_desc);
while(desc_iter.hasNext()) {
desc_iter.next();
if(!desc_iter.value().available_versions.contains(replica_version)) continue;
total_count += desc_iter.value().available_versions.value(replica_version);
}
return total_count;
}
private:
QMap<QString, SiblingNodeDesc> replica_child_nodes_desc;
};
struct RequestDesc {
enum RequestType {
ReadReplica,
UpdateReplica,
DeleteReplica
};
RequestType type;
QString req_node_id;
QUrl req_node_url;
unsigned int req_rep_version;
};
class PrimaryReplicaHolder : public InternalReplicaHolder {
public:
virtual ~PrimaryReplicaHolder() {
reset();
}
virtual void reset() {
InternalReplicaHolder::reset();
latest_replica_version = recent_deleted_replica_version = 0;
if(!request_queue.isEmpty()) request_queue.clear();
}
bool register_new_request(RequestDesc& new_req_desc) {
switch(new_req_desc.type) {
case RequestDesc::ReadReplica:
new_req_desc.req_rep_version = latest_replica_version;
request_queue.enqueue(new_req_desc);
break;
case RequestDesc::UpdateReplica:
++latest_replica_version;
new_req_desc.req_rep_version = latest_replica_version;
request_queue.enqueue(new_req_desc);
break;
case RequestDesc::DeleteReplica:
if(count_rep_version(new_req_desc.req_rep_version) == 0) return false;
request_queue.enqueue(new_req_desc);
break;
default:
return false;
}
return true;
}
bool retrieve_next_request(RequestDesc& next_req_desc) {
next_req_desc = request_queue.dequeue();
if(auto_delete && next_req_desc.type == RequestDesc::ReadReplica && next_req_desc.req_rep_version > recent_deleted_replica_version) {
RequestDesc new_req_desc;
new_req_desc.req_node_id = get_node_holder_id();
new_req_desc.req_rep_version = recent_deleted_replica_version;
request_queue.enqueue(new_req_desc);
++recent_deleted_replica_version;
}
return true;
}
bool has_next_request() const {
return request_queue.isEmpty() ? false : true;
}
void set_auto_delete(const bool auto_delete_) {
auto_delete = auto_delete_;
}
private:
unsigned int latest_replica_version;
unsigned int recent_deleted_replica_version;
bool auto_delete;
QQueue<RequestDesc> request_queue;
};
#endif // REPLICATIONMODEL_H