Search in sources :

Example 1 with DocumentRemove

use of com.google.firestore.v1.DocumentRemove in project firebase-android-sdk by firebase.

the class RemoteSerializer method decodeWatchChange.

// Watch changes
public WatchChange decodeWatchChange(ListenResponse protoChange) {
    WatchChange watchChange;
    switch(protoChange.getResponseTypeCase()) {
        case TARGET_CHANGE:
            com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange();
            WatchTargetChangeType changeType;
            Status cause = null;
            switch(targetChange.getTargetChangeType()) {
                case NO_CHANGE:
                    changeType = WatchTargetChangeType.NoChange;
                    break;
                case ADD:
                    changeType = WatchTargetChangeType.Added;
                    break;
                case REMOVE:
                    changeType = WatchTargetChangeType.Removed;
                    cause = fromStatus(targetChange.getCause());
                    break;
                case CURRENT:
                    changeType = WatchTargetChangeType.Current;
                    break;
                case RESET:
                    changeType = WatchTargetChangeType.Reset;
                    break;
                case UNRECOGNIZED:
                default:
                    throw new IllegalArgumentException("Unknown target change type");
            }
            watchChange = new WatchTargetChange(changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause);
            break;
        case DOCUMENT_CHANGE:
            DocumentChange docChange = protoChange.getDocumentChange();
            List<Integer> added = docChange.getTargetIdsList();
            List<Integer> removed = docChange.getRemovedTargetIdsList();
            DocumentKey key = decodeKey(docChange.getDocument().getName());
            SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime());
            hardAssert(!version.equals(SnapshotVersion.NONE), "Got a document change without an update time");
            ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap());
            MutableDocument document = MutableDocument.newFoundDocument(key, version, data);
            watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document);
            break;
        case DOCUMENT_DELETE:
            DocumentDelete docDelete = protoChange.getDocumentDelete();
            removed = docDelete.getRemovedTargetIdsList();
            key = decodeKey(docDelete.getDocument());
            // Note that version might be unset in which case we use SnapshotVersion.NONE
            version = decodeVersion(docDelete.getReadTime());
            MutableDocument doc = MutableDocument.newNoDocument(key, version);
            watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc);
            break;
        case DOCUMENT_REMOVE:
            DocumentRemove docRemove = protoChange.getDocumentRemove();
            removed = docRemove.getRemovedTargetIdsList();
            key = decodeKey(docRemove.getDocument());
            watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null);
            break;
        case FILTER:
            com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter();
            // TODO: implement existence filter parsing (see b/33076578)
            ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount());
            int targetId = protoFilter.getTargetId();
            watchChange = new ExistenceFilterWatchChange(targetId, filter);
            break;
        case RESPONSETYPE_NOT_SET:
        default:
            throw new IllegalArgumentException("Unknown change type set");
    }
    return watchChange;
}
Also used : Status(io.grpc.Status) DocumentRemove(com.google.firestore.v1.DocumentRemove) MutableDocument(com.google.firebase.firestore.model.MutableDocument) WatchTargetChangeType(com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType) DocumentChange(com.google.firestore.v1.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) DocumentDelete(com.google.firestore.v1.DocumentDelete) ObjectValue(com.google.firebase.firestore.model.ObjectValue) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1 ExistenceFilterWatchChange (com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)1 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)1 WatchTargetChangeType (com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType)1 DocumentChange (com.google.firestore.v1.DocumentChange)1 DocumentDelete (com.google.firestore.v1.DocumentDelete)1 DocumentRemove (com.google.firestore.v1.DocumentRemove)1 Status (io.grpc.Status)1