Search in sources :

Example 1 with ListDocumentsRequest

use of com.google.firestore.v1beta1.ListDocumentsRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class ListDocuments method listDocumentsCall.

public void listDocumentsCall() {
    System.out.println("\n:: Listing all Documents ::\n");
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    ListDocumentsRequest ldr = ListDocumentsRequest.newBuilder().setParent("projects/firestoretestclient/databases/(default)/documents").setCollectionId("GrpcTestData").build();
    try {
        ListDocumentsResponse listDocumentsResponse = blockingStub.listDocuments(ldr);
        List<Document> allDocs = listDocumentsResponse.getDocumentsList();
        DrawDocument dd = new DrawDocument();
        for (Document doc : allDocs) {
            dd.draw(doc);
        }
        Menu menu = new Menu();
        menu.draw();
        System.out.println("Finished call...");
    } catch (Exception e) {
        System.out.println("Error executing streaming stub call: " + (e.getMessage() + "\n" + e.getCause().toString()));
    }
}
Also used : ListDocumentsRequest(com.google.firestore.v1beta1.ListDocumentsRequest) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) ListDocumentsResponse(com.google.firestore.v1beta1.ListDocumentsResponse) Menu(org.roguewave.grpc.util.gfx.Menu) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument) Document(com.google.firestore.v1beta1.Document) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument)

Example 2 with ListDocumentsRequest

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

the class CollectionReference method listDocuments.

/**
 * Retrieves the list of documents in this collection.
 *
 * <p>The document references returned may include references to "missing documents", i.e.
 * document locations that have no document present but which contain subcollections with
 * documents. Attempting to read such a document reference (e.g. via `get()` or `onSnapshot()`)
 * will return a `DocumentSnapshot` whose `exists()` method returns false.
 *
 * @return The list of documents in this collection.
 */
@Nonnull
public Iterable<DocumentReference> listDocuments() {
    ListDocumentsRequest.Builder request = ListDocumentsRequest.newBuilder();
    request.setParent(options.getParentPath().toString());
    request.setCollectionId(options.getCollectionId());
    request.setMask(DocumentMask.getDefaultInstance());
    request.setShowMissing(true);
    final ListDocumentsPagedResponse response;
    final TraceUtil traceUtil = TraceUtil.getInstance();
    Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_LISTDOCUMENTS);
    try (Scope scope = traceUtil.getTracer().withSpan(span)) {
        FirestoreRpc client = rpcContext.getClient();
        UnaryCallable<ListDocumentsRequest, ListDocumentsPagedResponse> callable = client.listDocumentsPagedCallable();
        ListDocumentsRequest build = request.build();
        ApiFuture<ListDocumentsPagedResponse> future = rpcContext.sendRequest(build, callable);
        response = ApiExceptions.callAndTranslateApiException(future);
    } catch (ApiException exception) {
        span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
        throw FirestoreException.forApiException(exception);
    } finally {
        span.end(TraceUtil.END_SPAN_OPTIONS);
    }
    return new Iterable<DocumentReference>() {

        @Override
        @Nonnull
        public Iterator<DocumentReference> iterator() {
            final Iterator<Document> iterator = response.iterateAll().iterator();
            return new Iterator<DocumentReference>() {

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public DocumentReference next() {
                    ResourcePath path = ResourcePath.create(iterator.next().getName());
                    return document(path.getId());
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }
            };
        }
    };
}
Also used : ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) ListDocumentsPagedResponse(com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse) Document(com.google.firestore.v1.Document) Span(io.opencensus.trace.Span) Scope(io.opencensus.common.Scope) FirestoreRpc(com.google.cloud.firestore.spi.v1.FirestoreRpc) Iterator(java.util.Iterator) ApiException(com.google.api.gax.rpc.ApiException) Nonnull(javax.annotation.Nonnull)

Example 3 with ListDocumentsRequest

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

the class FirestoreClientTest method listDocumentsTest.

@Test
public void listDocumentsTest() throws Exception {
    Document responsesElement = Document.newBuilder().build();
    ListDocumentsResponse expectedResponse = ListDocumentsResponse.newBuilder().setNextPageToken("").addAllDocuments(Arrays.asList(responsesElement)).build();
    mockFirestore.addResponse(expectedResponse);
    ListDocumentsRequest request = ListDocumentsRequest.newBuilder().setParent("parent-995424086").setCollectionId("collectionId1636075609").setPageSize(883849137).setPageToken("pageToken873572522").setOrderBy("orderBy-1207110587").setMask(DocumentMask.newBuilder().build()).setShowMissing(true).build();
    ListDocumentsPagedResponse pagedListResponse = client.listDocuments(request);
    List<Document> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getDocumentsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockFirestore.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListDocumentsRequest actualRequest = ((ListDocumentsRequest) actualRequests.get(0));
    Assert.assertEquals(request.getParent(), actualRequest.getParent());
    Assert.assertEquals(request.getCollectionId(), actualRequest.getCollectionId());
    Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
    Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
    Assert.assertEquals(request.getOrderBy(), actualRequest.getOrderBy());
    Assert.assertEquals(request.getMask(), actualRequest.getMask());
    Assert.assertEquals(request.getTransaction(), actualRequest.getTransaction());
    Assert.assertEquals(request.getReadTime(), actualRequest.getReadTime());
    Assert.assertEquals(request.getShowMissing(), actualRequest.getShowMissing());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) AbstractMessage(com.google.protobuf.AbstractMessage) ListDocumentsResponse(com.google.firestore.v1.ListDocumentsResponse) ListDocumentsPagedResponse(com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse) Document(com.google.firestore.v1.Document) Test(org.junit.Test)

