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

API rescue master-branch PR: Use Generators for ORM #10484

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Sep 2, 2022

This PR rescues #6518 from the master branch. See the original PR for discussion about the implementation.

This PR was done once before but had to be reverted. See #10450 and #10483
The CI failures that were being caused have been resolved. The problem was that the PR being rescued was using old logic for iterators in the MySQLQuery and MySQLStatement classes. The correct logic is now in place - it was taken from the nextRecord() methods from those classes.

Note that yield results in returning a Generator which is a subclass of Iterator - so all getIterator() methods affected now return Iterator - but I won't add the typehints for that in this PR as that's not directly the same concern as rescuing the master branch PR.

NOTE

To avoid having to revert this again, don't merge until CI is green.

Parent Issue

@GuySartorelli GuySartorelli mentioned this pull request Sep 2, 2022
5 tasks
@GuySartorelli
Copy link
Member Author

GuySartorelli commented Sep 2, 2022

CI failures are a result of #10389
Hold off merging until that has been resolved and we can get CI green.

@GuySartorelli
Copy link
Member Author

Note: CI failures are for postgres (which will be resolved by silverstripe/silverstripe-postgresql#138) and PDO (which was deprecated as of this RFC so we're removing it anyway).

Sam Minnee and others added 17 commits September 15, 2022 13:28
Generators (PHP 5.5+) make this kind of code structure much easier to
build.
Using a generator here means that we don’t need to prepare a duplicate
array in-memory before iterating.
It wasn’t respecting pagination.
It’s best for foreach() to call this for us.
API: Query no longer has iterator methods current(), first(), rewind(), next()

Using generators reduces the amount of boilerplate needed for this
code.

Turning it into an IteratorAggregate means that the iterator can be
re-created for each subsequent foreach call. This means that the
rewind() and seek() functionality can be discarded.
SSViewer iterates on Iterators that it receives twice: first to get the
total number of items, then to actually render each item.

This necessitates a rewind. In order to make more use of generators,
which are not rewindable, I’d like to remove the need for a rewind.

I’ve done this by caching the content of the iterator as an array
within SSViewer_Scope.

Although this means a bit of memory usage, there are no cases in which
code will get to this point without iterating on all items, which would
use the memory anyway. It would only create onerous impacts in cases
where you are iterating on very long iterators, which would mean you’re
rendering a very large page anyway, and probably have other performance
issues.
`getIterator()` now returns a generator by default.
Setting a proper return type for these will be done in a separate PR
@GuySartorelli GuySartorelli force-pushed the pulls/5/rescue-master-orm-generators branch from 3008b4e to 0f2ec49 Compare September 15, 2022 01:29
@GuySartorelli GuySartorelli force-pushed the pulls/5/rescue-master-orm-generators branch from 0f2ec49 to e140c37 Compare September 15, 2022 04:23
@emteknetnz emteknetnz merged commit 71dca01 into silverstripe:5 Sep 15, 2022
@emteknetnz emteknetnz deleted the pulls/5/rescue-master-orm-generators branch September 15, 2022 04:51
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