Removes the Statement cache since statements are not thread safe
This commit is contained in:
+2
-2
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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()
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
-50
@@ -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<StatementKey, SQLiteStatement>(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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user