Search in sources :

Example 6 with FirestoreBlockingStub

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

the class CreateDocument method createDocumentCall.

public void createDocumentCall() {
    System.out.println("\n:: Creating New Document ::\n");
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Document Name: ");
    String docName = sc.next();
    HashMap<String, Value> fieldsMap;
    MakeFieldsMap mfm = new MakeFieldsMap();
    fieldsMap = mfm.makeFieldsMap();
    Document newDoc = Document.newBuilder().putAllFields(fieldsMap).build();
    CreateDocumentRequest createDocumentRequest = CreateDocumentRequest.newBuilder().setDocument(newDoc).setCollectionId("GrpcTestData").setParent("projects/firestoretestclient/databases/(default)/documents").setDocumentId(docName).build();
    Document finishedDoc;
    try {
        finishedDoc = blockingStub.createDocument(createDocumentRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    DrawDocument dd = new DrawDocument();
    dd.draw(finishedDoc);
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) Scanner(java.util.Scanner) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) CreateDocumentRequest(com.google.firestore.v1beta1.CreateDocumentRequest) MakeFieldsMap(org.roguewave.grpc.util.gfx.MakeFieldsMap) Value(com.google.firestore.v1beta1.Value) 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 7 with FirestoreBlockingStub

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

the class Write method writeCall.

public void writeCall() {
    System.out.println(":: Starting Write Stream ::");
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    FirestoreStub firestoreStub = new GRPCFirebaseClientFactory().createFirebaseClient().getFirestoreStub();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter document name: ");
    String docName = sc.next();
    GetDocumentRequest getDocumentRequest = GetDocumentRequest.newBuilder().setName("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + docName).build();
    Document doc;
    try {
        doc = blockingStub.getDocument(getDocumentRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    // Retrieve initial stream token and stream id
    WriteRequest writeRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").build();
    StreamObserver<WriteResponse> writeResponseStreamObserver = new StreamObserver<WriteResponse>() {

        @Override
        public void onNext(WriteResponse writeResponse) {
            Write.streamToken = writeResponse.getStreamToken();
            Write.streamId = writeResponse.getStreamId();
        }

        @Override
        public void onError(Throwable throwable) {
            System.out.println(throwable.getMessage() + throwable.getCause());
        }

        @Override
        public void onCompleted() {
        }
    };
    StreamObserver<WriteRequest> writeRequestStreamObserver = firestoreStub.write(writeResponseStreamObserver);
    writeRequestStreamObserver.onNext(writeRequest);
    String fieldName = "";
    String fieldValue = "";
    while (!fieldName.matches("DONE")) {
        System.out.print("Field Name (Enter DONE to quit): ");
        fieldName = sc.next();
        if (!fieldName.matches("DONE")) {
            System.out.print("Field Value: ");
            fieldValue = sc.next();
            Value fsValue = Value.newBuilder().setStringValue(fieldValue).build();
            doc = doc.toBuilder().putFields(fieldName, fsValue).build();
            DocumentMask docMask = DocumentMask.newBuilder().addFieldPaths(fieldName).build();
            com.google.firestore.v1beta1.Write currentWrite = com.google.firestore.v1beta1.Write.newBuilder().setUpdate(doc).setUpdateMask(docMask).build();
            WriteRequest currentWriteRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setStreamToken(Write.streamToken).setStreamId(Write.streamId).addWrites(currentWrite).build();
            writeRequestStreamObserver.onNext(currentWriteRequest);
        }
    }
    writeRequestStreamObserver.onCompleted();
    System.out.println("Finished streaming writes!");
    Menu menu = new Menu();
    menu.draw();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) Scanner(java.util.Scanner) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) FirestoreStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub) ByteString(com.google.protobuf.ByteString) com.google.firestore.v1beta1(com.google.firestore.v1beta1) Menu(org.roguewave.grpc.util.gfx.Menu)

Example 8 with FirestoreBlockingStub

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

the class Rollback method rollbackCall.

public void rollbackCall() {
    System.out.println("\n:: Rolling Back Transaction ::\n");
    if (Main.transactionId == null) {
        System.out.println("WARNING:  No current transaction open, run BeginTransaction first...");
        return;
    } else {
        System.out.println("Found Transaction ID '" + Main.transactionId.toString() + "'.  Committing....");
    }
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    RollbackRequest rollbackRequest = RollbackRequest.newBuilder().setTransaction(Main.transactionId).setDatabase("projects/firestoretestclient/databases/(default)").build();
    try {
        blockingStub.rollback(rollbackRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    System.out.println("Success!");
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) Menu(org.roguewave.grpc.util.gfx.Menu) RollbackRequest(com.google.firestore.v1beta1.RollbackRequest)

Aggregations

FirestoreBlockingStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub)8 GRPCFirebaseClientFactory (org.roguewave.grpc.util.GRPCFirebaseClientFactory)8 Menu (org.roguewave.grpc.util.gfx.Menu)8 Scanner (java.util.Scanner)4 DrawDocument (org.roguewave.grpc.util.gfx.DrawDocument)4 Document (com.google.firestore.v1beta1.Document)2 MakeFieldsMap (org.roguewave.grpc.util.gfx.MakeFieldsMap)2 com.google.firestore.v1beta1 (com.google.firestore.v1beta1)1 BeginTransactionRequest (com.google.firestore.v1beta1.BeginTransactionRequest)1 BeginTransactionResponse (com.google.firestore.v1beta1.BeginTransactionResponse)1 CommitRequest (com.google.firestore.v1beta1.CommitRequest)1 CreateDocumentRequest (com.google.firestore.v1beta1.CreateDocumentRequest)1 FirestoreStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub)1 ListDocumentsRequest (com.google.firestore.v1beta1.ListDocumentsRequest)1 ListDocumentsResponse (com.google.firestore.v1beta1.ListDocumentsResponse)1 RollbackRequest (com.google.firestore.v1beta1.RollbackRequest)1 TransactionOptions (com.google.firestore.v1beta1.TransactionOptions)1 Value (com.google.firestore.v1beta1.Value)1 ByteString (com.google.protobuf.ByteString)1 StreamObserver (io.grpc.stub.StreamObserver)1