prune method
Deletes what no bundle will use again: every index of a root that no
longer exists, and indexes of other configurations or encoders that no
operation used for unusedFor. Indexes an operation is writing are
left for a later prune. Indexes earlier releases wrote elsewhere under
indexes/ are never touched.
A lock file is deleted only once no operation used it for unusedFor,
as well, so no operation is opening it; one that opened it just before
it went locks the file at its path instead, which WayfinderFileLock
checks, so two operations never hold the same index.
It also deletes the get lock files of project roots that no longer
exist, once unused for unusedFor
(WayfinderProfileResolver.pruneProjectLocks), and the temporary files
of model downloads abandoned at least a day ago under models/.
Implementation
Future<KnowledgePruneResult> prune({
Duration unusedFor = const Duration(days: 30),
}) => _admit(() async {
final data = await _resolvedDataDirectory();
final indexes = Directory(p.join(data, 'indexes', 'bundles'));
final removed = <KnowledgePrunedIndex>[];
final busy = <String>[];
final now = DateTime.now();
if (await indexes.exists()) {
// Listed first: pruning deletes bundle directories that become empty.
for (final bundleDirectory
in await indexes.list(followLinks: false).toList()) {
if (bundleDirectory is! Directory ||
!_key.hasMatch(p.basename(bundleDirectory.path))) {
continue;
}
var changed = false;
for (final namespace
in await bundleDirectory.list(followLinks: false).toList()) {
if (namespace is! Directory ||
!_key.hasMatch(p.basename(namespace.path))) {
continue;
}
final key = p.basename(namespace.path);
final file = File(p.join(namespace.path, _writerName));
if (!await file.exists()) {
changed = await _deleteIfEmpty(namespace) || changed;
continue;
}
// Windows refuses to read a lock file another handle holds, so the
// note is read only while the lock is free.
if (await WayfinderFileLock.isHeld(file)) {
busy.add(namespace.path);
continue;
}
String? root;
try {
if (jsonDecode(await file.readAsString()) case {
'root': final String value,
}) {
root = value;
}
} on FileSystemException {
busy.add(namespace.path);
continue;
} on FormatException {
// No note yet: the root is unknown.
}
Future<bool> unused() async {
final FileStat stat;
try {
stat = await file.stat();
} on FileSystemException {
return false;
}
return stat.type == FileSystemEntityType.file &&
now.difference(stat.modified) >= unusedFor;
}
Future<String?> reason() async {
if (root != null && !await Directory(root).exists()) {
return 'missing-root';
}
if (key == _configurationKey) return null;
return await unused() ? 'other-configuration' : null;
}
if (await reason() == null) {
// A kept index still loses generations whose grace has passed.
final lock = await WayfinderFileLock.tryExclusive(file);
if (lock == null) continue;
try {
await _collectExpired((
root: root ?? '',
directory: namespace,
writer: file,
));
} finally {
await lock.release();
}
continue;
}
final lock = await WayfinderFileLock.tryExclusive(file);
if (lock == null) {
busy.add(namespace.path);
continue;
}
try {
// A holder may have used it between the check and the lock.
final why = await reason();
if (why == null) continue;
final bool cleared;
try {
cleared = await _clear(namespace);
} on WayfinderException {
busy.add(namespace.path);
continue;
}
if (cleared) {
removed.add(
KnowledgePrunedIndex(
directory: namespace.path,
root: root,
reason: why,
),
);
}
if (await unused() &&
await _holdsOnlyLock(namespace) &&
await _deleteLockFile(lock)) {
changed = await _deleteIfEmpty(namespace) || changed;
}
} finally {
await lock.release();
}
}
if (changed) await _deleteIfEmpty(bundleDirectory);
}
}
return KnowledgePruneResult(
removed: removed,
busy: busy,
projectLocks: await WayfinderProfileResolver(
dataDirectory: Directory(data),
).pruneProjectLocks(unusedFor: unusedFor),
downloads: await EmbeddingModelCache.removeAbandonedDownloads(
Directory(data),
),
);
});