Initial attempt at parsing commit requests
This commit is contained in:
105
src/main.cpp
105
src/main.cpp
@@ -1,3 +1,4 @@
|
||||
#include "commit_request.hpp"
|
||||
#include "config.hpp"
|
||||
#include <iostream>
|
||||
|
||||
@@ -34,5 +35,109 @@ int main(int argc, char *argv[]) {
|
||||
<< config->subscription.keepalive_interval.count() << " seconds"
|
||||
<< std::endl;
|
||||
|
||||
// Demonstrate CommitRequest functionality
|
||||
std::cout << "\n--- CommitRequest Demo ---" << std::endl;
|
||||
|
||||
CommitRequest request;
|
||||
|
||||
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"
|
||||
}
|
||||
]
|
||||
})";
|
||||
|
||||
if (request.parse_json(sample_json)) {
|
||||
std::cout << "✓ Successfully parsed commit request:" << std::endl;
|
||||
std::cout << " Request ID: "
|
||||
<< (request.request_id() ? 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")
|
||||
<< " key=" << op.key
|
||||
<< " value=" << (op.value ? op.value.value() : "none")
|
||||
<< std::endl;
|
||||
}
|
||||
} 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 streaming_json =
|
||||
R"({"request_id": "stream-456", "leader_id": "stream-leader", "read_version": 999})";
|
||||
|
||||
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 < streaming_json.size() &&
|
||||
status == CommitRequest::ParseStatus::Incomplete) {
|
||||
size_t len = std::min(chunk_size, streaming_json.size() - offset);
|
||||
std::string chunk = streaming_json.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) {
|
||||
std::cout << "✓ Streaming parse successful:" << std::endl;
|
||||
std::cout << " Request ID: " << streaming_request.request_id().value()
|
||||
<< std::endl;
|
||||
std::cout << " Leader ID: " << streaming_request.leader_id()
|
||||
<< std::endl;
|
||||
std::cout << " Read Version: " << streaming_request.read_version()
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "✗ Streaming parse failed" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "✗ Failed to initialize streaming parser" << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user