Use same request for one shot and streaming examples
This commit is contained in:
67
src/main.cpp
67
src/main.cpp
@@ -2,6 +2,30 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
void print_stats(const CommitRequest &request) {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
std::string config_file = "config.toml";
|
std::string config_file = "config.toml";
|
||||||
|
|
||||||
@@ -40,7 +64,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
CommitRequest request;
|
CommitRequest request;
|
||||||
|
|
||||||
std::string sample_json = R"({
|
const std::string sample_json = R"({
|
||||||
"request_id": "demo-12345",
|
"request_id": "demo-12345",
|
||||||
"leader_id": "leader-abc",
|
"leader_id": "leader-abc",
|
||||||
"read_version": 42,
|
"read_version": 42,
|
||||||
@@ -59,28 +83,10 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
})";
|
})";
|
||||||
|
auto copy = sample_json;
|
||||||
|
|
||||||
if (request.parse_json(sample_json)) {
|
if (request.parse_json(copy)) {
|
||||||
std::cout << "✓ Successfully parsed commit request:" << std::endl;
|
print_stats(request);
|
||||||
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 {
|
} else {
|
||||||
std::cout << "✗ Failed to parse commit request" << std::endl;
|
std::cout << "✗ Failed to parse commit request" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -94,8 +100,7 @@ int main(int argc, char *argv[]) {
|
|||||||
std::cout << "✓ Initialized streaming parser" << std::endl;
|
std::cout << "✓ Initialized streaming parser" << std::endl;
|
||||||
|
|
||||||
// Simulate receiving data in small chunks like from a network socket
|
// Simulate receiving data in small chunks like from a network socket
|
||||||
std::string streaming_json =
|
std::string copy = sample_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 chunk_size = 15; // Small chunks to simulate network packets
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
@@ -103,10 +108,10 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
CommitRequest::ParseStatus status = CommitRequest::ParseStatus::Incomplete;
|
CommitRequest::ParseStatus status = CommitRequest::ParseStatus::Incomplete;
|
||||||
|
|
||||||
while (offset < streaming_json.size() &&
|
while (offset < copy.size() &&
|
||||||
status == CommitRequest::ParseStatus::Incomplete) {
|
status == CommitRequest::ParseStatus::Incomplete) {
|
||||||
size_t len = std::min(chunk_size, streaming_json.size() - offset);
|
size_t len = std::min(chunk_size, copy.size() - offset);
|
||||||
std::string chunk = streaming_json.substr(offset, len);
|
std::string chunk = copy.substr(offset, len);
|
||||||
|
|
||||||
std::cout << " Chunk " << ++chunk_count << " (" << len << " bytes): \""
|
std::cout << " Chunk " << ++chunk_count << " (" << len << " bytes): \""
|
||||||
<< chunk << "\"" << std::endl;
|
<< chunk << "\"" << std::endl;
|
||||||
@@ -125,13 +130,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (status == CommitRequest::ParseStatus::Complete) {
|
if (status == CommitRequest::ParseStatus::Complete) {
|
||||||
std::cout << "✓ Streaming parse successful:" << std::endl;
|
print_stats(streaming_request);
|
||||||
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 {
|
} else {
|
||||||
std::cout << "✗ Streaming parse failed" << std::endl;
|
std::cout << "✗ Streaming parse failed" << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user