Adds a module interface for the SQL Lite db with drop functions too
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
|
||||
class AddressableModule {
|
||||
class AddressableModule : IModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
|
||||
+6
-2
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip09Deletions.DeletionEvent
|
||||
|
||||
class DeletionRequestModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
class DeletionRequestModule : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
// rejects deleted events.
|
||||
db.execSQL(
|
||||
"""
|
||||
@@ -61,6 +61,10 @@ class DeletionRequestModule {
|
||||
)
|
||||
}
|
||||
|
||||
override fun drop(db: SQLiteDatabase) {}
|
||||
|
||||
override fun deleteAll(db: SQLiteDatabase) {}
|
||||
|
||||
fun insert(
|
||||
event: Event,
|
||||
headerId: Long,
|
||||
|
||||
+6
-2
@@ -22,8 +22,8 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
|
||||
class EphemeralModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
class EphemeralModule : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
// Rejects all ephemeral events.
|
||||
db.execSQL(
|
||||
"""
|
||||
@@ -37,4 +37,8 @@ class EphemeralModule {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun drop(db: SQLiteDatabase) {}
|
||||
|
||||
override fun deleteAll(db: SQLiteDatabase) {}
|
||||
}
|
||||
|
||||
+8
-3
@@ -34,8 +34,8 @@ import com.vitorpamplona.quartz.utils.EventFactory
|
||||
class EventIndexesModule(
|
||||
val fts: FullTextSearchModule,
|
||||
val tagIndexStrategy: IndexingStrategy = IndexingStrategy(),
|
||||
) {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
) : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
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 =
|
||||
"""
|
||||
INSERT INTO event_headers
|
||||
@@ -469,7 +474,7 @@ class EventIndexesModule(
|
||||
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_headers")
|
||||
}
|
||||
|
||||
+7
-3
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip40Expiration.expiration
|
||||
|
||||
class ExpirationModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
class ExpirationModule : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
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)")
|
||||
}
|
||||
|
||||
override fun drop(db: SQLiteDatabase) {
|
||||
db.execSQL("DROP TABLE IF EXISTS event_expirations")
|
||||
}
|
||||
|
||||
val insertExpiration =
|
||||
"""
|
||||
INSERT OR ROLLBACK INTO event_expirations (event_header_row_id, expiration)
|
||||
@@ -86,7 +90,7 @@ class ExpirationModule {
|
||||
StatementCache.get(deleteExpiredEvents, db).execute()
|
||||
}
|
||||
|
||||
fun deleteAll(db: SQLiteDatabase) {
|
||||
override fun deleteAll(db: SQLiteDatabase) {
|
||||
db.execSQL("DELETE FROM event_expirations")
|
||||
}
|
||||
}
|
||||
|
||||
+7
-3
@@ -25,12 +25,12 @@ import android.database.sqlite.SQLiteException
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip50Search.SearchableEvent
|
||||
|
||||
class FullTextSearchModule {
|
||||
class FullTextSearchModule : IModule {
|
||||
val tableName = "event_fts"
|
||||
val eventHeaderRowIdName = "event_header_row_id"
|
||||
val contentName = "content"
|
||||
|
||||
fun create(db: SQLiteDatabase) {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
val ftsVersion = FullTextSearchModule().versionFinder(db)
|
||||
db.execSQL(
|
||||
"""
|
||||
@@ -53,6 +53,10 @@ class FullTextSearchModule {
|
||||
)
|
||||
}
|
||||
|
||||
override fun drop(db: SQLiteDatabase) {
|
||||
db.execSQL("DROP TABLE IF EXISTS $tableName")
|
||||
}
|
||||
|
||||
val insertFTS =
|
||||
"""
|
||||
INSERT OR ROLLBACK INTO $tableName ($eventHeaderRowIdName, $contentName)
|
||||
@@ -86,7 +90,7 @@ class FullTextSearchModule {
|
||||
3
|
||||
}
|
||||
|
||||
fun deleteAll(db: SQLiteDatabase) {
|
||||
override fun deleteAll(db: SQLiteDatabase) {
|
||||
db.execSQL("DELETE FROM event_fts")
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -22,8 +22,8 @@ package com.vitorpamplona.quartz.nip01Core.store.sqlite
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
|
||||
class ReplaceableModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
class ReplaceableModule : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE UNIQUE INDEX replaceable_idx
|
||||
@@ -67,4 +67,8 @@ class ReplaceableModule {
|
||||
""".trimIndent(),
|
||||
)
|
||||
}
|
||||
|
||||
override fun drop(db: SQLiteDatabase) {}
|
||||
|
||||
override fun deleteAll(db: SQLiteDatabase) {}
|
||||
}
|
||||
|
||||
+7
-3
@@ -24,8 +24,8 @@ import android.database.sqlite.SQLiteDatabase
|
||||
import com.vitorpamplona.quartz.nip01Core.core.Event
|
||||
import com.vitorpamplona.quartz.nip62RequestToVanish.RequestToVanishEvent
|
||||
|
||||
class RightToVanishModule {
|
||||
fun create(db: SQLiteDatabase) {
|
||||
class RightToVanishModule : IModule {
|
||||
override fun create(db: SQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""
|
||||
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 =
|
||||
"""
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
+16
-12
@@ -54,6 +54,18 @@ class SQLiteEventStore(
|
||||
val expirationModule = ExpirationModule()
|
||||
val rightToVanishModule = RightToVanishModule()
|
||||
|
||||
val modules =
|
||||
listOf(
|
||||
eventIndexModule,
|
||||
replaceableModule,
|
||||
addressableModule,
|
||||
ephemeralModule,
|
||||
deletionModule,
|
||||
expirationModule,
|
||||
rightToVanishModule,
|
||||
fullTextSearchModule,
|
||||
)
|
||||
|
||||
override fun onConfigure(db: SQLiteDatabase) {
|
||||
super.onConfigure(db)
|
||||
|
||||
@@ -70,14 +82,9 @@ class SQLiteEventStore(
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
eventIndexModule.create(db)
|
||||
replaceableModule.create(db)
|
||||
addressableModule.create(db)
|
||||
ephemeralModule.create(db)
|
||||
deletionModule.create(db)
|
||||
expirationModule.create(db)
|
||||
rightToVanishModule.create(db)
|
||||
fullTextSearchModule.create(db)
|
||||
modules.forEach {
|
||||
it.create(db)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpgrade(
|
||||
@@ -88,10 +95,7 @@ class SQLiteEventStore(
|
||||
|
||||
fun clearDB() {
|
||||
val db = writableDatabase
|
||||
fullTextSearchModule.deleteAll(db)
|
||||
rightToVanishModule.deleteAll(db)
|
||||
expirationModule.deleteAll(db)
|
||||
eventIndexModule.deleteAll(db)
|
||||
modules.reversed().forEach { it.deleteAll(db) }
|
||||
}
|
||||
|
||||
private fun innerInsertEvent(
|
||||
|
||||
Reference in New Issue
Block a user