View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.db.protocol.mysql.packet.binlog;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import lombok.ToString;
23  import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
24  import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
25  
26  /**
27   * MySQL binlog event header.
28   *
29   * @see <a href="https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_replication_binlog_event.html#sect_protocol_replication_binlog_event_header">Binlog Event header</a>
30   */
31  @RequiredArgsConstructor
32  @Getter
33  @ToString
34  public final class MySQLBinlogEventHeader extends MySQLPacket {
35      
36      /**
37       * MySQL binlog event header length is 19 in binlog version 3 and 4.
38       */
39      public static final int MYSQL_BINLOG_EVENT_HEADER_LENGTH = 19;
40      
41      private final int timestamp;
42      
43      private final int eventType;
44      
45      private final int serverId;
46      
47      /**
48       * Size of the event (header, post-header, body).
49       */
50      private final int eventSize;
51      
52      private final int logPos;
53      
54      private final int flags;
55      
56      private final int checksumLength;
57      
58      public MySQLBinlogEventHeader(final MySQLPacketPayload payload, final int checksumLength) {
59          timestamp = payload.readInt4();
60          eventType = payload.readInt1();
61          serverId = payload.readInt4();
62          eventSize = payload.readInt4();
63          logPos = payload.readInt4();
64          flags = payload.readInt2();
65          this.checksumLength = checksumLength;
66      }
67      
68      @Override
69      protected void write(final MySQLPacketPayload payload) {
70          payload.writeInt4(timestamp);
71          payload.writeInt1(eventType);
72          payload.writeInt4(serverId);
73          payload.writeInt4(eventSize);
74          payload.writeInt4(logPos);
75          payload.writeInt2(flags);
76      }
77  }