what-the-bank/sources/com/google/firebase/firestore/local/MemoryMutationQueue.java

183 lines
8.2 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.firestore.local;
import com.google.firebase.Timestamp;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.auth.User;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.mutation.Mutation;
import com.google.firebase.firestore.model.mutation.MutationBatch;
import com.google.firebase.firestore.remote.WriteStream;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.Preconditions;
import com.google.firebase.firestore.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import o.QwV;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class MemoryMutationQueue implements MutationQueue {
private final MemoryIndexManager indexManager;
private final MemoryPersistence persistence;
private final List<MutationBatch> queue = new ArrayList();
private ImmutableSortedSet<DocumentReference> batchesByDocumentKey = new ImmutableSortedSet<>(Collections.emptyList(), DocumentReference.BY_KEY);
private int nextBatchId = 1;
private QwV lastStreamToken = WriteStream.EMPTY_STREAM_TOKEN;
/* JADX INFO: Access modifiers changed from: package-private */
public MemoryMutationQueue(MemoryPersistence memoryPersistence, User user) {
this.persistence = memoryPersistence;
this.indexManager = memoryPersistence.getIndexManager(user);
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final void start() {
if (isEmpty()) {
this.nextBatchId = 1;
}
}
public final boolean isEmpty() {
return this.queue.isEmpty();
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final void acknowledgeBatch(MutationBatch mutationBatch, QwV qwV) {
int batchId = mutationBatch.getBatchId();
int indexOfExistingBatchId = indexOfExistingBatchId(batchId, "acknowledged");
Assert.hardAssert(indexOfExistingBatchId == 0, "Can only acknowledge the first batch in the mutation queue", new Object[0]);
MutationBatch mutationBatch2 = this.queue.get(indexOfExistingBatchId);
Assert.hardAssert(batchId == mutationBatch2.getBatchId(), "Queue ordering failure: expected batch %d, got batch %d", Integer.valueOf(batchId), Integer.valueOf(mutationBatch2.getBatchId()));
this.lastStreamToken = (QwV) Preconditions.checkNotNull(qwV);
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final void setLastStreamToken(QwV qwV) {
this.lastStreamToken = (QwV) Preconditions.checkNotNull(qwV);
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final MutationBatch addMutationBatch(Timestamp timestamp, List<Mutation> list, List<Mutation> list2) {
Assert.hardAssert(!list2.isEmpty(), "Mutation batches should not be empty", new Object[0]);
int i = this.nextBatchId;
this.nextBatchId = i + 1;
int size = this.queue.size();
if (size > 0) {
Assert.hardAssert(this.queue.get(size - 1).getBatchId() < i, "Mutation batchIds must be monotonically increasing order", new Object[0]);
}
MutationBatch mutationBatch = new MutationBatch(i, timestamp, list, list2);
this.queue.add(mutationBatch);
for (Mutation mutation : list2) {
this.batchesByDocumentKey = this.batchesByDocumentKey.insert(new DocumentReference(mutation.getKey(), i));
this.indexManager.addToCollectionParentIndex(mutation.getKey().getCollectionPath());
}
return mutationBatch;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final MutationBatch lookupMutationBatch(int i) {
int indexOfBatchId = indexOfBatchId(i);
if (indexOfBatchId < 0 || indexOfBatchId >= this.queue.size()) {
return null;
}
MutationBatch mutationBatch = this.queue.get(indexOfBatchId);
Assert.hardAssert(mutationBatch.getBatchId() == i, "If found batch must match", new Object[0]);
return mutationBatch;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final MutationBatch getNextMutationBatchAfterBatchId(int i) {
int indexOfBatchId = indexOfBatchId(i + 1);
if (indexOfBatchId < 0) {
indexOfBatchId = 0;
}
if (this.queue.size() > indexOfBatchId) {
return this.queue.get(indexOfBatchId);
}
return null;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final List<MutationBatch> getAllMutationBatches() {
return Collections.unmodifiableList(this.queue);
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final List<MutationBatch> getAllMutationBatchesAffectingDocumentKeys(Iterable<DocumentKey> iterable) {
ImmutableSortedSet<Integer> immutableSortedSet = new ImmutableSortedSet<>(Collections.emptyList(), Util.comparator());
for (DocumentKey documentKey : iterable) {
Iterator<DocumentReference> iteratorFrom = this.batchesByDocumentKey.iteratorFrom(new DocumentReference(documentKey, 0));
while (iteratorFrom.hasNext()) {
DocumentReference next = iteratorFrom.next();
if (!documentKey.equals(next.getKey())) {
break;
}
immutableSortedSet = immutableSortedSet.insert(Integer.valueOf(next.getId()));
}
}
return lookupMutationBatches(immutableSortedSet);
}
private List<MutationBatch> lookupMutationBatches(ImmutableSortedSet<Integer> immutableSortedSet) {
ArrayList arrayList = new ArrayList();
Iterator<Integer> it = immutableSortedSet.iterator();
while (it.hasNext()) {
MutationBatch lookupMutationBatch = lookupMutationBatch(it.next().intValue());
if (lookupMutationBatch != null) {
arrayList.add(lookupMutationBatch);
}
}
return arrayList;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final void removeMutationBatch(MutationBatch mutationBatch) {
Assert.hardAssert(indexOfExistingBatchId(mutationBatch.getBatchId(), "removed") == 0, "Can only remove the first entry of the mutation queue", new Object[0]);
this.queue.remove(0);
ImmutableSortedSet<DocumentReference> immutableSortedSet = this.batchesByDocumentKey;
Iterator<Mutation> it = mutationBatch.getMutations().iterator();
while (it.hasNext()) {
DocumentKey key = it.next().getKey();
this.persistence.getReferenceDelegate().removeMutationReference(key);
immutableSortedSet = immutableSortedSet.remove(new DocumentReference(key, mutationBatch.getBatchId()));
}
this.batchesByDocumentKey = immutableSortedSet;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final void performConsistencyCheck() {
if (this.queue.isEmpty()) {
Assert.hardAssert(this.batchesByDocumentKey.isEmpty(), "Document leak -- detected dangling mutation references when queue is empty.", new Object[0]);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public final boolean containsKey(DocumentKey documentKey) {
Iterator<DocumentReference> iteratorFrom = this.batchesByDocumentKey.iteratorFrom(new DocumentReference(documentKey, 0));
if (iteratorFrom.hasNext()) {
return iteratorFrom.next().getKey().equals(documentKey);
}
return false;
}
private int indexOfBatchId(int i) {
if (this.queue.isEmpty()) {
return 0;
}
return i - this.queue.get(0).getBatchId();
}
private int indexOfExistingBatchId(int i, String str) {
int indexOfBatchId = indexOfBatchId(i);
Assert.hardAssert(indexOfBatchId >= 0 && indexOfBatchId < this.queue.size(), "Batches must exist to be %s", str);
return indexOfBatchId;
}
@Override // com.google.firebase.firestore.local.MutationQueue
public final QwV getLastStreamToken() {
return this.lastStreamToken;
}
}