File size: 3,855 Bytes
ef8334d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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;
}