|
| 1 | +package replication |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/klauspost/compress/zstd" |
| 9 | + |
| 10 | + . "github.com/go-mysql-org/go-mysql/mysql" |
| 11 | +) |
| 12 | + |
| 13 | +// On The Wire: Field Types |
| 14 | +const ( |
| 15 | + OTW_PAYLOAD_HEADER_END_MARK = iota |
| 16 | + OTW_PAYLOAD_SIZE_FIELD |
| 17 | + OTW_PAYLOAD_COMPRESSION_TYPE_FIELD |
| 18 | + OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD |
| 19 | +) |
| 20 | + |
| 21 | +// Compression Types |
| 22 | +const ( |
| 23 | + ZSTD = 0 |
| 24 | + NONE = 255 |
| 25 | +) |
| 26 | + |
| 27 | +func fieldTypeName(ft uint64) string { |
| 28 | + switch ft { |
| 29 | + case OTW_PAYLOAD_HEADER_END_MARK: |
| 30 | + return "HeaderEndMark" |
| 31 | + case OTW_PAYLOAD_SIZE_FIELD: |
| 32 | + return "SizeField" |
| 33 | + case OTW_PAYLOAD_COMPRESSION_TYPE_FIELD: |
| 34 | + return "CompressionType" |
| 35 | + case OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD: |
| 36 | + return "UncompressedSize" |
| 37 | + default: |
| 38 | + return "Unknown" |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type TransactionPayloadEvent struct { |
| 43 | + Data []byte |
| 44 | + Size uint64 |
| 45 | + UncompressedSize uint64 |
| 46 | + CompressionType uint64 |
| 47 | + Payload []byte |
| 48 | +} |
| 49 | + |
| 50 | +func (e *TransactionPayloadEvent) compressionType() string { |
| 51 | + switch e.CompressionType { |
| 52 | + case ZSTD: |
| 53 | + return "ZSTD" |
| 54 | + case NONE: |
| 55 | + return "NONE" |
| 56 | + default: |
| 57 | + return "Unknown" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (e *TransactionPayloadEvent) Dump(w io.Writer) { |
| 62 | + fmt.Fprintf(w, "Payload Size: %d\n", e.Size) |
| 63 | + fmt.Fprintf(w, "Payload Uncompressed Size: %d\n", e.UncompressedSize) |
| 64 | + fmt.Fprintf(w, "Payload CompressionType: %s\n", e.compressionType()) |
| 65 | + fmt.Fprintf(w, "Payload Body: \n%s", hex.Dump(e.Payload)) |
| 66 | + // fmt.Fprintf(w, "Transaction Payload Event data: \n%s", hex.Dump(e.Data)) |
| 67 | + |
| 68 | + decoder, _ := zstd.NewReader(nil, zstd.WithDecoderConcurrency(0)) |
| 69 | + payloadUncompressed, _ := decoder.DecodeAll(e.Payload, nil) |
| 70 | + fmt.Fprintf(w, "Decompressed: \n%s", hex.Dump(payloadUncompressed)) |
| 71 | + fmt.Fprintln(w) |
| 72 | +} |
| 73 | + |
| 74 | +func (e *TransactionPayloadEvent) Decode(data []byte) error { |
| 75 | + e.Data = data |
| 76 | + offset := uint64(0) |
| 77 | + |
| 78 | + for { |
| 79 | + fieldType := FixedLengthInt(data[offset : offset+1]) |
| 80 | + offset++ |
| 81 | + |
| 82 | + if fieldType == OTW_PAYLOAD_HEADER_END_MARK { |
| 83 | + e.Payload = data[offset:] |
| 84 | + break |
| 85 | + } else { |
| 86 | + fieldLength := FixedLengthInt(data[offset : offset+1]) |
| 87 | + offset++ |
| 88 | + |
| 89 | + switch fieldType { |
| 90 | + case OTW_PAYLOAD_SIZE_FIELD: |
| 91 | + e.Size = FixedLengthInt(data[offset : offset+fieldLength]) |
| 92 | + case OTW_PAYLOAD_COMPRESSION_TYPE_FIELD: |
| 93 | + e.CompressionType = FixedLengthInt(data[offset : offset+fieldLength]) |
| 94 | + case OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD: |
| 95 | + e.UncompressedSize = FixedLengthInt(data[offset : offset+fieldLength]) |
| 96 | + } |
| 97 | + |
| 98 | + offset += fieldLength |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments