From e68d948eaaee6af8796ef5f68aec257d9d0bbabb Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Thu, 10 Oct 2024 15:25:25 +0800 Subject: [PATCH] refactor(crdt): update revert method and remove reset_to Modify revert to accept optional override parent, remove reset_to method, and comment out revert-related test code in main function --- crdt.hpp | 39 +++++++++------------------------------ tests.cpp | 8 ++++---- 2 files changed, 13 insertions(+), 34 deletions(-) diff --git a/crdt.hpp b/crdt.hpp index 379385d..a1c708a 100644 --- a/crdt.hpp +++ b/crdt.hpp @@ -301,45 +301,24 @@ class CRDT : public std::enable_shared_from_this> revert() { - if (!parent_) { - throw std::runtime_error("Cannot revert without a parent CRDT."); + constexpr CrdtVector> + revert(const CRDT *override_parent = nullptr) { + const CRDT *reference_crdt = + override_parent ? override_parent : parent_; + + if (!reference_crdt) { + throw std::runtime_error("Cannot revert without a parent CRDT or override parent."); } // Step 1: Retrieve all changes made by the child since base_version_ CrdtVector> child_changes = this->get_changes_since(base_version_); - // Step 2: Generate inverse changes using the parent as the reference CRDT - CrdtVector> inverse_changes = invert_changes(child_changes, *parent_); + // Step 2: Generate inverse changes using the reference CRDT + CrdtVector> inverse_changes = invert_changes(child_changes, *reference_crdt); return inverse_changes; } - /// Resets the CRDT to a state based on a reference CRDT and a set of changes. - /// - /// # Arguments - /// - /// * `reference_crdt` - A reference CRDT to use as the base state. - /// * `changes` - A vector of changes to apply after resetting to the reference state. - /// - /// Complexity: O(n + m), where n is the number of records in the reference CRDT and m is the number of changes - constexpr void reset_to(const CRDT &reference_crdt, - CrdtVector> &&changes = {}) { - // Clear existing data - data_.clear(); - tombstones_.clear(); - - // Copy the state from the reference CRDT - data_ = reference_crdt.data_; - tombstones_ = reference_crdt.tombstones_; - clock_ = reference_crdt.clock_; - - // Apply the provided changes - if (!changes.empty()) { - apply_changes(std::move(changes)); - } - } - /// Inserts a new record or updates an existing record in the CRDT. /// /// # Arguments diff --git a/tests.cpp b/tests.cpp index 417cda4..db9b003 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1130,14 +1130,14 @@ int main() { assert_true(child_crdt.get_data().at(record_id_parent).fields.at("child_field2") == "child_value2", "Revert Test 1: Child should have 'child_field2' with 'child_value2'"); - // Step 4: Revert Child CRDT - CrdtVector> inverse_changes = child_crdt.revert(); + // // Step 4: Revert Child CRDT + // CrdtVector> inverse_changes = child_crdt.revert(); //! Cannot work because inverse_changes is in a special format that cannot be simply merged back into the CRDT //! it is meant to be used by the application layer to revert changes, not by the CRDT itself for now - // Apply inverse changes to child CRDT to undo modifications - child_crdt.merge_changes(std::move(inverse_changes), true); + // // Apply inverse changes to child CRDT to undo modifications + // child_crdt.merge_changes(std::move(inverse_changes), true); // // Step 5: Validate States // // Child should now match the parent