|
| 1 | +#pragma once |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * |
| 5 | + * Template implementations (as opposed to mere declarations). |
| 6 | + * |
| 7 | + * This file is an example of the "impl.hh" pattern. See the |
| 8 | + * contributing guide. |
| 9 | + * |
| 10 | + * One only needs to include this when instantiating DependencyGraph |
| 11 | + * with custom NodeId or EdgeProperty types beyond the pre-instantiated |
| 12 | + * common types (StorePath, std::string). |
| 13 | + */ |
| 14 | + |
| 15 | +#include "nix/store/dependency-graph.hh" |
| 16 | +#include "nix/store/store-api.hh" |
| 17 | +#include "nix/util/error.hh" |
| 18 | + |
| 19 | +#include <boost/graph/graph_traits.hpp> |
| 20 | +#include <boost/graph/reverse_graph.hpp> |
| 21 | +#include <boost/graph/properties.hpp> |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | +#include <ranges> |
| 25 | + |
| 26 | +namespace nix { |
| 27 | + |
| 28 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 29 | +DependencyGraph<NodeId, EdgeProperty>::DependencyGraph(Store & store, const StorePathSet & closure) |
| 30 | + requires std::same_as<NodeId, StorePath> |
| 31 | +{ |
| 32 | + for (auto & path : closure) { |
| 33 | + for (auto & ref : store.queryPathInfo(path)->references) { |
| 34 | + addEdge(path, ref); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 40 | +typename DependencyGraph<NodeId, EdgeProperty>::vertex_descriptor |
| 41 | +DependencyGraph<NodeId, EdgeProperty>::addOrGetVertex(const NodeId & id) |
| 42 | +{ |
| 43 | + auto it = nodeToVertex.find(id); |
| 44 | + if (it != nodeToVertex.end()) { |
| 45 | + return it->second; |
| 46 | + } |
| 47 | + |
| 48 | + auto v = boost::add_vertex(VertexProperty{id}, graph); |
| 49 | + nodeToVertex[id] = v; |
| 50 | + return v; |
| 51 | +} |
| 52 | + |
| 53 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 54 | +void DependencyGraph<NodeId, EdgeProperty>::addEdge(const NodeId & from, const NodeId & to) |
| 55 | +{ |
| 56 | + auto vFrom = addOrGetVertex(from); |
| 57 | + auto vTo = addOrGetVertex(to); |
| 58 | + boost::add_edge(vFrom, vTo, graph); |
| 59 | +} |
| 60 | + |
| 61 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 62 | +void DependencyGraph<NodeId, EdgeProperty>::addEdge(const NodeId & from, const NodeId & to, const EdgeProperty & prop) |
| 63 | + requires(!std::same_as<EdgeProperty, boost::no_property>) |
| 64 | +{ |
| 65 | + auto vFrom = addOrGetVertex(from); |
| 66 | + auto vTo = addOrGetVertex(to); |
| 67 | + |
| 68 | + auto [existingEdge, found] = boost::edge(vFrom, vTo, graph); |
| 69 | + if (found) { |
| 70 | + if constexpr (std::same_as<EdgeProperty, FileListEdgeProperty>) { |
| 71 | + auto & edgeFiles = graph[existingEdge].files; |
| 72 | + edgeFiles.insert(edgeFiles.end(), prop.files.begin(), prop.files.end()); |
| 73 | + } |
| 74 | + } else { |
| 75 | + boost::add_edge(vFrom, vTo, prop, graph); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 80 | +std::optional<typename DependencyGraph<NodeId, EdgeProperty>::vertex_descriptor> |
| 81 | +DependencyGraph<NodeId, EdgeProperty>::getVertex(const NodeId & id) const |
| 82 | +{ |
| 83 | + auto it = nodeToVertex.find(id); |
| 84 | + if (it == nodeToVertex.end()) { |
| 85 | + return std::nullopt; |
| 86 | + } |
| 87 | + return it->second; |
| 88 | +} |
| 89 | + |
| 90 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 91 | +const NodeId & DependencyGraph<NodeId, EdgeProperty>::getNodeId(vertex_descriptor v) const |
| 92 | +{ |
| 93 | + return graph[v].id; |
| 94 | +} |
| 95 | + |
| 96 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 97 | +bool DependencyGraph<NodeId, EdgeProperty>::hasNode(const NodeId & id) const |
| 98 | +{ |
| 99 | + return nodeToVertex.contains(id); |
| 100 | +} |
| 101 | + |
| 102 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 103 | +typename DependencyGraph<NodeId, EdgeProperty>::vertex_descriptor |
| 104 | +DependencyGraph<NodeId, EdgeProperty>::getVertexOrThrow(const NodeId & id) const |
| 105 | +{ |
| 106 | + auto opt = getVertex(id); |
| 107 | + if (!opt.has_value()) { |
| 108 | + throw Error("node not found in graph"); |
| 109 | + } |
| 110 | + return *opt; |
| 111 | +} |
| 112 | + |
| 113 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 114 | +void DependencyGraph<NodeId, EdgeProperty>::computeDistancesFrom(const NodeId & target) |
| 115 | +{ |
| 116 | + auto targetVertex = getVertexOrThrow(target); |
| 117 | + size_t n = boost::num_vertices(graph); |
| 118 | + |
| 119 | + // Setup property maps for distances and predecessors |
| 120 | + std::vector<size_t> distances(n, std::numeric_limits<size_t>::max()); |
| 121 | + std::vector<vertex_descriptor> predecessors(n); |
| 122 | + |
| 123 | + // Initialize source |
| 124 | + distances[targetVertex] = 0; |
| 125 | + predecessors[targetVertex] = targetVertex; |
| 126 | + |
| 127 | + // Use reverse_graph to follow incoming edges (predecessors) |
| 128 | + auto reversedGraph = boost::make_reverse_graph(graph); |
| 129 | + |
| 130 | + // Create uniform weight map (all edges have weight 1) |
| 131 | + auto weightMap = |
| 132 | + boost::make_constant_property<typename boost::graph_traits<decltype(reversedGraph)>::edge_descriptor>(1); |
| 133 | + |
| 134 | + // Run Dijkstra on reversed graph with uniform weights |
| 135 | + boost::dijkstra_shortest_paths( |
| 136 | + reversedGraph, |
| 137 | + targetVertex, |
| 138 | + boost::weight_map(weightMap) |
| 139 | + .distance_map( |
| 140 | + boost::make_iterator_property_map(distances.begin(), boost::get(boost::vertex_index, reversedGraph))) |
| 141 | + .predecessor_map( |
| 142 | + boost::make_iterator_property_map( |
| 143 | + predecessors.begin(), boost::get(boost::vertex_index, reversedGraph)))); |
| 144 | + |
| 145 | + cachedDistances = std::move(distances); |
| 146 | + cachedPredecessors = std::move(predecessors); |
| 147 | + distanceTarget = target; |
| 148 | +} |
| 149 | + |
| 150 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 151 | +size_t DependencyGraph<NodeId, EdgeProperty>::getDistance(const NodeId & node) const |
| 152 | +{ |
| 153 | + if (!cachedDistances.has_value()) { |
| 154 | + throw Error("must call computeDistancesFrom() before querying distances"); |
| 155 | + } |
| 156 | + auto v = getVertexOrThrow(node); |
| 157 | + return (*cachedDistances)[v]; |
| 158 | +} |
| 159 | + |
| 160 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 161 | +std::optional<NodeId> DependencyGraph<NodeId, EdgeProperty>::getPredecessor(const NodeId & node) const |
| 162 | +{ |
| 163 | + if (!cachedPredecessors.has_value()) { |
| 164 | + throw Error("must call computeDistancesFrom() before querying predecessors"); |
| 165 | + } |
| 166 | + auto v = getVertexOrThrow(node); |
| 167 | + auto pred = (*cachedPredecessors)[v]; |
| 168 | + |
| 169 | + // If predecessor points to itself, it's the target (no predecessor) |
| 170 | + if (pred == v) { |
| 171 | + return std::nullopt; |
| 172 | + } |
| 173 | + |
| 174 | + return getNodeId(pred); |
| 175 | +} |
| 176 | + |
| 177 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 178 | +std::vector<NodeId> DependencyGraph<NodeId, EdgeProperty>::getSuccessors(const NodeId & node) const |
| 179 | +{ |
| 180 | + auto v = getVertexOrThrow(node); |
| 181 | + auto [adjBegin, adjEnd] = boost::adjacent_vertices(v, graph); |
| 182 | + |
| 183 | + return std::ranges::subrange(adjBegin, adjEnd) | std::views::transform([&](auto v) { return getNodeId(v); }) |
| 184 | + | std::ranges::to<std::vector>(); |
| 185 | +} |
| 186 | + |
| 187 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 188 | +std::optional<EdgeProperty> |
| 189 | +DependencyGraph<NodeId, EdgeProperty>::getEdgeProperty(const NodeId & from, const NodeId & to) const |
| 190 | + requires(!std::same_as<EdgeProperty, boost::no_property>) |
| 191 | +{ |
| 192 | + auto vFrom = getVertexOrThrow(from); |
| 193 | + auto vTo = getVertexOrThrow(to); |
| 194 | + |
| 195 | + auto [edge, found] = boost::edge(vFrom, vTo, graph); |
| 196 | + if (!found) { |
| 197 | + return std::nullopt; |
| 198 | + } |
| 199 | + |
| 200 | + return graph[edge]; |
| 201 | +} |
| 202 | + |
| 203 | +template<GraphNodeId NodeId, typename EdgeProperty> |
| 204 | +std::vector<NodeId> DependencyGraph<NodeId, EdgeProperty>::getAllNodes() const |
| 205 | +{ |
| 206 | + return nodeToVertex | std::views::keys | std::ranges::to<std::vector>(); |
| 207 | +} |
| 208 | + |
| 209 | +} // namespace nix |
0 commit comments