use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.
the class SQLiteMutationQueue method addMutationBatch.
@Override
public MutationBatch addMutationBatch(Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
int batchId = nextBatchId;
nextBatchId += 1;
MutationBatch batch = new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
MessageLite proto = serializer.encodeMutationBatch(batch);
db.execute("INSERT INTO mutations (uid, batch_id, mutations) VALUES (?, ?, ?)", uid, batchId, proto.toByteArray());
// PORTING NOTE: Unlike LevelDB, these entries must be unique.
// Since user and batchId are fixed within this function body, it's enough to track unique keys
// added in this batch.
Set<DocumentKey> inserted = new HashSet<>();
SQLiteStatement indexInserter = db.prepare("INSERT INTO document_mutations (uid, path, batch_id) VALUES (?, ?, ?)");
for (Mutation mutation : mutations) {
DocumentKey key = mutation.getKey();
if (!inserted.add(key)) {
continue;
}
String path = EncodedPath.encode(key.getPath());
db.execute(indexInserter, uid, path, batchId);
indexManager.addToCollectionParentIndex(key.getCollectionPath());
}
return batch;
}
use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.
the class Transaction method commit.
public Task<Void> commit() {
ensureCommitNotCalled();
if (lastWriteError != null) {
return Tasks.forException(lastWriteError);
}
HashSet<DocumentKey> unwritten = new HashSet<>(readVersions.keySet());
// For each mutation, note that the doc was written.
for (Mutation mutation : mutations) {
unwritten.remove(mutation.getKey());
}
// For each document that was read but not written to, we want to perform a `verify` operation.
for (DocumentKey key : unwritten) {
mutations.add(new VerifyMutation(key, precondition(key)));
}
committed = true;
return datastore.commit(mutations).continueWithTask(Executors.DIRECT_EXECUTOR, task -> {
if (task.isSuccessful()) {
return Tasks.forResult(null);
} else {
return Tasks.forException(task.getException());
}
});
}
use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.
the class MemoryMutationQueue method removeMutationBatch.
@Override
public void removeMutationBatch(MutationBatch batch) {
// Find the position of the first batch for removal. This need not be the first entry in the
// queue.
int batchIndex = indexOfExistingBatchId(batch.getBatchId(), "removed");
hardAssert(batchIndex == 0, "Can only remove the first entry of the mutation queue");
queue.remove(0);
// Remove entries from the index too.
ImmutableSortedSet<DocumentReference> references = batchesByDocumentKey;
for (Mutation mutation : batch.getMutations()) {
DocumentKey key = mutation.getKey();
persistence.getReferenceDelegate().removeMutationReference(key);
DocumentReference reference = new DocumentReference(key, batch.getBatchId());
references = references.remove(reference);
}
batchesByDocumentKey = references;
}
use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.
the class MemoryMutationQueue method addMutationBatch.
@Override
public MutationBatch addMutationBatch(Timestamp localWriteTime, List<Mutation> baseMutations, List<Mutation> mutations) {
hardAssert(!mutations.isEmpty(), "Mutation batches should not be empty");
int batchId = nextBatchId;
nextBatchId += 1;
int size = queue.size();
if (size > 0) {
MutationBatch prior = queue.get(size - 1);
hardAssert(prior.getBatchId() < batchId, "Mutation batchIds must be monotonically increasing order");
}
MutationBatch batch = new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
queue.add(batch);
// Track references by document key and index collection parents.
for (Mutation mutation : mutations) {
batchesByDocumentKey = batchesByDocumentKey.insert(new DocumentReference(mutation.getKey(), batchId));
indexManager.addToCollectionParentIndex(mutation.getKey().getCollectionPath());
}
return batch;
}
use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.
the class WriteStream method writeMutations.
/**
* Sends a list of mutations to the Firestore backend to apply
*
* @param mutations The mutations
*/
void writeMutations(List<Mutation> mutations) {
hardAssert(isOpen(), "Writing mutations requires an opened stream");
hardAssert(handshakeComplete, "Handshake must be complete before writing mutations");
WriteRequest.Builder request = WriteRequest.newBuilder();
for (Mutation mutation : mutations) {
request.addWrites(serializer.encodeMutation(mutation));
}
request.setStreamToken(lastStreamToken);
writeRequest(request.build());
}
Aggregations