WayfinderKnowledgeStore constructor
A store over dataDirectory, usually WayfinderDataDirectory.resolve().
With retainEncoder, the store opens one encoder on first use and
keeps it, with each bundle's parsed snapshot up to cacheBytes of
snapshot text, for the operations that follow. Operations still run
concurrently: each call into the shared encoder, a batch of document
embeddings, a query vector or a token count, waits for the one before
it, so a search can run between an index's batches. An operation that
fails in the encoder, or with an error that is not a
WayfinderException, discards both, and the encoder is disposed once
no operation uses it; an expected failure, such as index-stale,
index-busy or cancelled, keeps them. This suits a long-lived
server. Without it, each operation opens and disposes its own encoder.
dispose waits for accepted operations and releases the encoder.
With encoderIdleTimeout as well, the store releases the retained
encoder once no operation has used it for that long, as
releaseEncoder does: the parsed snapshots stay, and the next
operation that needs an encoder opens one again. Only an index run
that embeds and a search use the encoder; status, forget,
forgetProjectRoot, prune and an index that finds its index
current do not, so they never keep it open. Until it runs, the pending
release keeps the isolate alive; dispose cancels it. It must be
positive, and needs retainEncoder.
A superseded generation stays readable for gracePeriod after a newer
one is published, and the next index or prune after that deletes
it.
Implementation
WayfinderKnowledgeStore({
required this.dataDirectory,
required this.encoder,
this.retainEncoder = false,
this.cacheBytes = 64 << 20,
this.gracePeriod = const Duration(minutes: 10),
this.encoderIdleTimeout,
}) {
if (cacheBytes < 0) {
throw ArgumentError.value(cacheBytes, 'cacheBytes', 'must not be < 0');
}
if (gracePeriod.isNegative) {
throw ArgumentError.value(gracePeriod, 'gracePeriod', 'must not be < 0');
}
if (encoderIdleTimeout case final idle?) {
if (idle <= Duration.zero) {
throw ArgumentError.value(
idle,
'encoderIdleTimeout',
'must be positive',
);
}
if (!retainEncoder) {
throw ArgumentError.value(
idle,
'encoderIdleTimeout',
'needs retainEncoder',
);
}
}
}