Search in sources :

Example 16 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class ReferenceSet method removeReferencesForId.

/**
 * Clears all references with a given ID. Calls removeReference() for each key removed.
 *
 * @return The keys of the documents that were removed.
 */
public ImmutableSortedSet<DocumentKey> removeReferencesForId(int targetId) {
    DocumentKey emptyKey = DocumentKey.empty();
    DocumentReference startRef = new DocumentReference(emptyKey, targetId);
    Iterator<DocumentReference> it = referencesByTarget.iteratorFrom(startRef);
    ImmutableSortedSet<DocumentKey> keys = DocumentKey.emptyKeySet();
    while (it.hasNext()) {
        DocumentReference ref = it.next();
        if (ref.getId() == targetId) {
            keys = keys.insert(ref.getKey());
            removeReference(ref);
        } else {
            break;
        }
    }
    return keys;
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 17 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class ReferenceSet method containsKey.

public boolean containsKey(DocumentKey key) {
    DocumentReference ref = new DocumentReference(key, 0);
    Iterator<DocumentReference> iterator = referencesByKey.iteratorFrom(ref);
    if (!iterator.hasNext()) {
        return false;
    }
    DocumentKey firstKey = iterator.next().getKey();
    return firstKey.equals(key);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 18 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class SQLiteIndexManager method start.

@Override
public void start() {
    Map<Integer, FieldIndex.IndexState> indexStates = new HashMap<>();
    // Fetch all index states if persisted for the user. These states contain per user information
    // on how up to date the index is.
    db.query("SELECT index_id, sequence_number, read_time_seconds, read_time_nanos, document_key, " + "largest_batch_id FROM index_state WHERE uid = ?").binding(uid).forEach(row -> {
        int indexId = row.getInt(0);
        long sequenceNumber = row.getLong(1);
        SnapshotVersion readTime = new SnapshotVersion(new Timestamp(row.getLong(2), row.getInt(3)));
        DocumentKey documentKey = DocumentKey.fromPath(EncodedPath.decodeResourcePath(row.getString(4)));
        int largestBatchId = row.getInt(5);
        indexStates.put(indexId, FieldIndex.IndexState.create(sequenceNumber, readTime, documentKey, largestBatchId));
    });
    // Fetch all indices and combine with user's index state if available.
    db.query("SELECT index_id, collection_group, index_proto FROM index_configuration").forEach(row -> {
        try {
            int indexId = row.getInt(0);
            String collectionGroup = row.getString(1);
            List<FieldIndex.Segment> segments = serializer.decodeFieldIndexSegments(Index.parseFrom(row.getBlob(2)));
            // If we fetched an index state for the user above, combine it with this index.
            // We use the default state if we don't have an index state (e.g. the index was
            // created while a different user as logged in).
            FieldIndex.IndexState indexState = indexStates.containsKey(indexId) ? indexStates.get(indexId) : FieldIndex.INITIAL_STATE;
            FieldIndex fieldIndex = FieldIndex.create(indexId, collectionGroup, segments, indexState);
            // Store the index and update `memoizedMaxIndexId` and `memoizedMaxSequenceNumber`.
            memoizeIndex(fieldIndex);
        } catch (InvalidProtocolBufferException e) {
            throw fail("Failed to decode index: " + e);
        }
    });
    started = true;
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) HashMap(java.util.HashMap) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Timestamp(com.google.firebase.Timestamp) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 19 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class SQLiteMutationQueue method removeMutationBatch.

@Override
public void removeMutationBatch(MutationBatch batch) {
    SQLiteStatement mutationDeleter = db.prepare("DELETE FROM mutations WHERE uid = ? AND batch_id = ?");
    SQLiteStatement indexDeleter = db.prepare("DELETE FROM document_mutations WHERE uid = ? AND path = ? AND batch_id = ?");
    int batchId = batch.getBatchId();
    int deleted = db.execute(mutationDeleter, uid, batchId);
    hardAssert(deleted != 0, "Mutation batch (%s, %d) did not exist", uid, batch.getBatchId());
    for (Mutation mutation : batch.getMutations()) {
        DocumentKey key = mutation.getKey();
        String path = EncodedPath.encode(key.getPath());
        db.execute(indexDeleter, uid, path, batchId);
        db.getReferenceDelegate().removeMutationReference(key);
    }
}
Also used : 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)

Example 20 with DocumentKey

use of com.google.firebase.firestore.model.DocumentKey in project firebase-android-sdk by firebase.

the class MemoryRemoteDocumentCache method removeAll.

@Override
public void removeAll(Collection<DocumentKey> keys) {
    hardAssert(indexManager != null, "setIndexManager() not called");
    ImmutableSortedMap<DocumentKey, Document> deletedDocs = emptyDocumentMap();
    for (DocumentKey key : keys) {
        docs = docs.remove(key);
        deletedDocs = deletedDocs.insert(key, MutableDocument.newNoDocument(key, SnapshotVersion.NONE));
    }
    indexManager.updateIndexEntries(deletedDocs);
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)134 MutableDocument (com.google.firebase.firestore.model.MutableDocument)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)29 ArrayList (java.util.ArrayList)25 Document (com.google.firebase.firestore.model.Document)23 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)16 Mutation (com.google.firebase.firestore.model.mutation.Mutation)15 Map (java.util.Map)15 ResourcePath (com.google.firebase.firestore.model.ResourcePath)13 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)11 HashSet (java.util.HashSet)11 Overlay (com.google.firebase.firestore.model.mutation.Overlay)10 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)9 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)7 ByteString (com.google.protobuf.ByteString)7 Query (com.google.firebase.firestore.core.Query)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 Timestamp (com.google.firebase.Timestamp)5