#include "durable.hpp" namespace durable { Domain::Domain() : next_sequence_(1) { } bool Domain::valid_id(int id) { return id >= 1 && id <= 32; } bool Domain::valid_topic(int topic) { return topic >= 1 && topic <= 8; } bool Domain::valid_durability(int durability) { return durability == 0 || durability == 1; } bool Domain::valid_depth(int depth) { return depth >= 1 && depth <= 16; } bool Domain::valid_capacity(int capacity) { return capacity >= 1 && capacity <= 64; } bool Domain::valid_payload(Number payload) { return payload >= -1000000 && payload <= 1000000; } int Domain::writer_arguments(int id, int topic, int durability, int depth) const { if (!valid_id(id)) { return -22; } if (!valid_topic(topic)) { return -22; } if (!valid_durability(durability)) { return -22; } if (!valid_depth(depth)) { return -22; } return 0; } int Domain::reader_arguments(int id, int topic, int durability, int capacity) const { if (!valid_id(id)) { return -22; } if (!valid_topic(topic)) { return -22; } if (!valid_durability(durability)) { return -22; } if (!valid_capacity(capacity)) { return -22; } return 0; } int Domain::open_writer(int id, int topic, int durability, int depth) { int result = writer_arguments(id, topic, durability, depth); if (result != 0) { return result; } if (writers_.find(id) != writers_.end()) { return -17; } const auto old = generations_.find(id); const Number generation = old == generations_.end() ? 1 : old->second + 1; Writer writer; writer.id = id; writer.generation = generation; writer.topic = topic; writer.durability = durability; writer.depth = depth; writer.published = 0; writers_.emplace(id, writer); generations_[id] = generation; histories_.attach(id, generation, topic, durability, depth); return 0; } int Domain::close_writer(int id) { auto found = writers_.find(id); if (found == writers_.end()) { return -2; } writers_.erase(found); return 0; } int Domain::set_depth(int id, int depth) { auto found = writers_.find(id); if (found == writers_.end()) { return -2; } if (!valid_depth(depth)) { return -22; } Writer &writer = found->second; histories_.resize(id, writer.generation, depth); writer.depth = depth; return 0; } Sample Domain::make_sample(Writer &writer, Number payload) { Sample sample; sample.sequence = next_sequence_; sample.writer = writer.id; sample.generation = writer.generation; sample.topic = writer.topic; sample.payload = payload; sample.replayed = 0; ++next_sequence_; ++writer.published; return sample; } Number Domain::commit(Writer &writer, Number payload) { const Sample sample = make_sample(writer, payload); histories_.remember(sample); broadcast(sample); return sample.sequence; } int Domain::publish(int id, Number payload, Number &sequence) { auto found = writers_.find(id); if (found == writers_.end()) { return -2; } if (!valid_payload(payload)) { return -22; } sequence = commit(found->second, payload); return 0; } int Domain::history(int id, std::vector &out) const { const auto found = writers_.find(id); if (found == writers_.end()) { return -2; } std::vector result = histories_.read(id, found->second.generation); out.swap(result); return 0; } void Domain::reset() { writers_.clear(); readers_.clear(); generations_.clear(); histories_.reset(); next_sequence_ = 1; } }