#include "cache_api.hpp" #include namespace readercache { struct Cache::Impl { struct Entry { int handle; int resource; long long value; long long version; int readers; bool writer; bool closing; }; Pool& pool; std::vector entries; std::vector held; explicit Impl(Pool& source) : pool(source) {} Entry* find(int handle) { auto position = std::find_if(entries.begin(), entries.end(), [handle](const Entry& entry) { return entry.handle == handle; }); return position == entries.end() ? nullptr : &*position; } const Entry* find(int handle) const { for (const auto& entry : entries) { if (entry.handle == handle) { return &entry; } } return nullptr; } bool has_ticket(int ticket) const { return std::any_of(held.begin(), held.end(), [ticket](const Ticket& item) { return item.ticket == ticket; }); } void finish(int handle) { Entry* entry = find(handle); if (!entry || entry->readers || entry->writer) { return; } const int resource = entry->resource; pool.destroy(resource); pool.release(resource); entries.erase(std::remove_if(entries.begin(), entries.end(), [handle](const Entry& item) { return item.handle == handle; }), entries.end()); } }; Cache::Cache(Pool& pool) : impl_(new Impl(pool)) {} Cache::~Cache() = default; int Cache::create(int handle, long long initial) { if (handle < 1 || handle > 1000000) { return 1; } if (impl_->find(handle)) { return 3; } int resource = impl_->pool.allocate(); if (!impl_->pool.initialize(resource)) { impl_->pool.release(resource); return 5; } impl_->entries.push_back(Impl::Entry{handle, resource, initial, 0, 0, false, false}); return 0; } int Cache::start_write(int handle) { Impl::Entry* entry = impl_->find(handle); if (!entry) { return 4; } if (entry->closing) { return 6; } if (entry->readers || entry->writer) { return 2; } if (!impl_->pool.enter_writer(entry->resource)) { return 7; } entry->writer = true; return 0; } int Cache::commit(int handle, long long value) { Impl::Entry* entry = impl_->find(handle); if (!entry) { return 4; } if (!entry->writer) { return 1; } entry->value = value; ++entry->version; impl_->pool.leave_writer(entry->resource); entry->writer = false; if (entry->closing) { impl_->finish(handle); } return 0; } int Cache::abort(int handle) { Impl::Entry* entry = impl_->find(handle); if (!entry) { return 4; } if (!entry->writer) { return 1; } impl_->pool.leave_writer(entry->resource); entry->writer = false; if (entry->closing) { impl_->finish(handle); } return 0; } int Cache::write(int handle, long long value) { int status = start_write(handle); if (status != 0) { return status; } return commit(handle, value); } int Cache::begin(int handle, int ticket, long long& value, long long& version) { Impl::Entry* entry = impl_->find(handle); if (!entry) { return 4; } if (entry->closing) { return 6; } if (ticket < 1 || ticket > 1000000) { return 1; } if (impl_->has_ticket(ticket)) { return 3; } if (entry->writer) { return 2; } if (!impl_->pool.enter_reader(entry->resource)) { return 7; } ++entry->readers; value = entry->value; version = entry->version; impl_->held.push_back(Ticket{ticket, handle, value, version}); return 0; } int Cache::end(int ticket) { auto position = std::find_if(impl_->held.begin(), impl_->held.end(), [ticket](const Ticket& item) { return item.ticket == ticket; }); if (position == impl_->held.end()) { return 4; } const int handle = position->handle; Impl::Entry* entry = impl_->find(handle); impl_->pool.leave_reader(entry->resource); --entry->readers; impl_->held.erase(position); if (entry->closing && entry->readers == 0 && !entry->writer) { impl_->finish(handle); } return 0; } int Cache::close(int handle) { Impl::Entry* entry = impl_->find(handle); if (!entry) { return 4; } if (entry->closing) { return 0; } entry->closing = true; if (entry->readers == 0 && !entry->writer) { impl_->finish(handle); } return 0; } std::vector Cache::views() const { std::vector result; for (const auto& entry : impl_->entries) { result.push_back(View{entry.handle, entry.closing ? 1 : 0, entry.resource, entry.value, entry.version, entry.readers, entry.writer ? 1 : 0}); } std::sort(result.begin(), result.end(), [](const View& left, const View& right) { return left.handle < right.handle; }); return result; } std::vector Cache::tickets() const { auto result = impl_->held; std::sort(result.begin(), result.end(), [](const Ticket& left, const Ticket& right) { return left.ticket < right.ticket; }); return result; } }