Skip to content

Commit 7c141d3

Browse files
Merge pull request #392 from darnuria/doc/gtid
Documentation of Gtid/GtidSet
2 parents 9e337ee + 4b2d07e commit 7c141d3

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pymysqlreplication/gtid.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ def __sub_interval(self, itvl):
140140
self.intervals = new
141141

142142
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+
"""
143149
if other.sid != self.sid:
144150
return False
145151

@@ -205,6 +211,31 @@ def encoded_length(self):
205211
len(self.intervals))
206212

207213
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+
"""
208239
buffer = b''
209240
# sid
210241
buffer += binascii.unhexlify(self.sid.replace('-', ''))
@@ -221,6 +252,10 @@ def encode(self):
221252

222253
@classmethod
223254
def decode(cls, payload):
255+
"""Decode from binary a Gtid
256+
257+
:param BytesIO payload to decode
258+
"""
224259
assert isinstance(payload, BytesIO), \
225260
'payload is expected to be a BytesIO'
226261
sid = b''
@@ -308,6 +343,7 @@ def _to_gtid(element):
308343
self.gtids = [Gtid(x.strip(' \n')) for x in gtid_set.split(',')]
309344

310345
def merge_gtid(self, gtid):
346+
"""Insert a Gtid in current GtidSet."""
311347
new_gtids = []
312348
for existing in self.gtids:
313349
if existing.sid == gtid.sid:
@@ -320,6 +356,8 @@ def merge_gtid(self, gtid):
320356

321357
def __contains__(self, other):
322358
"""
359+
Test if self contains other, could be a GtidSet or a Gtid.
360+
323361
Raises:
324362
- NotImplementedError other is not a GtidSet neither a Gtid,
325363
please convert it first to one of them
@@ -366,13 +404,33 @@ def encoded_length(self):
366404
sum(x.encoded_length for x in self.gtids))
367405

368406
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+
"""
369423
return b'' + (struct.pack('<Q', len(self.gtids)) +
370424
b''.join(x.encode() for x in self.gtids))
371425

372426
encode = encoded
373427

374428
@classmethod
375429
def decode(cls, payload):
430+
"""Decode a GtidSet from binary.
431+
432+
:param BytesIO payload to decode
433+
"""
376434
assert isinstance(payload, BytesIO), \
377435
'payload is expected to be a BytesIO'
378436
(n_sid,) = struct.unpack('<Q', payload.read(8))

0 commit comments

Comments
 (0)