#include "module_plan.h" #include #include #include namespace bootplan { namespace { struct Visit { std::string module; unsigned phase = 0; std::size_t index = 0; }; bool has_load(const std::vector &loads, const std::string &module) { for (const Load &load : loads) if (load.module == module) return true; return false; } } struct PlannerState { Configuration configuration; Snapshot view; Request request; std::vector stack; std::set active; std::set pending; explicit PlannerState(const Configuration &source) : configuration(source) {} bool available(const std::string &module) const { return std::binary_search(configuration.available.begin(), configuration.available.end(), module); } bool blacklisted(const std::string &module) const { return std::binary_search(configuration.blacklist.begin(), configuration.blacklist.end(), module); } const Dependency *dependency(const std::string &module) const { for (const Dependency &entry : configuration.dependencies) if (entry.module == module) return &entry; return nullptr; } std::vector options(const std::string &module) const { for (const OptionGroup &entry : configuration.options) if (entry.module == module) return entry.tokens; return {}; } void discard() { stack.clear(); active.clear(); pending.clear(); view.staged.clear(); view.pending_missing.clear(); view.busy = false; } void commit() { for (Load &load : view.staged) view.committed.push_back(std::move(load)); std::set merged(view.missing.begin(), view.missing.end()); merged.insert(pending.begin(), pending.end()); view.missing.assign(merged.begin(), merged.end()); ++view.accepted; discard(); } }; 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.view.busy) return Error::busy; std::string module; if (!canonical_module(request.module, module) || !state.available(module)) return Error::invalid; state.request = Request{module, request.via_alias}; state.stack.push_back(Visit{module, 0, 0}); state.view.busy = true; return Error::none; } Event Planner::tick() { PlannerState &state = *state_; if (!state.view.busy) return Event{Error::idle, Kind::idle, ""}; if (state.request.via_alias && state.blacklisted(state.request.module)) { const std::string module = state.request.module; state.view.skipped.push_back(module); ++state.view.suppressed; state.discard(); return Event{Error::none, Kind::skipped, module}; } for (;;) { Visit &visit = state.stack.back(); const std::string module = visit.module; if (visit.phase == 0) { if (!state.available(module)) { state.pending.insert(module); state.view.pending_missing.assign(state.pending.begin(), state.pending.end()); state.stack.pop_back(); return Event{Error::none, Kind::missing, module}; } if (state.active.count(module)) { ++state.view.failed; state.discard(); return Event{Error::cycle, Kind::cycle, module}; } if (has_load(state.view.committed, module) || has_load(state.view.staged, module)) { state.stack.pop_back(); if (state.stack.empty()) state.commit(); return Event{Error::none, Kind::reused, module}; } state.active.insert(module); visit.phase = 1; return Event{Error::none, Kind::open, module}; } const Dependency *dependency = state.dependency(module); if (visit.phase == 1) { if (dependency && visit.index < dependency->pre.size()) { const std::string target = dependency->pre[visit.index++]; state.stack.push_back(Visit{target, 0, 0}); continue; } visit.phase = 2; } if (visit.phase == 2) { visit.phase = 3; visit.index = 0; state.view.staged.push_back(Load{module, state.options(module)}); return Event{Error::none, Kind::emit, module}; } if (visit.phase == 3) { if (dependency && visit.index < dependency->post.size()) { const std::string target = dependency->post[visit.index++]; state.stack.push_back(Visit{target, 0, 0}); continue; } visit.phase = 4; } state.active.erase(module); state.stack.pop_back(); if (state.stack.empty()) state.commit(); return Event{Error::none, Kind::close, module}; } } void Planner::cancel() { if (state_->view.busy) { ++state_->view.cancelled; state_->discard(); } } void Planner::reset() { state_->discard(); state_->view = Snapshot{}; state_->request = Request{}; } Snapshot Planner::snapshot() const { return state_->view; } }