From 720e4d2c87eb9ef75d48907d1ac2459b473408b0 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 26 Dec 2025 14:29:10 -0500 Subject: [PATCH] Forces the use of the index on Addressables and Replaceables --- .../quartz/nip01Core/store/sqlite/AddressableModule.kt | 5 ++++- .../quartz/nip01Core/store/sqlite/ReplaceableModule.kt | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/AddressableModule.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/AddressableModule.kt index 27f625d66..735997518 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/AddressableModule.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/AddressableModule.kt @@ -36,6 +36,8 @@ class AddressableModule : IModule { // if a newer addressable is inserted the unique index // above will be triggered. Delete cascade will take // care of the event_tags table + // the duplicate: kind >= 30000 AND kind < 40000 + // helps SQLlite find the index above db.execSQL( """ CREATE TRIGGER delete_older_addressable_event @@ -48,7 +50,8 @@ class AddressableModule : IModule { event_headers.kind = NEW.kind AND event_headers.pubkey = NEW.pubkey AND event_headers.d_tag = NEW.d_tag AND - event_headers.created_at < NEW.created_at; + event_headers.created_at < NEW.created_at AND + event_headers.kind >= 30000 AND event_headers.kind < 40000; END; """.trimIndent(), ) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ReplaceableModule.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ReplaceableModule.kt index 04a2d3c7f..7aa87e594 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ReplaceableModule.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/store/sqlite/ReplaceableModule.kt @@ -28,7 +28,7 @@ class ReplaceableModule : IModule { """ CREATE UNIQUE INDEX replaceable_idx ON event_headers (kind, pubkey) - WHERE (kind >= 10000 AND kind < 20000) OR (kind IN (0, 3)) + WHERE (kind IN (0, 3)) OR (kind >= 10000 AND kind < 20000) """.trimIndent(), ) @@ -41,14 +41,15 @@ class ReplaceableModule : IModule { CREATE TRIGGER delete_older_replaceable_event BEFORE INSERT ON event_headers FOR EACH ROW - WHEN (NEW.kind >= 10000 AND NEW.kind < 20000) OR (NEW.kind IN (0, 3)) + WHEN (NEW.kind IN (0, 3)) OR (NEW.kind >= 10000 AND NEW.kind < 20000) BEGIN -- Delete older records if this is the newest DELETE FROM event_headers WHERE event_headers.kind = NEW.kind AND event_headers.pubkey = NEW.pubkey AND - event_headers.created_at < NEW.created_at; + event_headers.created_at < NEW.created_at AND + ((event_headers.kind IN (0, 3)) OR (event_headers.kind >= 10000 AND event_headers.kind < 20000)); END; """.trimIndent(), )