Skip to content

[Firestore] Snippets for document snapshots and paginated queries. #1243

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

Merged
merged 7 commits into from
Nov 2, 2018

Conversation

kurtisvg
Copy link
Contributor

Add Java snippets for the Paginating Data with Query Cursors query.

@kurtisvg kurtisvg requested a review from frankyn October 29, 2018 17:03
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 29, 2018
@kurtisvg kurtisvg requested a review from dzlier-gcp October 29, 2018 17:03
@Test
public void testPaginateCursor() throws Exception {
// Snippet executes it's own query. Failures result in thrown Exceptions
queryDataSnippets.paginateCursor();
Copy link
Member

Choose a reason for hiding this comment

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

This test doesn't verify that the API is returning what's expected, just that it doesn't through any exceptions.

This is my concern with methods that don't return anything, except this one doesn't even log anything to stdout either, so there's no way to catch any bugs or changes from one version to another.

Maybe that's not technically a goal or responsibility with our samples, but if a bug gets through the API and doesn't get caught in our samples, that's going to mean people taking our code and using it in their own projects, and it not working for them either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a fair concern, and one I agree with. The reason I went with this over a more involved test was because:

  1. I followed the golang example
  2. there are only 4 results in the first page, so the second page is technically empty.

Do you have a better way in mind to test it? I could check for an empty list - would that be preferred? I decided against it at the time, mostly because there are similar issues with some snippets in this folder and figured it would be best just to wait until they are modified to fit the new sample format.

Copy link
Member

Choose a reason for hiding this comment

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

figured it would be best just to wait until they are modified to fit the new sample format.

That might be the answer for now, depending on how soon all these samples are going to be updated. Are there any queries that are long enough to be paginated? Or can we just reduce the page element count to force the pagination even if it's not practical to get a page of 2 results?

In any case, I'd like it to at least write its findings to stdout, if not return them to be verified by the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the snippet slightly to return the queries and rerun for the tests.

.get();

// Wait for results.
List<QueryDocumentSnapshot> docs = firstPage.get(30, TimeUnit.SECONDS).getDocuments();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is performing another get? I'm a little confused by snippet given it calls get: one here and another on the previous line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first .get() makes an async API call and returns a future that represents the results. The second .get represents blocking until the results are ready, and returns the result of the first API call as a value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

// [START fs_document_snapshot_cursor]
// Fetch the Snapshot
ApiFuture<DocumentSnapshot> future = db.collection("cities").document("SF").get();
DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the magic number represent other than 30 seconds?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the amount of time to block for results -- if this limit is exceeded, an exception is thrown.

Copy link
Contributor

@frankyn frankyn Oct 31, 2018

Choose a reason for hiding this comment

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

Could you use a variable name instead of a magic number to state the same information?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The number isn't magical, it is just a safe amount of time. I don't think a variable would make sense in the context, since the value depends on the users situation and is only used once. I have updated with a comment to better explain what the future is doing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that helps!

// [START fs_document_snapshot_cursor]
// Fetch the Snapshot
ApiFuture<DocumentSnapshot> future = db.collection("cities").document("SF").get();
DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that helps!

@kurtisvg kurtisvg merged commit fd030d1 into master Nov 2, 2018
@kurtisvg kurtisvg deleted the doc-snapshot branch November 2, 2018 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants