HnswStore

class HnswStore(m: Int = 16, efConstruction: Int = 200, efSearch: Int = 64, ttl: Duration? = null, clock: Clock = Clock.systemUTC()) : CacheStore

An in-process CacheStore that scales past the exact-scan dev.kmemo.store.InMemoryStore with an approximate-nearest-neighbour (HNSW) index, while staying pure Kotlin with no extra dependency.

Correctness is not approximate. The HNSW graph only proposes candidates; this store then rescores them exactly with Vectors.dot and applies scope and TTL from an authoritative map, so liveness, expiry, size, remove and replacement are exact. Approximation costs only recall — a true nearest neighbour is occasionally missed — which is why the exact store stays the reference and why this one is opt-in, for caches large enough that an O(n) scan per lookup hurts.

Stale graph nodes (from removals and replacements) are filtered on read and periodically compacted by rebuilding a scope's index once they dominate it.

Safe across coroutines and threads: every operation takes a mutex, as in dev.kmemo.store.InMemoryStore.

Parameters

m

HNSW neighbours per node (see HnswIndex).

efConstruction

build-time candidate width (see HnswIndex).

efSearch

query-time candidate width; the recall/latency dial.

ttl

how long an entry stays valid, or null to keep it until removed.

clock

time source; substitute a fixed clock in tests instead of sleeping.

Constructors

Link copied to clipboard
constructor(m: Int = 16, efConstruction: Int = 200, efSearch: Int = 64, ttl: Duration? = null, clock: Clock = Clock.systemUTC())

Functions

Link copied to clipboard
open suspend override fun clear(scope: String?)
Link copied to clipboard
open suspend override fun put(entry: CacheEntry)
Link copied to clipboard
open suspend override fun remove(id: String): Boolean
Link copied to clipboard
open suspend override fun search(scope: String, embedding: FloatArray, limit: Int): List<ScoredEntry>
Link copied to clipboard
open suspend override fun size(scope: String?): Int
Link copied to clipboard
open suspend fun touch(id: String)