Search in sources :

Example 1 with BatchGetDocumentsResponse

use of com.google.firestore.v1beta1.BatchGetDocumentsResponse 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());
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Scanner(java.util.Scanner) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Document(com.google.firestore.v1beta1.Document) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument) BatchGetDocumentsRequest(com.google.firestore.v1beta1.BatchGetDocumentsRequest) BatchGetDocumentsResponse(com.google.firestore.v1beta1.BatchGetDocumentsResponse) Menu(org.roguewave.grpc.util.gfx.Menu) FirestoreGrpc(com.google.firestore.v1beta1.FirestoreGrpc) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument)

Example 2 with BatchGetDocumentsResponse

use of com.google.firestore.v1beta1.BatchGetDocumentsResponse in project java-firestore by googleapis.

the class LocalFirestoreHelper method getAllResponse.

public static Answer<BatchGetDocumentsResponse> getAllResponse(final Map<String, Value>... fields) {
    BatchGetDocumentsResponse[] responses = new BatchGetDocumentsResponse[fields.length];
    for (int i = 0; i < fields.length; ++i) {
        String name = DOCUMENT_NAME;
        if (fields.length > 1) {
            name += i + 1;
        }
        BatchGetDocumentsResponse.Builder response = BatchGetDocumentsResponse.newBuilder();
        response.getFoundBuilder().setCreateTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(1).setNanos(2));
        response.getFoundBuilder().setUpdateTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(3).setNanos(4));
        response.setReadTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(5).setNanos(6));
        response.getFoundBuilder().setName(name).putAllFields(fields[i]);
        responses[i] = response.build();
    }
    return streamingResponse(responses, null);
}
Also used : BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) ByteString(com.google.protobuf.ByteString)

Example 3 with BatchGetDocumentsResponse

use of com.google.firestore.v1beta1.BatchGetDocumentsResponse 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));
}
Also used : BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) ByteString(com.google.protobuf.ByteString) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) Test(org.junit.Test)

Example 4 with BatchGetDocumentsResponse

use of com.google.firestore.v1beta1.BatchGetDocumentsResponse 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());
}
Also used : BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) BatchGetDocumentsFn(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.BatchGetDocumentsFn)

Example 5 with BatchGetDocumentsResponse

use of com.google.firestore.v1beta1.BatchGetDocumentsResponse in project firebase-android-sdk by firebase.

the class Datastore method lookup.

public Task<List<MutableDocument>> lookup(List<DocumentKey> keys) {
    BatchGetDocumentsRequest.Builder builder = BatchGetDocumentsRequest.newBuilder();
    builder.setDatabase(serializer.databaseName());
    for (DocumentKey key : keys) {
        builder.addDocuments(serializer.encodeKey(key));
    }
    List<BatchGetDocumentsResponse> responses = new ArrayList<>();
    TaskCompletionSource<List<MutableDocument>> completionSource = new TaskCompletionSource<>();
    channel.runStreamingResponseRpc(FirestoreGrpc.getBatchGetDocumentsMethod(), builder.build(), new FirestoreChannel.StreamingListener<BatchGetDocumentsResponse>() {

        @Override
        public void onMessage(BatchGetDocumentsResponse message) {
            responses.add(message);
            if (responses.size() == keys.size()) {
                Map<DocumentKey, MutableDocument> resultMap = new HashMap<>();
                for (BatchGetDocumentsResponse response : responses) {
                    MutableDocument doc = serializer.decodeMaybeDocument(response);
                    resultMap.put(doc.getKey(), doc);
                }
                List<MutableDocument> results = new ArrayList<>();
                for (DocumentKey key : keys) {
                    results.add(resultMap.get(key));
                }
                completionSource.trySetResult(results);
            }
        }

        @Override
        public void onClose(Status status) {
            if (status.isOk()) {
                completionSource.trySetResult(Collections.emptyList());
            } else {
                FirebaseFirestoreException exception = exceptionFromStatus(status);
                if (exception.getCode() == FirebaseFirestoreException.Code.UNAUTHENTICATED) {
                    channel.invalidateToken();
                }
                completionSource.trySetException(exception);
            }
        }
    });
    return completionSource.getTask();
}
Also used : Status(io.grpc.Status) Util.exceptionFromStatus(com.google.firebase.firestore.util.Util.exceptionFromStatus) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) DocumentKey(com.google.firebase.firestore.model.DocumentKey) BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BatchGetDocumentsResponse (com.google.firestore.v1.BatchGetDocumentsResponse)6 BatchGetDocumentsRequest (com.google.firestore.v1.BatchGetDocumentsRequest)5 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 ArrayList (java.util.ArrayList)2 TaskCompletionSource (com.google.android.gms.tasks.TaskCompletionSource)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 Util.exceptionFromStatus (com.google.firebase.firestore.util.Util.exceptionFromStatus)1 BatchGetDocumentsRequest (com.google.firestore.v1beta1.BatchGetDocumentsRequest)1 BatchGetDocumentsResponse (com.google.firestore.v1beta1.BatchGetDocumentsResponse)1 Document (com.google.firestore.v1beta1.Document)1 FirestoreGrpc (com.google.firestore.v1beta1.FirestoreGrpc)1 Status (io.grpc.Status)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 StreamObserver (io.grpc.stub.StreamObserver)1 HashMap (java.util.HashMap)1