#include "module_plan.h" #include #include #include #include namespace bootplan { namespace { enum class Phase { entering, pre, emitting, post, leaving }; struct Frame { std::string module; Phase phase = Phase::entering; std::size_t position = 0; }; Event event(Error error, Kind kind, const std::string &module) { Event result; result.error = error; result.kind = kind; result.module = module; return result; } } struct PlannerState { std::set available; std::set blacklist; std::map> options; std::map dependencies; std::vector committed; std::set committed_names; std::vector skipped; std::set missing; std::size_t accepted = 0; std::size_t suppressed = 0; std::size_t failed = 0; std::size_t cancelled = 0; bool busy = false; bool suppress = false; std::string root; std::vector stack; std::set active; std::vector staged; std::set staged_names; std::set pending_missing; explicit PlannerState(const Configuration &configuration) { available.insert(configuration.available.begin(), configuration.available.end()); blacklist.insert(configuration.blacklist.begin(), configuration.blacklist.end()); for (const OptionGroup &group : configuration.options) { options.emplace(group.module, group.tokens); } for (const Dependency &group : configuration.dependencies) { dependencies.emplace(group.module, group); } } void discard() { stack.clear(); active.clear(); staged.clear(); staged_names.clear(); pending_missing.clear(); root.clear(); busy = false; suppress = false; } void commit() { for (const Load &item : staged) { committed.push_back(item); committed_names.insert(item.module); } missing.insert(pending_missing.begin(), pending_missing.end()); ++accepted; discard(); } void complete_frame() { stack.pop_back(); if (stack.empty()) { commit(); } } void append_staged(const std::string &name) { Load item; item.module = name; const auto found = options.find(name); if (found != options.end()) { item.options = found->second; } staged.push_back(std::move(item)); staged_names.insert(name); } const std::vector &prerequisites(const std::string &name) const { static const std::vector empty; const auto found = dependencies.find(name); return found == dependencies.end() ? empty : found->second.pre; } const std::vector &successors(const std::string &name) const { static const std::vector empty; const auto found = dependencies.find(name); return found == dependencies.end() ? empty : found->second.post; } Snapshot observation() const { Snapshot result; result.busy = busy; result.committed = committed; result.staged = staged; result.skipped = skipped; result.missing.assign(missing.begin(), missing.end()); result.pending_missing.assign(pending_missing.begin(), pending_missing.end()); result.accepted = accepted; result.suppressed = suppressed; result.failed = failed; result.cancelled = cancelled; return result; } }; Planner::Planner(const Configuration &configuration) : state_(new PlannerState(configuration)) { } Planner::~Planner() = default; Planner::Planner(Planner &&) noexcept = default; Planner &Planner::operator=(Planner &&) noexcept = default; Error Planner::begin(const Request &request) { PlannerState &state = *state_; if (state.busy) { return Error::busy; } std::string canonical; if (!canonical_module(request.module, canonical) || state.available.count(canonical) == 0) { return Error::invalid; } state.discard(); state.root = canonical; state.suppress = request.via_alias && state.blacklist.count(canonical) != 0; state.busy = true; Frame root; root.module = canonical; state.stack.push_back(std::move(root)); return Error::none; } Event Planner::tick() { PlannerState &state = *state_; if (!state.busy) { return event(Error::idle, Kind::idle, ""); } if (state.suppress) { const std::string name = state.root; state.skipped.push_back(name); ++state.suppressed; state.discard(); return event(Error::none, Kind::skipped, name); } for (;;) { Frame &frame = state.stack.back(); const std::string name = frame.module; if (frame.phase == Phase::entering) { if (state.available.count(name) == 0) { state.pending_missing.insert(name); state.complete_frame(); return event(Error::none, Kind::missing, name); } if (state.active.count(name) != 0) { ++state.failed; state.discard(); return event(Error::cycle, Kind::cycle, name); } if (state.committed_names.count(name) != 0 || state.staged_names.count(name) != 0) { state.complete_frame(); return event(Error::none, Kind::reused, name); } state.active.insert(name); frame.phase = Phase::pre; frame.position = 0; return event(Error::none, Kind::open, name); } if (frame.phase == Phase::pre) { const auto &pre = state.prerequisites(name); if (frame.position < pre.size()) { const std::string next = pre[frame.position]; ++frame.position; Frame child; child.module = next; state.stack.push_back(std::move(child)); continue; } frame.phase = Phase::emitting; continue; } if (frame.phase == Phase::emitting) { state.append_staged(name); frame.phase = Phase::post; frame.position = 0; return event(Error::none, Kind::emit, name); } if (frame.phase == Phase::post) { const auto &post = state.successors(name); if (frame.position < post.size()) { const std::string next = post[frame.position]; ++frame.position; Frame child; child.module = next; state.stack.push_back(std::move(child)); continue; } frame.phase = Phase::leaving; continue; } state.active.erase(name); state.complete_frame(); return event(Error::none, Kind::close, name); } } void Planner::cancel() { PlannerState &state = *state_; if (state.busy) { ++state.cancelled; state.discard(); } } void Planner::reset() { PlannerState &state = *state_; state.discard(); state.committed.clear(); state.committed_names.clear(); state.skipped.clear(); state.missing.clear(); state.accepted = 0; state.suppressed = 0; state.failed = 0; state.cancelled = 0; } Snapshot Planner::snapshot() const { return state_->observation(); } }