Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge PSQLRow into PostgresRow #219

Merged
merged 16 commits into from
Feb 25, 2022

Conversation

fabianfett
Copy link
Collaborator

@fabianfett fabianfett commented Feb 17, 2022

Okay, this PR has some meat in it! As it changes the existing PostgresRow quite significantly. We can do this non API breaking, however we may break some users expectations. However we can make those explicit with deprecations.

Motivation

Currently PostgresRow creates an internal array for every row to allow random access lookup for cells in the row. However I suspect this to not be always necessary. For this reason I propose changing PostgresRow to work like PSQLRow has worked so far. To allow dependent software that depends on the RandomAccessCollection lookups to continue to work correctly we introduce a PostgresRandomAccessRow. This one can be created from a PostgresRow and has all nice O(1) behaviors.

Changes

  • Add PostgresCell (should be landed extra in Add PostgresCell #220) before this one
  • Remove PSQLRow
  • Make PostgresRow light
  • Introduce PostgresRandomAccessRow

Result

  • Fastest code by default
  • Random access to cells when needed

Copy link
Member

@gwynne gwynne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple notes


extension PostgresRow: Equatable {
public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.data == rhs.data && lhs.columns == rhs.columns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the lookupTable a deliberate omission here? (I have no objection if so, just double-checking.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Cause it is purely derived from the columns metadata.

}

extension PostgresRow {
// TODO: Remove this function. Only here to keep the tests running as of today.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move these methods into the tests target, given this status?

@codecov-commenter
Copy link

codecov-commenter commented Feb 18, 2022

Codecov Report

Merging #219 (618878a) into main (c493f0e) will increase coverage by 3.73%.
The diff coverage is 11.00%.

@@            Coverage Diff             @@
##             main     #219       /-   ##
==========================================
  Coverage   39.62%   43.36%    3.73%     
==========================================
  Files         116      115       -1     
  Lines        9271     9327       56     
==========================================
  Hits         3674     4045      371     
  Misses       5597     5282     -315     
Flag Coverage Δ
unittests 43.36% <11.00%> ( 3.73%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...es/PostgresNIO/Connection/PostgresConnection.swift 0.00% <0.00%> (ø)
...IO/Connection/PostgresDatabase PreparedQuery.swift 0.00% <ø> (ø)
Sources/PostgresNIO/New/PSQLRow-multi-decode.swift 4.44% <ø> ( 0.19%) ⬆️
Sources/PostgresNIO/New/PostgresCell.swift 96.77% <ø> ( 3.22%) ⬆️
Sources/PostgresNIO/Data/PostgresRow.swift 2.55% <2.84%> ( 2.55%) ⬆️
Sources/PostgresNIO/New/PostgresRowSequence.swift 89.73% <75.00%> ( 6.47%) ⬆️
Sources/PostgresNIO/New/PSQLRowStream.swift 84.54% <100.00%> ( 6.96%) ⬆️
...s/PostgresNIO/New/Data/Array PostgresCodable.swift 94.68% <0.00%> ( 1.06%) ⬆️
... and 28 more

Copy link
Member

@gwynne gwynne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love cleanups ❤️ More, more! 🙃

# Conflicts:
#	Sources/PostgresNIO/Connection/PostgresConnection Database.swift
#	Sources/PostgresNIO/New/PSQLRow-multi-decode.swift
#	Sources/PostgresNIO/New/PSQLRow.swift
#	Tests/IntegrationTests/PSQLIntegrationTests.swift
# Conflicts:
#	Sources/PostgresNIO/New/PostgresRowSequence.swift
# Conflicts:
#	Sources/PostgresNIO/Connection/PostgresConnection Database.swift
# Conflicts:
#	Sources/PostgresNIO/Connection/PostgresConnection.swift
#	Sources/PostgresNIO/Postgres PSQLCompat.swift
@fabianfett
Copy link
Collaborator Author

Okay, this was already approved, but I think now it is finally ready... Just to make everyone aware, the important bits are:

  1. Cells from PostgresRow are not random accessible in O(1) anymore

  2. PostgresRow now conforms to Sequence and Collection. The Element type is PostgresCell. The Index type is a custom struct with internal properties. This underlines the Collection but not RandomAccessCollection properties.

  3. PostgresRandomAccessRow is a RandomAccessCollection. The Element type is PostgresCell. The Index type is Int.

  4. A PostgresRandomAccessRow is created from a PostgresRow like this:

    var randomAccessRow = row.makeRandomAccess() // create PostgresRandomAccessRow from PostgresRow
  5. The following subscripts are possible.

    let cell = randomAccessRow[0] // access first cell in the row, returns PostgresCell
    let cell = randomAccessRow["id"] // access cell with column name "id" in the row, returns PostgresCell
    let data = randomAccessRow[data: 0] // access first cell in the row, returns PostgresData
    let data = randomAccessRow[data: "id"] // access cell with column name "id" in the row, returns PostgresData

Copy link
Member

@gwynne gwynne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still love it - let's keep it going! 👍 🎉

@fabianfett fabianfett merged commit 2938198 into vapor:main Feb 25, 2022
@fabianfett fabianfett deleted the ff-merge-psqlrow-postgresrow branch February 25, 2022 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants