forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedQueryStateMachine.swift
419 lines (366 loc) · 17.4 KB
/
ExtendedQueryStateMachine.swift
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
struct ExtendedQueryStateMachine {
enum State {
case initialized(ExecuteExtendedQueryContext)
case parseDescribeBindExecuteSyncSent(ExecuteExtendedQueryContext)
case parseCompleteReceived(ExecuteExtendedQueryContext)
case parameterDescriptionReceived(ExecuteExtendedQueryContext)
case rowDescriptionReceived(ExecuteExtendedQueryContext, [PSQLBackendMessage.RowDescription.Column])
case noDataMessageReceived(ExecuteExtendedQueryContext)
/// A state that is used if a noData message was received before. If a row description was received `bufferingRows` is
/// used after receiving a `bindComplete` message
case bindCompleteReceived(ExecuteExtendedQueryContext)
case bufferingRows([PSQLBackendMessage.RowDescription.Column], CircularBuffer<[PSQLData]>, readOnEmpty: Bool)
case waitingForNextRow([PSQLBackendMessage.RowDescription.Column], CircularBuffer<[PSQLData]>, EventLoopPromise<StateMachineStreamNextResult>)
case commandComplete(commandTag: String)
case error(PSQLError)
case modifying
}
enum Action {
case sendParseDescribeBindExecuteSync(query: String, binds: [PSQLEncodable])
case sendBindExecuteSync(statementName: String, binds: [PSQLEncodable])
// --- general actions
case failQuery(ExecuteExtendedQueryContext, with: PSQLError)
case succeedQuery(ExecuteExtendedQueryContext, columns: [PSQLBackendMessage.RowDescription.Column])
case succeedQueryNoRowsComming(ExecuteExtendedQueryContext, commandTag: String)
// --- streaming actions
// actions if query has requested next row but we are waiting for backend
case forwardRow([PSQLData], to: EventLoopPromise<StateMachineStreamNextResult>)
case forwardCommandComplete(CircularBuffer<[PSQLData]>, commandTag: String, to: EventLoopPromise<StateMachineStreamNextResult>)
case forwardStreamError(PSQLError, to: EventLoopPromise<StateMachineStreamNextResult>)
// actions if query has not asked for next row but are pushing the final bytes to it
case forwardStreamErrorToCurrentQuery(PSQLError, read: Bool)
case forwardStreamCompletedToCurrentQuery(CircularBuffer<[PSQLData]>, commandTag: String, read: Bool)
case read
case wait
}
var state: State
init(queryContext: ExecuteExtendedQueryContext) {
self.state = .initialized(queryContext)
}
mutating func start() -> Action {
guard case .initialized(let queryContext) = self.state else {
preconditionFailure("Start should only be called, if the query has been initialized")
}
switch queryContext.query {
case .unnamed(let query):
return self.avoidingStateMachineCoW { state -> Action in
state = .parseDescribeBindExecuteSyncSent(queryContext)
return .sendParseDescribeBindExecuteSync(query: query, binds: queryContext.bind)
}
case .preparedStatement(let name, let rowDescription):
return self.avoidingStateMachineCoW { state -> Action in
switch rowDescription {
case .some(let rowDescription):
state = .rowDescriptionReceived(queryContext, rowDescription.columns)
case .none:
state = .noDataMessageReceived(queryContext)
}
return .sendBindExecuteSync(statementName: name, binds: queryContext.bind)
}
}
}
mutating func parseCompletedReceived() -> Action {
guard case .parseDescribeBindExecuteSyncSent(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.parseComplete))
}
return self.avoidingStateMachineCoW { state -> Action in
state = .parseCompleteReceived(queryContext)
return .wait
}
}
mutating func parameterDescriptionReceived(_ parameterDescription: PSQLBackendMessage.ParameterDescription) -> Action {
guard case .parseCompleteReceived(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.parameterDescription(parameterDescription)))
}
return self.avoidingStateMachineCoW { state -> Action in
state = .parameterDescriptionReceived(queryContext)
return .wait
}
}
mutating func noDataReceived() -> Action {
guard case .parameterDescriptionReceived(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.noData))
}
return self.avoidingStateMachineCoW { state -> Action in
state = .noDataMessageReceived(queryContext)
return .wait
}
}
mutating func rowDescriptionReceived(_ rowDescription: PSQLBackendMessage.RowDescription) -> Action {
guard case .parameterDescriptionReceived(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.rowDescription(rowDescription)))
}
return self.avoidingStateMachineCoW { state -> Action in
state = .rowDescriptionReceived(queryContext, rowDescription.columns)
return .wait
}
}
mutating func bindCompleteReceived() -> Action {
switch self.state {
case .rowDescriptionReceived(let context, let columns):
return self.avoidingStateMachineCoW { state -> Action in
state = .bufferingRows(columns, CircularBuffer(), readOnEmpty: false)
return .succeedQuery(context, columns: columns)
}
case .noDataMessageReceived(let queryContext):
return self.avoidingStateMachineCoW { state -> Action in
state = .bindCompleteReceived(queryContext)
return .wait
}
case .initialized,
.parseDescribeBindExecuteSyncSent,
.parseCompleteReceived,
.parameterDescriptionReceived,
.bindCompleteReceived,
.bufferingRows,
.waitingForNextRow,
.commandComplete,
.error:
return self.setAndFireError(.unexpectedBackendMessage(.bindComplete))
case .modifying:
preconditionFailure("Invalid state")
}
}
mutating func dataRowReceived(_ dataRow: PSQLBackendMessage.DataRow) -> Action {
switch self.state {
case .bufferingRows(let columns, var buffer, let readOnEmpty):
// When receiving a data row, we must ensure that the data row column count
// matches the previously received row description column count.
guard dataRow.columns.count == columns.count else {
return self.setAndFireError(.unexpectedBackendMessage(.dataRow(dataRow)))
}
return self.avoidingStateMachineCoW { state -> Action in
let row = dataRow.columns.enumerated().map { (index, buffer) in
PSQLData(bytes: buffer, dataType: columns[index].dataType)
}
buffer.append(row)
state = .bufferingRows(columns, buffer, readOnEmpty: readOnEmpty)
return .wait
}
case .waitingForNextRow(let columns, let buffer, let promise):
// When receiving a data row, we must ensure that the data row column count
// matches the previously received row description column count.
guard dataRow.columns.count == columns.count else {
return self.setAndFireError(.unexpectedBackendMessage(.dataRow(dataRow)))
}
return self.avoidingStateMachineCoW { state -> Action in
precondition(buffer.isEmpty, "Expected the buffer to be empty")
let row = dataRow.columns.enumerated().map { (index, buffer) in
PSQLData(bytes: buffer, dataType: columns[index].dataType)
}
state = .bufferingRows(columns, buffer, readOnEmpty: false)
return .forwardRow(row, to: promise)
}
case .initialized,
.parseDescribeBindExecuteSyncSent,
.parseCompleteReceived,
.parameterDescriptionReceived,
.noDataMessageReceived,
.rowDescriptionReceived,
.bindCompleteReceived,
.commandComplete,
.error:
return self.setAndFireError(.unexpectedBackendMessage(.dataRow(dataRow)))
case .modifying:
preconditionFailure("Invalid state")
}
}
mutating func commandCompletedReceived(_ commandTag: String) -> Action {
switch self.state {
case .bindCompleteReceived(let context):
return self.avoidingStateMachineCoW { state -> Action in
state = .commandComplete(commandTag: commandTag)
return .succeedQueryNoRowsComming(context, commandTag: commandTag)
}
case .bufferingRows(_, let buffer, let readOnEmpty):
return self.avoidingStateMachineCoW { state -> Action in
state = .commandComplete(commandTag: commandTag)
return .forwardStreamCompletedToCurrentQuery(buffer, commandTag: commandTag, read: readOnEmpty)
}
case .waitingForNextRow(_, let buffer, let promise):
return self.avoidingStateMachineCoW { state -> Action in
precondition(buffer.isEmpty, "Expected the buffer to be empty")
state = .commandComplete(commandTag: commandTag)
return .forwardCommandComplete(buffer, commandTag: commandTag, to: promise)
}
case .initialized,
.parseDescribeBindExecuteSyncSent,
.parseCompleteReceived,
.parameterDescriptionReceived,
.noDataMessageReceived,
.rowDescriptionReceived,
.commandComplete,
.error:
return self.setAndFireError(.unexpectedBackendMessage(.commandComplete(commandTag)))
case .modifying:
preconditionFailure("Invalid state")
}
}
mutating func emptyQueryResponseReceived() -> Action {
preconditionFailure("Unimplemented")
}
mutating func errorReceived(_ errorMessage: PSQLBackendMessage.ErrorResponse) -> Action {
let error = PSQLError.server(errorMessage)
switch self.state {
case .initialized:
return self.setAndFireError(.unexpectedBackendMessage(.error(errorMessage)))
case .parseDescribeBindExecuteSyncSent,
.parseCompleteReceived,
.parameterDescriptionReceived,
.bindCompleteReceived:
return self.setAndFireError(error)
case .rowDescriptionReceived, .noDataMessageReceived:
return self.setAndFireError(error)
case .bufferingRows:
return self.setAndFireError(error)
case .waitingForNextRow:
return self.setAndFireError(error)
case .commandComplete:
return self.setAndFireError(.unexpectedBackendMessage(.error(errorMessage)))
case .error:
preconditionFailure("""
This state must not be reached. If the query `.isComplete`, the
ConnectionStateMachine must not send any further events to the substate machine.
""")
case .modifying:
preconditionFailure("Invalid state")
}
}
mutating func noticeReceived(_ notice: PSQLBackendMessage.NoticeResponse) -> Action {
//self.queryObject.noticeReceived(notice)
return .wait
}
mutating func errorHappened(_ error: PSQLError) -> Action {
return self.setAndFireError(error)
}
// MARK: Customer Actions
mutating func consumeNextRow(promise: EventLoopPromise<StateMachineStreamNextResult>) -> Action {
switch self.state {
case .waitingForNextRow:
preconditionFailure("Too greedy. `consumeNextRow()` only needs to be called once.")
case .bufferingRows(let columns, var buffer, let readOnEmpty):
return self.avoidingStateMachineCoW { state -> Action in
guard let row = buffer.popFirst() else {
state = .waitingForNextRow(columns, buffer, promise)
return readOnEmpty ? .read : .wait
}
state = .bufferingRows(columns, buffer, readOnEmpty: readOnEmpty)
return .forwardRow(row, to: promise)
}
case .initialized,
.parseDescribeBindExecuteSyncSent,
.parseCompleteReceived,
.parameterDescriptionReceived,
.noDataMessageReceived,
.rowDescriptionReceived,
.bindCompleteReceived:
preconditionFailure("Requested to consume next row without anything going on.")
case .commandComplete, .error:
preconditionFailure("The stream is already closed or in a failure state; rows can not be consumed at this time.")
case .modifying:
preconditionFailure("Invalid state")
}
}
// MARK: Channel actions
mutating func readEventCaught() -> Action {
switch self.state {
case .parseDescribeBindExecuteSyncSent:
return .read
case .parseCompleteReceived:
return .read
case .parameterDescriptionReceived:
return .read
case .noDataMessageReceived:
return .read
case .rowDescriptionReceived:
return .read
case .bindCompleteReceived:
return .read
case .bufferingRows(let columns, let buffer, _):
return self.avoidingStateMachineCoW { state -> Action in
state = .bufferingRows(columns, buffer, readOnEmpty: true)
return .wait
}
case .waitingForNextRow:
// we are in the stream and the consumer has already asked us for more rows,
// therefore we need to read!
return .read
case .initialized,
.commandComplete,
.error:
// we already have the complete stream received, now we are waiting for a
// `readyForQuery` package. To receive this we need to read!
return .read
case .modifying:
preconditionFailure("Invalid state")
}
}
// MARK: Private Methods
private mutating func setAndFireError(_ error: PSQLError) -> Action {
switch self.state {
case .initialized(let context),
.parseDescribeBindExecuteSyncSent(let context),
.parseCompleteReceived(let context),
.parameterDescriptionReceived(let context),
.rowDescriptionReceived(let context, _),
.noDataMessageReceived(let context),
.bindCompleteReceived(let context):
self.state = .error(error)
return .failQuery(context, with: error)
case .bufferingRows(_, _, readOnEmpty: let readOnEmpty):
self.state = .error(error)
return .forwardStreamErrorToCurrentQuery(error, read: readOnEmpty)
case .waitingForNextRow(_, _, let promise):
self.state = .error(error)
return .forwardStreamError(error, to: promise)
case .commandComplete, .error:
preconditionFailure("""
This state must not be reached. If the query `.isComplete`, the
ConnectionStateMachine must not send any further events to the substate machine.
""")
case .modifying:
preconditionFailure("Invalid state")
}
}
var isComplete: Bool {
switch self.state {
case .commandComplete,
.error:
return true
default:
return false
}
}
}
extension ExtendedQueryStateMachine {
/// So, uh...this function needs some explaining.
///
/// While the state machine logic above is great, there is a downside to having all of the state machine data in
/// associated data on enumerations: any modification of that data will trigger copy on write for heap-allocated
/// data. That means that for _every operation on the state machine_ we will CoW our underlying state, which is
/// not good.
///
/// The way we can avoid this is by using this helper function. It will temporarily set state to a value with no
/// associated data, before attempting the body of the function. It will also verify that the state machine never
/// remains in this bad state.
///
/// A key note here is that all callers must ensure that they return to a good state before they exit.
///
/// Sadly, because it's generic and has a closure, we need to force it to be inlined at all call sites, which is
/// not ideal.
@inline(__always)
private mutating func avoidingStateMachineCoW<ReturnType>(_ body: (inout State) -> ReturnType) -> ReturnType {
self.state = .modifying
defer {
assert(!self.isModifying)
}
return body(&self.state)
}
private var isModifying: Bool {
if case .modifying = self.state {
return true
} else {
return false
}
}
}