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

Iterating items from specific position #101

Open
waldorllc opened this issue Jun 15, 2023 · 1 comment
Open

Iterating items from specific position #101

waldorllc opened this issue Jun 15, 2023 · 1 comment

Comments

@waldorllc
Copy link

I couldn't figure out how to start iterating items from specific position. If I have a huge json file and at some point something goes wrong, how to continue process from some position without need to iterate from the beginning for many hours again? Many thanks!

@halaxa
Copy link
Owner

halaxa commented Jun 15, 2023

There's no built-in option for this. However, it should be possible to open a stream (file), use fseek() to get to the desired position, and then pass the stream to JSON Machine. Let's say you ended up somewhere in the middle of a huge file and your items have some kind of ID. I'd suggest finding the ID in the file in some huge-file-friendly editor, noting the byte position of the next item to be iterated (for example 987654321), and then doing something along the lines of:

...
$stream = fopen('file.json', 'r');
fseek($stream, 987654321);

foreach(Items::fromStream($stream) as $item) {
    ...
}

Keep in mind that the JSON pointer you used when you started iteration before the failure from the beginning will most likely not work as the position you specified might be inside some structure already.

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

No branches or pull requests

2 participants