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());
}
}
});
}
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;
}
}
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;
}
}
Aggregations