From e4f3a5685121fb0f1b0bd78fe6675032d2d6116d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sat, 20 Dec 2025 12:25:16 -0500 Subject: [PATCH] Removes the Statement cache since statements are not thread safe --- .../store/sqlite/ExpirationModule.kt | 4 +- .../store/sqlite/FullTextSearchModule.kt | 2 +- .../store/sqlite/RightToVanishModule.kt | 2 +- .../nip01Core/store/sqlite/StatementCache.kt | 50 ------------------- 4 files changed, 4 insertions(+), 54 deletions(-) delete mode 100644 quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/StatementCache.kt diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ExpirationModule.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ExpirationModule.kt index c93f53c9b..03f1992f8 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ExpirationModule.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ExpirationModule.kt @@ -70,7 +70,7 @@ class ExpirationModule : IModule { ) { val exp = event.expiration() if (exp != null && exp > 0) { - val stmt = StatementCache.get(insertExpiration, db) + val stmt = db.compileStatement(insertExpiration) stmt.bindLong(1, headerId) stmt.bindLong(2, exp) stmt.executeInsert() @@ -87,7 +87,7 @@ class ExpirationModule : IModule { """.trimIndent() fun deleteExpiredEvents(db: SQLiteDatabase) { - StatementCache.get(deleteExpiredEvents, db).execute() + db.compileStatement(deleteExpiredEvents).execute() } override fun deleteAll(db: SQLiteDatabase) { diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/FullTextSearchModule.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/FullTextSearchModule.kt index 96739daaa..99102617a 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/FullTextSearchModule.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/FullTextSearchModule.kt @@ -69,7 +69,7 @@ class FullTextSearchModule : IModule { db: SQLiteDatabase, ) { if (event is SearchableEvent) { - val stmt = StatementCache.get(insertFTS, db) + val stmt = db.compileStatement(insertFTS) stmt.bindLong(1, headerId) stmt.bindString(2, event.indexableContent()) stmt.executeInsert() diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/RightToVanishModule.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/RightToVanishModule.kt index 7f7132ef4..db07539c8 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/RightToVanishModule.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/RightToVanishModule.kt @@ -103,7 +103,7 @@ class RightToVanishModule : IModule { db: SQLiteDatabase, ) { if (event is RequestToVanishEvent && event.shouldVanishFrom(relayUrl)) { - val stmt = StatementCache.get(insertRTV, db) + val stmt = db.compileStatement(insertRTV) stmt.bindLong(1, headerId) stmt.bindString(2, event.pubKey) stmt.bindLong(3, event.createdAt) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/StatementCache.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/StatementCache.kt deleted file mode 100644 index 8d3078453..000000000 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/StatementCache.kt +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (c) 2025 Vitor Pamplona - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN - * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.vitorpamplona.quartz.nip01Core.store.sqlite - -import android.database.sqlite.SQLiteDatabase -import android.database.sqlite.SQLiteStatement -import android.util.LruCache - -object StatementCache { - data class StatementKey( - val sql: String, - val dbHashcode: Int, - ) - - val cachedStatements = LruCache(10) - - fun get( - sql: String, - db: SQLiteDatabase, - ): SQLiteStatement { - val key = StatementKey(sql, db.hashCode()) - val cached = cachedStatements.get(key) - return if (cached != null) { - cached.clearBindings() - cached - } else { - val stat = db.compileStatement(sql) - cachedStatements.put(key, stat) - stat - } - } -}