package androidx.room; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.util.Log; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; /* loaded from: classes.dex */ public class e { /* renamed from: m, reason: collision with root package name */ private static final String[] f2558m = {"UPDATE", "DELETE", "INSERT"}; /* renamed from: b, reason: collision with root package name */ final String[] f2560b; /* renamed from: c, reason: collision with root package name */ private Map f2561c; /* renamed from: d, reason: collision with root package name */ final h f2562d; /* renamed from: g, reason: collision with root package name */ volatile p0.f f2565g; /* renamed from: h, reason: collision with root package name */ private b f2566h; /* renamed from: i, reason: collision with root package name */ private final androidx.room.d f2567i; /* renamed from: k, reason: collision with root package name */ private f f2569k; /* renamed from: e, reason: collision with root package name */ AtomicBoolean f2563e = new AtomicBoolean(false); /* renamed from: f, reason: collision with root package name */ private volatile boolean f2564f = false; /* renamed from: j, reason: collision with root package name */ final k.b f2568j = new k.b(); /* renamed from: l, reason: collision with root package name */ Runnable f2570l = new a(); /* renamed from: a, reason: collision with root package name */ final HashMap f2559a = new HashMap(); class a implements Runnable { a() { } private Set a() { HashSet hashSet = new HashSet(); Cursor p10 = e.this.f2562d.p(new p0.a("SELECT * FROM room_table_modification_log WHERE invalidated = 1;")); while (p10.moveToNext()) { try { hashSet.add(Integer.valueOf(p10.getInt(0))); } catch (Throwable th) { p10.close(); throw th; } } p10.close(); if (!hashSet.isEmpty()) { e.this.f2565g.E(); } return hashSet; } @Override // java.lang.Runnable public void run() { Lock h10 = e.this.f2562d.h(); Set set = null; try { try { h10.lock(); } catch (SQLiteException | IllegalStateException e10) { Log.e("ROOM", "Cannot run invalidation tracker. Is the db closed?", e10); } if (e.this.c()) { if (e.this.f2563e.compareAndSet(true, false)) { if (e.this.f2562d.k()) { return; } h hVar = e.this.f2562d; if (hVar.f2609g) { p0.b b02 = hVar.i().b0(); b02.v(); try { set = a(); b02.U(); b02.h0(); } catch (Throwable th) { b02.h0(); throw th; } } else { set = a(); } if (set == null || set.isEmpty()) { return; } synchronized (e.this.f2568j) { Iterator it = e.this.f2568j.iterator(); while (it.hasNext()) { ((d) ((Map.Entry) it.next()).getValue()).a(set); } } } } } finally { h10.unlock(); } } } static class b { /* renamed from: a, reason: collision with root package name */ final long[] f2572a; /* renamed from: b, reason: collision with root package name */ final boolean[] f2573b; /* renamed from: c, reason: collision with root package name */ final int[] f2574c; /* renamed from: d, reason: collision with root package name */ boolean f2575d; /* renamed from: e, reason: collision with root package name */ boolean f2576e; b(int i10) { long[] jArr = new long[i10]; this.f2572a = jArr; boolean[] zArr = new boolean[i10]; this.f2573b = zArr; this.f2574c = new int[i10]; Arrays.fill(jArr, 0L); Arrays.fill(zArr, false); } int[] a() { synchronized (this) { if (this.f2575d && !this.f2576e) { int length = this.f2572a.length; int i10 = 0; while (true) { int i11 = 1; if (i10 >= length) { this.f2576e = true; this.f2575d = false; return this.f2574c; } boolean z10 = this.f2572a[i10] > 0; boolean[] zArr = this.f2573b; if (z10 != zArr[i10]) { int[] iArr = this.f2574c; if (!z10) { i11 = 2; } iArr[i10] = i11; } else { this.f2574c[i10] = 0; } zArr[i10] = z10; i10++; } } return null; } } boolean b(int... iArr) { boolean z10; synchronized (this) { z10 = false; for (int i10 : iArr) { long[] jArr = this.f2572a; long j10 = jArr[i10]; jArr[i10] = 1 + j10; if (j10 == 0) { z10 = true; this.f2575d = true; } } } return z10; } boolean c(int... iArr) { boolean z10; synchronized (this) { z10 = false; for (int i10 : iArr) { long[] jArr = this.f2572a; long j10 = jArr[i10]; jArr[i10] = j10 - 1; if (j10 == 1) { z10 = true; this.f2575d = true; } } } return z10; } void d() { synchronized (this) { this.f2576e = false; } } } public static abstract class c { /* renamed from: a, reason: collision with root package name */ final String[] f2577a; public c(String[] strArr) { this.f2577a = (String[]) Arrays.copyOf(strArr, strArr.length); } abstract boolean a(); public abstract void b(Set set); } static class d { /* renamed from: a, reason: collision with root package name */ final int[] f2578a; /* renamed from: b, reason: collision with root package name */ private final String[] f2579b; /* renamed from: c, reason: collision with root package name */ final c f2580c; /* renamed from: d, reason: collision with root package name */ private final Set f2581d; d(c cVar, int[] iArr, String[] strArr) { this.f2580c = cVar; this.f2578a = iArr; this.f2579b = strArr; if (iArr.length != 1) { this.f2581d = null; return; } HashSet hashSet = new HashSet(); hashSet.add(strArr[0]); this.f2581d = Collections.unmodifiableSet(hashSet); } void a(Set set) { int length = this.f2578a.length; Set set2 = null; for (int i10 = 0; i10 < length; i10++) { if (set.contains(Integer.valueOf(this.f2578a[i10]))) { if (length == 1) { set2 = this.f2581d; } else { if (set2 == null) { set2 = new HashSet(length); } set2.add(this.f2579b[i10]); } } } if (set2 != null) { this.f2580c.b(set2); } } void b(String[] strArr) { Set set = null; if (this.f2579b.length == 1) { int length = strArr.length; int i10 = 0; while (true) { if (i10 >= length) { break; } if (strArr[i10].equalsIgnoreCase(this.f2579b[0])) { set = this.f2581d; break; } i10++; } } else { HashSet hashSet = new HashSet(); for (String str : strArr) { String[] strArr2 = this.f2579b; int length2 = strArr2.length; int i11 = 0; while (true) { if (i11 < length2) { String str2 = strArr2[i11]; if (str2.equalsIgnoreCase(str)) { hashSet.add(str2); break; } i11++; } } } if (hashSet.size() > 0) { set = hashSet; } } if (set != null) { this.f2580c.b(set); } } } public e(h hVar, Map map, Map map2, String... strArr) { this.f2562d = hVar; this.f2566h = new b(strArr.length); this.f2561c = map2; this.f2567i = new androidx.room.d(hVar); int length = strArr.length; this.f2560b = new String[length]; for (int i10 = 0; i10 < length; i10++) { String str = strArr[i10]; Locale locale = Locale.US; String lowerCase = str.toLowerCase(locale); this.f2559a.put(lowerCase, Integer.valueOf(i10)); String str2 = (String) map.get(strArr[i10]); if (str2 != null) { this.f2560b[i10] = str2.toLowerCase(locale); } else { this.f2560b[i10] = lowerCase; } } for (Map.Entry entry : map.entrySet()) { String str3 = (String) entry.getValue(); Locale locale2 = Locale.US; String lowerCase2 = str3.toLowerCase(locale2); if (this.f2559a.containsKey(lowerCase2)) { String lowerCase3 = ((String) entry.getKey()).toLowerCase(locale2); HashMap hashMap = this.f2559a; hashMap.put(lowerCase3, hashMap.get(lowerCase2)); } } } private static void b(StringBuilder sb, String str, String str2) { sb.append("`"); sb.append("room_table_modification_trigger_"); sb.append(str); sb.append("_"); sb.append(str2); sb.append("`"); } private String[] h(String[] strArr) { HashSet hashSet = new HashSet(); for (String str : strArr) { String lowerCase = str.toLowerCase(Locale.US); if (this.f2561c.containsKey(lowerCase)) { hashSet.addAll((Collection) this.f2561c.get(lowerCase)); } else { hashSet.add(str); } } return (String[]) hashSet.toArray(new String[hashSet.size()]); } private void j(p0.b bVar, int i10) { bVar.B("INSERT OR IGNORE INTO room_table_modification_log VALUES(" + i10 + ", 0)"); String str = this.f2560b[i10]; StringBuilder sb = new StringBuilder(); for (String str2 : f2558m) { sb.setLength(0); sb.append("CREATE TEMP TRIGGER IF NOT EXISTS "); b(sb, str, str2); sb.append(" AFTER "); sb.append(str2); sb.append(" ON `"); sb.append(str); sb.append("` BEGIN UPDATE "); sb.append("room_table_modification_log"); sb.append(" SET "); sb.append("invalidated"); sb.append(" = 1"); sb.append(" WHERE "); sb.append("table_id"); sb.append(" = "); sb.append(i10); sb.append(" AND "); sb.append("invalidated"); sb.append(" = 0"); sb.append("; END"); bVar.B(sb.toString()); } } private void k(p0.b bVar, int i10) { String str = this.f2560b[i10]; StringBuilder sb = new StringBuilder(); for (String str2 : f2558m) { sb.setLength(0); sb.append("DROP TRIGGER IF EXISTS "); b(sb, str, str2); bVar.B(sb.toString()); } } public void a(c cVar) { d dVar; String[] h10 = h(cVar.f2577a); int[] iArr = new int[h10.length]; int length = h10.length; for (int i10 = 0; i10 < length; i10++) { Integer num = (Integer) this.f2559a.get(h10[i10].toLowerCase(Locale.US)); if (num == null) { throw new IllegalArgumentException("There is no table with name " + h10[i10]); } iArr[i10] = num.intValue(); } d dVar2 = new d(cVar, iArr, h10); synchronized (this.f2568j) { dVar = (d) this.f2568j.f(cVar, dVar2); } if (dVar == null && this.f2566h.b(iArr)) { l(); } } boolean c() { if (!this.f2562d.o()) { return false; } if (!this.f2564f) { this.f2562d.i().b0(); } if (this.f2564f) { return true; } Log.e("ROOM", "database is not initialized even though it is open"); return false; } void d(p0.b bVar) { synchronized (this) { if (this.f2564f) { Log.e("ROOM", "Invalidation tracker is initialized twice :/."); return; } bVar.B("PRAGMA temp_store = MEMORY;"); bVar.B("PRAGMA recursive_triggers='ON';"); bVar.B("CREATE TEMP TABLE room_table_modification_log(table_id INTEGER PRIMARY KEY, invalidated INTEGER NOT NULL DEFAULT 0)"); m(bVar); this.f2565g = bVar.F("UPDATE room_table_modification_log SET invalidated = 0 WHERE invalidated = 1 "); this.f2564f = true; } } public void e(String... strArr) { synchronized (this.f2568j) { Iterator it = this.f2568j.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if (!((c) entry.getKey()).a()) { ((d) entry.getValue()).b(strArr); } } } } public void f() { if (this.f2563e.compareAndSet(false, true)) { this.f2562d.j().execute(this.f2570l); } } public void g(c cVar) { d dVar; synchronized (this.f2568j) { dVar = (d) this.f2568j.g(cVar); } if (dVar == null || !this.f2566h.c(dVar.f2578a)) { return; } l(); } void i(Context context, String str) { this.f2569k = new f(context, str, this, this.f2562d.j()); } void l() { if (this.f2562d.o()) { m(this.f2562d.i().b0()); } } void m(p0.b bVar) { if (bVar.p0()) { return; } while (true) { try { Lock h10 = this.f2562d.h(); h10.lock(); try { int[] a10 = this.f2566h.a(); if (a10 == null) { return; } int length = a10.length; bVar.v(); for (int i10 = 0; i10 < length; i10++) { try { int i11 = a10[i10]; if (i11 == 1) { j(bVar, i10); } else if (i11 == 2) { k(bVar, i10); } } finally { } } bVar.U(); bVar.h0(); this.f2566h.d(); } finally { h10.unlock(); } } catch (SQLiteException | IllegalStateException e10) { Log.e("ROOM", "Cannot run invalidation tracker. Is the db closed?", e10); return; } } } }