Search in sources :

Example 1 with ProtoChange

use of com.google.startupos.common.firestore.ProtoChange in project startup-os by google.

the class FirestoreClientTool method testFunctionality.

public void testFunctionality() {
    WriteResult result = client.setProtoDocument("test/bla", Diff.newBuilder().setId(123).build());
    System.out.println("Update time : " + result.getUpdateTime());
    client.addCollectionListener("/reviewer/ci/requests", CiRequest.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<CiRequest>>() {

        @Override
        public void onEvent(@Nullable ProtoQuerySnapshot<CiRequest> snapshot, @Nullable RuntimeException e) {
            if (e != null) {
                System.err.println("Listen failed: " + e);
                return;
            }
            ImmutableList<CiRequest> protos = snapshot.getProtos();
            for (CiRequest ciRequest : protos) {
                System.out.println(ciRequest);
            }
            ImmutableList<ProtoChange<CiRequest>> protoChanges = snapshot.getProtoChanges();
            for (ProtoChange<CiRequest> protoChange : protoChanges) {
                System.out.println(protoChange.getProto());
                System.out.println(protoChange.getType());
                System.out.println(protoChange.getOldIndex());
                System.out.println(protoChange.getNewIndex());
            }
        }
    });
}
Also used : CiRequest(com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest) WriteResult(com.google.cloud.firestore.WriteResult) ImmutableList(com.google.common.collect.ImmutableList) ProtoQuerySnapshot(com.google.startupos.common.firestore.ProtoQuerySnapshot) ProtoChange(com.google.startupos.common.firestore.ProtoChange)

Example 2 with ProtoChange

use of com.google.startupos.common.firestore.ProtoChange in project startup-os by google.

the class CiTask method tryRegisterListener.

public void tryRegisterListener() {
    if (!listenerRegistered) {
        firestoreClient.addCollectionListener(ReviewerConstants.CI_REQUESTS_PATH, CiRequest.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<CiRequest>>() {

            @Override
            public void onEvent(@Nullable ProtoQuerySnapshot<CiRequest> snapshot, @Nullable RuntimeException e) {
                if (e != null) {
                    System.err.println("Listen failed: " + e);
                    return;
                }
                ArrayList<CiRequest> updatedRequests = new ArrayList(requests);
                for (ProtoChange<CiRequest> protoChange : snapshot.getProtoChanges()) {
                    switch(protoChange.getType()) {
                        case ADDED:
                            updatedRequests.add(protoChange.getProto());
                            break;
                        case MODIFIED:
                            // The indices are made so that this type of sequential remove & add works, see:
                            // https://googleapis.github.io/google-cloud-java/google-cloud-clients/apidocs/com/google/cloud/firestore/DocumentChange.html#getNewIndex--
                            updatedRequests.remove(protoChange.getOldIndex());
                            updatedRequests.add(protoChange.getNewIndex(), protoChange.getProto());
                            break;
                        case REMOVED:
                            updatedRequests.remove(protoChange.getOldIndex());
                            break;
                        default:
                            throw new IllegalStateException("Unknown enum " + protoChange.getType());
                    }
                }
                requests = ImmutableList.copyOf(updatedRequests);
                run();
            }
        });
        listenerRegistered = true;
    }
}
Also used : CiRequest(com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest) ProtoQuerySnapshot(com.google.startupos.common.firestore.ProtoQuerySnapshot) ArrayList(java.util.ArrayList) ProtoChange(com.google.startupos.common.firestore.ProtoChange)

Example 3 with ProtoChange

use of com.google.startupos.common.firestore.ProtoChange in project startup-os by google.

the class SubmitterTask method tryRegisterListener.

public void tryRegisterListener() {
    if (!listenerRegistered) {
        firestoreClient.addCollectionListener(ReviewerConstants.DIFF_COLLECTION, Diff.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<Diff>>() {

            @Override
            public void onEvent(@Nullable ProtoQuerySnapshot<Diff> snapshot, @Nullable RuntimeException e) {
                if (e != null) {
                    System.err.println("Listen failed: " + e);
                    return;
                }
                ArrayList<Diff> updatedDiffs = new ArrayList(diffs);
                log.atInfo().log("Received %d changes", snapshot.getProtoChanges().size());
                for (ProtoChange<Diff> protoChange : snapshot.getProtoChanges()) {
                    switch(protoChange.getType()) {
                        case ADDED:
                            updatedDiffs.add(protoChange.getProto());
                            break;
                        case MODIFIED:
                            // The indices are made so that this type of sequential remove & add works, see:
                            // https://googleapis.github.io/google-cloud-java/google-cloud-clients/apidocs/com/google/cloud/firestore/DocumentChange.html#getNewIndex--
                            updatedDiffs.remove(protoChange.getOldIndex());
                            updatedDiffs.add(protoChange.getNewIndex(), protoChange.getProto());
                            break;
                        case REMOVED:
                            updatedDiffs.remove(protoChange.getOldIndex());
                            break;
                        default:
                            throw new IllegalStateException("Unknown enum " + protoChange.getType());
                    }
                }
                diffs = ImmutableList.copyOf(updatedDiffs);
                run();
            }
        });
        listenerRegistered = true;
    }
}
Also used : Diff(com.google.startupos.tools.reviewer.local_server.service.Protos.Diff) ProtoQuerySnapshot(com.google.startupos.common.firestore.ProtoQuerySnapshot) ArrayList(java.util.ArrayList) ProtoChange(com.google.startupos.common.firestore.ProtoChange)

Aggregations

ProtoChange (com.google.startupos.common.firestore.ProtoChange)3 ProtoQuerySnapshot (com.google.startupos.common.firestore.ProtoQuerySnapshot)3 CiRequest (com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest)2 ArrayList (java.util.ArrayList)2 WriteResult (com.google.cloud.firestore.WriteResult)1 ImmutableList (com.google.common.collect.ImmutableList)1 Diff (com.google.startupos.tools.reviewer.local_server.service.Protos.Diff)1