-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-81: Add caching iterator #327
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
Conversation
d12cde7
to
550ea92
Compare
private function wrapTraversable(\Traversable $traversable) | ||
{ | ||
foreach ($traversable as $key => $value) { | ||
yield $key => $value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conflicts with composer.json
requiring PHP >= 5.4. I'll see what we can do about bumping this for 1.2.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I forgot about the PHP requirement. If you'd like to avoid bumping the minimum PHP version to 5.5 I could try rewriting it with an IteratorIterator
.
@jmikola Is 1.2.0 still going to stay PHP 5.4 compatible, or move up to 5.6? |
We're planning to bump 1.2.0 to PHP 5.5 specifically for this feature. |
550ea92
to
84f3d2e
Compare
@jmikola Rebased against master, let me know if there's anything else to do. |
This isn't a caching iterator. This implementation mimics a buffered iterator (which is a good idea imo, rather than caching iterator). |
Oh, it is caching iterator. My scroll through the class confused me, as I saw couple of I think a BufferedIterator makes more sense then the caching iterator. |
Yes, it is, but it's hybrid. The main idea was to have a way to iterate a cursor multiple times, without rerunning the query/command in question and without loading all data into memory upfront (e.g. if only the first result is consumed). Thus, the iterator behaves like a regular iterator until it's rewound for the first time, at which time it returns results from the cache. |
Closing, as I incorporated these commits into #424. |
This is a shot at implementing an iterator like described in PHPLIB-81. It is designed to wrap any iterator, cache its results on the first run and the return the results on subsequent runs. It can be used to iterate multiple times over cursors.
I've also implemented
Countable
in the iterator, exposing acount
method since this doesn't suffer from the original issue ofMongoCursor::count
(which could return a number that does not match the items returned in the cursor).At this time, the iterator is not used anywhere and its use is left to the user of the library. Of course, operations could be changed to return a
CachingIterator
instead of aCursor
. The problem is thatMongoDB\Driver\Cursor
does not implement an interface (whichCachingIterator
could implement to provide a common parent class) and can't be extended due to itsfinal
constructor.