Repository navigation
Expand file tree
/
Copy pathwire.go
More file actions
196 lines (175 loc) · 4.6 KB
/
Copy pathwire.go
File metadata and controls
196 lines (175 loc) · 4.6 KB
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
package dbsync
import (
"webtyp.com/changelog"
"webtyp.com/model"
)
// Routes under DefaultPrefix, their query keys and the JSON keys of their answers.
const (
routeRegister = "/register"
routeHead = "/head"
routeSnapshot = "/snapshot"
routePull = "/pull"
routeRow = "/row"
routeEvents = "/events"
queryTable = "table"
queryAfter = "after"
queryLimit = "limit"
queryCursor = "cursor"
queryID = "id"
keyReplica = "replica"
keyEpoch = "epoch"
keyHead = "head"
keyCursor = "cursor"
keyRows = "rows"
keyNext = "next"
keyChanges = "changes"
keyTable = "table"
keyRowID = "row_id"
keyOp = "op"
keyRow = "row"
channelHead = "head"
opUpsert = string(changelog.OpUpsert)
opDelete = string(changelog.OpDelete)
)
// replicaWire answers /register.
type replicaWire struct{ replica int64 }
func (w *replicaWire) IsNil() bool { return w == nil }
func (w *replicaWire) EncodeFields(o model.FieldWriter) { o.Int(keyReplica, w.replica) }
func (w *replicaWire) DecodeFields(r model.FieldReader) {
if v, ok := r.Int(keyReplica); ok {
w.replica = v
}
}
// headWire answers /head.
type headWire struct {
epoch string
head int64
}
func (w *headWire) IsNil() bool { return w == nil }
func (w *headWire) EncodeFields(o model.FieldWriter) {
o.String(keyEpoch, w.epoch)
o.Int(keyHead, w.head)
}
func (w *headWire) DecodeFields(r model.FieldReader) {
if v, ok := r.String(keyEpoch); ok {
w.epoch = v
}
if v, ok := r.Int(keyHead); ok {
w.head = v
}
}
// snapshotOut is what /snapshot writes.
type snapshotOut struct {
rows []model.Encodable
next string
}
func (w *snapshotOut) IsNil() bool { return w == nil }
func (w *snapshotOut) EncodeFields(o model.FieldWriter) {
a := o.Array(keyRows, len(w.rows))
for _, r := range w.rows {
a.Object(r)
}
a.Close()
o.String(keyNext, w.next)
}
// snapshotIn is what the client reads from /snapshot: rows of one known table.
type snapshotIn struct {
factory func() model.Model
rows []model.Model
next string
}
func (w *snapshotIn) IsNil() bool { return w == nil }
func (w *snapshotIn) DecodeFields(r model.FieldReader) {
if a, ok := r.Array(keyRows); ok {
for i := 0; i < a.Len(); i++ {
m := w.factory()
if a.Object(i, m) {
w.rows = append(w.rows, m)
}
}
}
if v, ok := r.String(keyNext); ok {
w.next = v
}
}
// changeWire is one entry of /pull. row is nil for a delete, and for a table the client does not keep.
type changeWire struct {
tables []func() model.Model // client side: picks the model of table
table string
rowID string
op string
row model.Model // client: the decoded row
send model.Encodable // server: the row as sent (possibly fewer columns)
}
func (w *changeWire) IsNil() bool { return w == nil }
func (w *changeWire) EncodeFields(o model.FieldWriter) {
o.String(keyTable, w.table)
o.String(keyRowID, w.rowID)
o.String(keyOp, w.op)
if w.send != nil {
o.Object(keyRow, w.send)
}
}
func (w *changeWire) DecodeFields(r model.FieldReader) {
w.table, _ = r.String(keyTable)
w.rowID, _ = r.String(keyRowID)
w.op, _ = r.String(keyOp)
if w.op != opUpsert {
return
}
if f := factoryOf(w.tables, w.table); f != nil {
m := f()
if r.Object(keyRow, m) {
w.row = m
}
}
}
// pullWire answers /pull.
type pullWire struct {
tables []func() model.Model
epoch string
head int64
cursor int64
changes []*changeWire
}
func (w *pullWire) IsNil() bool { return w == nil }
func (w *pullWire) EncodeFields(o model.FieldWriter) {
o.String(keyEpoch, w.epoch)
o.Int(keyHead, w.head)
o.Int(keyCursor, w.cursor)
a := o.Array(keyChanges, len(w.changes))
for _, c := range w.changes {
a.Object(c)
}
a.Close()
}
func (w *pullWire) DecodeFields(r model.FieldReader) {
w.epoch, _ = r.String(keyEpoch)
w.head, _ = r.Int(keyHead)
w.cursor, _ = r.Int(keyCursor)
if a, ok := r.Array(keyChanges); ok {
for i := 0; i < a.Len(); i++ {
c := &changeWire{tables: w.tables}
if a.Object(i, c) {
w.changes = append(w.changes, c)
}
}
}
}
// rowWire answers /row. send is the row as sent (server); row receives it (client).
type rowWire struct {
row model.Model
send model.Encodable
}
func (w *rowWire) IsNil() bool { return w == nil }
func (w *rowWire) EncodeFields(o model.FieldWriter) { o.Object(keyRow, w.send) }
func (w *rowWire) DecodeFields(r model.FieldReader) { r.Object(keyRow, w.row) }
// factoryOf returns the factory of tables whose model is named table, or nil.
func factoryOf(tables []func() model.Model, table string) func() model.Model {
for _, f := range tables {
if f().ModelName() == table {
return f
}
}
return nil
}