forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLRowStream.swift
304 lines (256 loc) · 10.7 KB
/
PSQLRowStream.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
import NIOCore
import Logging
final class PSQLRowStream {
enum RowSource {
case stream(PSQLRowsDataSource)
case noRows(Result<String, Error>)
}
let eventLoop: EventLoop
let logger: Logger
private enum UpstreamState {
case streaming(buffer: CircularBuffer<PSQLBackendMessage.DataRow>, dataSource: PSQLRowsDataSource)
case finished(buffer: CircularBuffer<PSQLBackendMessage.DataRow>, commandTag: String)
case failure(Error)
case consumed(Result<String, Error>)
case modifying
}
private enum DownstreamState {
case iteratingRows(onRow: (PSQLRow) throws -> (), EventLoopPromise<Void>)
case waitingForAll(EventLoopPromise<[PSQLRow]>)
case consuming
}
internal let rowDescription: [PSQLBackendMessage.RowDescription.Column]
private let lookupTable: [String: Int]
private var upstreamState: UpstreamState
private var downstreamState: DownstreamState
private let jsonDecoder: PSQLJSONDecoder
init(rowDescription: [PSQLBackendMessage.RowDescription.Column],
queryContext: ExtendedQueryContext,
eventLoop: EventLoop,
rowSource: RowSource)
{
let buffer = CircularBuffer<PSQLBackendMessage.DataRow>()
self.downstreamState = .consuming
switch rowSource {
case .stream(let dataSource):
self.upstreamState = .streaming(buffer: buffer, dataSource: dataSource)
case .noRows(.success(let commandTag)):
self.upstreamState = .finished(buffer: .init(), commandTag: commandTag)
case .noRows(.failure(let error)):
self.upstreamState = .failure(error)
}
self.eventLoop = eventLoop
self.logger = queryContext.logger
self.jsonDecoder = queryContext.jsonDecoder
self.rowDescription = rowDescription
var lookup = [String: Int]()
lookup.reserveCapacity(rowDescription.count)
rowDescription.enumerated().forEach { (index, column) in
lookup[column.name] = index
}
self.lookupTable = lookup
}
func all() -> EventLoopFuture<[PSQLRow]> {
if self.eventLoop.inEventLoop {
return self.all0()
} else {
return self.eventLoop.flatSubmit {
self.all0()
}
}
}
private func all0() -> EventLoopFuture<[PSQLRow]> {
self.eventLoop.preconditionInEventLoop()
guard case .consuming = self.downstreamState else {
preconditionFailure("Invalid state")
}
switch self.upstreamState {
case .streaming(_, let dataSource):
dataSource.request(for: self)
let promise = self.eventLoop.makePromise(of: [PSQLRow].self)
self.downstreamState = .waitingForAll(promise)
return promise.futureResult
case .finished(let buffer, let commandTag):
self.upstreamState = .modifying
let rows = buffer.map {
PSQLRow(data: $0, lookupTable: self.lookupTable, columns: self.rowDescription, jsonDecoder: self.jsonDecoder)
}
self.downstreamState = .consuming
self.upstreamState = .consumed(.success(commandTag))
return self.eventLoop.makeSucceededFuture(rows)
case .consumed:
preconditionFailure("We already signaled, that the stream has completed, why are we asked again?")
case .modifying:
preconditionFailure("Invalid state")
case .failure(let error):
self.upstreamState = .consumed(.failure(error))
return self.eventLoop.makeFailedFuture(error)
}
}
func onRow(_ onRow: @escaping (PSQLRow) throws -> ()) -> EventLoopFuture<Void> {
if self.eventLoop.inEventLoop {
return self.onRow0(onRow)
} else {
return self.eventLoop.flatSubmit {
self.onRow0(onRow)
}
}
}
private func onRow0(_ onRow: @escaping (PSQLRow) throws -> ()) -> EventLoopFuture<Void> {
self.eventLoop.preconditionInEventLoop()
switch self.upstreamState {
case .streaming(var buffer, let dataSource):
let promise = self.eventLoop.makePromise(of: Void.self)
do {
for data in buffer {
let row = PSQLRow(
data: data,
lookupTable: self.lookupTable,
columns: self.rowDescription,
jsonDecoder: self.jsonDecoder
)
try onRow(row)
}
buffer.removeAll()
self.upstreamState = .streaming(buffer: buffer, dataSource: dataSource)
self.downstreamState = .iteratingRows(onRow: onRow, promise)
// immediately request more
dataSource.request(for: self)
} catch {
self.upstreamState = .failure(error)
dataSource.cancel(for: self)
promise.fail(error)
}
return promise.futureResult
case .finished(let buffer, let commandTag):
do {
for data in buffer {
let row = PSQLRow(
data: data,
lookupTable: self.lookupTable,
columns: self.rowDescription,
jsonDecoder: self.jsonDecoder
)
try onRow(row)
}
self.upstreamState = .consumed(.success(commandTag))
self.downstreamState = .consuming
return self.eventLoop.makeSucceededVoidFuture()
} catch {
self.upstreamState = .consumed(.failure(error))
return self.eventLoop.makeFailedFuture(error)
}
case .consumed:
preconditionFailure("We already signaled, that the stream has completed, why are we asked again?")
case .modifying:
preconditionFailure("Invalid state")
case .failure(let error):
self.upstreamState = .consumed(.failure(error))
return self.eventLoop.makeFailedFuture(error)
}
}
internal func noticeReceived(_ notice: PSQLBackendMessage.NoticeResponse) {
self.logger.debug("Notice Received", metadata: [
.notice: "\(notice)"
])
}
internal func receive(_ newRows: CircularBuffer<PSQLBackendMessage.DataRow>) {
precondition(!newRows.isEmpty, "Expected to get rows!")
self.eventLoop.preconditionInEventLoop()
self.logger.trace("Row stream received rows", metadata: [
"row_count": "\(newRows.count)"
])
guard case .streaming(var buffer, let dataSource) = self.upstreamState else {
preconditionFailure("Invalid state")
}
switch self.downstreamState {
case .iteratingRows(let onRow, let promise):
precondition(buffer.isEmpty)
do {
for data in newRows {
let row = PSQLRow(
data: data,
lookupTable: self.lookupTable,
columns: self.rowDescription,
jsonDecoder: self.jsonDecoder
)
try onRow(row)
}
// immediately request more
dataSource.request(for: self)
} catch {
dataSource.cancel(for: self)
self.upstreamState = .failure(error)
promise.fail(error)
return
}
case .waitingForAll:
self.upstreamState = .modifying
buffer.append(contentsOf: newRows)
self.upstreamState = .streaming(buffer: buffer, dataSource: dataSource)
// immediately request more
dataSource.request(for: self)
case .consuming:
// this might happen, if the query has finished while the user is consuming data
// we don't need to ask for more since the user is consuming anyway
self.upstreamState = .modifying
buffer.append(contentsOf: newRows)
self.upstreamState = .streaming(buffer: buffer, dataSource: dataSource)
}
}
internal func receive(completion result: Result<String, Error>) {
self.eventLoop.preconditionInEventLoop()
guard case .streaming(let oldBuffer, _) = self.upstreamState else {
preconditionFailure("Invalid state")
}
switch self.downstreamState {
case .iteratingRows(_, let promise):
precondition(oldBuffer.isEmpty)
self.downstreamState = .consuming
self.upstreamState = .consumed(result)
switch result {
case .success:
promise.succeed(())
case .failure(let error):
promise.fail(error)
}
case .consuming:
switch result {
case .success(let commandTag):
self.upstreamState = .finished(buffer: oldBuffer, commandTag: commandTag)
case .failure(let error):
self.upstreamState = .failure(error)
}
case .waitingForAll(let promise):
switch result {
case .failure(let error):
self.upstreamState = .consumed(.failure(error))
promise.fail(error)
case .success(let commandTag):
let rows = oldBuffer.map {
PSQLRow(data: $0, lookupTable: self.lookupTable, columns: self.rowDescription, jsonDecoder: self.jsonDecoder)
}
self.upstreamState = .consumed(.success(commandTag))
promise.succeed(rows)
}
}
}
func cancel() {
guard case .streaming(_, let dataSource) = self.upstreamState else {
// We don't need to cancel any upstream resource. All needed data is already
// included in this
return
}
dataSource.cancel(for: self)
}
var commandTag: String {
guard case .consumed(.success(let commandTag)) = self.upstreamState else {
preconditionFailure("commandTag may only be called if all rows have been consumed")
}
return commandTag
}
}
protocol PSQLRowsDataSource {
func request(for stream: PSQLRowStream)
func cancel(for stream: PSQLRowStream)
}