-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve row decoding performance (#221)
* Leverage better-performing new APIs from PostgresNIO, also solving (most) deprecation warnings. Also some additional minor improvements to connection handling. * Generating NIOSSLContexts is (very) expensive, do it only once (per connection source, at least) rather than for every connection. * CI cleanup - update Swift versions, use generic names, don't over-test, use new checkout action
- Loading branch information
Showing
6 changed files
with
112 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,40 @@ | ||
extension PostgresRow { | ||
public func sql(decoder: PostgresDataDecoder = .init()) -> SQLRow { | ||
return _PostgreSQLRow(row: self, decoder: decoder) | ||
return _PostgresSQLRow(row: self.makeRandomAccess(), decoder: decoder) | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private struct _PostgreSQLRow: SQLRow { | ||
let row: PostgresRow | ||
private struct _PostgresSQLRow: SQLRow { | ||
let randomAccessView: PostgresRandomAccessRow | ||
let decoder: PostgresDataDecoder | ||
|
||
enum _Error: Error { | ||
case missingColumn(String) | ||
} | ||
|
||
init(row: PostgresRandomAccessRow, decoder: PostgresDataDecoder) { | ||
self.randomAccessView = row | ||
self.decoder = decoder | ||
} | ||
|
||
var allColumns: [String] { | ||
self.row.rowDescription.fields.map { $0.name } | ||
self.randomAccessView.map { $0.columnName } | ||
} | ||
|
||
func contains(column: String) -> Bool { | ||
self.row.rowDescription.fields | ||
.contains { $0.name == column } | ||
self.randomAccessView.contains(column) | ||
} | ||
|
||
func decodeNil(column: String) throws -> Bool { | ||
self.row.column(column)?.value == nil | ||
!self.randomAccessView.contains(column) || self.randomAccessView[column].bytes == nil | ||
} | ||
|
||
func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable { | ||
guard let data = self.row.column(column) else { | ||
guard self.randomAccessView.contains(column) else { | ||
throw _Error.missingColumn(column) | ||
} | ||
return try self.decoder.decode(D.self, from: data) | ||
return try self.decoder.decode(D.self, from: self.randomAccessView[data: column]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters