sincekit / src /sincekit.cpp
pgg-engineering's picture
Release SinceKit 0.1.0 with native benchmarks and trace replay
ef8334d verified
Raw History Blame Contribute Delete
3.86 kB
#include "sincekit_batch.h"
#include <charconv>
#include <cstdint>
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
namespace {
constexpr const char* help =
"Usage: sincekit CAPACITY\n"
"Read one command per line. IDs are dense nonnegative integers below CAPACITY.\n"
" touch ID Publish a create or update.\n"
" erase ID Publish a deletion.\n"
" plan C... Print one JSON batch for the input reader cursors (zero or more).\n"
" revision Print the current revision.\n"
"Each write advances the revision by one. A fresh process starts at zero.\n";
std::uint64_t integer(std::string_view token) {
std::uint64_t value = 0;
const auto result = std::from_chars(token.data(), token.data() + token.size(), value);
if (result.ec != std::errc{} || result.ptr != token.data() + token.size())
throw std::invalid_argument("expected unsigned decimal integer");
return value;
}
std::uint32_t size_or_id(std::string_view token) {
const auto value = integer(token);
if (value > std::numeric_limits<std::uint32_t>::max())
throw std::out_of_range("integer exceeds uint32 range");
return static_cast<std::uint32_t>(value);
}
void print_plan(const sincekit::BatchPlan& plan) {
// Revisions and cursors in the JSON API use decimal strings so JS clients
// can retain the full 64-bit sequence. IDs and counts are at most uint32.
std::cout << "{\"revision\":\"" << plan.revision << "\",\"rows\":[";
bool first = true;
for (const auto& row : plan.rows) {
if (!first) std::cout << ',';
first = false;
std::cout << '[' << row.id << ",\"" << row.revision << "\","
<< (row.deleted ? "true" : "false") << ']';
}
std::cout << "],\"prefix_lengths\":[";
for (std::size_t i = 0; i < plan.prefix_lengths.size(); ++i) {
if (i) std::cout << ',';
std::cout << plan.prefix_lengths[i];
}
std::cout << "]}\n" << std::flush;
}
void command(sincekit::Index& index, const std::string& line) {
std::istringstream input(line);
std::string op, arg, extra;
if (!(input >> op) || op.front() == '#') return;
if (op == "touch" || op == "erase") {
if (!(input >> arg) || (input >> extra))
throw std::invalid_argument("write needs exactly one ID");
const auto id = size_or_id(arg);
if (op == "touch") index.touch(id);
else index.erase(id);
} else if (op == "plan") {
std::vector<std::uint64_t> cursors;
while (input >> arg) cursors.push_back(integer(arg));
print_plan(sincekit::since_many(index, cursors));
} else if (op == "revision") {
if (input >> extra) throw std::invalid_argument("revision takes no arguments");
std::cout << "{\"revision\":\"" << index.revision() << "\"}\n" << std::flush;
} else {
throw std::invalid_argument("unknown command");
}
}
} // namespace
int main(int argc, char** argv) {
if (argc == 2 && (std::string_view(argv[1]) == "--help" ||
std::string_view(argv[1]) == "-h")) {
std::cout << help;
return 0;
}
if (argc != 2) {
std::cerr << help;
return 2;
}
std::size_t line_number = 0;
try {
sincekit::Index index(size_or_id(argv[1]));
for (std::string line; std::getline(std::cin, line);) {
++line_number;
command(index, line);
}
if (std::cin.bad() || !std::cout) throw std::runtime_error("stream failure");
} catch (const std::exception& error) {
std::cerr << "sincekit: ";
if (line_number) std::cerr << "line " << line_number << ": ";
std::cerr << error.what() << '\n';
return 1;
}
return 0;
}