use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class LocalStore method handleUserChange.
// PORTING NOTE: no shutdown for LocalStore or persistence components on Android.
public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {
// Swap out the mutation queue, grabbing the pending mutation batches before and after.
List<MutationBatch> oldBatches = mutationQueue.getAllMutationBatches();
initializeUserComponents(user);
startIndexManager();
startMutationQueue();
List<MutationBatch> newBatches = mutationQueue.getAllMutationBatches();
// Union the old/new changed keys.
ImmutableSortedSet<DocumentKey> changedKeys = DocumentKey.emptyKeySet();
for (List<MutationBatch> batches : asList(oldBatches, newBatches)) {
for (MutationBatch batch : batches) {
for (Mutation mutation : batch.getMutations()) {
changedKeys = changedKeys.insert(mutation.getKey());
}
}
}
// Return the set of all (potentially) changed documents as the result of the user change.
return localDocuments.getDocuments(changedKeys);
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class MemoryMutationQueue method indexOfBatchId.
// Helpers
/**
* Finds the index of the given batchId in the mutation queue. This operation is O(1).
*
* @return The computed index of the batch with the given batchId, based on the state of the
* queue. Note this index can be negative if the requested batchId has already been removed
* from the queue or past the end of the queue if the batchId is larger than the last added
* batch.
*/
private int indexOfBatchId(int batchId) {
if (queue.isEmpty()) {
// As an index this is past the end of the queue
return 0;
}
// Examine the front of the queue to figure out the difference between the batchId and indexes
// in the array. Note that since the queue is ordered by batchId, if the first batch has a
// larger batchId then the requested batchId doesn't exist in the queue.
MutationBatch firstBatch = queue.get(0);
int firstBatchId = firstBatch.getBatchId();
return batchId - firstBatchId;
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class MemoryMutationQueue method getAllMutationBatchesAffectingDocumentKey.
@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(DocumentKey documentKey) {
DocumentReference start = new DocumentReference(documentKey, 0);
List<MutationBatch> result = new ArrayList<>();
Iterator<DocumentReference> iterator = batchesByDocumentKey.iteratorFrom(start);
while (iterator.hasNext()) {
DocumentReference reference = iterator.next();
if (!documentKey.equals(reference.getKey())) {
break;
}
MutationBatch batch = lookupMutationBatch(reference.getId());
hardAssert(batch != null, "Batches in the index must exist in the main table");
result.add(batch);
}
return result;
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class MemoryMutationQueue method acknowledgeBatch.
@Override
public void acknowledgeBatch(MutationBatch batch, ByteString streamToken) {
int batchId = batch.getBatchId();
int batchIndex = indexOfExistingBatchId(batchId, "acknowledged");
hardAssert(batchIndex == 0, "Can only acknowledge the first batch in the mutation queue");
// Verify that the batch in the queue is the one to be acknowledged.
MutationBatch check = queue.get(batchIndex);
hardAssert(batchId == check.getBatchId(), "Queue ordering failure: expected batch %d, got batch %d", batchId, check.getBatchId());
lastStreamToken = checkNotNull(streamToken);
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class LocalSerializerTest method testMultipleMutationsAreSquashed.
// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testMultipleMutationsAreSquashed() {
// INPUT:
// SetMutation -> SetMutation -> TransformMutation -> DeleteMutation -> PatchMutation ->
// TransformMutation -> PatchMutation
// OUTPUT (squashed):
// SetMutation -> SetMutation -> DeleteMutation -> PatchMutation -> PatchMutation
WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addAllWrites(asList(setProto, setProto, transformProto, deleteProto, patchProto, transformProto, patchProto)).setLocalWriteTime(writeTimeProto).build();
MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
assertEquals(5, decoded.getMutations().size());
List<Write> allExpected = asList(new TestWriteBuilder().addSet().build(), new TestWriteBuilder().addSet().addUpdateTransforms().build(), new TestWriteBuilder().addDelete().build(), new TestWriteBuilder().addPatch().addUpdateTransforms().build(), new TestWriteBuilder().addPatch().build());
for (int i = 0; i < decoded.getMutations().size(); i++) {
Mutation mutation = decoded.getMutations().get(i);
Write encoded = remoteSerializer.encodeMutation(mutation);
assertEquals(allExpected.get(i), encoded);
}
}
Aggregations