#include "reference_ledger.hpp" namespace reference { Ledger::Ledger() = default; bool Ledger::valid_id(Number id) { return id >= 1 && id <= 1000000; } int Ledger::lookup(Number id) const { if (!valid_id(id)) return -22; return journals_.count(id) ? 0 : -2; } int Ledger::reset(Number cpus) { if (cpus < 1 || cpus > 8) return -22; cpus_ = cpus; journals_.clear(); releases_.clear(); creation_sequence_ = 0; return 0; } int Ledger::create(Number id, Number payload) { if (!valid_id(id)) return -22; if (journals_.count(id)) return -17; if (journals_.size() == 8) return -28; journals_.emplace(id, Journal{id,payload,++creation_sequence_,{}}); return 0; } Number Ledger::balance(const Journal &journal) const { Number total = 1; for (auto action : journal.actions) { if (action.kind == 1) total += action.amount; if (action.kind == 2) total -= action.amount; if (action.kind == 5) total -= 1; } return total; } void Ledger::fold(Projection &image, Number cpu) const { image.base += image.local[cpu] - image.folded[cpu]; image.folded[cpu] = image.local[cpu]; } Ledger::Projection Ledger::replay(const Journal &journal) const { Projection image; Number total = 1; for (auto action : journal.actions) { if (action.kind == 1) { image.local[action.cpu] += action.amount; total += action.amount; } else if (action.kind == 2) { image.local[action.cpu] -= action.amount; total -= action.amount; } else if (action.kind == 3) { fold(image, action.cpu); } else if (action.kind == 4) { for (Number cpu=0; cpu(releases_.size())+1, journal.id,journal.incarnation,journal.payload}); } int Ledger::reference_change(Number kind, Number id, Number cpu, Number amount) { int status = lookup(id); if (status) return status; if (cpu < 0 || cpu >= cpus_) return -22; if (amount < 1 || amount > 1000000000000LL) return -34; Journal &journal = journals_.at(id); auto image = replay(journal); if ((kind == 1 && image.state != 0) || image.state == 2) return -108; Number now = balance(journal); if (kind == 1 && amount > 1000000000000LL-now) return -34; if (kind == 2 && amount > now-(image.state == 0 ? 1 : 0)) return -34; journal.actions.push_back(Op{kind,id,cpu,amount}); if (kind == 2 && image.state == 1) finish(journal); return 0; } int Ledger::get(Number id, Number cpu, Number amount) { return reference_change(1,id,cpu,amount); } int Ledger::put(Number id, Number cpu, Number amount) { return reference_change(2,id,cpu,amount); } int Ledger::fold_cpu(Number id, Number cpu) { int status=lookup(id); if (status) return status; if (cpu < 0 || cpu >= cpus_) return -22; journals_.at(id).actions.push_back(Op{3,id,cpu,0}); return 0; } int Ledger::fold_all(Number id) { int status=lookup(id); if (status) return status; journals_.at(id).actions.push_back(Op{4,id,0,0}); return 0; } int Ledger::kill(Number id) { int status=lookup(id); if (status) return status; auto &journal=journals_.at(id); if (replay(journal).state != 0) return -16; journal.actions.push_back(Op{5,id,0,0}); finish(journal); return 0; } int Ledger::erase(Number id) { int status=lookup(id); if (status) return status; if (replay(journals_.at(id)).state != 2) return -16; journals_.erase(id); return 0; } int Ledger::query(Number id, View &output) const { int status=lookup(id); if (status) return status; const auto &j=journals_.at(id); auto image=replay(j); output=View{id,j.payload,j.incarnation,image.state,image.base,balance(j),cpus_}; return 0; } int Ledger::query_cpu(Number id, Number cpu, CpuView &output) const { int status=lookup(id); if (status) return status; if (cpu < 0 || cpu >= cpus_) return -22; auto image=replay(journals_.at(id)); output=CpuView{image.local[cpu],image.folded[cpu],image.local[cpu]-image.folded[cpu]}; return 0; } int Ledger::total(Number id, Number &output) const { int status=lookup(id); if (status) return status; output=balance(journals_.at(id)); return 0; } Number Ledger::resident_count() const { return static_cast(journals_.size()); } Number Ledger::cpu_count() const { return cpus_; } std::vector Ledger::events() const { return releases_; } int Ledger::contains(Number id) const { if (!valid_id(id)) return -22; return journals_.count(id) ? 1 : 0; } Stats Ledger::stats() const { Stats s{}; for (auto &pair:journals_) { auto image=replay(pair.second); if (image.state == 0) ++s.active; if (image.state == 1) ++s.killed; if (image.state == 2) ++s.released; s.total+=balance(pair.second); for (Number cpu=0;cpu(releases_.size()); return s; } int Ledger::select(const std::vector &ids, std::vector &output) const { if (ids.size()>8) return -22; std::vector values; for (auto id:ids) { View v{}; int status=query(id,v); if (status) return status; values.push_back(v); } output=values; return 0; } int Ledger::event(Number index, Release &output) const { if (index<0 || static_cast(index)>=releases_.size()) return -34; output=releases_[static_cast(index)]; return 0; } }