-
Notifications
You must be signed in to change notification settings - Fork 173
/
rdb_json.go
185 lines (162 loc) · 4.1 KB
/
rdb_json.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
package encoding
import (
"encoding/json"
"fmt"
"time"
"github.com/juju/errors"
"github.com/moiot/gravity/pkg/core"
)
type rdbJsonSerde struct {
timezone time.Location
}
// version 0.1
type MysqlJson_0_1_header struct {
Version string `json:"version"`
Database string `json:"database"`
Table string `json:"table"`
Type string `json:"type"`
Timestamp int64 `json:"ts"`
TimeZone string `json:"time_zone"`
Host string `json:"host"`
}
type DmlStringMsg_0_1 struct {
MysqlJson_0_1_header
Data map[string]*string `json:"data"`
Old map[string]*string `json:"old"`
Pks map[string]*string `json:"pks,omitempty"`
}
type DDLMsg_0_1 struct {
MysqlJson_0_1_header
SQL string `json:"sql"`
}
const timeLayout = "2006-01-02 15:04:05.999999999"
func formatVersion01Data(d map[string]interface{}) map[string]*string {
ret := make(map[string]*string)
for k, v := range d {
if v == nil {
ret[k] = nil
continue
}
switch v := v.(type) {
case string:
ret[k] = new(string)
*ret[k] = v
case nil:
ret[k] = nil
case time.Time:
ret[k] = new(string)
*ret[k] = v.Format(timeLayout)
default:
ret[k] = new(string)
*ret[k] = fmt.Sprint(v)
}
}
return ret
}
func serializeVersion01JsonMsg(msg *core.Msg) ([]byte, error) {
header := MysqlJson_0_1_header{
Version: Version01,
Database: msg.Database,
Table: msg.Table,
Timestamp: msg.Timestamp.Unix(),
TimeZone: "Asia/Shanghai",
Host: msg.Host,
}
if msg.DmlMsg != nil {
dmlStringMsg := &DmlStringMsg_0_1{
MysqlJson_0_1_header: header,
Data: formatVersion01Data(msg.DmlMsg.Data),
Old: formatVersion01Data(msg.DmlMsg.Old),
Pks: formatVersion01Data(msg.DmlMsg.Pks),
}
switch msg.DmlMsg.Operation {
case core.Insert:
dmlStringMsg.MysqlJson_0_1_header.Type = "insert"
case core.Update:
dmlStringMsg.MysqlJson_0_1_header.Type = "update"
case core.Delete:
dmlStringMsg.MysqlJson_0_1_header.Type = "delete"
}
return json.Marshal(dmlStringMsg)
}
if msg.DdlMsg != nil {
header.Type = "ddl"
return json.Marshal(DDLMsg_0_1{
MysqlJson_0_1_header: header,
SQL: msg.DdlMsg.Statement,
})
}
return nil, errors.Errorf("invalid msg")
}
// version 2.0
type MysqlJson_2_0_header struct {
Version string `json:"version"`
Database string `json:"database"`
Table string `json:"table"`
Type string `json:"type"`
}
type DmlJson_2_0 struct {
MysqlJson_2_0_header
Data map[string]interface{}
Old map[string]interface{}
Pks map[string]interface{}
}
type DDLJson_2_0 struct {
MysqlJson_2_0_header
SQL string `json:"sql"`
}
func formatVersion20Data(d map[string]interface{}) map[string]interface{} {
for k, v := range d {
switch v := v.(type) {
case time.Time:
d[k] = v.Format(time.RFC3339Nano)
}
}
return d
}
func serializeVersion20JsonMsg(msg *core.Msg) ([]byte, error) {
header := MysqlJson_2_0_header{
Version: Version20Alpha,
Database: msg.Database,
Table: msg.Table,
}
if msg.DmlMsg != nil {
dmlMsg := &DmlJson_2_0{
MysqlJson_2_0_header: header,
Data: formatVersion20Data(msg.DmlMsg.Data),
Old: formatVersion20Data(msg.DmlMsg.Old),
Pks: formatVersion20Data(msg.DmlMsg.Pks),
}
switch msg.DmlMsg.Operation {
case core.Insert:
dmlMsg.MysqlJson_2_0_header.Type = "insert"
case core.Update:
dmlMsg.MysqlJson_2_0_header.Type = "update"
case core.Delete:
dmlMsg.MysqlJson_2_0_header.Type = "delete"
}
return json.Marshal(dmlMsg)
}
if msg.DdlMsg != nil {
header.Type = "ddl"
ddlMsg := DDLJson_2_0{
MysqlJson_2_0_header: header,
SQL: msg.DdlMsg.Statement,
}
return json.Marshal(ddlMsg)
}
return nil, errors.Errorf("invalid msg")
}
func (s *rdbJsonSerde) Serialize(msg *core.Msg, version string) ([]byte, error) {
switch version {
case Version01:
return serializeVersion01JsonMsg(msg)
case Version20Alpha:
return serializeVersion20JsonMsg(msg)
default:
return nil, errors.Errorf("invalid version")
}
}
func (*rdbJsonSerde) Deserialize(b []byte) (core.Msg, error) {
panic("implement me")
}