#include "reference_ledger.hpp" #include #include #include using reference::Number; using reference::View; void print_view(const View &v) { std::cout << ' ' << v.id << ' ' << v.payload << ' ' << v.incarnation << ' ' << v.state << ' ' << v.base << ' ' << v.total << ' ' << v.cpus; } int main() { reference::Ledger ledgers[2]; unsigned selected = 0; std::string op; while (std::cin >> op) { auto &ledger = ledgers[selected]; Number id = 0, cpu = 0, n = 0, nonce = 0; if (op == "U") { std::cin >> selected; std::cout << "U " << selected << '\n'; } else if (op == "Z") { std::cin >> n; std::cout << "R " << ledger.reset(n) << '\n'; } else if (op == "C") { std::cin >> id >> n; std::cout << "R " << ledger.create(id, n) << '\n'; } else if (op == "G" || op == "P") { std::cin >> id >> cpu >> n; int rc = op == "G" ? ledger.get(id, cpu, n) : ledger.put(id, cpu, n); std::cout << "R " << rc << '\n'; } else if (op == "F") { std::cin >> id >> cpu; std::cout << "R " << ledger.fold_cpu(id, cpu) << '\n'; } else if (op == "A" || op == "K" || op == "E" || op == "I") { std::cin >> id; int rc = 0; if (op == "A") rc = ledger.fold_all(id); if (op == "K") rc = ledger.kill(id); if (op == "E") rc = ledger.erase(id); if (op == "I") rc = ledger.contains(id); std::cout << (op == "I" ? "I " : "R ") << rc << '\n'; } else if (op == "Q") { std::cin >> id >> nonce; View value{nonce, nonce, nonce, nonce, nonce, nonce, nonce}; int rc = ledger.query(id, value); std::cout << "Q " << rc; print_view(value); std::cout << '\n'; } else if (op == "D") { std::cin >> id >> cpu >> nonce; reference::CpuView value{nonce, nonce, nonce}; int rc = ledger.query_cpu(id, cpu, value); std::cout << "D " << rc << ' ' << value.local << ' ' << value.folded << ' ' << value.pending << '\n'; } else if (op == "T") { std::cin >> id >> nonce; int rc = ledger.total(id, nonce); std::cout << "T " << rc << ' ' << nonce << '\n'; } else if (op == "N") { std::cout << "N " << ledger.resident_count() << ' ' << ledger.cpu_count() << '\n'; } else if (op == "L") { auto events = ledger.events(); std::cout << "L " << events.size(); for (const auto &v : events) { std::cout << ' ' << v.sequence << ' ' << v.id << ' ' << v.incarnation << ' ' << v.payload; } std::cout << '\n'; } else if (op == "S") { auto v = ledger.stats(); std::cout << "S " << v.active << ' ' << v.killed << ' ' << v.released << ' ' << v.total << ' ' << v.pending << ' ' << v.releases << '\n'; } else if (op == "H") { std::cin >> n >> nonce; reference::Release value{nonce, nonce, nonce, nonce}; int rc = ledger.event(n, value); std::cout << "H " << rc << ' ' << value.sequence << ' ' << value.id << ' ' << value.incarnation << ' ' << value.payload << '\n'; } else if (op == "V") { std::cin >> n >> nonce; std::vector ids(static_cast(n)); for (auto &v : ids) std::cin >> v; std::vector output{{nonce, nonce, nonce, nonce, nonce, nonce, nonce}}; int rc = ledger.select(ids, output); std::cout << "V " << rc << ' ' << output.size(); for (const auto &v : output) print_view(v); std::cout << '\n'; } else if (op == "B") { std::cin >> n >> nonce; std::vector operations(static_cast(n)); for (auto &v : operations) std::cin >> v.kind >> v.id >> v.cpu >> v.amount; std::vector totals{nonce, nonce + 1}; int rc = ledger.batch(operations, totals); std::cout << "B " << rc << ' ' << totals.size(); for (auto v : totals) std::cout << ' ' << v; std::cout << '\n'; } else { return 2; } if (!std::cin) return 2; } return std::cin.bad() ? 2 : 0; }