/** * @param gtids the string representation of the GTIDs. */ public GtidSet(String gtids) { gtids = gtids.replaceAll("\n", "").replaceAll("\r", ""); new com.github.shyiko.mysql.binlog.GtidSet(gtids).getUUIDSets().forEach(uuidSet -> { uuidSetsByServerId.put(uuidSet.getUUID(), new UUIDSet(uuidSet)); }); StringBuilder sb = new StringBuilder(); uuidSetsByServerId.values().forEach(uuidSet -> { if (sb.length() != 0) sb.append(','); sb.append(uuidSet.toString()); }); }
@Override public String toString() { List<String> gtids = new ArrayList<String>(); for (UUIDSet uuidSet : map.values()) { gtids.add(uuidSet.getUUID() + ":" + join(uuidSet.intervals, ":")); } return join(gtids, ","); }
/** * Add or replace the UUIDSet * @param uuidSet UUIDSet to be added * @return the old {@link UUIDSet} for the server given in uuidSet param, * or {@code null} if there are no UUIDSet for the given server. */ public UUIDSet putUUIDSet(UUIDSet uuidSet) { return map.put(uuidSet.getUUID(), uuidSet); }
@Override public byte[] toByteArray() throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); buffer.writeInteger(CommandType.BINLOG_DUMP_GTID.ordinal(), 1); buffer.writeInteger(0, 2); // flag buffer.writeLong(this.serverId, 4); buffer.writeInteger(this.binlogFilename.length(), 4); buffer.writeString(this.binlogFilename); buffer.writeLong(this.binlogPosition, 8); Collection<GtidSet.UUIDSet> uuidSets = gtidSet.getUUIDSets(); int dataSize = 8 /* number of uuidSets */; for (GtidSet.UUIDSet uuidSet : uuidSets) { dataSize += 16 /* uuid */ + 8 /* number of intervals */ + uuidSet.getIntervals().size() /* number of intervals */ * 16 /* start-end */; } buffer.writeInteger(dataSize, 4); buffer.writeLong(uuidSets.size(), 8); for (GtidSet.UUIDSet uuidSet : uuidSets) { buffer.write(hexToByteArray(uuidSet.getUUID().replace("-", ""))); Collection<GtidSet.Interval> intervals = uuidSet.getIntervals(); buffer.writeLong(intervals.size(), 8); for (GtidSet.Interval interval : intervals) { buffer.writeLong(interval.getStart(), 8); buffer.writeLong(interval.getEnd() + 1 /* right-open */, 8); } } return buffer.toByteArray(); }
/** * Determine if the GTIDs represented by this object are contained completely within the supplied set of GTIDs. * Note that if two {@link GtidSet}s are equal, then they both are subsets of the other. * @param other the other set of GTIDs; may be null * @return {@code true} if all of the GTIDs in this set are equal to or completely contained within the supplied * set of GTIDs, or {@code false} otherwise */ public boolean isContainedWithin(GtidSet other) { if (other == null) { return false; } if (this == other) { return true; } if (this.equals(other)) { return true; } for (UUIDSet uuidSet : map.values()) { UUIDSet thatSet = other.getUUIDSet(uuidSet.getUUID()); if (!uuidSet.isContainedWithin(thatSet)) { return false; } } return true; }
protected UUIDSet(com.github.shyiko.mysql.binlog.GtidSet.UUIDSet uuidSet) { this.uuid = uuidSet.getUUID(); uuidSet.getIntervals().forEach(interval -> { intervals.add(new Interval(interval.getStart(), interval.getEnd())); }); Collections.sort(this.intervals); if (this.intervals.size() > 1) { // Collapse adjacent intervals ... for (int i = intervals.size() - 1; i != 0; --i) { Interval before = this.intervals.get(i - 1); Interval after = this.intervals.get(i); if ((before.getEnd() + 1) == after.getStart()) { this.intervals.set(i - 1, new Interval(before.getStart(), after.getEnd())); this.intervals.remove(i); } } } } protected UUIDSet(String uuid, Interval interval) {