perf(quartz): direct-slot driver for replaceable + addressable queries

`profileOf(pk)` and `relaysOf(pk)` are the two hottest read patterns
in any Nostr client, both expressed as `Filter(authors=[pk],
kinds=[0|10002])`. Before this change every such query walked
`idx/kind/<k>/`, sorted the listing, and probed candidates — O(N)
in the kind's total event count even though we always wanted exactly
one event.

The planner now intercepts before the directory walk: when a filter
pins us to replaceable / addressable kinds with `authors` (and
`d`-tag for addressables), we read the slot file directly. One
`Files.exists()` + one `readString()` per (kind, author[, d]) triple,
no listing, no sort.

Falls through to the generic walk for anything that doesn't fit
(non-uniqueness kinds, missing d-tag, search clause, etc.). Two
new tests in FsSlotsTest assert the shortcut serves correctly
even when `idx/` has been externally wiped — proving the
shortcut isn't accidentally relying on the index.

115 fs tests green.
This commit is contained in:
Claude
2026-04-25 03:22:56 +00:00
parent 7ed525fe65
commit d460584d81
2 changed files with 124 additions and 0 deletions
@@ -161,6 +161,51 @@ class FsSlotsTest {
assertTrue(entries.single().endsWith("-${v2.id}"), "author index entry must point at winner")
}
@Test
fun `slot shortcut serves replaceable queries even when idx is wiped`() {
// Belt-and-suspenders for the planner shortcut: a query pinned to
// (kinds=[0], authors=[pk]) must hit the slot directly without
// touching idx/. Wipe idx/ to prove the shortcut isn't relying on
// it.
val v = metadata("p", 100)
store.insert(v)
java.nio.file.Files
.walk(root.resolve("idx"))
.use { s ->
s.sorted(Comparator.reverseOrder()).forEach {
java.nio.file.Files
.deleteIfExists(it)
}
}
val got = store.query<MetadataEvent>(Filter(authors = listOf(signer.pubKey), kinds = listOf(MetadataEvent.KIND)))
assertEquals(listOf(v.id), got.map { it.id }, "slot shortcut should serve from replaceable/, not idx/")
}
@Test
fun `slot shortcut serves addressable queries when d-tag supplied`() {
val v = article("intro", "v", 10)
store.insert(v)
java.nio.file.Files
.walk(root.resolve("idx"))
.use { s ->
s.sorted(Comparator.reverseOrder()).forEach {
java.nio.file.Files
.deleteIfExists(it)
}
}
val got =
store.query<LongTextNoteEvent>(
Filter(
authors = listOf(signer.pubKey),
kinds = listOf(LongTextNoteEvent.KIND),
tags = mapOf("d" to listOf("intro")),
),
)
assertEquals(listOf(v.id), got.map { it.id })
}
@Test
fun `delete of current replaceable winner clears the slot`() {
val v = metadata("only", 100)