forked from mkabilov/pg2ch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
201 lines (162 loc) · 4.53 KB
/
decoder.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
package decoder
// based on https://github.com/kyleconroy/pgoutput
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/mkabilov/pg2ch/pkg/message"
"github.com/mkabilov/pg2ch/pkg/utils/dbtypes"
)
type decoder struct {
order binary.ByteOrder
buf *bytes.Buffer
}
const (
truncateCascadeBit = 1
truncateRestartIdentityBit = 2
InsertMsgType = 'I'
DeleteMsgType = 'D'
UpdateMsgType = 'U'
TruncateMsgType = 'T'
BeginMsgType = 'B'
CommitMsgType = 'C'
TypeMsgType = 'Y'
OriginMsgType = 'O'
RelationMsgType = 'R'
)
func (d *decoder) bool() bool { return d.buf.Next(1)[0] != 0 }
func (d *decoder) uint8() uint8 { return d.buf.Next(1)[0] }
func (d *decoder) uint16() uint16 { return d.order.Uint16(d.buf.Next(2)) }
func (d *decoder) uint32() uint32 { return d.order.Uint32(d.buf.Next(4)) }
func (d *decoder) uint64() uint64 { return d.order.Uint64(d.buf.Next(8)) }
func (d *decoder) oid() dbtypes.OID { return dbtypes.OID(d.uint32()) }
func (d *decoder) lsn() dbtypes.LSN { return dbtypes.LSN(d.uint64()) }
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) string() string {
s, err := d.buf.ReadBytes(0)
if err != nil {
panic(err)
}
return string(s[:len(s)-1])
}
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
}
_ = d.buf.UnreadByte()
return false
}
func (d *decoder) tupleData() []*message.Tuple {
size := int(d.uint16())
data := make([]*message.Tuple, size)
for i := 0; i < size; i++ {
switch d.buf.Next(1)[0] {
case 't':
vsize := int(d.order.Uint32(d.buf.Next(4)))
data[i] = &message.Tuple{Kind: message.TupleText, Value: d.buf.Next(vsize)}
case 'n':
data[i] = &message.Tuple{Kind: message.TupleNull, Value: []byte{}}
case 'u':
data[i] = &message.Tuple{Kind: message.TupleUnchanged, Value: []byte{}}
}
}
return data
}
func (d *decoder) columns() []message.Column {
size := int(d.uint16())
data := make([]message.Column, size)
for i := 0; i < size; i++ {
data[i] = message.Column{}
data[i].IsKey = d.bool()
data[i].Name = d.string()
data[i].TypeOID = d.oid()
data[i].Mode = d.int32()
}
return data
}
// Parse a logical replication message.
// See https://www.postgresql.org/docs/current/static/protocol-logicalrep-message-formats.html
func Parse(src []byte) (message.Message, error) {
msgType := src[0]
d := &decoder{order: binary.BigEndian, buf: bytes.NewBuffer(src[1:])}
switch msgType {
case BeginMsgType:
m := &message.Begin{}
m.FinalLSN = d.lsn()
m.Timestamp = d.timestamp()
m.XID = d.int32()
return m, nil
case CommitMsgType:
m := &message.Commit{}
m.Flags = d.uint8()
m.LSN = d.lsn()
m.TransactionLSN = d.lsn()
m.Timestamp = d.timestamp()
return m, nil
case OriginMsgType:
m := &message.Origin{}
m.LSN = d.lsn()
m.Name = d.string()
return m, nil
case RelationMsgType:
m := &message.Relation{}
m.OID = d.oid()
m.Namespace = d.string()
m.Name = d.string()
m.ReplicaIdentity = message.ReplicaIdentity(d.uint8())
m.Columns = d.columns()
return m, nil
case TypeMsgType:
m := &message.Type{}
m.OID = d.oid()
m.Namespace = d.string()
m.Name = d.string()
return m, nil
case InsertMsgType:
m := &message.Insert{}
m.RelationOID = d.oid()
m.IsNew = d.uint8() == 'N'
m.NewRow = d.tupleData()
return m, nil
case UpdateMsgType:
m := &message.Update{}
m.RelationOID = d.oid()
m.IsKey = d.rowInfo('K')
m.IsOld = d.rowInfo('O')
if m.IsKey || m.IsOld {
m.OldRow = d.tupleData()
}
m.IsNew = d.uint8() == 'N'
m.NewRow = d.tupleData()
return m, nil
case DeleteMsgType:
m := &message.Delete{}
m.RelationOID = d.oid()
m.IsKey = d.rowInfo('K')
m.IsOld = d.rowInfo('O')
m.OldRow = d.tupleData()
return m, nil
case TruncateMsgType:
m := &message.Truncate{}
relationsCnt := int(d.uint32())
options := d.uint8()
m.Cascade = options&truncateCascadeBit == 1
m.RestartIdentity = options&truncateRestartIdentityBit == 1
m.RelationOIDs = make([]dbtypes.OID, relationsCnt)
for i := 0; i < relationsCnt; i++ {
m.RelationOIDs[i] = d.oid()
}
return m, nil
default:
return nil, fmt.Errorf("unknown message type for %s (%d)", []byte{msgType}, msgType)
}
}