Search in sources :

Example 21 with MutationBatch

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();
    }
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch)

Example 22 with MutationBatch

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();
}
Also used : MutationBatchResult(com.google.firebase.firestore.model.mutation.MutationBatchResult) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch)

Example 23 with MutationBatch

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;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString)

Example 24 with MutationBatch

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;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) SQLiteStatement(android.database.sqlite.SQLiteStatement) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) ByteString(com.google.protobuf.ByteString) MessageLite(com.google.protobuf.MessageLite) HashSet(java.util.HashSet)

Example 25 with MutationBatch

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;
}
Also used : ArrayList(java.util.ArrayList) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) DocumentKey(com.google.firebase.firestore.model.DocumentKey) HashSet(java.util.HashSet)

Aggregations

MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)40 Test (org.junit.Test)15 Mutation (com.google.firebase.firestore.model.mutation.Mutation)13 ArrayList (java.util.ArrayList)12 DocumentKey (com.google.firebase.firestore.model.DocumentKey)11 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)8 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)6 Write (com.google.firestore.v1.Write)5 ByteString (com.google.protobuf.ByteString)5 HashSet (java.util.HashSet)5 WriteBatch (com.google.firebase.firestore.proto.WriteBatch)4 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)4 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Timestamp (com.google.firebase.Timestamp)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 Query (com.google.firebase.firestore.core.Query)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 MutationBatchResult (com.google.firebase.firestore.model.mutation.MutationBatchResult)2 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)2