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;
}
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);
}
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;
}
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);
}
}
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);
}
Aggregations