143 lines
4.6 KiB
C++
143 lines
4.6 KiB
C++
#include "commit_request.hpp"
|
|
#include "config.hpp"
|
|
#include <iostream>
|
|
|
|
void print_stats(const CommitRequest &request) {
|
|
|
|
std::cout << "✓ Successfully parsed commit request:" << std::endl;
|
|
std::cout << " Request ID: "
|
|
<< (request.request_id().has_value() ? request.request_id().value()
|
|
: "none")
|
|
<< std::endl;
|
|
std::cout << " Leader ID: " << request.leader_id() << std::endl;
|
|
std::cout << " Read Version: " << request.read_version() << std::endl;
|
|
std::cout << " Preconditions: " << request.preconditions().size()
|
|
<< std::endl;
|
|
std::cout << " Operations: " << request.operations().size() << std::endl;
|
|
std::cout << " Arena memory used: " << request.used_bytes() << " bytes"
|
|
<< std::endl;
|
|
|
|
if (!request.operations().empty()) {
|
|
const auto &op = request.operations()[0];
|
|
std::cout << " First operation: "
|
|
<< (op.type == Operation::Type::Write ? "write" : "other")
|
|
<< " param1=" << op.param1 << " param2=" << op.param2
|
|
<< std::endl;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
std::string config_file = "config.toml";
|
|
|
|
if (argc > 1) {
|
|
config_file = argv[1];
|
|
}
|
|
|
|
auto config = weaseldb::ConfigParser::load_from_file(config_file);
|
|
|
|
if (!config) {
|
|
std::cerr << "Failed to load config from: " << config_file << std::endl;
|
|
std::cerr << "Using default configuration..." << std::endl;
|
|
config = weaseldb::Config{};
|
|
}
|
|
|
|
std::cout << "Configuration loaded successfully:" << std::endl;
|
|
std::cout << "Server bind address: " << config->server.bind_address
|
|
<< std::endl;
|
|
std::cout << "Server port: " << config->server.port << std::endl;
|
|
std::cout << "Max request size: " << config->server.max_request_size_bytes
|
|
<< " bytes" << std::endl;
|
|
std::cout << "Min request ID length: " << config->commit.min_request_id_length
|
|
<< std::endl;
|
|
std::cout << "Request ID retention: "
|
|
<< config->commit.request_id_retention_time.count() << " hours"
|
|
<< std::endl;
|
|
std::cout << "Subscription buffer size: "
|
|
<< config->subscription.max_buffer_size_bytes << " bytes"
|
|
<< std::endl;
|
|
std::cout << "Keepalive interval: "
|
|
<< config->subscription.keepalive_interval.count() << " seconds"
|
|
<< std::endl;
|
|
|
|
// Demonstrate CommitRequest functionality
|
|
std::cout << "\n--- CommitRequest Demo ---" << std::endl;
|
|
|
|
CommitRequest request;
|
|
|
|
const std::string sample_json = R"({
|
|
"request_id": "demo-12345",
|
|
"leader_id": "leader-abc",
|
|
"read_version": 42,
|
|
"preconditions": [
|
|
{
|
|
"type": "point_read",
|
|
"version": 41,
|
|
"key": "dGVzdEtleQ=="
|
|
}
|
|
],
|
|
"operations": [
|
|
{
|
|
"type": "write",
|
|
"key": "dGVzdEtleQ==",
|
|
"value": "dGVzdFZhbHVl"
|
|
}
|
|
]
|
|
})";
|
|
auto copy = sample_json;
|
|
|
|
if (request.parse_json(copy.data(), copy.size())) {
|
|
print_stats(request);
|
|
} else {
|
|
std::cout << "✗ Failed to parse commit request" << std::endl;
|
|
}
|
|
|
|
// Demonstrate streaming parsing
|
|
std::cout << "\n--- Streaming Parse Demo ---" << std::endl;
|
|
|
|
CommitRequest streaming_request;
|
|
|
|
if (streaming_request.begin_streaming_parse()) {
|
|
std::cout << "✓ Initialized streaming parser" << std::endl;
|
|
|
|
// Simulate receiving data in small chunks like from a network socket
|
|
std::string copy = sample_json;
|
|
|
|
size_t chunk_size = 15; // Small chunks to simulate network packets
|
|
size_t offset = 0;
|
|
int chunk_count = 0;
|
|
|
|
CommitRequest::ParseStatus status = CommitRequest::ParseStatus::Incomplete;
|
|
|
|
while (offset < copy.size() &&
|
|
status == CommitRequest::ParseStatus::Incomplete) {
|
|
size_t len = std::min(chunk_size, copy.size() - offset);
|
|
std::string chunk = copy.substr(offset, len);
|
|
|
|
std::cout << " Chunk " << ++chunk_count << " (" << len << " bytes): \""
|
|
<< chunk << "\"" << std::endl;
|
|
|
|
// Need mutable data for weaseljson
|
|
std::string mutable_chunk = chunk;
|
|
status = streaming_request.parse_chunk(mutable_chunk.data(),
|
|
mutable_chunk.size());
|
|
|
|
offset += len;
|
|
}
|
|
|
|
if (status == CommitRequest::ParseStatus::Incomplete) {
|
|
std::cout << " Finalizing parse..." << std::endl;
|
|
status = streaming_request.finish_streaming_parse();
|
|
}
|
|
|
|
if (status == CommitRequest::ParseStatus::Complete) {
|
|
print_stats(streaming_request);
|
|
} else {
|
|
std::cout << "✗ Streaming parse failed" << std::endl;
|
|
}
|
|
} else {
|
|
std::cout << "✗ Failed to initialize streaming parser" << std::endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|