WIP making Operation and Precondition smaller
This commit is contained in:
@@ -16,10 +16,9 @@ struct Precondition {
|
||||
enum class Type { PointRead, RangeRead };
|
||||
|
||||
Type type;
|
||||
std::optional<uint64_t> version;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
uint64_t version;
|
||||
std::string_view begin;
|
||||
std::string_view end;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -29,10 +28,8 @@ struct Operation {
|
||||
enum class Type { Write, Delete, RangeDelete };
|
||||
|
||||
Type type;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> value;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
std::string_view param1;
|
||||
std::string_view param2;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -42,6 +39,25 @@ struct Operation {
|
||||
* memory management and ownership.
|
||||
*/
|
||||
class CommitRequest {
|
||||
struct PreconditionParseState {
|
||||
Precondition::Type type;
|
||||
std::optional<uint64_t> version;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represents an operation in a commit request.
|
||||
*/
|
||||
struct OperationParseState {
|
||||
Operation::Type type;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> value;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
};
|
||||
|
||||
public:
|
||||
// Parser state
|
||||
enum class ParseState {
|
||||
@@ -77,8 +93,8 @@ public:
|
||||
bool parse_complete = false;
|
||||
|
||||
// Current objects being parsed
|
||||
Precondition current_precondition{};
|
||||
Operation current_operation{};
|
||||
PreconditionParseState current_precondition{};
|
||||
OperationParseState current_operation{};
|
||||
|
||||
// Parsing state for nested structures
|
||||
ArenaString precondition_type;
|
||||
@@ -115,8 +131,8 @@ public:
|
||||
*/
|
||||
explicit CommitRequest(size_t arena_size = 4096)
|
||||
: arena_(arena_size),
|
||||
preconditions_(ArenaStlAllocator<Precondition>(&arena_)),
|
||||
operations_(ArenaStlAllocator<Operation>(&arena_)),
|
||||
preconditions_(ArenaStlAllocator<PreconditionParseState>(&arena_)),
|
||||
operations_(ArenaStlAllocator<OperationParseState>(&arena_)),
|
||||
parser_context_(&arena_) {}
|
||||
|
||||
/**
|
||||
@@ -171,7 +187,7 @@ public:
|
||||
* @param json_str The JSON string to parse
|
||||
* @return true if parsing succeeded, false otherwise
|
||||
*/
|
||||
bool parse_json(std::string_view json_str);
|
||||
bool parse_json(char *data, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Initialize streaming JSON parsing.
|
||||
@@ -199,69 +215,9 @@ public:
|
||||
*/
|
||||
bool is_parse_complete() const {
|
||||
return parser_context_.parse_complete && !parser_context_.parse_error &&
|
||||
is_valid();
|
||||
!leader_id_.empty() && has_read_version_been_set_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate that all required fields are present.
|
||||
* @return true if all required fields are present
|
||||
*/
|
||||
bool is_valid() const {
|
||||
return !leader_id_.empty() && has_read_version_been_set_ &&
|
||||
validate_preconditions() && validate_operations();
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Validate that all preconditions have required fields.
|
||||
* @return true if all preconditions are valid
|
||||
*/
|
||||
bool validate_preconditions() const {
|
||||
for (const auto &precondition : preconditions_) {
|
||||
switch (precondition.type) {
|
||||
case Precondition::Type::PointRead:
|
||||
if (!precondition.key.has_value()) {
|
||||
return false; // key is required for point_read
|
||||
}
|
||||
break;
|
||||
case Precondition::Type::RangeRead:
|
||||
if (!precondition.begin.has_value() || !precondition.end.has_value()) {
|
||||
return false; // begin and end are required for range_read
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Validate that all operations have required fields.
|
||||
* @return true if all operations are valid
|
||||
*/
|
||||
bool validate_operations() const {
|
||||
for (const auto &operation : operations_) {
|
||||
switch (operation.type) {
|
||||
case Operation::Type::Write:
|
||||
if (!operation.key.has_value() || !operation.value.has_value()) {
|
||||
return false; // key and value are required for write
|
||||
}
|
||||
break;
|
||||
case Operation::Type::Delete:
|
||||
if (!operation.key.has_value()) {
|
||||
return false; // key is required for delete
|
||||
}
|
||||
break;
|
||||
case Operation::Type::RangeDelete:
|
||||
if (!operation.begin.has_value() || !operation.end.has_value()) {
|
||||
return false; // begin and end are required for range_delete
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Check if there was a parse error.
|
||||
* @return true if there was a parse error
|
||||
@@ -354,6 +310,8 @@ private:
|
||||
*/
|
||||
std::string_view store_string(std::string_view str);
|
||||
|
||||
void on_complete();
|
||||
|
||||
/**
|
||||
* @brief Decode a base64 string and store it in the arena.
|
||||
* @param base64_str The base64 encoded string
|
||||
|
||||
Reference in New Issue
Block a user