-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmessageV2.go
326 lines (259 loc) · 7.65 KB
/
messageV2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package pglogrepl
import (
"encoding/binary"
"time"
)
// MessageDecoderV2 decodes message from V2 protocol into struct.
type MessageDecoderV2 interface {
MessageDecoder
DecodeV2(src []byte, inStream bool) error
}
// StreamStartMessageV2 is a stream start message.
type StreamStartMessageV2 struct {
baseMessage
Xid uint32
// A value of 1 indicates this is the first stream segment for this XID, 0 for any other stream segment
FirstSegment uint8
}
// DecodeV2 decodes to message from V2 src.
func (m *StreamStartMessageV2) DecodeV2(src []byte, _ bool) (err error) {
if len(src) < 5 {
return m.lengthError("StreamStartMessageV2", 5, len(src))
}
var low, used int
m.Xid, used = m.decodeUint32(src)
low += used
m.FirstSegment = src[low]
m.SetType(MessageTypeStreamStart)
return nil
}
// StreamStopMessageV2 is a stream stop message.
type StreamStopMessageV2 struct {
baseMessage
}
// DecodeV2 decodes to message from V2 src.
func (m *StreamStopMessageV2) DecodeV2(_ []byte, _ bool) (err error) {
// stream stop has no data.
m.SetType(MessageTypeStreamStop)
return nil
}
// StreamCommitMessageV2 is a stream commit message.
type StreamCommitMessageV2 struct {
baseMessage
Xid uint32
Flags uint8 // currently unused
CommitLSN LSN
TransactionEndLSN LSN
CommitTime time.Time
}
// DecodeV2 decodes to message from V2 src.
func (m *StreamCommitMessageV2) DecodeV2(src []byte, _ bool) (err error) {
if len(src) < 29 {
return m.lengthError("StreamCommitMessageV2", 29, len(src))
}
var low, used int
m.Xid, used = m.decodeUint32(src)
low += used
m.Flags = src[low]
low += 1
m.CommitLSN, used = m.decodeLSN(src[low:])
low += used
m.TransactionEndLSN, used = m.decodeLSN(src[low:])
low += used
m.CommitTime, _ = m.decodeTime(src[low:])
m.SetType(MessageTypeStreamCommit)
return nil
}
// StreamAbortMessageV2 is a stream abort message.
type StreamAbortMessageV2 struct {
baseMessage
Xid uint32
// Xid of the subtransaction (will be same as xid of the transaction for top-level transactions).
SubXid uint32
}
// DecodeV2 decodes to message from V2 src.
func (m *StreamAbortMessageV2) DecodeV2(src []byte, _ bool) (err error) {
if len(src) < 8 {
return m.lengthError("StreamAbortMessageV2", 8, len(src))
}
var low, used int
m.Xid, used = m.decodeUint32(src)
low += used
m.SubXid, _ = m.decodeUint32(src[low:])
m.SetType(MessageTypeStreamAbort)
return nil
}
// ParseV2 parse a logical replication message from protocol version #2
// it accepts a slice of bytes read from PG and inStream parameter
// inStream must be true when StreamStartMessageV2 has been read
// it must be false after StreamStopMessageV2 has been read
func ParseV2(data []byte, inStream bool) (m Message, err error) {
var decoder MessageDecoder
msgType := MessageType(data[0])
switch msgType {
case MessageTypeStreamStart:
decoder = new(StreamStartMessageV2)
case MessageTypeStreamStop:
decoder = new(StreamStopMessageV2)
case MessageTypeStreamCommit:
decoder = new(StreamCommitMessageV2)
case MessageTypeStreamAbort:
decoder = new(StreamAbortMessageV2)
case MessageTypeMessage:
decoder = new(LogicalDecodingMessageV2)
case MessageTypeRelation:
decoder = new(RelationMessageV2)
case MessageTypeType:
decoder = new(TypeMessageV2)
case MessageTypeInsert:
decoder = new(InsertMessageV2)
case MessageTypeUpdate:
decoder = new(UpdateMessageV2)
case MessageTypeDelete:
decoder = new(DeleteMessageV2)
case MessageTypeTruncate:
decoder = new(TruncateMessageV2)
default:
decoder = getCommonDecoder(msgType)
}
if decoder == nil {
return nil, errMsgNotSupported
}
if v2, ok := decoder.(MessageDecoderV2); ok {
if err = v2.DecodeV2(data[1:], inStream); err != nil {
return nil, err
}
} else if err = decoder.Decode(data[1:]); err != nil {
return nil, err
}
return decoder.(Message), nil
}
// InStreamMessageV2WithXid is a V2 protocol message
type InStreamMessageV2WithXid struct {
// Xid of the transaction (only present for streamed transactions).
Xid uint32
}
// LogicalDecodingMessageV2 is a logical decoding message.
type LogicalDecodingMessageV2 struct {
LogicalDecodingMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *LogicalDecodingMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.LogicalDecodingMessage.Decode(src)
}
if len(src) < 18 {
return m.lengthError("LogicalDecodingMessage", 18, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.LogicalDecodingMessage.Decode(src)
}
// RelationMessageV2 is a relation message.
type RelationMessageV2 struct {
RelationMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *RelationMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.RelationMessage.Decode(src)
}
if len(src) < 11 {
return m.lengthError("RelationMessageV2", 11, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.RelationMessage.Decode(src)
}
// TypeMessageV2 is a type message.
type TypeMessageV2 struct {
TypeMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *TypeMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.TypeMessage.Decode(src)
}
if len(src) < 10 {
return m.lengthError("TypeMessageV2", 10, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.TypeMessage.Decode(src)
}
// InsertMessageV2 is an insert message.
type InsertMessageV2 struct {
InsertMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *InsertMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.InsertMessage.Decode(src)
}
if len(src) < 12 {
return m.lengthError("InsertMessageV2", 12, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.InsertMessage.Decode(src)
}
// UpdateMessageV2 is an update message.
type UpdateMessageV2 struct {
UpdateMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *UpdateMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.UpdateMessage.Decode(src)
}
if len(src) < 10 {
return m.lengthError("UpdateMessageV2", 10, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.UpdateMessage.Decode(src)
}
// DeleteMessageV2 is a delete message.
type DeleteMessageV2 struct {
DeleteMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *DeleteMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.DeleteMessage.Decode(src)
}
if len(src) < 8 {
return m.lengthError("DeleteMessageV2", 8, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.DeleteMessage.Decode(src)
}
// TruncateMessageV2 is a truncate message.
type TruncateMessageV2 struct {
TruncateMessage
InStreamMessageV2WithXid
}
// DecodeV2 decodes to message from V2 src.
func (m *TruncateMessageV2) DecodeV2(src []byte, inStream bool) (err error) {
if !inStream {
return m.TruncateMessage.Decode(src)
}
if len(src) < 13 {
return m.lengthError("TruncateMessageV2", 13, len(src))
}
src = readXidAndAdvance(src, &m.InStreamMessageV2WithXid, inStream)
return m.TruncateMessage.Decode(src)
}
func readXidAndAdvance(src []byte, mXid *InStreamMessageV2WithXid, inStream bool) []byte {
var xid uint32
var used int
if inStream {
xid, used = decodeUint32(src)
mXid.Xid = xid
}
return src[used:]
}
func decodeUint32(src []byte) (uint32, int) {
return binary.BigEndian.Uint32(src), 4
}