Files
moon-nix/src/closure/closure.mbt
T

47 lines
1.4 KiB
MoonBit

///|
/// Compute the transitive closure of store paths over `References`.
///
/// `roots` are `<hash>-<name>` base names. `fetch` returns the narinfo for a
/// store hash (or None when the cache lacks it — which is an error here: a
/// closure with a missing member is not installable). Result is ordered
/// dependencies-first (post-order DFS), roots last, each path once.
pub async fn compute(
roots : Array[String],
fetch : async (String) -> @narinfo.NarInfo? raise @store.NixError,
) -> Array[@narinfo.NarInfo] raise @store.NixError {
let done : Map[String, Bool] = Map([])
let order : Array[@narinfo.NarInfo] = []
for r in roots {
visit(r, fetch, done, order, 0)
}
order
}
///|
async fn visit(
base : String,
fetch : async (String) -> @narinfo.NarInfo? raise @store.NixError,
done : Map[String, Bool],
order : Array[@narinfo.NarInfo],
depth : Int32,
) -> Unit raise @store.NixError {
if done.contains(base) {
return
}
done[base] = true // mark first: references may be cyclic only via self
if depth > 10000 {
raise Parse("closure recursion too deep at \{base}")
}
let sp = @store.StorePath::parse_base(base)
let ni = match fetch(sp.hash) {
Some(n) => n
None => raise Network("path \{base} not found in binary cache")
}
for r in ni.references {
if r != base {
visit(r, fetch, done, order, depth + 1)
}
}
order.push(ni)
}