use of com.google.firestore.v1beta1.BatchGetDocumentsRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class BatchGetDocuments method batchGetDocumentsCall.
public void batchGetDocumentsCall() {
List<String> docList = new ArrayList<String>();
System.out.println("\n :: Batch Retrieve Documents :: \n");
Scanner sc = new Scanner(System.in);
String input = "initial";
FirestoreGrpc.FirestoreStub firestoreStub = new GRPCFirebaseClientFactory().createFirebaseClient().getFirestoreStub();
DrawDocument dd = new DrawDocument();
while (!input.matches("DONE")) {
System.out.print("Enter Document Id (Enter DONE when finished): ");
input = sc.next();
if (!input.matches("DONE")) {
docList.add("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + input);
}
}
BatchGetDocumentsRequest batchGetDocsRequest = BatchGetDocumentsRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").addAllDocuments(docList).build();
final CountDownLatch finishLatch = new CountDownLatch(1);
StreamObserver respStream = new StreamObserver() {
@Override
public void onNext(Object resp) {
BatchGetDocumentsResponse response = (BatchGetDocumentsResponse) resp;
Document doc = response.getFound();
dd.draw(doc);
}
@Override
public void onError(Throwable throwable) {
System.out.println("Error During Call: " + throwable.getMessage());
finishLatch.countDown();
}
@Override
public void onCompleted() {
Menu menu = new Menu();
menu.draw();
finishLatch.countDown();
}
};
try {
firestoreStub.batchGetDocuments(batchGetDocsRequest, respStream);
finishLatch.await(1, TimeUnit.MINUTES);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
}
}
use of com.google.firestore.v1beta1.BatchGetDocumentsRequest in project java-firestore by googleapis.
the class FirestoreClientTest method batchGetDocumentsTest.
@Test
public void batchGetDocumentsTest() throws Exception {
BatchGetDocumentsResponse expectedResponse = BatchGetDocumentsResponse.newBuilder().setTransaction(ByteString.EMPTY).setReadTime(Timestamp.newBuilder().build()).build();
mockFirestore.addResponse(expectedResponse);
BatchGetDocumentsRequest request = BatchGetDocumentsRequest.newBuilder().setDatabase("database1789464955").addAllDocuments(new ArrayList<String>()).setMask(DocumentMask.newBuilder().build()).build();
MockStreamObserver<BatchGetDocumentsResponse> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<BatchGetDocumentsRequest, BatchGetDocumentsResponse> callable = client.batchGetDocumentsCallable();
callable.serverStreamingCall(request, responseObserver);
List<BatchGetDocumentsResponse> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
use of com.google.firestore.v1beta1.BatchGetDocumentsRequest in project beam by apache.
the class FirestoreV1FnBatchGetDocumentsTest method resumeFromLastReadValue.
@Override
public void resumeFromLastReadValue() throws Exception {
final BatchGetDocumentsResponse response1 = newMissing(1);
final BatchGetDocumentsResponse response2 = newFound(2);
final BatchGetDocumentsResponse response3 = newMissing(3);
final BatchGetDocumentsResponse response4 = newFound(4);
final BatchGetDocumentsRequest request1 = BatchGetDocumentsRequest.newBuilder().setDatabase(String.format("projects/%s/databases/(default)", projectId)).addDocuments(response1.getMissing()).addDocuments(response2.getFound().getName()).addDocuments(response3.getMissing()).addDocuments(response4.getFound().getName()).build();
BatchGetDocumentsRequest request2 = BatchGetDocumentsRequest.newBuilder().setDatabase(String.format("projects/%s/databases/(default)", projectId)).addDocuments(response3.getMissing()).addDocuments(response4.getFound().getName()).build();
BatchGetDocumentsRequest request3 = BatchGetDocumentsRequest.newBuilder().setDatabase(String.format("projects/%s/databases/(default)", projectId)).addDocuments(response4.getFound().getName()).build();
when(responseStream1.iterator()).thenReturn(new AbstractIterator<BatchGetDocumentsResponse>() {
private int counter = 10;
@Override
protected BatchGetDocumentsResponse computeNext() {
int count = counter++;
if (count == 10) {
return response1;
} else if (count == 11) {
return response2;
} else {
throw RETRYABLE_ERROR;
}
}
});
when(responseStream2.iterator()).thenReturn(new AbstractIterator<BatchGetDocumentsResponse>() {
private int counter = 20;
@Override
protected BatchGetDocumentsResponse computeNext() {
int count = counter++;
if (count == 20) {
return response3;
} else {
throw RETRYABLE_ERROR;
}
}
});
when(responseStream3.iterator()).thenReturn(ImmutableList.of(response4).iterator());
doNothing().when(attempt).checkCanRetry(any(), eq(RETRYABLE_ERROR));
when(callable.call(request1)).thenReturn(responseStream1);
when(callable.call(request2)).thenReturn(responseStream2);
when(callable.call(request3)).thenReturn(responseStream3);
when(stub.batchGetDocumentsCallable()).thenReturn(callable);
when(ff.getFirestoreStub(any())).thenReturn(stub);
when(ff.getRpcQos(any())).thenReturn(rpcQos);
when(rpcQos.newReadAttempt(any())).thenReturn(attempt);
when(attempt.awaitSafeToProceed(any())).thenReturn(true);
ArgumentCaptor<BatchGetDocumentsResponse> responsesCaptor = ArgumentCaptor.forClass(BatchGetDocumentsResponse.class);
doNothing().when(processContext).output(responsesCaptor.capture());
when(processContext.element()).thenReturn(request1);
BatchGetDocumentsFn fn = new BatchGetDocumentsFn(clock, ff, rpcQosOptions);
runFunction(fn);
List<BatchGetDocumentsResponse> expectedResponses = ImmutableList.of(response1, response2, response3, response4);
List<BatchGetDocumentsResponse> actualResponses = responsesCaptor.getAllValues();
assertEquals(expectedResponses, actualResponses);
verify(callable, times(1)).call(request1);
verify(callable, times(1)).call(request2);
verify(attempt, times(4)).recordStreamValue(any());
}
use of com.google.firestore.v1beta1.BatchGetDocumentsRequest in project beam by apache.
the class FirestoreV1FnBatchGetDocumentsTest method endToEnd.
@Test
public void endToEnd() throws Exception {
final BatchGetDocumentsRequest request = BatchGetDocumentsRequest.newBuilder().setDatabase(String.format("projects/%s/databases/(default)", projectId)).addDocuments("doc_1-1").addDocuments("doc_1-2").addDocuments("doc_1-3").build();
final BatchGetDocumentsResponse response1 = newFound(1);
final BatchGetDocumentsResponse response2 = newFound(2);
final BatchGetDocumentsResponse response3 = newFound(3);
List<BatchGetDocumentsResponse> responses = ImmutableList.of(response1, response2, response3);
when(responseStream1.iterator()).thenReturn(responses.iterator());
when(callable.call(request)).thenReturn(responseStream1);
when(stub.batchGetDocumentsCallable()).thenReturn(callable);
when(ff.getFirestoreStub(any())).thenReturn(stub);
when(ff.getRpcQos(any())).thenReturn(FirestoreStatefulComponentFactory.INSTANCE.getRpcQos(rpcQosOptions));
ArgumentCaptor<BatchGetDocumentsResponse> responsesCaptor = ArgumentCaptor.forClass(BatchGetDocumentsResponse.class);
doNothing().when(processContext).output(responsesCaptor.capture());
when(processContext.element()).thenReturn(request);
runFunction(getFn(clock, ff, rpcQosOptions));
List<BatchGetDocumentsResponse> allValues = responsesCaptor.getAllValues();
assertEquals(responses, allValues);
}
use of com.google.firestore.v1beta1.BatchGetDocumentsRequest in project java-firestore by googleapis.
the class TransactionTest method getMultipleDocumentsWithFieldMask.
@Test
public void getMultipleDocumentsWithFieldMask() throws Exception {
doReturn(beginResponse()).doReturn(commitResponse(0, 0)).when(firestoreMock).sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());
doAnswer(getAllResponse(SINGLE_FIELD_PROTO)).when(firestoreMock).streamRequest(requestCapture.capture(), streamObserverCapture.capture(), Matchers.<ServerStreamingCallable>any());
final DocumentReference doc1 = firestoreMock.document("coll/doc1");
final FieldMask fieldMask = FieldMask.of(FieldPath.of("foo", "bar"));
ApiFuture<List<DocumentSnapshot>> transaction = firestoreMock.runTransaction(t -> t.getAll(new DocumentReference[] { doc1 }, fieldMask).get(), options);
transaction.get();
List<Message> requests = requestCapture.getAllValues();
assertEquals(3, requests.size());
assertEquals(begin(), requests.get(0));
BatchGetDocumentsRequest expectedGetAll = getAll(TRANSACTION_ID, doc1.getResourcePath().toString());
expectedGetAll = expectedGetAll.toBuilder().setMask(DocumentMask.newBuilder().addFieldPaths("foo.bar")).build();
assertEquals(expectedGetAll, requests.get(1));
assertEquals(commit(TRANSACTION_ID), requests.get(2));
}
Aggregations