Example 4 with ListDocumentsRequest

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

the class FirestoreClientTest method listDocumentsExceptionTest.

@Test
public void listDocumentsExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockFirestore.addException(exception);
    try {
        ListDocumentsRequest request = ListDocumentsRequest.newBuilder().setParent("parent-995424086").setCollectionId("collectionId1636075609").setPageSize(883849137).setPageToken("pageToken873572522").setOrderBy("orderBy-1207110587").setMask(DocumentMask.newBuilder().build()).setShowMissing(true).build();
        client.listDocuments(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 5 with ListDocumentsRequest

use of com.google.firestore.v1beta1.ListDocumentsRequest in project beam by apache.

the class FirestoreV1FnListDocumentsTest method endToEnd.

@Test
public void endToEnd() throws Exception {
    // First page of the response
    ListDocumentsRequest request1 = ListDocumentsRequest.newBuilder().setParent(String.format("projects/%s/databases/(default)/document", projectId)).build();
    ListDocumentsResponse response1 = ListDocumentsResponse.newBuilder().addDocuments(Document.newBuilder().setName("doc_1-1").putAllFields(ImmutableMap.of("foo", Value.newBuilder().setStringValue("bar").build())).build()).addDocuments(Document.newBuilder().setName("doc_1-2").putAllFields(ImmutableMap.of("foo", Value.newBuilder().setStringValue("bar").build())).build()).addDocuments(Document.newBuilder().setName("doc_1-3").putAllFields(ImmutableMap.of("foo", Value.newBuilder().setStringValue("bar").build())).build()).setNextPageToken("page2").build();
    when(page1.getNextPageToken()).thenReturn(response1.getNextPageToken());
    when(page1.getResponse()).thenReturn(response1);
    when(page1.hasNextPage()).thenReturn(true);
    // Second page of the response
    ListDocumentsResponse response2 = ListDocumentsResponse.newBuilder().addDocuments(Document.newBuilder().setName("doc_2-1").putAllFields(ImmutableMap.of("foo", Value.newBuilder().setStringValue("bar").build())).build()).build();
    when(page2.getResponse()).thenReturn(response2);
    when(page2.hasNextPage()).thenReturn(false);
    when(pagedResponse1.iteratePages()).thenReturn(ImmutableList.of(page1, page2));
    when(callable.call(request1)).thenReturn(pagedResponse1);
    when(stub.listDocumentsPagedCallable()).thenReturn(callable);
    when(ff.getFirestoreStub(any())).thenReturn(stub);
    RpcQosOptions options = RpcQosOptions.defaultOptions();
    when(ff.getRpcQos(any())).thenReturn(FirestoreStatefulComponentFactory.INSTANCE.getRpcQos(options));
    ArgumentCaptor<ListDocumentsResponse> responses = ArgumentCaptor.forClass(ListDocumentsResponse.class);
    doNothing().when(processContext).output(responses.capture());
    when(processContext.element()).thenReturn(request1);
    ListDocumentsFn fn = new ListDocumentsFn(clock, ff, options);
    runFunction(fn);
    List<ListDocumentsResponse> expected = newArrayList(response1, response2);
    List<ListDocumentsResponse> allValues = responses.getAllValues();
    assertEquals(expected, allValues);
}
Also used : ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) ListDocumentsFn(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.ListDocumentsFn) ListDocumentsResponse(com.google.firestore.v1.ListDocumentsResponse) Test(org.junit.Test)

Aggregations

ListDocumentsRequest (com.google.firestore.v1.ListDocumentsRequest)6 ListDocumentsPagedResponse (com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPagedResponse)3 ListDocumentsResponse (com.google.firestore.v1.ListDocumentsResponse)3 Test (org.junit.Test)3 Document (com.google.firestore.v1.Document)2 Iterator (java.util.Iterator)2 ListDocumentsFn (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.ListDocumentsFn)2 ApiException (com.google.api.gax.rpc.ApiException)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 FirestoreRpc (com.google.cloud.firestore.spi.v1.FirestoreRpc)1 ListDocumentsPage (com.google.cloud.firestore.v1.FirestoreClient.ListDocumentsPage)1 Document (com.google.firestore.v1beta1.Document)1 FirestoreBlockingStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub)1 ListDocumentsRequest (com.google.firestore.v1beta1.ListDocumentsRequest)1 ListDocumentsResponse (com.google.firestore.v1beta1.ListDocumentsResponse)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 Scope (io.opencensus.common.Scope)1 Span (io.opencensus.trace.Span)1 Nonnull (javax.annotation.Nonnull)1