Adds a module interface for the SQL Lite db with drop functions too

This commit is contained in:
Vitor Pamplona
2025-12-18 12:01:17 -05:00
parent 7840bc3318
commit 12beeea8ee
9 changed files with 64 additions and 31 deletions
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
class AddressableModule { class AddressableModule : IModule {
fun create(db: SQLiteDatabase) { fun create(db: SQLiteDatabase) {
db.execSQL( db.execSQL(
""" """
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent
class DeletionRequestModule { class DeletionRequestModule : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
// rejects deleted events. // rejects deleted events.
db.execSQL( db.execSQL(
""" """
@@ -61,6 +61,10 @@ class DeletionRequestModule {
) )
} }
override fun drop(db: SQLiteDatabase) {}
override fun deleteAll(db: SQLiteDatabase) {}
fun insert( fun insert(
event: Event, event: Event,
headerId: Long, headerId: Long,
@@ -22,8 +22,8 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
class EphemeralModule { class EphemeralModule : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
// Rejects all ephemeral events. // Rejects all ephemeral events.
db.execSQL( db.execSQL(
""" """
@@ -37,4 +37,8 @@ class EphemeralModule {
""".trimIndent(), """.trimIndent(),
) )
} }
override fun drop(db: SQLiteDatabase) {}
override fun deleteAll(db: SQLiteDatabase) {}
} }
@@ -34,8 +34,8 @@ import com.vitorpamplona.quartz.utils.EventFactory
class EventIndexesModule( class EventIndexesModule(
val fts: FullTextSearchModule, val fts: FullTextSearchModule,
val tagIndexStrategy: IndexingStrategy = IndexingStrategy(), val tagIndexStrategy: IndexingStrategy = IndexingStrategy(),
) { ) : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
db.execSQL( db.execSQL(
""" """
CREATE TABLE event_headers ( CREATE TABLE event_headers (
@@ -92,6 +92,11 @@ class EventIndexesModule(
) )
} }
override fun drop(db: SQLiteDatabase) {
db.execSQL("DROP TABLE IF EXISTS event_tags")
db.execSQL("DROP TABLE IF EXISTS event_headers")
}
val sqlInsertHeader = val sqlInsertHeader =
""" """
INSERT INTO event_headers INSERT INTO event_headers
@@ -469,7 +474,7 @@ class EventIndexesModule(
return RowIdSubQuery("$projection WHERE $whereClause", clause.args) return RowIdSubQuery("$projection WHERE $whereClause", clause.args)
} }
fun deleteAll(db: SQLiteDatabase) { override fun deleteAll(db: SQLiteDatabase) {
db.execSQL("DELETE FROM event_tags") db.execSQL("DELETE FROM event_tags")
db.execSQL("DELETE FROM event_headers") db.execSQL("DELETE FROM event_headers")
} }
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip40Expiration.expiration import com.vitorpamplona.quartz.nip40Expiration.expiration
class ExpirationModule { class ExpirationModule : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
db.execSQL( db.execSQL(
""" """
CREATE TABLE event_expirations ( CREATE TABLE event_expirations (
@@ -53,6 +53,10 @@ class ExpirationModule {
db.execSQL("CREATE UNIQUE INDEX events_exp_id ON event_expirations (event_header_row_id)") db.execSQL("CREATE UNIQUE INDEX events_exp_id ON event_expirations (event_header_row_id)")
} }
override fun drop(db: SQLiteDatabase) {
db.execSQL("DROP TABLE IF EXISTS event_expirations")
}
val insertExpiration = val insertExpiration =
""" """
INSERT OR ROLLBACK INTO event_expirations (event_header_row_id, expiration) INSERT OR ROLLBACK INTO event_expirations (event_header_row_id, expiration)
@@ -86,7 +90,7 @@ class ExpirationModule {
StatementCache.get(deleteExpiredEvents, db).execute() StatementCache.get(deleteExpiredEvents, db).execute()
} }
fun deleteAll(db: SQLiteDatabase) { override fun deleteAll(db: SQLiteDatabase) {
db.execSQL("DELETE FROM event_expirations") db.execSQL("DELETE FROM event_expirations")
} }
} }
@@ -25,12 +25,12 @@ import android.database.sqlite.SQLiteException
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip50Search.SearchableEvent import com.vitorpamplona.quartz.nip50Search.SearchableEvent
class FullTextSearchModule { class FullTextSearchModule : IModule {
val tableName = "event_fts" val tableName = "event_fts"
val eventHeaderRowIdName = "event_header_row_id" val eventHeaderRowIdName = "event_header_row_id"
val contentName = "content" val contentName = "content"
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
val ftsVersion = FullTextSearchModule().versionFinder(db) val ftsVersion = FullTextSearchModule().versionFinder(db)
db.execSQL( db.execSQL(
""" """
@@ -53,6 +53,10 @@ class FullTextSearchModule {
) )
} }
override fun drop(db: SQLiteDatabase) {
db.execSQL("DROP TABLE IF EXISTS $tableName")
}
val insertFTS = val insertFTS =
""" """
INSERT OR ROLLBACK INTO $tableName ($eventHeaderRowIdName, $contentName) INSERT OR ROLLBACK INTO $tableName ($eventHeaderRowIdName, $contentName)
@@ -86,7 +90,7 @@ class FullTextSearchModule {
3 3
} }
fun deleteAll(db: SQLiteDatabase) { override fun deleteAll(db: SQLiteDatabase) {
db.execSQL("DELETE FROM event_fts") db.execSQL("DELETE FROM event_fts")
} }
} }
@@ -22,8 +22,8 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
class ReplaceableModule { class ReplaceableModule : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
db.execSQL( db.execSQL(
""" """
CREATE UNIQUE INDEX replaceable_idx CREATE UNIQUE INDEX replaceable_idx
@@ -67,4 +67,8 @@ class ReplaceableModule {
""".trimIndent(), """.trimIndent(),
) )
} }
override fun drop(db: SQLiteDatabase) {}
override fun deleteAll(db: SQLiteDatabase) {}
} }
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip62RequestToVanish.RequestToVanishEvent import com.vitorpamplona.quartz.nip62RequestToVanish.RequestToVanishEvent
class RightToVanishModule { class RightToVanishModule : IModule {
fun create(db: SQLiteDatabase) { override fun create(db: SQLiteDatabase) {
db.execSQL( db.execSQL(
""" """
CREATE TABLE event_vanish ( CREATE TABLE event_vanish (
@@ -86,6 +86,10 @@ class RightToVanishModule {
) )
} }
override fun drop(db: SQLiteDatabase) {
db.execSQL("DROP TABLE IF EXISTS event_vanish")
}
val insertRTV = val insertRTV =
""" """
INSERT OR ROLLBACK INTO event_vanish (event_header_row_id, pubkey, created_at) INSERT OR ROLLBACK INTO event_vanish (event_header_row_id, pubkey, created_at)
@@ -107,7 +111,7 @@ class RightToVanishModule {
} }
} }
fun deleteAll(db: SQLiteDatabase) { override fun deleteAll(db: SQLiteDatabase) {
db.execSQL("DELETE FROM event_vanish") db.execSQL("DELETE FROM event_vanish")
} }
} }
@@ -54,6 +54,18 @@ class SQLiteEventStore(
val expirationModule = ExpirationModule() val expirationModule = ExpirationModule()
val rightToVanishModule = RightToVanishModule() val rightToVanishModule = RightToVanishModule()
val modules =
listOf(
eventIndexModule,
replaceableModule,
addressableModule,
ephemeralModule,
deletionModule,
expirationModule,
rightToVanishModule,
fullTextSearchModule,
)
override fun onConfigure(db: SQLiteDatabase) { override fun onConfigure(db: SQLiteDatabase) {
super.onConfigure(db) super.onConfigure(db)
@@ -70,14 +82,9 @@ class SQLiteEventStore(
} }
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
eventIndexModule.create(db) modules.forEach {
replaceableModule.create(db) it.create(db)
addressableModule.create(db) }
ephemeralModule.create(db)
deletionModule.create(db)
expirationModule.create(db)
rightToVanishModule.create(db)
fullTextSearchModule.create(db)
} }
override fun onUpgrade( override fun onUpgrade(
@@ -88,10 +95,7 @@ class SQLiteEventStore(
fun clearDB() { fun clearDB() {
val db = writableDatabase val db = writableDatabase
fullTextSearchModule.deleteAll(db) modules.reversed().forEach { it.deleteAll(db) }
rightToVanishModule.deleteAll(db)
expirationModule.deleteAll(db)
eventIndexModule.deleteAll(db)
} }
private fun innerInsertEvent( private fun innerInsertEvent(