package com.google.firebase.firestore.local; import android.database.Cursor; import com.google.firebase.firestore.auth.User; import com.google.firebase.firestore.local.SQLitePersistence; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.ResourcePath; import com.google.firebase.firestore.model.mutation.Mutation; import com.google.firebase.firestore.model.mutation.Overlay; import com.google.firebase.firestore.util.Assert; import com.google.firebase.firestore.util.BackgroundQueue; import com.google.firebase.firestore.util.Consumer; import com.google.firebase.firestore.util.Executors; import com.google.firebase.firestore.util.Function; import com.google.firebase.firestore.util.Preconditions; import com.google.protobuf.InvalidProtocolBufferException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.SortedSet; import o.JsL; /* loaded from: classes2.dex */ public class SQLiteDocumentOverlayCache implements DocumentOverlayCache { private final SQLitePersistence db; private final LocalSerializer serializer; private final String uid; public SQLiteDocumentOverlayCache(SQLitePersistence sQLitePersistence, LocalSerializer localSerializer, User user) { this.db = sQLitePersistence; this.serializer = localSerializer; this.uid = user.isAuthenticated() ? user.getUid() : ""; } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public Overlay getOverlay(DocumentKey documentKey) { return (Overlay) this.db.query("SELECT overlay_mutation, largest_batch_id FROM document_overlays WHERE uid = ? AND collection_path = ? AND document_id = ?").binding(this.uid, EncodedPath.encode(documentKey.getPath().popLast()), documentKey.getPath().getLastSegment()).firstValue(new Function(this) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda0 public final SQLiteDocumentOverlayCache f$0; @Override // com.google.firebase.firestore.util.Function public final Object apply(Object obj) { return this.f$0.m198xe0a2a77c((Cursor) obj); } { this.f$0 = this; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$getOverlay$0$com-google-firebase-firestore-local-SQLiteDocumentOverlayCache, reason: not valid java name */ public /* synthetic */ Overlay m198xe0a2a77c(Cursor cursor) { return decodeOverlay(cursor.getBlob(0), cursor.getInt(1)); } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public Map getOverlays(SortedSet sortedSet) { Assert.hardAssert(sortedSet.comparator() == null, "getOverlays() requires natural order", new Object[0]); HashMap hashMap = new HashMap(); BackgroundQueue backgroundQueue = new BackgroundQueue(); ResourcePath resourcePath = ResourcePath.EMPTY; ArrayList arrayList = new ArrayList(); for (DocumentKey documentKey : sortedSet) { if (!resourcePath.equals(documentKey.getCollectionPath())) { processSingleCollection(hashMap, backgroundQueue, resourcePath, arrayList); resourcePath = documentKey.getCollectionPath(); arrayList.clear(); } arrayList.add(documentKey.getDocumentId()); } processSingleCollection(hashMap, backgroundQueue, resourcePath, arrayList); backgroundQueue.drain(); return hashMap; } private void processSingleCollection(final Map map, final BackgroundQueue backgroundQueue, ResourcePath resourcePath, List list) { if (list.isEmpty()) { return; } SQLitePersistence.LongQuery longQuery = new SQLitePersistence.LongQuery(this.db, "SELECT overlay_mutation, largest_batch_id FROM document_overlays WHERE uid = ? AND collection_path = ? AND document_id IN (", Arrays.asList(this.uid, EncodedPath.encode(resourcePath)), list, ")"); while (longQuery.hasMoreSubqueries()) { longQuery.performNextSubquery().forEach(new Consumer(this, backgroundQueue, map) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda1 public final SQLiteDocumentOverlayCache f$0; public final BackgroundQueue f$1; public final Map f$2; @Override // com.google.firebase.firestore.util.Consumer public final void accept(Object obj) { this.f$0.m203x634144da(this.f$1, this.f$2, (Cursor) obj); } { this.f$0 = this; this.f$1 = backgroundQueue; this.f$2 = map; } }); } } private void saveOverlay(int i, DocumentKey documentKey, Mutation mutation) { String collectionGroup = documentKey.getCollectionGroup(); String encode = EncodedPath.encode(documentKey.getPath().popLast()); String lastSegment = documentKey.getPath().getLastSegment(); this.db.execute("INSERT OR REPLACE INTO document_overlays (uid, collection_group, collection_path, document_id, largest_batch_id, overlay_mutation) VALUES (?, ?, ?, ?, ?, ?)", this.uid, collectionGroup, encode, lastSegment, Integer.valueOf(i), this.serializer.encodeMutation(mutation).toByteArray()); } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public void saveOverlays(int i, Map map) { for (Map.Entry entry : map.entrySet()) { DocumentKey key = entry.getKey(); saveOverlay(i, key, (Mutation) Preconditions.checkNotNull(entry.getValue(), "null value for key: %s", key)); } } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public void removeOverlaysForBatchId(int i) { this.db.execute("DELETE FROM document_overlays WHERE uid = ? AND largest_batch_id = ?", this.uid, Integer.valueOf(i)); } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public Map getOverlays(ResourcePath resourcePath, int i) { final HashMap hashMap = new HashMap(); final BackgroundQueue backgroundQueue = new BackgroundQueue(); this.db.query("SELECT overlay_mutation, largest_batch_id FROM document_overlays WHERE uid = ? AND collection_path = ? AND largest_batch_id > ?").binding(this.uid, EncodedPath.encode(resourcePath), Integer.valueOf(i)).forEach(new Consumer(this, backgroundQueue, hashMap) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda3 public final SQLiteDocumentOverlayCache f$0; public final BackgroundQueue f$1; public final Map f$2; @Override // com.google.firebase.firestore.util.Consumer public final void accept(Object obj) { this.f$0.m199x2236d2b5(this.f$1, this.f$2, (Cursor) obj); } { this.f$0 = this; this.f$1 = backgroundQueue; this.f$2 = hashMap; } }); backgroundQueue.drain(); return hashMap; } @Override // com.google.firebase.firestore.local.DocumentOverlayCache public Map getOverlays(String str, int i, int i2) { final HashMap hashMap = new HashMap(); final String[] strArr = new String[1]; final String[] strArr2 = new String[1]; final int[] iArr = new int[1]; final BackgroundQueue backgroundQueue = new BackgroundQueue(); this.db.query("SELECT overlay_mutation, largest_batch_id, collection_path, document_id FROM document_overlays WHERE uid = ? AND collection_group = ? AND largest_batch_id > ? ORDER BY largest_batch_id, collection_path, document_id LIMIT ?").binding(this.uid, str, Integer.valueOf(i), Integer.valueOf(i2)).forEach(new Consumer(this, iArr, strArr, strArr2, backgroundQueue, hashMap) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda4 public final SQLiteDocumentOverlayCache f$0; public final int[] f$1; public final String[] f$2; public final String[] f$3; public final BackgroundQueue f$4; public final Map f$5; @Override // com.google.firebase.firestore.util.Consumer public final void accept(Object obj) { this.f$0.m200xa097d694(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, (Cursor) obj); } { this.f$0 = this; this.f$1 = iArr; this.f$2 = strArr; this.f$3 = strArr2; this.f$4 = backgroundQueue; this.f$5 = hashMap; } }); if (strArr[0] == null) { return hashMap; } SQLitePersistence.Query query = this.db.query("SELECT overlay_mutation, largest_batch_id FROM document_overlays WHERE uid = ? AND collection_group = ? AND (collection_path > ? OR (collection_path = ? AND document_id > ?)) AND largest_batch_id = ?"); String str2 = this.uid; String str3 = strArr[0]; query.binding(str2, str, str3, str3, strArr2[0], Integer.valueOf(iArr[0])).forEach(new Consumer(this, backgroundQueue, hashMap) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda5 public final SQLiteDocumentOverlayCache f$0; public final BackgroundQueue f$1; public final Map f$2; @Override // com.google.firebase.firestore.util.Consumer public final void accept(Object obj) { this.f$0.m201x1ef8da73(this.f$1, this.f$2, (Cursor) obj); } { this.f$0 = this; this.f$1 = backgroundQueue; this.f$2 = hashMap; } }); backgroundQueue.drain(); return hashMap; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$getOverlays$3$com-google-firebase-firestore-local-SQLiteDocumentOverlayCache, reason: not valid java name */ public /* synthetic */ void m200xa097d694(int[] iArr, String[] strArr, String[] strArr2, BackgroundQueue backgroundQueue, Map map, Cursor cursor) { iArr[0] = cursor.getInt(1); strArr[0] = cursor.getString(2); strArr2[0] = cursor.getString(3); m203x634144da(backgroundQueue, map, cursor); } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r3v2, types: [java.util.concurrent.Executor] */ /* renamed from: processOverlaysInBackground, reason: merged with bridge method [inline-methods] and merged with bridge method [inline-methods] and merged with bridge method [inline-methods] */ public void m203x634144da(BackgroundQueue backgroundQueue, final Map map, Cursor cursor) { final byte[] blob = cursor.getBlob(0); final int i = cursor.getInt(1); BackgroundQueue backgroundQueue2 = backgroundQueue; if (cursor.isLast()) { backgroundQueue2 = Executors.DIRECT_EXECUTOR; } backgroundQueue2.execute(new Runnable(this, blob, i, map) { // from class: com.google.firebase.firestore.local.SQLiteDocumentOverlayCache$$ExternalSyntheticLambda2 public final SQLiteDocumentOverlayCache f$0; public final byte[] f$1; public final int f$2; public final Map f$3; @Override // java.lang.Runnable public final void run() { this.f$0.m202x1d10fe66(this.f$1, this.f$2, this.f$3); } { this.f$0 = this; this.f$1 = blob; this.f$2 = i; this.f$3 = map; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$processOverlaysInBackground$5$com-google-firebase-firestore-local-SQLiteDocumentOverlayCache, reason: not valid java name */ public /* synthetic */ void m202x1d10fe66(byte[] bArr, int i, Map map) { Overlay decodeOverlay = decodeOverlay(bArr, i); synchronized (map) { map.put(decodeOverlay.getKey(), decodeOverlay); } } private Overlay decodeOverlay(byte[] bArr, int i) { try { return Overlay.create(i, this.serializer.decodeMutation(JsL.c(bArr))); } catch (InvalidProtocolBufferException e) { throw Assert.fail("Overlay failed to parse: %s", e); } } }