@@ -140,6 +140,12 @@ def __sub_interval(self, itvl):
140
140
self .intervals = new
141
141
142
142
def __contains__ (self , other ):
143
+ """Test if other is contained within self.
144
+ First we compare sid they must be equals.
145
+
146
+ Then we search if intervals from other are contained within
147
+ self intervals.
148
+ """
143
149
if other .sid != self .sid :
144
150
return False
145
151
@@ -205,6 +211,31 @@ def encoded_length(self):
205
211
len (self .intervals ))
206
212
207
213
def encode (self ):
214
+ """Encode a Gtid in binary
215
+ Bytes are in **little endian**.
216
+
217
+ Format:
218
+
219
+ - sid will be uncoded as hex-binary without the dashes as a [u8; 16]
220
+ - size of the interval list as a u64
221
+ - all the interval as a pair: (start: u64, end: u64).
222
+
223
+ ## Diagram
224
+
225
+ ```txt
226
+ Alligned on u64 bit.
227
+ +-+-+-+-+-+-+-+-+-+-+
228
+ | sid [16;u8] |
229
+ | |
230
+ +-+-+-+-+-+-+-+-+-+-+
231
+ | intervals_len u64 |
232
+ +-+-+-+-+-+-+-+-+-+-+
233
+ |start u64 <-+
234
+ - - - - - - - - - - - + Repeated
235
+ |stop u64 <-+ interval_len times
236
+ - - - - - - - - - - -
237
+ ```
238
+ """
208
239
buffer = b''
209
240
# sid
210
241
buffer += binascii .unhexlify (self .sid .replace ('-' , '' ))
@@ -221,6 +252,10 @@ def encode(self):
221
252
222
253
@classmethod
223
254
def decode (cls , payload ):
255
+ """Decode from binary a Gtid
256
+
257
+ :param BytesIO payload to decode
258
+ """
224
259
assert isinstance (payload , BytesIO ), \
225
260
'payload is expected to be a BytesIO'
226
261
sid = b''
@@ -308,6 +343,7 @@ def _to_gtid(element):
308
343
self .gtids = [Gtid (x .strip (' \n ' )) for x in gtid_set .split (',' )]
309
344
310
345
def merge_gtid (self , gtid ):
346
+ """Insert a Gtid in current GtidSet."""
311
347
new_gtids = []
312
348
for existing in self .gtids :
313
349
if existing .sid == gtid .sid :
@@ -320,6 +356,8 @@ def merge_gtid(self, gtid):
320
356
321
357
def __contains__ (self , other ):
322
358
"""
359
+ Test if self contains other, could be a GtidSet or a Gtid.
360
+
323
361
Raises:
324
362
- NotImplementedError other is not a GtidSet neither a Gtid,
325
363
please convert it first to one of them
@@ -366,13 +404,33 @@ def encoded_length(self):
366
404
sum (x .encoded_length for x in self .gtids ))
367
405
368
406
def encoded (self ):
407
+ """Encode a GtidSet in binary
408
+ Bytes are in **little endian**.
409
+
410
+ - `n_sid`: u64 is the number of Gtid to read
411
+ - `Gtid`: `n_sid` * `Gtid_encoded_size` times.
412
+ See`Gtid.encode` documentation for details.
413
+
414
+ ```txt
415
+ Alligned on u64 bit.
416
+ +-+-+-+-+-+-+-+-+-+-+
417
+ | n_gtid u64 |
418
+ +-+-+-+-+-+-+-+-+-+-+
419
+ | Gtid | - Repeated n_gtid times
420
+ - - - - - - - - - - -
421
+ ```
422
+ """
369
423
return b'' + (struct .pack ('<Q' , len (self .gtids )) +
370
424
b'' .join (x .encode () for x in self .gtids ))
371
425
372
426
encode = encoded
373
427
374
428
@classmethod
375
429
def decode (cls , payload ):
430
+ """Decode a GtidSet from binary.
431
+
432
+ :param BytesIO payload to decode
433
+ """
376
434
assert isinstance (payload , BytesIO ), \
377
435
'payload is expected to be a BytesIO'
378
436
(n_sid ,) = struct .unpack ('<Q' , payload .read (8 ))
0 commit comments