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

feat: add API to serialize Queries to Proto #241

Merged
merged 3 commits into from Jun 16, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Jun 5, 2020

This PR adds an API to serialize and deserialize Queries to and from RunQueryRequests. The new API endpoints are Query.toProto and Query.fromProto.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jun 5, 2020
@codecov
Copy link

codecov bot commented Jun 6, 2020

Codecov Report

Merging #241 into master will decrease coverage by 0.02%.
The diff coverage is 81.09%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #241      +/-   ##
============================================
- Coverage     73.01%   72.99%   -0.03%     
- Complexity     1036     1044       +8     
============================================
  Files            64       64              
  Lines          5444     5525      +81     
  Branches        618      645      +27     
============================================
+ Hits           3975     4033      +58     
- Misses         1271     1281      +10     
- Partials        198      211      +13     
Impacted Files Coverage Δ Complexity Δ
.../java/com/google/cloud/firestore/ResourcePath.java 85.71% <0.00%> (ø) 20.00 <0.00> (ø)
...om/google/cloud/firestore/CollectionReference.java 47.72% <33.33%> (-2.28%) 10.00 <1.00> (ø)
...a/com/google/cloud/firestore/DocumentSnapshot.java 83.05% <75.00%> (ø) 53.00 <1.00> (ø)
...rc/main/java/com/google/cloud/firestore/Query.java 88.09% <82.55%> (-2.88%) 121.00 <27.00> (+9.00) ⬇️
.../com/google/cloud/firestore/DocumentReference.java 72.72% <89.28%> (-0.84%) 31.00 <22.00> (-1.00)
...ain/java/com/google/cloud/firestore/FieldPath.java 88.09% <100.00%> (+0.59%) 31.00 <3.00> (+2.00)
...java/com/google/cloud/firestore/FirestoreImpl.java 78.78% <100.00%> (+0.16%) 25.00 <1.00> (-1.00) ⬆️
.../google/cloud/firestore/QueryDocumentSnapshot.java 76.92% <100.00%> (ø) 3.00 <1.00> (ø)
.../com/google/cloud/firestore/UserDataConverter.java 97.46% <0.00%> (+2.53%) 24.00% <0.00%> (+1.00%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 144910c...3058753. Read the comment docs.

@schmidt-sebastian schmidt-sebastian changed the title WIP: Query serialization Query serialization Jun 6, 2020
@schmidt-sebastian
Copy link
Contributor Author

@BenWhitehead This is now ready for review.

*
* @return the serialized RunQueryRequest
*/
public RunQueryRequest serializeToProto() {
Copy link

Choose a reason for hiding this comment

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

The internal convention seems to be toProto and fromProto, which seems perfectly fine. Why the extra long name here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No strong opinion here. Renamed.

@schmidt-sebastian schmidt-sebastian changed the title Query serialization feat: add the ability to serialize Queries to Proto Jun 9, 2020
@schmidt-sebastian schmidt-sebastian changed the title feat: add the ability to serialize Queries to Proto feat: add API to serialize Queries to Proto Jun 9, 2020
Copy link

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@schmidt-sebastian
Copy link
Contributor Author

@BenWhitehead Do we have a way forward regarding the fromProto(FirestoreImpl firestore, ...) API? Would it be sufficient to change the type to Firestore and cast to FirestoreImpl?

@BenWhitehead
Copy link
Collaborator

I've just pushed a new commit that refactors things so we don't have to depend on FirestoreImpl as the input and allows for Proxy'd instances to satisfy our constraints.


@InternalApi
@InternalExtensionOnly
interface FirestoreRpcContext<FS extends Firestore> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My takeaway from our discussion last week that that this was intended to be public. Let me know if I misunderstood you :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that was my original intent, but after poking at things and seeing the amount of interconnectedness between the data and the operations I think we need to scope this to the level FirestoreImpl currently is. If this interface were as simple as getDatabseName() and getResourcePath() I think it'd be easier for us to have it public, but there is a lot of coupling against these methods that we don't want on the public api surface.

Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

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

LGTM. I think we can probably merge and release this now.

### Feature
Add new methods `toProto` and `fromProto` to Query, allowing
serialization to and deserialization from RunQueryRequest.

### Refactoring
FieldFilter in Query has had a `fromProto` defined on it.

Properties in FieldFilter and corresponding subclasses have been
refactored to use proto types internally rather than previously using
high level api types.

Add a new interface FirestoreRpcContext which defines all the methods
needed by model classes that were only provided by FirestoreImpl.
FirestoreImpl now implements FirestoreRpcContext, and the new interface
methods are all the same implementation as before.

All dependency on FirestoreImpl from the following classes has been
replaced with FirestoreRpcContext:
* CollectionReference
* DocumentReference
* DocumentSnapshot
* Query
* QueryDocumentSnapshot

Motivation for this change is due to the fact that we want to expose a
new method on Query `fromProto` to allow a Query to be loaded from a
proto. However, Query requires additional context & access to be able
execute queries and thus has a dependency on a Firestore instance as
well as those methods now defined in FirestoreRpcContext.

Due to the constraints of Java's type system, the constraint on the
instance passed to `fromProto` is runtime checked versus compile
time checked. The method signature accepts any `Firestore` instance, but
we assert that the instance is also a `FirestoreRpcContext`. This isn't
ideal as it is a runtime error when not satisfied. However, Firestore is
annotated @InternalExtensionOnly so we have policy control that the
instance returned from FirestoreOptions will always also implement
FirestoreRpcContext.

A test has been added to verify that a proxy instance of FirestoreImpl
could satisfy the compile and runtime checks present such that use
should allow the code to work with java ee container based approaches
using dynamic class proxies for things like instrumentation and
dependency injection.

As part of the change, the constructors for the above mentioned model
classes have had their scope narrowed to package private from protected.
The classes themselves are now also annotated with @InternalExtensionOnly
along with the note in their Javadoc stating the policy.

clirr rules have been added to ignore the constructor changes in the
model classes.

Co-authored-by: Sebastian Schmidt <mrschmidt@google.com>
Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com>
@BenWhitehead BenWhitehead merged commit bae22e0 into master Jun 16, 2020
@BenWhitehead BenWhitehead deleted the mrschmidt/serializequery branch June 16, 2020 23:53
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 17, 2020
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.

None yet

4 participants