use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class RemoteStore method handleWriteError.
private void handleWriteError(Status status) {
hardAssert(!status.isOk(), "Handling write error with status OK.");
// Only handle permanent errors here. If it's transient, just let the retry logic kick in.
if (Datastore.isPermanentWriteError(status)) {
// If this was a permanent error, the request itself was the problem so it's not going
// to succeed if we resend it.
MutationBatch batch = writePipeline.poll();
// In this case it's also unlikely that the server itself is melting down -- this was
// just a bad request, so inhibit backoff on the next restart
writeStream.inhibitBackoff();
remoteStoreCallback.handleRejectedWrite(batch.getBatchId(), status);
// It's possible that with the completion of this mutation another slot has freed up.
fillWritePipeline();
}
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class RemoteStore method handleWriteStreamMutationResults.
/**
* Handles a successful StreamingWriteResponse from the server that contains a mutation result.
*/
private void handleWriteStreamMutationResults(SnapshotVersion commitVersion, List<MutationResult> results) {
// This is a response to a write containing mutations and should be correlated to the first
// write in our write pipeline.
MutationBatch batch = writePipeline.poll();
MutationBatchResult mutationBatchResult = MutationBatchResult.create(batch, commitVersion, results, writeStream.getLastStreamToken());
remoteStoreCallback.handleSuccessfulWrite(mutationBatchResult);
// It's possible that with the completion of this mutation another slot has freed up.
fillWritePipeline();
}
use of com.google.firebase.firestore.model.mutation.MutationBatch in project firebase-android-sdk by firebase.
the class SQLiteMutationQueue method getAllMutationBatchesAffectingDocumentKey.
@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKey(DocumentKey documentKey) {
String path = EncodedPath.encode(documentKey.getPath());
List<MutationBatch> result = new ArrayList<>();
db.query("SELECT m.batch_id, SUBSTR(m.mutations, 1, ?) " + "FROM document_mutations dm, mutations m " + "WHERE dm.uid = ? " + "AND dm.path = ? " + "AND dm.uid = m.uid " + "AND dm.batch_id = m.batch_id " + "ORDER BY dm.batch_id").binding(BLOB_MAX_INLINE_LENGTH, uid, path).forEach(row -> result.add(decodeInlineMutationBatch(row.getInt(0), row.getBlob(1))));
return result;
}
use of com.google.firebase.firestore.model.mutation.MutationBatch 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.MutationBatch in project firebase-android-sdk by firebase.
the class SQLiteMutationQueue method getAllMutationBatchesAffectingDocumentKeys.
@Override
public List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(Iterable<DocumentKey> documentKeys) {
List<Object> args = new ArrayList<>();
for (DocumentKey key : documentKeys) {
args.add(EncodedPath.encode(key.getPath()));
}
SQLitePersistence.LongQuery longQuery = new SQLitePersistence.LongQuery(db, "SELECT DISTINCT dm.batch_id, SUBSTR(m.mutations, 1, ?) " + "FROM document_mutations dm, mutations m " + "WHERE dm.uid = ? " + "AND dm.path IN (", Arrays.asList(BLOB_MAX_INLINE_LENGTH, uid), args, ") " + "AND dm.uid = m.uid " + "AND dm.batch_id = m.batch_id " + "ORDER BY dm.batch_id");
List<MutationBatch> result = new ArrayList<>();
Set<Integer> uniqueBatchIds = new HashSet<>();
while (longQuery.hasMoreSubqueries()) {
longQuery.performNextSubquery().forEach(row -> {
int batchId = row.getInt(0);
if (!uniqueBatchIds.contains(batchId)) {
uniqueBatchIds.add(batchId);
result.add(decodeInlineMutationBatch(batchId, row.getBlob(1)));
}
});
}
// performance penalty on the normal case.
if (longQuery.getSubqueriesPerformed() > 1) {
Collections.sort(result, (MutationBatch lhs, MutationBatch rhs) -> Util.compareIntegers(lhs.getBatchId(), rhs.getBatchId()));
}
return result;
}
Aggregations