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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
from __future__ import annotations
import binascii
import re
import struct
from io import BytesIO
class Gtid:
@staticmethod
def overlap(i1, i2):
return i1[0] < i2[1] and i1[1] > i2[0]
@staticmethod
def contains(i1, i2):
return i2[0] >= i1[0] and i2[1] <= i1[1]
@staticmethod
def parse_interval(interval):
"""
We parse a human-generated string here. So our end value b
is incremented to conform to the internal representation format.
"""
m = re.search("^([0-9]+)(?:-([0-9]+))?$", interval)
if not m:
raise ValueError("GTID format is incorrect: %r" % (interval,))
a = int(m.group(1))
b = int(m.group(2) or a)
return a, b + 1
@staticmethod
def parse(gtid: str):
m = re.search(
"^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$",
gtid,
)
if not m:
raise ValueError("GTID format is incorrect: %r" % (gtid,))
sid = m.group(1)
intervals = m.group(2)
intervals_parsed = [Gtid.parse_interval(x) for x in intervals.split(":")[1:]]
return sid, intervals_parsed
def _add_interval(self, itvl):
"""
Use the internal representation format and add it
to our intervals, merging if required.
"""
new = []
if itvl[0] > itvl[1]:
raise Exception("Malformed interval %s" % (itvl,))
if any(self.overlap(x, itvl) for x in self.intervals):
raise Exception("Overlapping interval %s" % (itvl,))
# Merge: arrange interval to fit existing set
for existing in sorted(self.intervals):
if itvl[0] == existing[1]:
itvl = (existing[0], itvl[1])
continue
if itvl[1] == existing[0]:
itvl = (itvl[0], existing[1])
continue
new.append(existing)
self.intervals = sorted(new + [itvl])
def __sub_interval(self, itvl):
"""Using the internal representation, remove an interval"""
new = []
if itvl[0] > itvl[1]:
raise Exception("Malformed interval %s" % (itvl,))
if not any(self.overlap(x, itvl) for x in self.intervals):
# No raise
return
# Merge: arrange existing set around interval
for existing in sorted(self.intervals):
if self.overlap(existing, itvl):
if existing[0] < itvl[0]:
new.append((existing[0], itvl[0]))
if existing[1] > itvl[1]:
new.append((itvl[1], existing[1]))
else:
new.append(existing)
self.intervals = new
def __contains__(self, other):
if other.sid != self.sid:
return False
return all(
any(self.contains(me, them) for me in self.intervals) for them in other.intervals
)
def __init__(self, gtid: str, sid=None, intervals=None):
if intervals is None:
intervals = []
if sid:
intervals = intervals
else:
sid, intervals = Gtid.parse(gtid)
self.sid = sid
self.intervals = []
for itvl in intervals:
self._add_interval(itvl)
def __add__(self, other):
"""Include the transactions of this gtid. Raise if the
attempted merge has different SID"""
if self.sid != other.sid:
raise Exception("Attempt to merge different SID%s != %s" % (self.sid, other.sid))
result = Gtid(str(self))
for itvl in other.intervals:
result._add_interval(itvl)
return result
def __sub__(self, other):
"""Remove intervals. Do not raise, if different SID simply
ignore"""
result = Gtid(str(self))
if self.sid != other.sid:
return result
for itvl in other.intervals:
result.__sub_interval(itvl)
return result
def __str__(self):
"""We represent the human value here - a single number
for one transaction, or a closed interval (decrementing b)"""
return "%s:%s" % (
self.sid,
":".join(
("%d-%d" % (x[0], x[1] - 1)) if x[0] + 1 != x[1] else str(x[0])
for x in self.intervals
),
)
def __repr__(self):
return '<Gtid "%s">' % self
@property
def encoded_length(self):
return (
16
+ 8 # sid
+ 2 # n_intervals
* 8 # stop/start
* len(self.intervals) # stop/start mark encoded as int64
)
def encode(self):
buffer = b""
# sid
buffer += binascii.unhexlify(self.sid.replace("-", ""))
# n_intervals
buffer += struct.pack("<Q", len(self.intervals))
for interval in self.intervals:
# Start position
buffer += struct.pack("<Q", interval[0])
# Stop position
buffer += struct.pack("<Q", interval[1])
return buffer
@classmethod
def decode(cls, payload: BytesIO):
sid = b""
sid = sid + binascii.hexlify(payload.read(4))
sid = sid + b"-"
sid = sid + binascii.hexlify(payload.read(2))
sid = sid + b"-"
sid = sid + binascii.hexlify(payload.read(2))
sid = sid + b"-"
sid = sid + binascii.hexlify(payload.read(2))
sid = sid + b"-"
sid = sid + binascii.hexlify(payload.read(6))
(n_intervals,) = struct.unpack("<Q", payload.read(8))
intervals = []
for i in range(0, n_intervals):
start, end = struct.unpack("<QQ", payload.read(16))
intervals.append((start, end - 1))
return cls(
"%s:%s"
% (
sid.decode("ascii"),
":".join(["%d-%d" % x for x in intervals]),
)
)
def __eq__(self, other):
if other.sid != self.sid:
return False
return self.intervals == other.intervals
def __lt__(self, other):
if other.sid != self.sid:
return self.sid < other.sid
return self.intervals < other.intervals
def __le__(self, other):
if other.sid != self.sid:
return self.sid <= other.sid
return self.intervals <= other.intervals
def __gt__(self, other):
if other.sid != self.sid:
return self.sid > other.sid
return self.intervals > other.intervals
def __ge__(self, other):
if other.sid != self.sid:
return self.sid >= other.sid
return self.intervals >= other.intervals
class GtidSet:
def __init__(self, gtid_set: set[Gtid]):
self._gtid_set = gtid_set
def merge_gtid(self, gtid: Gtid):
new_gtid_set = set()
for existing in self._gtid_set:
if existing.sid == gtid.sid:
new_gtid_set.add(existing + gtid)
else:
new_gtid_set.add(existing)
if gtid.sid not in (x.sid for x in new_gtid_set):
new_gtid_set.add(gtid)
self._gtid_set = new_gtid_set
def __contains__(self, other: Gtid | GtidSet):
if isinstance(other, GtidSet):
return all(other_gtid in self._gtid_set for other_gtid in other._gtid_set)
if isinstance(other, Gtid):
return any(other in x for x in self._gtid_set)
raise NotImplementedError
def __add__(self, other: Gtid | GtidSet):
if isinstance(other, Gtid):
new = GtidSet(self._gtid_set)
new.merge_gtid(other)
return new
if isinstance(other, GtidSet):
new = GtidSet(self._gtid_set)
for gtid in other._gtid_set:
new.merge_gtid(gtid)
return new
raise NotImplementedError
def __str__(self):
return ",".join(str(x) for x in self._gtid_set)
def __repr__(self):
return "<GtidSet %r>" % self._gtid_set
@property
def encoded_length(self):
return 8 + sum(x.encoded_length for x in self._gtid_set) # n_sids
def encoded(self):
return b"" + (
struct.pack("<Q", len(self._gtid_set)) + b"".join(x.encode() for x in self._gtid_set)
)
encode = encoded
@classmethod
def decode(cls, payload: BytesIO):
(n_sid,) = struct.unpack("<Q", payload.read(8))
return cls(set(Gtid.decode(payload) for _ in range(0, n_sid)))
def __eq__(self, other: GtidSet): # type: ignore[override]
return self._gtid_set == other._gtid_set
|