Skip to content

Commit dc2dec2

Browse files
authored
Merge pull request #159 from jolivares/master
Added PREVIOUS_GTIDS deserializer
2 parents 383ccd0 + 99983f2 commit dc2dec2

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2013 Stanley Shyiko
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.shyiko.mysql.binlog.event;
17+
18+
/**
19+
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
20+
*/
21+
public class PreviousGtidSetEventData implements EventData {
22+
private static final long serialVersionUID = 1L;
23+
24+
private final String gtidSet;
25+
26+
public PreviousGtidSetEventData(String gtidSet) {
27+
this.gtidSet = gtidSet;
28+
}
29+
30+
public String getGtidSet() {
31+
return gtidSet;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "PreviousGtidSetEventData {gtidSet='" + gtidSet + "'}";
37+
}
38+
39+
}

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ private void registerDefaultEventDataDeserializers() {
113113
new RowsQueryEventDataDeserializer());
114114
eventDataDeserializers.put(EventType.GTID,
115115
new GtidEventDataDeserializer());
116+
eventDataDeserializers.put(EventType.PREVIOUS_GTIDS,
117+
new PreviousGtidSetDeserializer());
116118
}
117119

118120
public void setEventDataDeserializer(EventType eventType, EventDataDeserializer eventDataDeserializer) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2013 Stanley Shyiko
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.shyiko.mysql.binlog.event.deserialization;
17+
18+
import static java.lang.String.format;
19+
20+
import java.io.IOException;
21+
22+
import com.github.shyiko.mysql.binlog.event.PreviousGtidSetEventData;
23+
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
24+
25+
/**
26+
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
27+
*/
28+
public class PreviousGtidSetDeserializer
29+
implements
30+
EventDataDeserializer<PreviousGtidSetEventData> {
31+
32+
@Override
33+
public PreviousGtidSetEventData deserialize(
34+
ByteArrayInputStream inputStream) throws IOException {
35+
int nUuids = inputStream.readInteger(8);
36+
String[] gtids = new String[nUuids];
37+
for (int i = 0; i < nUuids; i++) {
38+
String uuid = formatUUID(inputStream.read(16));
39+
40+
int nIntervals = inputStream.readInteger(8);
41+
String[] intervals = new String[nIntervals];
42+
for (int j = 0; j < nIntervals; j++) {
43+
long start = inputStream.readLong(8);
44+
long end = inputStream.readLong(8);
45+
intervals[j] = start + "-" + (end - 1);
46+
}
47+
48+
gtids[i] = format("%s:%s", uuid, join(intervals, ":"));
49+
}
50+
return new PreviousGtidSetEventData(join(gtids, ","));
51+
}
52+
53+
private String formatUUID(byte[] bytes) {
54+
return format("%s-%s-%s-%s-%s", byteArrayToHex(bytes, 0, 4),
55+
byteArrayToHex(bytes, 4, 2), byteArrayToHex(bytes, 6, 2),
56+
byteArrayToHex(bytes, 8, 2), byteArrayToHex(bytes, 10, 6));
57+
58+
}
59+
60+
private static String byteArrayToHex(byte[] a, int offset, int len) {
61+
StringBuilder sb = new StringBuilder();
62+
for (int idx = offset; idx < (offset + len) && idx < a.length; idx++) {
63+
sb.append(format("%02x", a[idx] & 0xff));
64+
}
65+
return sb.toString();
66+
}
67+
68+
private static String join(String[] values, String separator) {
69+
StringBuilder sb = new StringBuilder();
70+
for (int i = 0; i < values.length; i++) {
71+
if (i > 0) {
72+
sb.append(separator);
73+
}
74+
sb.append(values[i]);
75+
}
76+
return sb.toString();
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2013 Stanley Shyiko
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.shyiko.mysql.binlog.event.deserialization;
17+
18+
import static junit.framework.Assert.assertEquals;
19+
20+
import java.io.IOException;
21+
22+
import org.junit.Test;
23+
24+
import com.github.shyiko.mysql.binlog.event.PreviousGtidSetEventData;
25+
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
26+
27+
/**
28+
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
29+
*/
30+
public class PreviousGtidSetDeserializerTest {
31+
32+
private static final byte[] DATA = {2, 0, 0, 0, 0, 0, 0, 0, -75, -51, 22,
33+
36, 95, 48, 17, -28, -76, -23, 16, 81, 114, 27, -46, 65, 1, 0, 0, 0,
34+
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -15, 15, 108, 0, 0, 0, 0, 0,
35+
-69, 66, 29, 38, 95, 48, 17, -28, -76, -23, -40, -99, 103, 43, 46,
36+
-8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -47, 97, 119, 0,
37+
0, 0, 0, 0};
38+
39+
private static final String GTID_SET = "b5cd1624-5f30-11e4-b4e9-1051721bd241:1-7081968," +
40+
"bb421d26-5f30-11e4-b4e9-d89d672b2ef8:1-7823824";
41+
42+
@Test
43+
public void deserialize() throws IOException {
44+
PreviousGtidSetDeserializer deserializer = new PreviousGtidSetDeserializer();
45+
PreviousGtidSetEventData previousGtidSetData = deserializer
46+
.deserialize(new ByteArrayInputStream(DATA));
47+
48+
assertEquals(GTID_SET, previousGtidSetData.getGtidSet());
49+
}
50+
51+
}

0 commit comments

Comments
 (0)