Validate operations and preconditions
This commit is contained in:
@@ -17,7 +17,7 @@ struct Precondition {
|
||||
|
||||
Type type;
|
||||
std::optional<uint64_t> version;
|
||||
std::string_view key;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
};
|
||||
@@ -29,7 +29,7 @@ struct Operation {
|
||||
enum class Type { Write, Delete, RangeDelete };
|
||||
|
||||
Type type;
|
||||
std::string_view key;
|
||||
std::optional<std::string_view> key;
|
||||
std::optional<std::string_view> value;
|
||||
std::optional<std::string_view> begin;
|
||||
std::optional<std::string_view> end;
|
||||
@@ -207,9 +207,61 @@ public:
|
||||
* @return true if all required fields are present
|
||||
*/
|
||||
bool is_valid() const {
|
||||
return !leader_id_.empty() && has_read_version_been_set_;
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user