Skip to content

Commit 604079d

Browse files
authored
Merge pull request shyiko#168 from stevenczp/master
add XA_PREPARE Deserializer
2 parents e264621 + 616a0c8 commit 604079d

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
import java.util.Arrays;
19+
20+
/**
21+
* @author <a href="https://github.com/stevenczp">Steven Cheng</a>
22+
*/
23+
public class XAPrepareEventData implements EventData {
24+
private boolean onePhase;
25+
private int formatID;
26+
private int gtridLength;
27+
private int bqualLength;
28+
private byte[] data;
29+
private String gtrid;
30+
private String bqual;
31+
32+
public boolean isOnePhase() {
33+
return onePhase;
34+
}
35+
36+
public void setOnePhase(boolean onePhase) {
37+
this.onePhase = onePhase;
38+
}
39+
40+
public int getFormatID() {
41+
return formatID;
42+
}
43+
44+
public void setFormatID(int formatID) {
45+
this.formatID = formatID;
46+
}
47+
48+
public int getGtridLength() {
49+
return gtridLength;
50+
}
51+
52+
public void setGtridLength(int gtridLength) {
53+
this.gtridLength = gtridLength;
54+
}
55+
56+
public int getBqualLength() {
57+
return bqualLength;
58+
}
59+
60+
public void setBqualLength(int bqualLength) {
61+
this.bqualLength = bqualLength;
62+
}
63+
64+
public byte[] getData() {
65+
return data;
66+
}
67+
68+
public void setData(byte[] data) {
69+
this.data = data;
70+
gtrid = new String(data, 0, gtridLength);
71+
bqual = new String(data, gtridLength, bqualLength);
72+
}
73+
74+
public String getGtrid() {
75+
return gtrid;
76+
}
77+
78+
public String getBqual() {
79+
return bqual;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
final StringBuilder sb = new StringBuilder("XAPrepareEventData{");
85+
sb.append("onePhase=").append(onePhase);
86+
sb.append(", formatID=").append(formatID);
87+
sb.append(", gtridLength=").append(gtridLength);
88+
sb.append(", bqualLength=").append(bqualLength);
89+
sb.append(", data=").append(Arrays.toString(data));
90+
sb.append(", gtrid='").append(gtrid).append('\'');
91+
sb.append(", bqual='").append(bqual).append('\'');
92+
sb.append('}');
93+
return sb.toString();
94+
}
95+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ private void registerDefaultEventDataDeserializers() {
115115
new GtidEventDataDeserializer());
116116
eventDataDeserializers.put(EventType.PREVIOUS_GTIDS,
117117
new PreviousGtidSetDeserializer());
118+
eventDataDeserializers.put(EventType.XA_PREPARE,
119+
new XAPrepareEventDataDeserializer());
118120
}
119121

120122
public void setEventDataDeserializer(EventType eventType, EventDataDeserializer eventDataDeserializer) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 com.github.shyiko.mysql.binlog.event.XAPrepareEventData;
19+
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* https://github.com/mysql/mysql-server/blob/5.7/libbinlogevents/src/control_events.cpp#L590
25+
* <p>
26+
* onePhase : boolean, 1byte
27+
* formatID : int, 4byte
28+
* gtridLength : int, 4byte
29+
* bqualLength : int, 4byte
30+
* data : String, gtrid + bqual, (gtridLength + bqualLength)byte
31+
* <p>
32+
* @author <a href="https://github.com/stevenczp">Steven Cheng</a>
33+
*/
34+
public class XAPrepareEventDataDeserializer implements EventDataDeserializer<XAPrepareEventData> {
35+
@Override
36+
public XAPrepareEventData deserialize(ByteArrayInputStream inputStream) throws IOException {
37+
XAPrepareEventData xaPrepareEventData = new XAPrepareEventData();
38+
xaPrepareEventData.setOnePhase(inputStream.read() == 0x00 ? false : true);
39+
xaPrepareEventData.setFormatID(inputStream.readInteger(4));
40+
xaPrepareEventData.setGtridLength(inputStream.readInteger(4));
41+
xaPrepareEventData.setBqualLength(inputStream.readInteger(4));
42+
xaPrepareEventData.setData(inputStream.read(
43+
xaPrepareEventData.getGtridLength() + xaPrepareEventData.getBqualLength()));
44+
45+
return xaPrepareEventData;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 com.github.shyiko.mysql.binlog.event.XAPrepareEventData;
19+
import com.github.shyiko.mysql.binlog.io.ByteArrayInputStream;
20+
import org.testng.annotations.Test;
21+
22+
import java.io.IOException;
23+
24+
import static org.testng.Assert.assertEquals;
25+
26+
/**
27+
* @author <a href="https://github.com/stevenczp">Steven Cheng</a>
28+
*/
29+
public class XAPrepareEventDataDeserializerTest {
30+
private static final byte[] DATA =
31+
{0, 123, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 103, 116, 114, 105, 100, 98, 113, 117, 97, 108};
32+
33+
private static final boolean ONEPHASE = false;
34+
private static final int FORMATID = 123;
35+
private static final String GTRID = "gtrid";
36+
private static final String BQUAL = "bqual";
37+
38+
@Test
39+
public void deserialize() throws IOException {
40+
XAPrepareEventDataDeserializer deserializer = new XAPrepareEventDataDeserializer();
41+
XAPrepareEventData xaPrepareEventData =
42+
deserializer.deserialize(new ByteArrayInputStream(DATA));
43+
44+
assertEquals(ONEPHASE, xaPrepareEventData.isOnePhase());
45+
assertEquals(FORMATID, xaPrepareEventData.getFormatID());
46+
assertEquals(GTRID, xaPrepareEventData.getGtrid());
47+
assertEquals(BQUAL, xaPrepareEventData.getBqual());
48+
}
49+
}

0 commit comments

Comments
 (0)