Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 @@ -17,6 +17,7 @@
package com.google.cloud.firestore;

import com.google.api.core.ApiFuture;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.cloud.Service;
import java.util.List;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -117,6 +118,20 @@ <T> ApiFuture<T> runTransaction(
ApiFuture<List<DocumentSnapshot>> getAll(
@Nonnull DocumentReference[] documentReferences, @Nullable FieldMask fieldMask);

/**
* Retrieves multiple documents from Firestore while optionally applying a field mask to reduce
* the amount of data transmitted. Returned documents will be out of order.
*
* @param documentReferences Array with Document References to fetch.
* @param fieldMask If not null, specifies the subset of fields to return.
* @param responseObserver The observer to be notified when {@link DocumentSnapshot} details
* arrive.
*/
void getAll(
Copy link
Contributor

Choose a reason for hiding this comment

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

Please match the signature above and change the order to:

getAll(DocumentReference[] documentReferences, @Nullable FieldMask fieldMask, final ApiStreamObserver<DocumentSnapshot> responseObserver)

Putting the observer last also makes for much more readable code when a Lambda is used.

@Nonnull DocumentReference[] documentReferences,
@Nullable FieldMask fieldMask,
final ApiStreamObserver<DocumentSnapshot> responseObserver);

/**
* Gets a Firestore {@link WriteBatch} instance that can be used to combine multiple writes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,30 @@ public Iterable<CollectionReference> getCollections() {
@Override
public ApiFuture<List<DocumentSnapshot>> getAll(
@Nonnull DocumentReference... documentReferences) {
return this.getAll(documentReferences, null, null);
return this.getAll(documentReferences, null, (ByteString) null);
}

@Nonnull
@Override
public ApiFuture<List<DocumentSnapshot>> getAll(
@Nonnull DocumentReference[] documentReferences, @Nullable FieldMask fieldMask) {
return this.getAll(documentReferences, fieldMask, null);
return this.getAll(documentReferences, fieldMask, (ByteString) null);
}

/** Internal getAll() method that accepts an optional transaction id. */
ApiFuture<List<DocumentSnapshot>> getAll(
final DocumentReference[] documentReferences,
@Nonnull
@Override
public void getAll(
final @Nonnull DocumentReference[] documentReferences,
@Nullable FieldMask fieldMask,
@Nullable ByteString transactionId) {
final SettableApiFuture<List<DocumentSnapshot>> futureList = SettableApiFuture.create();
final Map<DocumentReference, DocumentSnapshot> resultMap = new HashMap<>();
@Nonnull final ApiStreamObserver<DocumentSnapshot> apiStreamObserver) {
this.getAll(documentReferences, fieldMask, null, apiStreamObserver);
}

void getAll(
final @Nonnull DocumentReference[] documentReferences,
@Nullable FieldMask fieldMask,
@Nullable ByteString transactionId,
final ApiStreamObserver<DocumentSnapshot> apiStreamObserver) {

ApiStreamObserver<BatchGetDocumentsResponse> responseObserver =
new ApiStreamObserver<BatchGetDocumentsResponse>() {
Expand All @@ -176,9 +183,6 @@ public void onNext(BatchGetDocumentsResponse response) {

switch (response.getResultCase()) {
case FOUND:
documentReference =
new DocumentReference(
FirestoreImpl.this, ResourcePath.create(response.getFound().getName()));
documentSnapshot =
DocumentSnapshot.fromDocument(
FirestoreImpl.this,
Expand All @@ -198,26 +202,19 @@ public void onNext(BatchGetDocumentsResponse response) {
default:
return;
}

resultMap.put(documentReference, documentSnapshot);
apiStreamObserver.onNext(documentSnapshot);
}

@Override
public void onError(Throwable throwable) {
tracer.getCurrentSpan().addAnnotation("Firestore.BatchGet: Error");
futureList.setException(throwable);
apiStreamObserver.onError(throwable);
}

@Override
public void onCompleted() {
tracer.getCurrentSpan().addAnnotation("Firestore.BatchGet: Complete");
List<DocumentSnapshot> documentSnapshots = new ArrayList<>();

for (DocumentReference documentReference : documentReferences) {
documentSnapshots.add(resultMap.get(documentReference));
}

futureList.set(documentSnapshots);
apiStreamObserver.onCompleted();
}
};

Expand All @@ -244,7 +241,39 @@ public void onCompleted() {
"numDocuments", AttributeValue.longAttributeValue(documentReferences.length)));

streamRequest(request.build(), responseObserver, firestoreClient.batchGetDocumentsCallable());
}

/** Internal getAll() method that accepts an optional transaction id. */
ApiFuture<List<DocumentSnapshot>> getAll(
final @Nonnull DocumentReference[] documentReferences,
@Nullable FieldMask fieldMask,
@Nullable ByteString transactionId) {
final SettableApiFuture<List<DocumentSnapshot>> futureList = SettableApiFuture.create();
final Map<DocumentReference, DocumentSnapshot> documentSnapshotMap = new HashMap<>();
getAll(
documentReferences,
fieldMask,
transactionId,
new ApiStreamObserver<DocumentSnapshot>() {
@Override
public void onNext(DocumentSnapshot documentSnapshot) {
documentSnapshotMap.put(documentSnapshot.getReference(), documentSnapshot);
}

@Override
public void onError(Throwable throwable) {
futureList.setException(throwable);
}

@Override
public void onCompleted() {
List<DocumentSnapshot> documentSnapshotsList = new ArrayList<>();
for (DocumentReference documentReference : documentReferences) {
documentSnapshotsList.add(documentSnapshotMap.get(documentReference));
}
futureList.set(documentSnapshotsList);
}
});
return futureList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.cloud.Timestamp;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentChange;
Expand Down Expand Up @@ -1327,4 +1330,53 @@ private static final class ListenerEvent {
this.error = error;
}
}

@Test
public void getAllWithObserver() throws Exception {
DocumentReference ref1 = randomColl.document("doc1");
ref1.set(ALL_SUPPORTED_TYPES_MAP).get();

DocumentReference ref2 = randomColl.document("doc2");
ref2.set(ALL_SUPPORTED_TYPES_MAP).get();

DocumentReference ref3 = randomColl.document("doc3");

final List<DocumentSnapshot> documentSnapshots =
Collections.synchronizedList(new ArrayList<DocumentSnapshot>());
final DocumentReference[] documentReferences = {ref1, ref2, ref3};
final SettableApiFuture<Void> future = SettableApiFuture.create();
firestore.getAll(
documentReferences,
FieldMask.of("foo"),
new ApiStreamObserver<DocumentSnapshot>() {

@Override
public void onNext(DocumentSnapshot documentSnapshot) {
documentSnapshots.add(documentSnapshot);
}

@Override
public void onError(Throwable throwable) {
future.setException(throwable);
}

@Override
public void onCompleted() {
future.set(null);
}
});

future.get();
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for updating this!


assertEquals(
ALL_SUPPORTED_TYPES_OBJECT, documentSnapshots.get(0).toObject(AllSupportedTypes.class));
assertEquals(
ALL_SUPPORTED_TYPES_OBJECT, documentSnapshots.get(1).toObject(AllSupportedTypes.class));
assertNotEquals(
ALL_SUPPORTED_TYPES_OBJECT, documentSnapshots.get(2).toObject(AllSupportedTypes.class));
assertEquals(ref1.getId(), documentSnapshots.get(0).getId());
assertEquals(ref2.getId(), documentSnapshots.get(1).getId());
assertEquals(ref3.getId(), documentSnapshots.get(2).getId());
assertEquals(3, documentSnapshots.size());
}
}