use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalStore method notifyLocalViewChanges.
/**
* Notify the local store of the changed views to locally pin / unpin documents.
*/
public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
persistence.runTransaction("notifyLocalViewChanges", () -> {
for (LocalViewChanges viewChange : viewChanges) {
int targetId = viewChange.getTargetId();
localViewReferences.addReferences(viewChange.getAdded(), targetId);
ImmutableSortedSet<DocumentKey> removed = viewChange.getRemoved();
for (DocumentKey key : removed) {
persistence.getReferenceDelegate().removeReference(key);
}
localViewReferences.removeReferences(removed, targetId);
if (!viewChange.isFromCache()) {
TargetData targetData = queryDataByTarget.get(targetId);
hardAssert(targetData != null, "Can't set limbo-free snapshot version for unknown target: %s", targetId);
// Advance the last limbo free snapshot version
SnapshotVersion lastLimboFreeSnapshotVersion = targetData.getSnapshotVersion();
TargetData updatedTargetData = targetData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
queryDataByTarget.put(targetId, updatedTargetData);
}
}
});
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalStore method applyWriteToRemoteDocuments.
private void applyWriteToRemoteDocuments(MutationBatchResult batchResult) {
MutationBatch batch = batchResult.getBatch();
Set<DocumentKey> docKeys = batch.getKeys();
for (DocumentKey docKey : docKeys) {
MutableDocument doc = remoteDocuments.get(docKey);
SnapshotVersion ackVersion = batchResult.getDocVersions().get(docKey);
hardAssert(ackVersion != null, "docVersions should contain every doc in the write.");
if (doc.getVersion().compareTo(ackVersion) < 0) {
batch.applyToRemoteDocument(doc, batchResult);
if (doc.isValidDocument()) {
remoteDocuments.add(doc, batchResult.getCommitVersion());
}
}
}
mutationQueue.removeMutationBatch(batch);
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalStore method applyRemoteEvent.
/**
* Updates the "ground-state" (remote) documents. We assume that the remote event reflects any
* write batches that have been acknowledged or rejected (i.e. we do not re-apply local mutations
* to updates from this event).
*
* <p>LocalDocuments are re-calculated if there are remaining mutations in the queue.
*/
public ImmutableSortedMap<DocumentKey, Document> applyRemoteEvent(RemoteEvent remoteEvent) {
SnapshotVersion remoteVersion = remoteEvent.getSnapshotVersion();
// TODO: Call queryEngine.handleDocumentChange() appropriately.
return persistence.runTransaction("Apply remote event", () -> {
Map<Integer, TargetChange> targetChanges = remoteEvent.getTargetChanges();
long sequenceNumber = persistence.getReferenceDelegate().getCurrentSequenceNumber();
for (Map.Entry<Integer, TargetChange> entry : targetChanges.entrySet()) {
Integer boxedTargetId = entry.getKey();
int targetId = boxedTargetId;
TargetChange change = entry.getValue();
TargetData oldTargetData = queryDataByTarget.get(targetId);
if (oldTargetData == null) {
// we persist the updated query data along with the updated assignment.
continue;
}
targetCache.removeMatchingKeys(change.getRemovedDocuments(), targetId);
targetCache.addMatchingKeys(change.getAddedDocuments(), targetId);
TargetData newTargetData = oldTargetData.withSequenceNumber(sequenceNumber);
if (remoteEvent.getTargetMismatches().contains(targetId)) {
newTargetData = newTargetData.withResumeToken(ByteString.EMPTY, SnapshotVersion.NONE).withLastLimboFreeSnapshotVersion(SnapshotVersion.NONE);
} else if (!change.getResumeToken().isEmpty()) {
newTargetData = newTargetData.withResumeToken(change.getResumeToken(), remoteEvent.getSnapshotVersion());
}
queryDataByTarget.put(targetId, newTargetData);
// since the last update).
if (shouldPersistTargetData(oldTargetData, newTargetData, change)) {
targetCache.updateTargetData(newTargetData);
}
}
Map<DocumentKey, MutableDocument> documentUpdates = remoteEvent.getDocumentUpdates();
Set<DocumentKey> limboDocuments = remoteEvent.getResolvedLimboDocuments();
for (DocumentKey key : documentUpdates.keySet()) {
if (limboDocuments.contains(key)) {
persistence.getReferenceDelegate().updateLimboDocument(key);
}
}
DocumentChangeResult result = populateDocumentChanges(documentUpdates);
Map<DocumentKey, MutableDocument> changedDocs = result.changedDocuments;
// HACK: The only reason we allow snapshot version NONE is so that we can synthesize
// remote events when we get permission denied errors while trying to resolve the
// state of a locally cached document that is in limbo.
SnapshotVersion lastRemoteVersion = targetCache.getLastRemoteSnapshotVersion();
if (!remoteVersion.equals(SnapshotVersion.NONE)) {
hardAssert(remoteVersion.compareTo(lastRemoteVersion) >= 0, "Watch stream reverted to previous snapshot?? (%s < %s)", remoteVersion, lastRemoteVersion);
targetCache.setLastRemoteSnapshotVersion(remoteVersion);
}
return localDocuments.getLocalViewOfDocuments(changedDocs, result.existenceChangedKeys);
});
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalSerializer method decodeUnknownDocument.
/**
* Decodes a UnknownDocument proto to the equivalent model.
*/
private MutableDocument decodeUnknownDocument(com.google.firebase.firestore.proto.UnknownDocument proto) {
DocumentKey key = rpcSerializer.decodeKey(proto.getName());
SnapshotVersion version = rpcSerializer.decodeVersion(proto.getVersion());
return MutableDocument.newUnknownDocument(key, version);
}
use of com.google.firebase.firestore.model.SnapshotVersion in project firebase-android-sdk by firebase.
the class LocalSerializer method decodeTargetData.
TargetData decodeTargetData(com.google.firebase.firestore.proto.Target targetProto) {
int targetId = targetProto.getTargetId();
SnapshotVersion version = rpcSerializer.decodeVersion(targetProto.getSnapshotVersion());
SnapshotVersion lastLimboFreeSnapshotVersion = rpcSerializer.decodeVersion(targetProto.getLastLimboFreeSnapshotVersion());
ByteString resumeToken = targetProto.getResumeToken();
long sequenceNumber = targetProto.getLastListenSequenceNumber();
Target target;
switch(targetProto.getTargetTypeCase()) {
case DOCUMENTS:
target = rpcSerializer.decodeDocumentsTarget(targetProto.getDocuments());
break;
case QUERY:
target = rpcSerializer.decodeQueryTarget(targetProto.getQuery());
break;
default:
throw fail("Unknown targetType %d", targetProto.getTargetTypeCase());
}
return new TargetData(target, targetId, sequenceNumber, QueryPurpose.LISTEN, version, lastLimboFreeSnapshotVersion, resumeToken);
}
Aggregations