Creating a IndexingStrategy interface

This commit is contained in:
Vitor Pamplona
2025-12-27 17:55:30 -05:00
parent 55774b0292
commit 390da16129
4 changed files with 43 additions and 15 deletions
@@ -35,7 +35,7 @@ import com.vitorpamplona.quartz.utils.EventFactory
class EventIndexesModule(
val fts: FullTextSearchModule,
val hasher: (db: SQLiteDatabase) -> TagNameValueHasher,
val tagIndexStrategy: IndexingStrategy = IndexingStrategy(),
val tagIndexStrategy: IndexingStrategy = DefaultIndexingStrategy(),
) : IModule {
override fun create(db: SQLiteDatabase) {
db.execSQL(
@@ -182,16 +182,6 @@ class EventIndexesModule(
return headerId
}
/**
* By default, we index all tags that have a single letter name and some value
*/
class IndexingStrategy {
fun shouldIndex(
kind: Int,
tag: Tag,
) = tag.size >= 2 && tag[0].length == 1
}
fun planQuery(
filter: Filter,
hasher: TagNameValueHasher,
@@ -24,13 +24,12 @@ import android.content.Context
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.store.IEventStore
import com.vitorpamplona.quartz.nip01Core.store.sqlite.EventIndexesModule.IndexingStrategy
class EventStore(
context: Context,
dbName: String? = "events.db",
val relayUrl: String? = "wss://quartz.local",
val tagIndexStrategy: IndexingStrategy = IndexingStrategy(),
val tagIndexStrategy: IndexingStrategy = DefaultIndexingStrategy(),
) : IEventStore {
val store = SQLiteEventStore(context, dbName, relayUrl, tagIndexStrategy)
@@ -0,0 +1,40 @@
/**
* 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 com.vitorpamplona.quartz.nip01Core.core.Tag
interface IndexingStrategy {
fun shouldIndex(
kind: Int,
tag: Tag,
): Boolean
}
/**
* By default, we index all tags that have a single letter name and some value
*/
class DefaultIndexingStrategy : IndexingStrategy {
override fun shouldIndex(
kind: Int,
tag: Tag,
) = tag.size >= 2 && tag[0].length == 1
}
@@ -30,7 +30,6 @@ import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.isEphemeral
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.store.IEventStore
import com.vitorpamplona.quartz.nip01Core.store.sqlite.EventIndexesModule.IndexingStrategy
import com.vitorpamplona.quartz.nip40Expiration.isExpired
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -39,7 +38,7 @@ class SQLiteEventStore(
val context: Context,
val dbName: String? = "events.db",
val relayUrl: String? = null,
val tagIndexStrategy: IndexingStrategy = IndexingStrategy(),
val tagIndexStrategy: IndexingStrategy = DefaultIndexingStrategy(),
) : SQLiteOpenHelper(context, dbName, null, DATABASE_VERSION) {
companion object {
const val DATABASE_VERSION = 2