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: Logical termination for firestore.getAll(...). #1517

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void getAll(
ResponseObserver<BatchGetDocumentsResponse> responseObserver =
new ResponseObserver<BatchGetDocumentsResponse>() {
int numResponses;
boolean hasCompleted = false;

@Override
public void onStart(StreamController streamController) {}
Expand Down Expand Up @@ -265,6 +266,13 @@ public void onResponse(BatchGetDocumentsResponse response) {
return;
}
apiStreamObserver.onNext(documentSnapshot);

// Logical termination: if we have already received as many documents as we had
// requested, we can
// raise the results without waiting for the termination from the server.
if (numResponses == documentReferences.length) {
onComplete();
}
}

@Override
Expand All @@ -277,6 +285,8 @@ public void onError(Throwable throwable) {

@Override
public void onComplete() {
if (hasCompleted) return;
hasCompleted = true;
tracer
.getCurrentSpan()
.addAnnotation(TraceUtil.SPAN_NAME_BATCHGETDOCUMENTS + ": Complete");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion;
import static com.google.cloud.firestore.LocalFirestoreHelper.commit;
import static com.google.cloud.firestore.LocalFirestoreHelper.commitResponse;
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponse;
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponseWithoutOnComplete;
import static com.google.cloud.firestore.LocalFirestoreHelper.transform;
import static com.google.cloud.firestore.LocalFirestoreHelper.update;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -80,7 +80,7 @@ public void encodeFieldPath() {

@Test
public void illegalFieldPath() throws Exception {
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
doAnswer(getAllResponseWithoutOnComplete(SINGLE_FIELD_PROTO))
.when(firestoreMock)
.streamRequest(
getAllCapture.capture(),
Expand Down Expand Up @@ -110,7 +110,7 @@ public void exposesOptions() {
@Test
public void getAll() throws Exception {
doAnswer(
getAllResponse(
getAllResponseWithoutOnComplete(
SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO))
.when(firestoreMock)
.streamRequest(
Expand All @@ -132,7 +132,7 @@ public void getAll() throws Exception {

@Test
public void getAllWithFieldMask() throws Exception {
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
doAnswer(getAllResponseWithoutOnComplete(SINGLE_FIELD_PROTO))
.when(firestoreMock)
.streamRequest(
getAllCapture.capture(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ public static Map<String, Object> map() {

public static Answer<BatchGetDocumentsResponse> getAllResponse(
final Map<String, Value>... fields) {
return getAllResponseImpl(true, fields);
}

public static Answer<BatchGetDocumentsResponse> getAllResponseWithoutOnComplete(
final Map<String, Value>... fields) {
return getAllResponseImpl(false, fields);
}

public static Answer<BatchGetDocumentsResponse> getAllResponseImpl(
boolean withOnComplete, final Map<String, Value>... fields) {
BatchGetDocumentsResponse[] responses = new BatchGetDocumentsResponse[fields.length];

for (int i = 0; i < fields.length; ++i) {
Expand All @@ -281,7 +291,13 @@ public static Answer<BatchGetDocumentsResponse> getAllResponse(
responses[i] = response.build();
}

return streamingResponse(responses, null);
if (withOnComplete) {
return streamingResponse(responses, null);
} else {
// Verify with logical termination, the return of results no longer depends on calling
// OnComplete.
return streamingResponseWithoutOnComplete(responses);
}
}

public static ApiFuture<Empty> rollbackResponse() {
Expand Down