queryWayfinderBundles function
Future<WayfinderQueryResult>
queryWayfinderBundles(
- List<
ProjectBundle> bundles, - WayfinderQuery query, {
- int offset = 0,
- int limit = 100,
- WayfinderCancellation? cancel,
Queries live files in bundles, refusing partial reads and observed edits.
This is not a transactional filesystem snapshot. No index, model, Profile
resolver, network service or computation executor is opened.
Implementation
Future<WayfinderQueryResult> queryWayfinderBundles(
List<ProjectBundle> bundles,
WayfinderQuery query, {
int offset = 0,
int limit = 100,
WayfinderCancellation? cancel,
}) async {
if (offset < 0 || limit < 1 || limit > 1000) {
_invalid('offset must be nonnegative and limit must be 1–1000.');
}
final rows =
<({ProjectBundle bundle, String path, Map<String, Object?> metadata})>[];
final observed = <({ProjectBundle bundle, String fingerprint})>[];
final identities = <String>{};
for (final bundle in bundles) {
throwIfCancelled(cancel);
if (!identities.add(bundle.path)) _invalid('Bundle paths must be unique.');
final before = await _fingerprint(bundle.root, cancel);
final loaded = await const OkfBundleLoader().inspect(bundle.root);
if (loaded.hasFindings) throw WayfinderQueryRefused(loaded.report);
final validation = loaded.validate();
if (!validation.isConformant) {
throw WayfinderQueryRefused(validation.report);
}
observed.add((bundle: bundle, fingerprint: before));
for (final entry in loaded.documents.entries) {
throwIfCancelled(cancel);
final metadata = entry.value.frontmatter;
if (query.filter.allows(metadata, entry.key)) {
rows.add((bundle: bundle, path: entry.key, metadata: metadata));
}
}
}
for (final entry in observed) {
if (await _fingerprint(entry.bundle.root, cancel) != entry.fingerprint) {
throw const WayfinderException(
'query-source-changed',
'Source files changed during the query; retry.',
);
}
}
throwIfCancelled(cancel);
Object? value(Map<String, Object?> raw, String field) =>
field == 'effective_status'
? (raw.containsKey('status') ? raw['status'] : 'stable')
: raw[field];
int compare(Object? a, Object? b, bool desc) {
if (a is! String) return b is String ? 1 : 0;
if (b is! String) return -1;
final result = a.compareTo(b);
return desc ? -result : result;
}
rows.sort((a, b) {
for (final order in query.orderBy) {
final left = switch (order.field) {
'id' => OkfConceptId.fromDocumentPath(a.path).value,
'path' => a.path,
_ => value(a.metadata, order.field),
};
final right = switch (order.field) {
'id' => OkfConceptId.fromDocumentPath(b.path).value,
'path' => b.path,
_ => value(b.metadata, order.field),
};
final result = compare(left, right, order.descending);
if (result != 0) return result;
}
final bundleOrder = a.bundle.path.compareTo(b.bundle.path);
return bundleOrder != 0
? bundleOrder
: OkfConceptId.fromDocumentPath(
a.path,
).value.compareTo(OkfConceptId.fromDocumentPath(b.path).value);
});
final grouped = query.groupBy != null;
final output = <Map<String, Object?>>[];
if (grouped) {
final counts = <String?, int>{};
for (final row in rows) {
final raw = value(row.metadata, query.groupBy!);
final key = raw is String ? raw : null;
counts.update(key, (n) => n + 1, ifAbsent: () => 1);
}
final keys = counts.keys.toList()..sort((a, b) => compare(a, b, false));
output.addAll([
for (final key in keys) {'key': key, 'count': counts[key]},
]);
} else {
output.addAll([
for (final row in rows)
{
'bundle': row.bundle.path,
'id': OkfConceptId.fromDocumentPath(row.path).value,
'path': row.path,
'values': {
for (final field in query.select) field: value(row.metadata, field),
},
},
]);
}
final conversion = _QueryJson();
final page = [
for (final row in output.skip(offset).take(limit)) conversion.convert(row),
];
return WayfinderQueryResult._({
'schema_version': '1',
'kind': grouped ? 'groups' : 'rows',
'query': query.toJson(),
'bundles': [for (final bundle in bundles) bundle.toJson()],
'total': rows.length,
if (grouped) 'group_total': output.length,
'offset': offset,
'returned': page.length,
'has_more': offset + page.length < output.length,
grouped ? 'groups' : 'rows': page,
});
}