|
1 | 1 | package com.thealgorithms.datastructures.crdt; |
2 | 2 |
|
| 3 | +import java.time.Instant; |
3 | 4 | import java.util.HashMap; |
4 | 5 | import java.util.Map; |
5 | 6 |
|
6 | 7 | /** |
7 | | - * Last-Write-Wins Element Set (LWWElementSet) is a state-based CRDT (Conflict-free Replicated Data Type) |
8 | | - * designed for managing sets in a distributed and concurrent environment. It supports the addition and removal |
9 | | - * of elements, using timestamps to determine the order of operations. The set is split into two subsets: |
10 | | - * the add set for elements to be added and the remove set for elements to be removed. |
| 8 | + * Last-Write-Wins Element Set (LWWElementSet) is a state-based CRDT (Conflict-free Replicated Data |
| 9 | + * Type) designed for managing sets in a distributed and concurrent environment. It supports the |
| 10 | + * addition and removal of elements, using timestamps to determine the order of operations. The set |
| 11 | + * is split into two subsets: the add set for elements to be added and the remove set for elements |
| 12 | + * to be removed. The LWWElementSet ensures that the most recent operation (based on the timestamp) |
| 13 | + * wins in the case of concurrent operations. |
11 | 14 | * |
12 | | - * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) |
13 | | - * @see <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Conflict-free_replicated_data_type</a> |
14 | | - * @see <a href="https://github.com/itakurah">itakurah (Niklas Hoefflin)</a> |
| 15 | + * @param <T> The type of the elements in the LWWElementSet. |
| 16 | + * @author <a href="https://github.com/itakurah">itakurah (GitHub)</a>, <a |
| 17 | + * href="https://www.linkedin.com/in/niklashoefflin/">Niklas Hoefflin (LinkedIn)</a> |
| 18 | + * @see <a href="https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type">Conflict free |
| 19 | + * replicated data type (Wikipedia)</a> |
| 20 | + * @see <a href="https://inria.hal.science/inria-00555588v1/document">A comprehensive study of |
| 21 | + * Convergent and Commutative Replicated Data Types</a> |
15 | 22 | */ |
16 | | - |
17 | | -class Element { |
18 | | - String key; |
19 | | - int timestamp; |
20 | | - Bias bias; |
| 23 | +class LWWElementSet<T> { |
| 24 | + final Map<T, Element<T>> addSet; |
| 25 | + final Map<T, Element<T>> removeSet; |
21 | 26 |
|
22 | 27 | /** |
23 | | - * Constructs a new Element with the specified key, timestamp and bias. |
24 | | - * |
25 | | - * @param key The key of the element. |
26 | | - * @param timestamp The timestamp associated with the element. |
27 | | - * @param bias The bias of the element (ADDS or REMOVALS). |
28 | | - */ |
29 | | - Element(String key, int timestamp, Bias bias) { |
30 | | - this.key = key; |
31 | | - this.timestamp = timestamp; |
32 | | - this.bias = bias; |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -enum Bias { |
37 | | - /** |
38 | | - * ADDS bias for the add set. |
39 | | - * REMOVALS bias for the remove set. |
40 | | - */ |
41 | | - ADDS, |
42 | | - REMOVALS |
43 | | -} |
44 | | - |
45 | | -class LWWElementSet { |
46 | | - private final Map<String, Element> addSet; |
47 | | - private final Map<String, Element> removeSet; |
48 | | - |
49 | | - /** |
50 | | - * Constructs an empty LWWElementSet. |
| 28 | + * Constructs an empty LWWElementSet. This constructor initializes the addSet and removeSet as |
| 29 | + * empty HashMaps. The addSet stores elements that are added, and the removeSet stores elements |
| 30 | + * that are removed. |
51 | 31 | */ |
52 | 32 | LWWElementSet() { |
53 | 33 | this.addSet = new HashMap<>(); |
54 | 34 | this.removeSet = new HashMap<>(); |
55 | 35 | } |
56 | 36 |
|
57 | 37 | /** |
58 | | - * Adds an element to the addSet. |
| 38 | + * Adds an element to the addSet with the current timestamp. This method stores the element in the |
| 39 | + * addSet, ensuring that the element is added to the set with an associated timestamp that |
| 40 | + * represents the time of the addition. |
59 | 41 | * |
60 | | - * @param e The element to be added. |
| 42 | + * @param key The key of the element to be added. |
61 | 43 | */ |
62 | | - public void add(Element e) { |
63 | | - addSet.put(e.key, e); |
| 44 | + public void add(T key) { |
| 45 | + addSet.put(key, new Element<>(key, Instant.now())); |
64 | 46 | } |
65 | 47 |
|
66 | 48 | /** |
67 | | - * Removes an element from the removeSet. |
| 49 | + * Removes an element by adding it to the removeSet with the current timestamp. This method adds |
| 50 | + * the element to the removeSet, marking it as removed with the current timestamp. |
68 | 51 | * |
69 | | - * @param e The element to be removed. |
| 52 | + * @param key The key of the element to be removed. |
70 | 53 | */ |
71 | | - public void remove(Element e) { |
72 | | - if (lookup(e)) { |
73 | | - removeSet.put(e.key, e); |
74 | | - } |
| 54 | + public void remove(T key) { |
| 55 | + removeSet.put(key, new Element<>(key, Instant.now())); |
75 | 56 | } |
76 | 57 |
|
77 | 58 | /** |
78 | | - * Checks if an element is in the LWWElementSet by comparing timestamps in the addSet and removeSet. |
| 59 | + * Checks if an element is in the LWWElementSet. An element is considered present if it exists in |
| 60 | + * the addSet and either does not exist in the removeSet, or its add timestamp is later than any |
| 61 | + * corresponding remove timestamp. |
79 | 62 | * |
80 | | - * @param e The element to be checked. |
81 | | - * @return True if the element is present, false otherwise. |
| 63 | + * @param key The key of the element to be checked. |
| 64 | + * @return {@code true} if the element is present in the set (i.e., its add timestamp is later |
| 65 | + * than its remove timestamp, or it is not in the remove set), {@code false} otherwise (i.e., |
| 66 | + * the element has been removed or its remove timestamp is later than its add timestamp). |
82 | 67 | */ |
83 | | - public boolean lookup(Element e) { |
84 | | - Element inAddSet = addSet.get(e.key); |
85 | | - Element inRemoveSet = removeSet.get(e.key); |
| 68 | + public boolean lookup(T key) { |
| 69 | + Element<T> inAddSet = addSet.get(key); |
| 70 | + Element<T> inRemoveSet = removeSet.get(key); |
86 | 71 |
|
87 | | - return (inAddSet != null && (inRemoveSet == null || inAddSet.timestamp > inRemoveSet.timestamp)); |
| 72 | + return inAddSet != null && (inRemoveSet == null || inAddSet.timestamp.isAfter(inRemoveSet.timestamp)); |
88 | 73 | } |
89 | 74 |
|
90 | 75 | /** |
91 | | - * Compares the LWWElementSet with another LWWElementSet to check if addSet and removeSet are a subset. |
| 76 | + * Merges another LWWElementSet into this set. This method takes the union of both the add-sets |
| 77 | + * and remove-sets from the two sets, resolving conflicts by keeping the element with the latest |
| 78 | + * timestamp. If an element appears in both the add-set and remove-set of both sets, the one with |
| 79 | + * the later timestamp will be retained. |
92 | 80 | * |
93 | | - * @param other The LWWElementSet to compare. |
94 | | - * @return True if the set is subset, false otherwise. |
| 81 | + * @param other The LWWElementSet to merge with the current set. |
95 | 82 | */ |
96 | | - public boolean compare(LWWElementSet other) { |
97 | | - return other.addSet.keySet().containsAll(addSet.keySet()) && other.removeSet.keySet().containsAll(removeSet.keySet()); |
| 83 | + public void merge(LWWElementSet<T> other) { |
| 84 | + for (Map.Entry<T, Element<T>> entry : other.addSet.entrySet()) { |
| 85 | + addSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict); |
| 86 | + } |
| 87 | + for (Map.Entry<T, Element<T>> entry : other.removeSet.entrySet()) { |
| 88 | + removeSet.merge(entry.getKey(), entry.getValue(), this::resolveConflict); |
| 89 | + } |
98 | 90 | } |
99 | 91 |
|
100 | 92 | /** |
101 | | - * Merges another LWWElementSet into this set by resolving conflicts based on timestamps. |
| 93 | + * Resolves conflicts between two elements by selecting the one with the later timestamp. This |
| 94 | + * method is used when merging two LWWElementSets to ensure that the most recent operation (based |
| 95 | + * on timestamps) is kept. |
102 | 96 | * |
103 | | - * @param other The LWWElementSet to merge. |
| 97 | + * @param e1 The first element. |
| 98 | + * @param e2 The second element. |
| 99 | + * @return The element with the later timestamp. |
104 | 100 | */ |
105 | | - public void merge(LWWElementSet other) { |
106 | | - for (Element e : other.addSet.values()) { |
107 | | - if (!addSet.containsKey(e.key) || compareTimestamps(addSet.get(e.key), e)) { |
108 | | - addSet.put(e.key, e); |
109 | | - } |
110 | | - } |
111 | | - |
112 | | - for (Element e : other.removeSet.values()) { |
113 | | - if (!removeSet.containsKey(e.key) || compareTimestamps(removeSet.get(e.key), e)) { |
114 | | - removeSet.put(e.key, e); |
115 | | - } |
116 | | - } |
| 101 | + private Element<T> resolveConflict(Element<T> e1, Element<T> e2) { |
| 102 | + return e1.timestamp.isAfter(e2.timestamp) ? e1 : e2; |
117 | 103 | } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Represents an element in the LWWElementSet, consisting of a key and a timestamp. This class is |
| 108 | + * used to store the elements in both the add and remove sets with their respective timestamps. |
| 109 | + * |
| 110 | + * @param <T> The type of the key associated with the element. |
| 111 | + */ |
| 112 | +class Element<T> { |
| 113 | + T key; |
| 114 | + Instant timestamp; |
118 | 115 |
|
119 | 116 | /** |
120 | | - * Compares timestamps of two elements based on their bias (ADDS or REMOVALS). |
| 117 | + * Constructs a new Element with the specified key and timestamp. |
121 | 118 | * |
122 | | - * @param e The first element. |
123 | | - * @param other The second element. |
124 | | - * @return True if the first element's timestamp is greater or the bias is ADDS and timestamps are equal. |
| 119 | + * @param key The key of the element. |
| 120 | + * @param timestamp The timestamp associated with the element. |
125 | 121 | */ |
126 | | - public boolean compareTimestamps(Element e, Element other) { |
127 | | - if (e.bias != other.bias) { |
128 | | - throw new IllegalArgumentException("Invalid bias value"); |
129 | | - } |
130 | | - Bias bias = e.bias; |
131 | | - int timestampComparison = Integer.compare(e.timestamp, other.timestamp); |
132 | | - |
133 | | - if (timestampComparison == 0) { |
134 | | - return bias != Bias.ADDS; |
135 | | - } |
136 | | - return timestampComparison < 0; |
| 122 | + Element(T key, Instant timestamp) { |
| 123 | + this.key = key; |
| 124 | + this.timestamp = timestamp; |
137 | 125 | } |
138 | 126 | } |
0 commit comments