forked from timsolov/pgoutput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
346 lines (311 loc) · 8.44 KB
/
parse.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package pgoutput
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/jackc/pgx/pgtype"
)
type decoder struct {
order binary.ByteOrder
buf *bytes.Buffer
}
func (d *decoder) bool() bool {
x := d.buf.Next(1)[0]
return x != 0
}
func (d *decoder) uint8() uint8 {
x := d.buf.Next(1)[0]
return x
}
func (d *decoder) uint16() uint16 {
x := d.order.Uint16(d.buf.Next(2))
return x
}
func (d *decoder) string() string {
s, err := d.buf.ReadBytes(0)
if err != nil {
// TODO: Return an error
panic(err)
}
return string(s[:len(s)-1])
}
func (d *decoder) uint32() uint32 {
x := d.order.Uint32(d.buf.Next(4))
return x
}
func (d *decoder) uint64() uint64 {
x := d.order.Uint64(d.buf.Next(8))
return x
}
func (d *decoder) int8() int8 { return int8(d.uint8()) }
func (d *decoder) int16() int16 { return int16(d.uint16()) }
func (d *decoder) int32() int32 { return int32(d.uint32()) }
func (d *decoder) int64() int64 { return int64(d.uint64()) }
func (d *decoder) timestamp() time.Time {
micro := int(d.uint64())
ts := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
return ts.Add(time.Duration(micro) * time.Microsecond)
}
func (d *decoder) rowinfo(char byte) bool {
if d.buf.Next(1)[0] == char {
return true
} else {
d.buf.UnreadByte()
return false
}
}
func (d *decoder) tupledata() []Tuple {
size := int(d.uint16())
data := make([]Tuple, size)
for i := 0; i < size; i++ {
switch d.buf.Next(1)[0] {
case 'n':
case 'u':
case 't':
vsize := int(d.order.Uint32(d.buf.Next(4)))
data[i] = Tuple{Flag: 't', Value: d.buf.Next(vsize)}
}
}
return data
}
func (d *decoder) columns() []Column {
size := int(d.uint16())
data := make([]Column, size)
for i := 0; i < size; i++ {
data[i] = Column{
Key: d.bool(),
Name: d.string(),
Type: d.uint32(),
Mode: d.uint32(),
}
}
return data
}
type Begin struct {
// The final LSN of the transaction.
LSN uint64
// Commit timestamp of the transaction. The value is in number of
// microseconds since PostgreSQL epoch (2000-01-01).
Timestamp time.Time
// Xid of the transaction.
XID int32
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Commit struct {
Flags uint8
// The final LSN of the transaction.
LSN uint64
// The final LSN of the transaction.
TransactionLSN uint64
Timestamp time.Time
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Relation struct {
// ID of the relation.
ID uint32
// Namespace (empty string for pg_catalog).
Namespace string
Name string
Replica uint8
Columns []Column
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
func (r Relation) IsEmpty() bool {
return r.ID == 0 && r.Name == "" && r.Replica == 0 && len(r.Columns) == 0
}
type Type struct {
// ID of the data type
ID uint32
Namespace string
Name string
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Insert struct {
/// ID of the relation corresponding to the ID in the relation message.
RelationID uint32
// Identifies the following TupleData message as a new tuple.
New bool
Row []Tuple
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Update struct {
/// ID of the relation corresponding to the ID in the relation message.
RelationID uint32
// Identifies the following TupleData message as a new tuple.
Old bool
Key bool
New bool
OldRow []Tuple
Row []Tuple
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Delete struct {
/// ID of the relation corresponding to the ID in the relation message.
RelationID uint32
// Identifies the following TupleData message as a new tuple.
Key bool // TODO
Old bool // TODO
Row []Tuple
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Truncate struct {
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type Origin struct {
LSN uint64
Name string
// position where this message starts in WAL
walStart uint64
// position where this message ends in WAL
walEnd uint64
}
type DecoderValue interface {
pgtype.TextDecoder
pgtype.Value
}
type Column struct {
Key bool
Name string
Type uint32
Mode uint32
}
type Tuple struct {
Flag int8
Value []byte
}
type Message interface {
SetWalStart(offset uint64)
WalStart() uint64
SetWalEnd(offset uint64)
WalEnd() uint64
}
func (m *Begin) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Relation) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Update) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Insert) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Delete) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Commit) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Origin) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Type) SetWalStart(offset uint64) { m.walStart = offset }
func (m *Truncate) SetWalStart(offset uint64) { m.walStart = offset }
func (m Begin) WalStart() uint64 { return m.walStart }
func (m Relation) WalStart() uint64 { return m.walStart }
func (m Update) WalStart() uint64 { return m.walStart }
func (m Insert) WalStart() uint64 { return m.walStart }
func (m Delete) WalStart() uint64 { return m.walStart }
func (m Commit) WalStart() uint64 { return m.walStart }
func (m Origin) WalStart() uint64 { return m.walStart }
func (m Type) WalStart() uint64 { return m.walStart }
func (m Truncate) WalStart() uint64 { return m.walStart }
func (m *Begin) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Relation) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Update) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Insert) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Delete) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Commit) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Origin) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Type) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m *Truncate) SetWalEnd(offset uint64) { m.walEnd = offset }
func (m Begin) WalEnd() uint64 { return m.walEnd }
func (m Relation) WalEnd() uint64 { return m.walEnd }
func (m Update) WalEnd() uint64 { return m.walEnd }
func (m Insert) WalEnd() uint64 { return m.walEnd }
func (m Delete) WalEnd() uint64 { return m.walEnd }
func (m Commit) WalEnd() uint64 { return m.walEnd }
func (m Origin) WalEnd() uint64 { return m.walEnd }
func (m Type) WalEnd() uint64 { return m.walEnd }
func (m Truncate) WalEnd() uint64 { return m.walEnd }
// Parse a logical replication message.
// See https://www.postgresql.org/docs/current/static/protocol-logicalrep-message-formats.html
func Parse(src []byte) (Message, error) {
msgType := src[0]
d := &decoder{order: binary.BigEndian, buf: bytes.NewBuffer(src[1:])}
switch msgType {
case 'B':
b := &Begin{}
b.LSN = d.uint64()
b.Timestamp = d.timestamp()
b.XID = d.int32()
return b, nil
case 'C':
c := &Commit{}
c.Flags = d.uint8()
c.LSN = d.uint64()
c.TransactionLSN = d.uint64()
c.Timestamp = d.timestamp()
return c, nil
case 'O':
o := &Origin{}
o.LSN = d.uint64()
o.Name = d.string()
return o, nil
case 'R':
r := &Relation{}
r.ID = d.uint32()
r.Namespace = d.string()
r.Name = d.string()
r.Replica = d.uint8()
r.Columns = d.columns()
return r, nil
case 'Y':
t := &Type{}
t.ID = d.uint32()
t.Namespace = d.string()
t.Name = d.string()
return t, nil
case 'I':
i := &Insert{}
i.RelationID = d.uint32()
i.New = d.uint8() > 0
i.Row = d.tupledata()
return i, nil
case 'U':
u := &Update{}
u.RelationID = d.uint32()
u.Key = d.rowinfo('K')
u.Old = d.rowinfo('O')
if u.Key || u.Old {
u.OldRow = d.tupledata()
}
u.New = d.uint8() > 0
u.Row = d.tupledata()
return u, nil
case 'D':
dl := &Delete{}
dl.RelationID = d.uint32()
dl.Key = d.rowinfo('K')
dl.Old = d.rowinfo('O')
dl.Row = d.tupledata()
return dl, nil
case 'T':
tr := &Truncate{}
return tr, nil
default:
return nil, fmt.Errorf("Unknown message type for %s (%d)", []byte{msgType}, msgType)
}
}