#include "cache_api.hpp" #include #include #include using namespace readercache; static void number(long long value) { std::cout << ' ' << value; } static void observe(int status, long long value, long long version, Cache& cache, Pool& pool) { std::cout << status; number(value); number(version); auto counters = pool.counters(); for (long long count : counters) { number(count); } auto views = cache.views(); number(static_cast(views.size())); for (const auto& view : views) { number(view.handle); number(view.phase); number(view.resource); number(view.value); number(view.version); number(view.readers); number(view.writer); } auto tickets = cache.tickets(); number(static_cast(tickets.size())); for (const auto& ticket : tickets) { number(ticket.ticket); number(ticket.handle); number(ticket.value); number(ticket.version); } number(static_cast(pool.events().size())); for (const auto& event : pool.events()) { number(event.kind); number(event.object); number(event.argument); } std::cout << '\n'; } static bool no_more(std::istringstream& input) { std::string extra; return !(input >> extra); } int main() { Pool pool; Cache cache(pool); std::string line; while (std::getline(std::cin, line)) { pool.clear_events(); std::istringstream input(line); std::string operation; int handle = 0; int ticket = 0; long long argument = 0; long long value = -777; long long version = -888; int status = 1; if (line.find_first_of("\v\f") != std::string::npos) { observe(status, value, version, cache, pool); continue; } if (!(input >> operation)) { observe(status, value, version, cache, pool); continue; } if (operation == "C") { if ((input >> handle >> argument) && no_more(input)) { status = cache.create(handle, argument); } } else if (operation == "W") { if ((input >> handle >> argument) && no_more(input)) { status = cache.write(handle, argument); } } else if (operation == "B") { if ((input >> handle >> ticket) && no_more(input)) { status = cache.begin(handle, ticket, value, version); } } else if (operation == "E") { if ((input >> ticket) && no_more(input)) { status = cache.end(ticket); } } else if (operation == "X") { if ((input >> handle) && no_more(input)) { status = cache.close(handle); } } else if (operation == "S") { if ((input >> handle) && no_more(input)) { status = cache.start_write(handle); } } else if (operation == "K") { if ((input >> handle >> argument) && no_more(input)) { status = cache.commit(handle, argument); } } else if (operation == "A") { if ((input >> handle) && no_more(input)) { status = cache.abort(handle); } } else if (operation == "F") { if (no_more(input)) { pool.fail_next_initialization(); status = 0; } } else if (operation == "Q") { if (no_more(input)) { status = 0; } } observe(status, value, version, cache, pool); } return 0; }