-
Notifications
You must be signed in to change notification settings - Fork 1
/
crdt.hpp
1195 lines (1051 loc) · 46.4 KB
/
crdt.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// crdt.hpp
#ifndef CRDT_HPP
#define CRDT_HPP
// Define this if you want to override the default collection types
// Basically define these before including this header and ensure this define is set before this header is included
// in any other files that include this file
#ifndef CRDT_COLLECTIONS_DEFINED
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <vector>
template <typename T> using CrdtVector = std::vector<T>;
using CrdtString = std::string;
template <typename K, typename V, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>>
using CrdtMap = std::unordered_map<K, V, Hash, KeyEqual>;
template <typename K, typename V, typename Comparator = std::less<K>> using CrdtSortedMap = std::map<K, V, Comparator>;
template <typename K, typename Hash = std::hash<K>, typename KeyEqual = std::equal_to<K>>
using CrdtSet = std::unordered_set<K, Hash, KeyEqual>;
template <typename T, typename Comparator> using CrdtSortedSet = std::set<T, Comparator>;
using CrdtNodeId = uint64_t;
#endif
#include <algorithm>
#include <iostream>
#include <optional>
#include <memory>
#include <type_traits>
#include <functional>
#include <variant>
// Add this helper struct at the beginning of the file, outside of the CRDT class
// Helper struct to check if a container has emplace_back method
template <typename T, typename = void> struct has_emplace_back : std::false_type {};
template <typename T>
struct has_emplace_back<T, std::void_t<decltype(std::declval<T>().emplace_back(std::declval<typename T::value_type>()))>>
: std::true_type {};
// Helper function to add an element to a container
template <typename Container, typename Element> void add_to_container(Container &container, Element &&element) {
if constexpr (has_emplace_back<Container>::value) {
container.emplace_back(std::forward<Element>(element));
} else {
container.emplace(std::forward<Element>(element));
}
}
/// Represents a single change in the CRDT.
template <typename K, typename V> struct Change {
K record_id;
std::optional<CrdtString> col_name; // std::nullopt represents tombstone of the record
std::optional<V> value; // note std::nullopt represents deletion of the column, not the record
uint64_t col_version;
uint64_t db_version;
CrdtNodeId node_id;
// this field is useful only locally when doing things like get_changes_since
// we record the local db_version when the change was created
uint64_t local_db_version;
// those optional flags are used to indicate the type of change, they are not stored in the records, users should manage them.
// they are very ephemeral and set only during insert_or_update, delete_record and merge_changes
uint32_t flags;
Change() = default;
Change(K rid, std::optional<CrdtString> cname, std::optional<V> val, uint64_t cver, uint64_t dver, CrdtNodeId nid,
uint64_t ldb_ver = 0, uint32_t f = 0)
: record_id(std::move(rid)), col_name(std::move(cname)), value(std::move(val)), col_version(cver), db_version(dver),
node_id(nid), local_db_version(ldb_ver), flags(f) {}
};
// Update the MergeRule concept to properly handle void context
template <typename Rule, typename K, typename V, typename Context = void>
concept MergeRule =
// Case 1: No context (void)
(std::is_void_v<Context> &&
requires(Rule r, const Change<K, V> &local, const Change<K, V> &remote) {
{ r(local, remote) } -> std::convertible_to<bool>;
}) ||
// Case 2: With context
(!std::is_void_v<Context> && requires(Rule r, const Change<K, V> &local, const Change<K, V> &remote, const Context &ctx) {
{ r(local, remote, ctx) } -> std::convertible_to<bool>;
});
// Default merge rule with proper void handling
template <typename K, typename V, typename Context = void> struct DefaultMergeRule {
// Primary version without context
constexpr bool operator()(const Change<K, V> &local, const Change<K, V> &remote) const {
if (remote.col_version > local.col_version) {
return true;
} else if (remote.col_version < local.col_version) {
return false;
} else {
if (remote.db_version > local.db_version) {
return true;
} else if (remote.db_version < local.db_version) {
return false;
} else {
return (remote.node_id > local.node_id);
}
}
}
};
// Specialization for non-void context
template <typename K, typename V, typename Context>
requires(!std::is_void_v<Context>)
struct DefaultMergeRule<K, V, Context> {
constexpr bool operator()(const Change<K, V> &local, const Change<K, V> &remote, const Context &) const {
DefaultMergeRule<K, V, void> default_rule;
return default_rule(local, remote);
}
};
// Define a concept for a custom change comparator
template <typename Comparator, typename K, typename V>
concept ChangeComparator = requires(Comparator c, const Change<K, V> &a, const Change<K, V> &b) {
{ c(a, b) } -> std::convertible_to<bool>;
};
// Default change comparator (current behavior)
template <typename K, typename V> struct DefaultChangeComparator {
constexpr bool operator()(const Change<K, V> &a, const Change<K, V> &b) const {
if (a.record_id != b.record_id)
return a.record_id < b.record_id;
if (a.col_name.has_value() != b.col_name.has_value())
return b.col_name.has_value(); // Deletions (nullopt) come last for each record
if (a.col_name != b.col_name)
return a.col_name < b.col_name;
if (a.col_version != b.col_version)
return a.col_version > b.col_version;
if (a.db_version != b.db_version)
return a.db_version > b.db_version;
if (a.node_id != b.node_id)
return a.node_id > b.node_id;
return false; // Consider equal if all fields match
}
};
/// Represents a default sort function.
struct DefaultSort {
template <typename Iterator, typename Comparator>
constexpr void operator()(Iterator begin, Iterator end, Comparator comp) const {
std::sort(begin, end, comp);
}
};
/// Represents a logical clock for maintaining causality.
class LogicalClock {
public:
LogicalClock() : time_(0) {}
/// Increments the clock for a local event.
constexpr uint64_t tick() { return ++time_; }
/// Updates the clock based on a received time.
constexpr uint64_t update(uint64_t received_time) {
time_ = std::max(time_, received_time);
return ++time_;
}
/// Sets the logical clock to a specific time.
constexpr void set_time(uint64_t t) { time_ = t; }
/// Retrieves the current time.
constexpr uint64_t current_time() const { return time_; }
private:
uint64_t time_;
};
/// Represents the version information for a column.
struct ColumnVersion {
uint64_t col_version;
uint64_t db_version;
CrdtNodeId node_id;
// this field is useful only locally when doing things like get_changes_since
// we record the local db_version when the change was created
uint64_t local_db_version;
constexpr ColumnVersion(uint64_t c, uint64_t d, CrdtNodeId n, uint64_t ldb_ver = 0)
: col_version(c), db_version(d), node_id(n), local_db_version(ldb_ver) {}
};
/// Represents a record in the CRDT.
template <typename V> struct Record {
CrdtMap<CrdtString, V> fields;
CrdtMap<CrdtString, ColumnVersion> column_versions;
Record() = default;
constexpr Record(CrdtMap<CrdtString, V> &&f, CrdtMap<CrdtString, ColumnVersion> &&cv)
: fields(std::move(f)), column_versions(std::move(cv)) {}
};
// Free function to compare two Record<V> instances
template <typename V> constexpr bool operator==(const Record<V> &lhs, const Record<V> &rhs) {
// Compare fields
if (lhs.fields.size() != rhs.fields.size())
return false;
for (const auto &[key, value] : lhs.fields) {
auto it = rhs.fields.find(key);
if (it == rhs.fields.end() || it->second != value)
return false;
}
// We don't care about column_versions, as those will be different for each node
return true;
}
// Concept for map-like containers
template <typename Container, typename Key, typename Value>
concept MapLike = requires(Container c, Key k, Value v) {
typename Container::key_type;
typename Container::mapped_type;
typename Container::value_type;
typename Container::iterator;
{ c[k] } -> std::convertible_to<Value &>;
{ c.find(k) } -> std::convertible_to<typename Container::iterator>;
{ c.emplace(k, v) };
{ c.try_emplace(k, v) } -> std::same_as<std::pair<typename Container::iterator, bool>>;
{ c.insert_or_assign(k, v) } -> std::same_as<std::pair<typename Container::iterator, bool>>;
{ c.clear() } -> std::same_as<void>;
{ c.erase(k) };
};
/// Represents the CRDT structure, generic over key (`K`) and value (`V`) types.
template <typename K, typename V, typename MergeContext = void,
MergeRule<K, V, MergeContext> MergeRuleType = DefaultMergeRule<K, V, MergeContext>,
ChangeComparator<K, V> ChangeComparatorType = DefaultChangeComparator<K, V>, typename SortFunctionType = DefaultSort,
MapLike<K, Record<V>> MapType = CrdtMap<K, Record<V>>>
class CRDT : public std::enable_shared_from_this<
CRDT<K, V, MergeContext, MergeRuleType, ChangeComparatorType, SortFunctionType, MapType>> {
public:
// Create a new empty CRDT
// Complexity: O(1)
CRDT(CrdtNodeId node_id, std::shared_ptr<CRDT> parent = nullptr, MergeRuleType merge_rule = MergeRuleType(),
ChangeComparatorType change_comparator = ChangeComparatorType(), SortFunctionType sort_func = SortFunctionType(),
std::conditional_t<std::is_void_v<MergeContext>,
std::monostate, // Use monostate for void case
MergeContext>
context = {})
: node_id_(node_id), clock_(), data_(), tombstones_(), parent_(parent), merge_rule_(std::move(merge_rule)),
change_comparator_(std::move(change_comparator)), sort_func_(std::move(sort_func)), merge_context_(std::move(context)) {
if (parent_) {
clock_ = parent_->clock_;
base_version_ = parent_->clock_.current_time();
} else {
base_version_ = 0;
}
}
/// Create a CRDT from a list of changes (e.g., loaded from disk).
///
/// # Arguments
///
/// * `node_id` - The unique identifier for this CRDT node.
/// * `changes` - A list of changes to apply to reconstruct the CRDT state.
///
/// Complexity: O(n), where n is the number of changes
constexpr CRDT(CrdtNodeId node_id, CrdtVector<Change<K, V>> &&changes) : node_id_(node_id), clock_(), data_(), tombstones_() {
apply_changes(std::move(changes));
}
/// Resets the CRDT to a state as if it was constructed with the given changes.
///
/// # Arguments
///
/// * `changes` - A list of changes to apply to reconstruct the CRDT state.
///
/// Complexity: O(n), where n is the number of changes
constexpr void reset(CrdtVector<Change<K, V>> &&changes) {
// Clear existing data
data_.clear();
tombstones_.clear();
// Reset the logical clock
clock_ = LogicalClock();
apply_changes(std::move(changes));
}
/// Reverts all changes made by this CRDT since it was created from the parent.
///
/// # Returns
///
/// A vector of `Change` objects representing the inverse changes needed to undo the child's changes.
///
/// # Complexity
///
/// O(c), where c is the number of changes since `base_version_`
constexpr CrdtVector<Change<K, V>> revert() {
if (!parent_) {
throw std::runtime_error("Cannot revert without a parent CRDT.");
}
// Step 1: Retrieve all changes made by the child since base_version_
CrdtVector<Change<K, V>> child_changes = this->get_changes_since(base_version_);
// Step 2: Generate inverse changes using the parent CRDT
return invert_changes(child_changes, *parent_);
}
/// Computes the difference between this CRDT and another CRDT.
///
/// # Arguments
///
/// * `other` - The CRDT to compare against.
///
/// # Returns
///
/// A vector of `Change` objects representing the changes needed to transform this CRDT into the other CRDT.
///
/// # Complexity
///
/// O(c), where c is the number of changes since the common ancestor
constexpr CrdtVector<Change<K, V>>
diff(const CRDT<K, V, MergeContext, MergeRuleType, ChangeComparatorType, SortFunctionType, MapType> &other) const {
// Find the common ancestor (lowest common db_version)
uint64_t common_version = std::min(clock_.current_time(), other.clock_.current_time());
// Get changes from this CRDT since the common ancestor
CrdtVector<Change<K, V>> this_changes = this->get_changes_since(common_version);
// Get changes from the other CRDT since the common ancestor
CrdtVector<Change<K, V>> other_changes = other.get_changes_since(common_version);
// Invert the changes from this CRDT
CrdtVector<Change<K, V>> inverted_this_changes = invert_changes(this_changes, other);
// Combine the inverted changes from this CRDT with the changes from the other CRDT
CrdtVector<Change<K, V>> diff_changes;
diff_changes.reserve(inverted_this_changes.size() + other_changes.size());
diff_changes.insert(diff_changes.end(), inverted_this_changes.begin(), inverted_this_changes.end());
diff_changes.insert(diff_changes.end(), other_changes.begin(), other_changes.end());
// Compress the changes to remove redundant operations
compress_changes(diff_changes);
return diff_changes;
}
/// Inserts a new record or updates an existing record in the CRDT.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `fields` - A variadic list of field name-value pairs.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename... Pairs> constexpr void insert_or_update(const K &record_id, Pairs &&...pairs) {
insert_or_update_impl<CrdtVector<Change<K, V>>>(record_id, 0, nullptr, std::forward<Pairs>(pairs)...);
}
/// Inserts a new record or updates an existing record in the CRDT, and stores changes.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `changes` - A reference to a container to store the changes.
/// * `fields` - A variadic list of field name-value pairs.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename ChangeContainer, typename... Pairs>
constexpr void insert_or_update(const K &record_id, ChangeContainer &changes, Pairs &&...pairs) {
insert_or_update_impl(record_id, 0, &changes, std::forward<Pairs>(pairs)...);
}
/// Inserts a new record or updates an existing record in the CRDT.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `flags` - A set of flags to indicate the type of change.
/// * `fields` - A variadic list of field name-value pairs.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename... Pairs> constexpr void insert_or_update(const K &record_id, uint32_t flags, Pairs &&...pairs) {
insert_or_update_impl<CrdtVector<Change<K, V>>>(record_id, flags, nullptr, std::forward<Pairs>(pairs)...);
}
/// Inserts a new record or updates an existing record in the CRDT, and stores changes.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `flags` - A set of flags to indicate the type of change.
/// * `changes` - A reference to a container to store the changes.
/// * `fields` - A variadic list of field name-value pairs.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename ChangeContainer, typename... Pairs>
constexpr void insert_or_update(const K &record_id, uint32_t flags, ChangeContainer &changes, Pairs &&...pairs) {
insert_or_update_impl(record_id, flags, &changes, std::forward<Pairs>(pairs)...);
}
/// Inserts a new record or updates an existing record in the CRDT using an iterable container of field-value pairs.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `fields` - An iterable container of field name-value pairs (will be consumed).
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename Container> constexpr void insert_or_update_from_container(const K &record_id, Container &&fields) {
insert_or_update_from_container_impl<CrdtVector<Change<K, V>>>(record_id, 0, std::forward<Container>(fields), nullptr);
}
/// Inserts a new record or updates an existing record in the CRDT using an iterable container of field-value pairs.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `flags` - A set of flags to indicate the type of change.
/// * `fields` - An iterable container of field name-value pairs (will be consumed).
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename Container>
constexpr void insert_or_update_from_container(const K &record_id, uint32_t flags, Container &&fields) {
insert_or_update_from_container_impl<CrdtVector<Change<K, V>>>(record_id, flags, std::forward<Container>(fields), nullptr);
}
/// Inserts a new record or updates an existing record in the CRDT using an iterable container of field-value pairs,
/// and stores changes.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `fields` - An iterable container of field name-value pairs (will be consumed).
/// * `changes` - A reference to a container to store the changes.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename Container, typename ChangeContainer>
constexpr void insert_or_update_from_container(const K &record_id, Container &&fields, ChangeContainer &changes) {
insert_or_update_from_container_impl(record_id, 0, std::forward<Container>(fields), &changes);
}
/// Inserts a new record or updates an existing record in the CRDT using an iterable container of field-value pairs,
/// and stores changes.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `flags` - A set of flags to indicate the type of change.
/// * `fields` - An iterable container of field name-value pairs (will be consumed).
/// * `changes - A reference to a container to store the changes.
///
/// Complexity: O(n), where n is the number of fields in the input
template <typename Container, typename ChangeContainer>
constexpr void insert_or_update_from_container(const K &record_id, uint32_t flags, Container &&fields,
ChangeContainer &changes) {
insert_or_update_from_container_impl(record_id, flags, std::forward<Container>(fields), &changes);
}
/// Deletes a record by marking it as tombstoned.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
///
/// Complexity: O(1)
void delete_record(const K &record_id, uint32_t flags = 0) {
delete_record_impl<CrdtVector<Change<K, V>>>(record_id, flags, nullptr);
}
/// Deletes a record by marking it as tombstoned, and stores the change.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `changes` - A reference to a container to store the change.
///
/// Complexity: O(1)
template <typename ChangeContainer> void delete_record(const K &record_id, ChangeContainer &changes, uint32_t flags = 0) {
delete_record_impl(record_id, flags, &changes);
}
/// Retrieves all changes since a given `last_db_version`.
///
/// # Arguments
///
/// * `last_db_version` - The database version to retrieve changes since.
///
/// # Returns
///
/// A vector of changes.
///
/// Complexity: O(n * m), where n is the number of records and m is the average number of columns per record
CrdtVector<Change<K, V>> get_changes_since(uint64_t last_db_version, CrdtSet<CrdtNodeId> excluding = {}) const {
CrdtVector<Change<K, V>> changes;
// Get changes from parent
if (parent_) {
auto parent_changes = parent_->get_changes_since(last_db_version);
changes.insert(changes.end(), parent_changes.begin(), parent_changes.end());
}
for (const auto &[record_id, record] : data_) {
for (const auto &[col_name, clock_info] : record.column_versions) {
if (clock_info.local_db_version > last_db_version && !excluding.contains(clock_info.node_id)) {
std::optional<V> value = std::nullopt;
std::optional<CrdtString> name = std::nullopt;
if (!record.fields.empty()) { // If record has fields, it's not deleted
auto field_it = record.fields.find(col_name);
if (field_it != record.fields.end()) {
value = field_it->second;
}
name = col_name;
}
changes.emplace_back(Change<K, V>(record_id, std::move(name), std::move(value), clock_info.col_version,
clock_info.db_version, clock_info.node_id, clock_info.local_db_version));
}
}
}
if (parent_) {
// Since we merge from the parent, we need to also run a compression pass
// to remove changes that have been overwritten by top level changes
// since we compare at first by col_version, it's fine even if our db_version is lower
// since we merge from the parent, we know that the changes are applied in order and col_version should always be increasing
compress_changes(changes);
}
return changes;
}
/// Merges a set of incoming changes into the CRDT.
///
/// # Arguments
///
/// * `changes` - A vector of changes to merge.
///
/// # Returns
///
/// If `ReturnAcceptedChanges` is `true`, returns a vector of accepted changes.
/// Otherwise, returns `void`.
///
/// Complexity: O(c), where c is the number of changes to merge
template <bool ReturnAcceptedChanges = false>
std::conditional_t<ReturnAcceptedChanges, CrdtVector<Change<K, V>>, void> merge_changes(CrdtVector<Change<K, V>> &&changes,
bool ignore_parent = false) {
CrdtVector<Change<K, V>> accepted_changes;
if (changes.empty()) {
if constexpr (ReturnAcceptedChanges) {
return accepted_changes;
} else {
return;
}
}
for (auto &&change : changes) {
const K &record_id = change.record_id;
std::optional<CrdtString> col_name = std::move(change.col_name);
uint64_t remote_col_version = change.col_version;
uint64_t remote_db_version = change.db_version;
CrdtNodeId remote_node_id = change.node_id;
std::optional<V> remote_value = std::move(change.value);
uint32_t flags = change.flags;
// Always update the logical clock to maintain causal consistency,
// prevent clock drift, and ensure accurate conflict resolution.
// This reflects the node's knowledge of global progress, even for
// non-accepted changes.
uint64_t new_local_db_version = clock_.update(remote_db_version);
// Skip all changes for tombstoned records
if (is_record_tombstoned(record_id, ignore_parent)) {
continue;
}
// Retrieve local column version information
const Record<V> *record_ptr = get_record_ptr(record_id, ignore_parent);
const ColumnVersion *local_col_info = nullptr;
if (record_ptr != nullptr) {
auto col_it = record_ptr->column_versions.find(col_name ? *col_name : "");
if (col_it != record_ptr->column_versions.end()) {
local_col_info = &col_it->second;
}
}
// Determine whether to accept the remote change
bool should_accept = false;
if (local_col_info == nullptr) {
should_accept = true;
} else {
Change<K, V> local_change(record_id, col_name ? *col_name : "", std::nullopt, local_col_info->col_version,
local_col_info->db_version, local_col_info->node_id, flags);
should_accept = should_accept_change(local_change, change);
}
if (should_accept) {
if (!col_name) {
// Handle deletion
tombstones_.emplace(record_id);
data_.erase(record_id);
// Update deletion clock info
CrdtMap<CrdtString, ColumnVersion> deletion_clock;
deletion_clock.emplace("", ColumnVersion(remote_col_version, remote_db_version, remote_node_id, new_local_db_version));
// Store deletion info in the data map
data_.emplace(record_id, Record<V>(CrdtMap<CrdtString, V>(), std::move(deletion_clock)));
if constexpr (ReturnAcceptedChanges) {
accepted_changes.emplace_back(Change<K, V>(record_id, std::nullopt, std::nullopt, remote_col_version,
remote_db_version, remote_node_id, new_local_db_version, flags));
}
} else {
// Handle insertion or update
Record<V> &record = get_or_create_record_unchecked(record_id, ignore_parent);
// Update field value
if (remote_value.has_value()) {
if constexpr (ReturnAcceptedChanges) {
record.fields[*col_name] = *remote_value;
} else {
record.fields[*col_name] = std::move(*remote_value);
}
} else {
// If remote_value is std::nullopt, remove the field
record.fields.erase(*col_name);
}
// Update the column version info
if constexpr (ReturnAcceptedChanges) {
record.column_versions.insert_or_assign(
*col_name, ColumnVersion(remote_col_version, remote_db_version, remote_node_id, new_local_db_version));
accepted_changes.emplace_back(Change<K, V>(record_id, std::move(col_name), std::move(remote_value),
remote_col_version, remote_db_version, remote_node_id,
new_local_db_version, flags));
} else {
record.column_versions.insert_or_assign(
std::move(*col_name), ColumnVersion(remote_col_version, remote_db_version, remote_node_id, new_local_db_version));
}
}
}
}
if constexpr (ReturnAcceptedChanges) {
return accepted_changes;
}
}
/// Compresses a vector of changes in-place by removing redundant changes that overwrite each other.
///
/// # Arguments
///
/// * `changes` - A vector of changes to compress (will be modified in-place).
///
/// Complexity: O(n log n), where n is the number of changes
template <bool Sorted = false> static void compress_changes(CrdtVector<Change<K, V>> &changes) {
if (changes.empty())
return;
auto new_end = compress_changes<Sorted>(changes.begin(), changes.end());
changes.erase(new_end, changes.end());
}
/// Compresses a range of changes by removing redundant changes that overwrite each other.
///
/// # Arguments
///
/// * `begin` - Iterator to the beginning of the range.
/// * `end` - Iterator to the end of the range.
///
/// # Returns
///
/// Iterator to the new end of the range after compression.
///
/// Complexity: O(n log n), where n is the number of changes
template <bool Sorted = false, typename Iterator> static Iterator compress_changes(Iterator begin, Iterator end) {
if (begin == end)
return end;
if constexpr (!Sorted) {
// Sort changes using the custom ChangeComparator
SortFunctionType()(begin, end, ChangeComparatorType());
}
// Use two-pointer technique to compress in-place
Iterator write = begin;
for (Iterator read = std::next(begin); read != end; ++read) {
if (read->record_id != write->record_id) {
// New record, always keep it
++write;
if (write != read) {
*write = std::move(*read);
}
} else if (!read->col_name.has_value() && write->col_name.has_value()) {
// Current read is a deletion, keep it and skip all previous changes for this record
*write = std::move(*read);
} else if (read->col_name != write->col_name) {
// New column for the same record
++write;
if (write != read) {
*write = std::move(*read);
}
}
// Else: same record and column, keep the existing one (which is the most recent due to sorting)
}
return std::next(write);
}
/// Prints the current data and tombstones for debugging purposes.
///
/// Complexity: O(n * m), where n is the number of records and m is the average number of fields per record
#ifndef NDEBUG
constexpr void print_data() const {
std::cout << "Node " << node_id_ << " Data:" << std::endl;
for (const auto &[record_id, record] : data_) {
if (tombstones_.find(record_id) != tombstones_.end()) {
continue; // Skip tombstoned records
}
std::cout << "ID: ";
print_value(record_id);
std::cout << std::endl;
for (const auto &[key, value] : record.fields) {
std::cout << " ";
print_value(key);
std::cout << ": ";
print_value(value);
std::cout << std::endl;
}
}
std::cout << "Tombstones: ";
for (const auto &tid : tombstones_) {
print_value(tid);
std::cout << " ";
}
std::cout << std::endl << std::endl;
}
#else
constexpr void print_data() const {}
#endif
// Complexity: O(1)
constexpr const LogicalClock &get_clock() const { return clock_; }
constexpr CrdtMap<K, Record<V>> get_data_combined() const {
if (!parent_) {
return data_;
}
CrdtMap<K, Record<V>> combined_data = parent_->get_data();
for (const auto &[key, record] : data_) {
combined_data[key] = record;
}
return combined_data;
}
constexpr auto &get_data() { return data_; }
/// Retrieves a pointer to a record if it exists, or nullptr if it doesn't.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `ignore_parent` - If true, only checks the current CRDT instance, ignoring the parent.
///
/// # Returns
///
/// A pointer to the Record<V> if found, or nullptr if not found.
///
/// Complexity: O(1) average case for hash table lookup
constexpr Record<V> *get_record(const K &record_id, bool ignore_parent = false) {
return get_record_ptr(record_id, ignore_parent);
}
constexpr const Record<V> *get_record(const K &record_id, bool ignore_parent = false) const {
return get_record_ptr(record_id, ignore_parent);
}
// Add this public method to the CRDT class
/// Checks if a record is tombstoned.
///
/// # Arguments
///
/// * `record_id` - The unique identifier for the record.
/// * `ignore_parent` - If true, only checks the current CRDT instance, ignoring the parent.
///
/// # Returns
///
/// True if the record is tombstoned, false otherwise.
///
/// Complexity: O(1) average case for hash table lookup
constexpr bool is_tombstoned(const K &record_id, bool ignore_parent = false) const {
return is_record_tombstoned(record_id, ignore_parent);
}
// Add this constructor to the CRDT class
CRDT(const CRDT &other)
: node_id_(other.node_id_), clock_(other.clock_), data_(other.data_), tombstones_(other.tombstones_),
parent_(other.parent_), base_version_(other.base_version_), merge_rule_(other.merge_rule_),
change_comparator_(other.change_comparator_), sort_func_(other.sort_func_), merge_context_(other.merge_context_) {
// Note: This creates a shallow copy of the parent pointer
}
CRDT &operator=(const CRDT &other) {
if (this != &other) {
node_id_ = other.node_id_;
clock_ = other.clock_;
data_ = other.data_;
tombstones_ = other.tombstones_;
parent_ = other.parent_;
base_version_ = other.base_version_;
merge_rule_ = other.merge_rule_;
change_comparator_ = other.change_comparator_;
sort_func_ = other.sort_func_;
merge_context_ = other.merge_context_;
}
return *this;
}
// Move constructor
CRDT(CRDT &&other) noexcept
: node_id_(other.node_id_), clock_(std::move(other.clock_)), data_(std::move(other.data_)),
tombstones_(std::move(other.tombstones_)), parent_(std::move(other.parent_)), base_version_(other.base_version_),
merge_rule_(std::move(other.merge_rule_)), change_comparator_(std::move(other.change_comparator_)),
sort_func_(std::move(other.sort_func_)), merge_context_(std::move(other.merge_context_)) {}
// Move assignment operator
CRDT &operator=(CRDT &&other) noexcept {
if (this != &other) {
node_id_ = other.node_id_;
clock_ = std::move(other.clock_);
data_ = std::move(other.data_);
tombstones_ = std::move(other.tombstones_);
parent_ = std::move(other.parent_);
base_version_ = other.base_version_;
merge_rule_ = std::move(other.merge_rule_);
change_comparator_ = std::move(other.change_comparator_);
sort_func_ = std::move(other.sort_func_);
merge_context_ = std::move(other.merge_context_);
}
return *this;
}
private:
CrdtNodeId node_id_;
LogicalClock clock_;
MapType data_;
CrdtSet<K> tombstones_;
// our clock won't be shared with the parent
// we optionally allow to merge from the parent or push to the parent
std::shared_ptr<CRDT<K, V, MergeContext, MergeRuleType, ChangeComparatorType, SortFunctionType, MapType>> parent_;
uint64_t base_version_; // Tracks the parent's db_version at the time of child creation
MergeRuleType merge_rule_;
ChangeComparatorType change_comparator_;
SortFunctionType sort_func_;
std::conditional_t<std::is_void_v<MergeContext>,
std::monostate, // Use monostate for void case
MergeContext>
merge_context_;
// Helper function to print values
template <typename T> static void print_value(const T &value) {
if constexpr (std::is_same_v<T, std::string> || std::is_arithmetic_v<T>) {
std::cout << value;
} else {
std::cout << "[non-printable]";
}
}
/// Applies a list of changes to reconstruct the CRDT state.
///
/// # Arguments
///
/// * `changes` - A list of changes to apply.
///
/// Complexity: O(n), where n is the number of changes
void apply_changes(CrdtVector<Change<K, V>> &&changes) {
// Determine the maximum db_version from the changes
uint64_t max_db_version = 0;
for (const auto &change : changes) {
if (change.db_version > max_db_version) {
max_db_version = change.db_version;
}
// also consider the local db_version if it's higher
if (change.local_db_version > max_db_version) {
max_db_version = change.local_db_version;
}
}
// Set the logical clock to the maximum db_version
clock_.set_time(max_db_version);
// Apply each change to reconstruct the CRDT state
for (auto &&change : changes) {
const K &record_id = change.record_id;
std::optional<CrdtString> col_name = std::move(change.col_name);
uint64_t remote_col_version = change.col_version;
uint64_t remote_db_version = change.db_version;
CrdtNodeId remote_node_id = change.node_id;
uint64_t remote_local_db_version = change.local_db_version;
std::optional<V> remote_value = std::move(change.value);
if (!col_name.has_value()) {
// Handle deletion
tombstones_.emplace(record_id);
data_.erase(record_id);
// Store empty record with deletion clock info
CrdtMap<CrdtString, ColumnVersion> deletion_clock;
deletion_clock.emplace("", ColumnVersion(remote_col_version, remote_db_version, remote_node_id, remote_local_db_version));
data_.emplace(record_id, Record<V>(CrdtMap<CrdtString, V>(), std::move(deletion_clock)));
} else {
if (!is_record_tombstoned(record_id)) {
// Handle insertion or update
Record<V> &record = get_or_create_record_unchecked(record_id);
// Insert or update the field value
if (remote_value.has_value()) {
record.fields[*col_name] = std::move(remote_value.value());
}
// Update the column version info
record.column_versions.insert_or_assign(std::move(*col_name), ColumnVersion(remote_col_version, remote_db_version,
remote_node_id, remote_local_db_version));
}
}
}
}
constexpr bool is_record_tombstoned(const K &record_id, bool ignore_parent = false) const {
if (tombstones_.find(record_id) != tombstones_.end()) {
return true;
}
if (parent_ && !ignore_parent) {
return parent_->is_record_tombstoned(record_id);
}
return false;
}
// Notice that this will not check if the record is tombstoned! Such check should be done by the caller
constexpr Record<V> &get_or_create_record_unchecked(const K &record_id, bool ignore_parent = false) {
auto [it, inserted] = data_.try_emplace(record_id, Record<V>());
if (inserted && parent_ && !ignore_parent) {
if (auto parent_record = parent_->get_record_ptr(record_id)) {
it->second = *parent_record;
}
}
return it->second;
}
constexpr Record<V> *get_record_ptr(const K &record_id, bool ignore_parent = false) {
auto it = data_.find(record_id);
if (it != data_.end()) {
return &(it->second);
}
if (ignore_parent) {
return nullptr;
} else {
return parent_ ? parent_->get_record_ptr(record_id) : nullptr;
}
}
constexpr const Record<V> *get_record_ptr(const K &record_id, bool ignore_parent = false) const {
auto it = data_.find(record_id);
if (it != data_.end()) {
return &(it->second);
}
if (ignore_parent) {
return nullptr;
} else {
return parent_ ? parent_->get_record_ptr(record_id) : nullptr;
}
}
/// Generates inverse changes for a given set of changes based on a reference CRDT state.
///
/// # Arguments
///
/// * `changes` - A vector of changes to invert.
/// * `reference_crdt` - A reference CRDT to use as the base state for inversion.
///
/// # Returns
///
/// A vector of inverse `Change` objects.
CrdtVector<Change<K, V>> invert_changes(
const CrdtVector<Change<K, V>> &changes,
const CRDT<K, V, MergeContext, MergeRuleType, ChangeComparatorType, SortFunctionType, MapType> &reference_crdt) const {