true-level-replication 0.10.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
replication/objects.py ADDED
@@ -0,0 +1,246 @@
1
+ from uuid import uuid4
2
+ from deepdiff import Delta
3
+
4
+ try:
5
+ import _pickle as pickle
6
+ except ImportError:
7
+ import pickle
8
+
9
+
10
+ def object_classes(type_id):
11
+ """ """
12
+ return _TYPE_MAP.get(type_id)
13
+
14
+
15
+ class ReplicationObject(object):
16
+ __slots__ = ["_sha"]
17
+
18
+ def _serialize(self):
19
+ """Convert the object into chunks of bytes"""
20
+ raise NotImplementedError()
21
+
22
+ def _deserialize(self, chunks):
23
+ """Load chunks of to object"""
24
+ raise NotImplementedError()
25
+
26
+ @staticmethod
27
+ def from_raw_chunks(chunks):
28
+ """Reconstruct a replication object from chunks"""
29
+ type_num = pickle.loads(chunks[0])
30
+ obj = object_classes(type_num)()
31
+ obj._deserialize(chunks)
32
+
33
+ return obj
34
+
35
+ def as_raw_chunks(self):
36
+ return self._serialize()
37
+
38
+
39
+ class Commit(ReplicationObject):
40
+ __slots__ = ["node_id", "delta", "deps"]
41
+
42
+ type_num = 1
43
+
44
+ def __init__(self):
45
+ self.node_id = None
46
+ self.deps = None
47
+ self.delta = None
48
+
49
+ def _serialize(self):
50
+ chunks = []
51
+ chunks.append(pickle.dumps(self.type_num, protocol=4))
52
+ chunks.append(self.node_id.encode())
53
+ chunks.append(pickle.dumps(self.deps, protocol=4))
54
+ chunks.append(self.delta.dumps())
55
+ return chunks
56
+
57
+ def _deserialize(self, chunks):
58
+ self.node_id = chunks[1].decode()
59
+ self.deps = pickle.loads(chunks[2])
60
+ self.delta = Delta(chunks[3])
61
+
62
+
63
+ class Node(ReplicationObject):
64
+ __slots__ = [
65
+ "uuid", # uuid used as key (string)
66
+ "data", # dcc data ref (DCC type)
67
+ "instance", # raw data (json)
68
+ "dependencies", # dependencies array (string)
69
+ "owner", # Data owner (string)
70
+ "last_commit", # Serialized local buffer (bytes)
71
+ "state", # Node state (int)
72
+ "sender", # Node sender origin (client uuid)
73
+ ]
74
+
75
+ type_num = 2
76
+
77
+ def __init__(
78
+ self,
79
+ owner=None,
80
+ instance=None,
81
+ uuid=None,
82
+ data=None,
83
+ sender=None,
84
+ dependencies=[],
85
+ state=-1,
86
+ ):
87
+
88
+ self.uuid = uuid if uuid else str(uuid4())
89
+ self.owner = owner
90
+ self.last_commit = None
91
+ self.state = state
92
+ self.data = {}
93
+ self.instance = instance
94
+ self.data = data
95
+ self.dependencies = dependencies
96
+ self.sender = sender
97
+
98
+ def _serialize(self):
99
+ chunks = []
100
+ chunks.append(pickle.dumps(self.type_num, protocol=4))
101
+ chunks.append(self.uuid.encode())
102
+ chunks.append(self.owner.encode())
103
+ chunks.append(pickle.dumps(self.dependencies, protocol=4))
104
+ chunks.append(pickle.dumps(self.data, protocol=4))
105
+
106
+ return chunks
107
+
108
+ def _deserialize(self, chunks):
109
+ self.uuid = chunks[1].decode()
110
+ self.owner = chunks[2].decode()
111
+ self.dependencies = pickle.loads(chunks[3])
112
+ self.data = pickle.loads(chunks[4])
113
+
114
+ def patch(self, commit: Commit):
115
+ self.data = self.data + commit.delta
116
+ self.dependencies = commit.deps
117
+
118
+ def add_dependency(self, dependency):
119
+ if not self.dependencies:
120
+ self.dependencies = []
121
+ if dependency not in self.dependencies:
122
+ self.dependencies.append(dependency)
123
+
124
+ def __repr__(self):
125
+ return f" {self.uuid} - owner: {self.owner} - deps: {self.dependencies}"
126
+
127
+
128
+ class Command(ReplicationObject):
129
+ type_num = 3
130
+
131
+ def __init__(self, owner=None, data=None):
132
+ self.owner = owner
133
+ self.data = data
134
+ self.str_type = type(self).__name__
135
+
136
+ def _serialize(self):
137
+ chunks = []
138
+ chunks.append(pickle.dumps(self.type_num, protocol=4))
139
+ chunks.append(self.owner.encode())
140
+ chunks.append(pickle.dumps(self.data, protocol=4))
141
+
142
+ return chunks
143
+
144
+ def _deserialize(self, chunks):
145
+ self.owner = chunks[1].decode()
146
+ self.data = pickle.loads(chunks[2])
147
+
148
+ def execute(self, graph):
149
+ raise NotImplementedError()
150
+
151
+
152
+ class Delete(Command):
153
+ type_num = 4
154
+
155
+ def execute(self, graph):
156
+ assert self.data
157
+
158
+ if graph and self.data in graph.keys():
159
+ # Clean all reference to this node
160
+ for key, value in graph.items():
161
+ if value.dependencies and self.data in value.dependencies:
162
+ value.dependencies.remove(self.data)
163
+ # Remove the node itself
164
+ del graph[self.data]
165
+
166
+
167
+ class Right(Command):
168
+ type_num = 5
169
+
170
+ def execute(self, graph):
171
+ assert self.data
172
+ nodes_ids = self.data.get("uuid")
173
+ new_owner = self.data.get("owner")
174
+
175
+ for node_id in nodes_ids:
176
+ node = graph.get(node_id)
177
+ if node:
178
+ node.owner = new_owner
179
+
180
+
181
+ class Config(Command):
182
+ type_num = 6
183
+ pass
184
+
185
+
186
+ class Snapshot(Command):
187
+ type_num = 7
188
+ pass
189
+
190
+
191
+ class ServerSnapshot(Command):
192
+ type_num = 8
193
+ pass
194
+
195
+
196
+ class Auth(Command):
197
+ type_num = 9
198
+ pass
199
+
200
+
201
+ class Disconnect(Command):
202
+ type_num = 10
203
+ pass
204
+
205
+
206
+ class Kick(Command):
207
+ type_num = 11
208
+ pass
209
+
210
+
211
+ class UpdateClientsState(Command):
212
+ type_num = 12
213
+ pass
214
+
215
+
216
+ class UpdateUserMetadata(Command):
217
+ type_num = 13
218
+ pass
219
+
220
+
221
+ class RequestServerInfo(Command):
222
+ type_num = 14
223
+ pass
224
+
225
+
226
+ OBJECT_CLASSES = (
227
+ Commit,
228
+ Node,
229
+ Command,
230
+ UpdateUserMetadata,
231
+ UpdateClientsState,
232
+ Kick,
233
+ Disconnect,
234
+ Auth,
235
+ ServerSnapshot,
236
+ Snapshot,
237
+ Config,
238
+ Right,
239
+ Delete,
240
+ RequestServerInfo,
241
+ )
242
+
243
+ _TYPE_MAP = {}
244
+
245
+ for cls in OBJECT_CLASSES:
246
+ _TYPE_MAP[cls.type_num] = cls