| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace DCL.CRDT |
| | 6 | | { |
| | 7 | | public class CRDTProtocol |
| | 8 | | { |
| | 9 | | public enum ProcessMessageResultType |
| | 10 | | { |
| | 11 | | /** |
| | 12 | | * Typical message and new state set. |
| | 13 | | * @state CHANGE |
| | 14 | | * @reason Incoming message has a timestamp greater |
| | 15 | | */ |
| | 16 | | StateUpdatedTimestamp = 1, |
| | 17 | |
|
| | 18 | | /** |
| | 19 | | * Typical message when it is considered old. |
| | 20 | | * @state it does NOT CHANGE. |
| | 21 | | * @reason incoming message has a timestamp lower. |
| | 22 | | */ |
| | 23 | | StateOutdatedTimestamp = 2, |
| | 24 | |
|
| | 25 | | /** |
| | 26 | | * Weird message, same timestamp and data. |
| | 27 | | * @state it does NOT CHANGE. |
| | 28 | | * @reason consistent state between peers. |
| | 29 | | */ |
| | 30 | | NoChanges = 3, |
| | 31 | |
|
| | 32 | | /** |
| | 33 | | * Less but typical message, same timestamp, resolution by data. |
| | 34 | | * @state it does NOT CHANGE. |
| | 35 | | * @reason incoming message has a LOWER data. |
| | 36 | | */ |
| | 37 | | StateOutdatedData = 4, |
| | 38 | |
|
| | 39 | | /** |
| | 40 | | * Less but typical message, same timestamp, resolution by data. |
| | 41 | | * @state CHANGE |
| | 42 | | * @reason incoming message has a GREATER data. |
| | 43 | | */ |
| | 44 | | StateUpdatedData = 5, |
| | 45 | |
|
| | 46 | | /** |
| | 47 | | * Entity was previously deleted. |
| | 48 | | * @state it does NOT CHANGE. |
| | 49 | | * @reason The message is considered old. |
| | 50 | | */ |
| | 51 | | EntityWasDeleted = 6, |
| | 52 | |
|
| | 53 | | /** |
| | 54 | | * Entity should be deleted. |
| | 55 | | * @state CHANGE |
| | 56 | | * @reason the state is storing old entities |
| | 57 | | */ |
| | 58 | | EntityDeleted = 7, |
| | 59 | |
|
| | 60 | | /** |
| | 61 | | * An APPEND with a success insert |
| | 62 | | * @state CHANGE |
| | 63 | | * @reason the element is not in set set yet |
| | 64 | | */ |
| | 65 | | StateElementAddedToSet = 8, |
| | 66 | | }; |
| | 67 | |
|
| 367 | 68 | | private int MAX_ELEMENT_SET = 100; |
| | 69 | |
|
| | 70 | | internal class CrdtEntityComponentData |
| | 71 | | { |
| | 72 | | public long entityId; |
| | 73 | | public int componentId; |
| | 74 | | public int timestamp; |
| | 75 | | public object data; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | internal class EntityComponentData : IComparable<EntityComponentData> |
| | 79 | | { |
| | 80 | | public int timestamp; |
| | 81 | | public object data; |
| | 82 | |
|
| | 83 | | public int CompareTo(EntityComponentData other) |
| | 84 | | { |
| 0 | 85 | | int timestampDiff = this.timestamp - other.timestamp; |
| | 86 | |
|
| 0 | 87 | | if (timestampDiff == 0) |
| | 88 | | { |
| 0 | 89 | | return CompareData(this.data, other.data); |
| | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | return Math.Sign(timestampDiff); |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | internal class CrdtState |
| | 99 | | { |
| 393 | 100 | | public readonly Dictionary<int, Dictionary<long, EntityComponentData>> singleComponents = new Dictionary<int |
| 393 | 101 | | public readonly Dictionary<int, Dictionary<long, List<EntityComponentData>>> setComponents = new Dictionary< |
| 393 | 102 | | public readonly Dictionary<int, int> deletedEntitiesSet = new Dictionary<int, int>(); |
| | 103 | | } |
| | 104 | |
|
| 367 | 105 | | internal CrdtState state = new CrdtState(); |
| | 106 | |
|
| | 107 | | public ProcessMessageResultType ProcessMessage(CrdtMessage message) |
| | 108 | | { |
| 124 | 109 | | int entityId = (int)message.EntityId; |
| 124 | 110 | | int entityNumber = entityId & 0xffff; |
| 124 | 111 | | int entityVersion = (entityId >> 16) & 0xffff; |
| 124 | 112 | | bool entityNumberWasDeleted = state.deletedEntitiesSet.TryGetValue(entityNumber, out int deletedVersion); |
| | 113 | |
|
| 124 | 114 | | if (entityNumberWasDeleted) |
| | 115 | | { |
| 24 | 116 | | if (deletedVersion >= entityVersion) |
| | 117 | | { |
| 24 | 118 | | return ProcessMessageResultType.EntityWasDeleted; |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| 100 | 122 | | if (message.Type == CrdtMessageType.DELETE_ENTITY) |
| | 123 | | { |
| 6 | 124 | | if (entityNumberWasDeleted) |
| | 125 | | { |
| 0 | 126 | | state.deletedEntitiesSet[entityNumber] = entityVersion; |
| | 127 | | } |
| | 128 | | else |
| | 129 | | { |
| 6 | 130 | | state.deletedEntitiesSet.Add(entityNumber, entityVersion); |
| | 131 | | } |
| | 132 | |
|
| 84 | 133 | | foreach (var component in state.singleComponents) |
| | 134 | | { |
| 36 | 135 | | component.Value.Remove(entityId); |
| | 136 | | } |
| | 137 | |
|
| 12 | 138 | | foreach (var componentSet in state.setComponents) |
| | 139 | | { |
| 0 | 140 | | componentSet.Value.Remove(entityId); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | // TODO: clean the state with this entityId |
| | 144 | |
|
| 6 | 145 | | return ProcessMessageResultType.EntityDeleted; |
| | 146 | | } |
| | 147 | |
|
| 94 | 148 | | if (message.Type == CrdtMessageType.APPEND_COMPONENT) |
| | 149 | | { |
| 0 | 150 | | bool elementAdded = TryAddSetComponentState(message.EntityId, message.ComponentId, message.Data, message |
| | 151 | |
|
| 0 | 152 | | if (elementAdded) |
| | 153 | | { |
| 0 | 154 | | return ProcessMessageResultType.StateElementAddedToSet; |
| | 155 | | } |
| | 156 | | else |
| | 157 | | { |
| 0 | 158 | | return ProcessMessageResultType.NoChanges; |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 94 | 162 | | TryGetSingleComponentState(message.EntityId, message.ComponentId, out EntityComponentData storedData); |
| | 163 | |
|
| | 164 | | // The received message is > than our current value, update our state.components. |
| 94 | 165 | | if (storedData == null || storedData.timestamp < message.Timestamp) |
| | 166 | | { |
| 72 | 167 | | UpdateSingleComponentState(message.EntityId, message.ComponentId, message.Data, message.Timestamp); |
| 72 | 168 | | return ProcessMessageResultType.StateUpdatedTimestamp; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | // Outdated Message. Resend our state message through the wire. |
| 22 | 172 | | if (storedData.timestamp > message.Timestamp) |
| | 173 | | { |
| 2 | 174 | | return ProcessMessageResultType.StateOutdatedTimestamp; |
| | 175 | | } |
| | 176 | |
|
| 20 | 177 | | int currentDataGreater = CompareData(storedData.data, message.Data); |
| | 178 | |
|
| | 179 | | // Same data, same timestamp. Weirdo echo message. |
| 20 | 180 | | if (currentDataGreater == 0) |
| | 181 | | { |
| 1 | 182 | | return ProcessMessageResultType.NoChanges; |
| | 183 | | } |
| | 184 | |
|
| | 185 | | // Current data is greater |
| 19 | 186 | | if (currentDataGreater > 0) |
| | 187 | | { |
| 3 | 188 | | return ProcessMessageResultType.StateOutdatedData; |
| | 189 | | } |
| | 190 | |
|
| | 191 | | // Curent data is lower |
| 16 | 192 | | UpdateSingleComponentState(message.EntityId, message.ComponentId, message.Data, message.Timestamp); |
| 16 | 193 | | return ProcessMessageResultType.StateUpdatedData; |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public List<CrdtMessage> GetStateAsMessages() |
| | 197 | | { |
| 2 | 198 | | List<CrdtMessage> crdtMessagesList = new List<CrdtMessage>(); |
| | 199 | |
|
| 8 | 200 | | foreach (var component in state.singleComponents) |
| | 201 | | { |
| 8 | 202 | | foreach (var entityComponentData in component.Value) |
| | 203 | | { |
| 2 | 204 | | crdtMessagesList.Add(new CrdtMessage |
| | 205 | | ( |
| | 206 | | type: entityComponentData.Value.data == null ? CrdtMessageType.DELETE_COMPONENT : CrdtMessageTyp |
| | 207 | | entityId: entityComponentData.Key, |
| | 208 | | componentId: component.Key, |
| | 209 | | timestamp: entityComponentData.Value.timestamp, |
| | 210 | | data: entityComponentData.Value.data |
| | 211 | | )); |
| | 212 | | } |
| | 213 | | } |
| | 214 | |
|
| 4 | 215 | | foreach (var component in state.setComponents) |
| | 216 | | { |
| 0 | 217 | | foreach (var set in component.Value) |
| | 218 | | { |
| 0 | 219 | | foreach (var entityComponentData in set.Value) |
| | 220 | | { |
| 0 | 221 | | crdtMessagesList.Add(new CrdtMessage |
| | 222 | | ( |
| | 223 | | type: CrdtMessageType.APPEND_COMPONENT, |
| | 224 | | entityId: set.Key, |
| | 225 | | componentId: component.Key, |
| | 226 | | timestamp: entityComponentData.timestamp, |
| | 227 | | data: entityComponentData.data |
| | 228 | | )); |
| | 229 | | } |
| | 230 | | } |
| | 231 | | } |
| | 232 | |
|
| 4 | 233 | | foreach (var entity in state.deletedEntitiesSet) |
| | 234 | | { |
| 0 | 235 | | long entityNumber = entity.Key; |
| 0 | 236 | | long entityVersion = entity.Value; |
| 0 | 237 | | long entityId = entityNumber | (entityVersion << 16); |
| | 238 | |
|
| 0 | 239 | | crdtMessagesList.Add(new CrdtMessage( |
| | 240 | | type: CrdtMessageType.DELETE_ENTITY, |
| | 241 | | entityId: entityId, |
| | 242 | | componentId: 0, |
| | 243 | | timestamp: 0, |
| | 244 | | data: null |
| | 245 | | )); |
| | 246 | | } |
| | 247 | |
|
| 2 | 248 | | return crdtMessagesList; |
| | 249 | | } |
| | 250 | |
|
| | 251 | | public CrdtMessage CreateLwwMessage(int entityId, int componentId, byte[] data) |
| | 252 | | { |
| 2 | 253 | | int timeStamp = 0; |
| | 254 | |
|
| 2 | 255 | | if (TryGetSingleComponentState(entityId, componentId, out EntityComponentData storedMessage)) |
| | 256 | | { |
| 0 | 257 | | timeStamp = storedMessage.timestamp + 1; |
| | 258 | | } |
| | 259 | |
|
| 2 | 260 | | return new CrdtMessage |
| | 261 | | ( |
| | 262 | | type: data == null ? CrdtMessageType.DELETE_COMPONENT : CrdtMessageType.PUT_COMPONENT, |
| | 263 | | entityId: entityId, |
| | 264 | | componentId: componentId, |
| | 265 | | timestamp: timeStamp, |
| | 266 | | data: data |
| | 267 | | ); |
| | 268 | | } |
| | 269 | |
|
| | 270 | | public CrdtMessage CreateSetMessage(int entityId, int componentId, byte[] data) |
| | 271 | | { |
| 0 | 272 | | int timeStamp = 0; |
| | 273 | |
|
| 0 | 274 | | if (TryGetSingleComponentState(entityId, componentId, out EntityComponentData storedMessage)) |
| | 275 | | { |
| 0 | 276 | | timeStamp = storedMessage.timestamp + 1; |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | return new CrdtMessage |
| | 280 | | ( |
| | 281 | | type: CrdtMessageType.APPEND_COMPONENT, |
| | 282 | | entityId: entityId, |
| | 283 | | componentId: componentId, |
| | 284 | | timestamp: timeStamp, |
| | 285 | | data: data |
| | 286 | | ); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | internal EntityComponentData GetState(long entityId, int componentId) |
| | 290 | | { |
| 3 | 291 | | TryGetSingleComponentState(entityId, componentId, out EntityComponentData entityComponentData); |
| 3 | 292 | | return entityComponentData; |
| | 293 | | } |
| | 294 | |
|
| | 295 | | private void UpdateSingleComponentState(long entityId, int componentId, object data, int remoteTimestamp) |
| | 296 | | { |
| 88 | 297 | | bool stateExists = TryGetSingleComponentState(entityId, componentId, out EntityComponentData currentStateVal |
| | 298 | |
|
| 88 | 299 | | if (stateExists) |
| | 300 | | { |
| 31 | 301 | | currentStateValue.data = data; |
| 31 | 302 | | currentStateValue.timestamp = Math.Max(remoteTimestamp, currentStateValue.timestamp); |
| | 303 | | } |
| | 304 | | else |
| | 305 | | { |
| 57 | 306 | | EntityComponentData newState = new EntityComponentData() |
| | 307 | | { |
| | 308 | | timestamp = remoteTimestamp, |
| | 309 | | data = data |
| | 310 | | }; |
| | 311 | |
|
| 57 | 312 | | state.singleComponents.TryGetValue(componentId, out Dictionary<long, EntityComponentData> componentSet); |
| | 313 | |
|
| 57 | 314 | | if (componentSet != null) |
| | 315 | | { |
| 1 | 316 | | componentSet.Add(entityId, newState); |
| | 317 | | } |
| | 318 | | else |
| | 319 | | { |
| 56 | 320 | | state.singleComponents.Add(componentId, new Dictionary<long, EntityComponentData>()); |
| 56 | 321 | | state.singleComponents[componentId].Add(entityId, newState); |
| | 322 | | } |
| | 323 | | } |
| 56 | 324 | | } |
| | 325 | |
|
| | 326 | | /** |
| | 327 | | * @returns true if the element is added or false if it already exists |
| | 328 | | */ |
| | 329 | | private bool TryAddSetComponentState(long entityId, int componentId, object data, int remoteTimestamp) |
| | 330 | | { |
| 0 | 331 | | bool stateExists = TryGetComponentSetState(entityId, componentId, out List<EntityComponentData> currentSetSt |
| | 332 | |
|
| 0 | 333 | | EntityComponentData newState = new EntityComponentData() |
| | 334 | | { |
| | 335 | | timestamp = remoteTimestamp, |
| | 336 | | data = data |
| | 337 | | }; |
| | 338 | |
|
| | 339 | | // The entity already has a Set |
| 0 | 340 | | if (stateExists) |
| | 341 | | { |
| 0 | 342 | | int index = currentSetState.BinarySearch(newState); |
| | 343 | |
|
| 0 | 344 | | if (index < 0) |
| | 345 | | { |
| 0 | 346 | | index = ~index; |
| 0 | 347 | | currentSetState.Insert(index, newState); |
| | 348 | |
|
| 0 | 349 | | if (currentSetState.Count > MAX_ELEMENT_SET) |
| | 350 | | { |
| 0 | 351 | | currentSetState.RemoveRange(MAX_ELEMENT_SET, currentSetState.Count - MAX_ELEMENT_SET); |
| | 352 | | } |
| | 353 | | } |
| | 354 | | else |
| | 355 | | { |
| | 356 | | // If the element already exist, we don't add it twice. |
| 0 | 357 | | return false; |
| | 358 | | } |
| | 359 | | } |
| | 360 | | else |
| | 361 | | { |
| 0 | 362 | | state.setComponents.TryGetValue(componentId, out Dictionary<long, List<EntityComponentData>> componentSe |
| | 363 | |
|
| | 364 | | // The component is already in the dictionary, we have to create the new List for the Entity |
| 0 | 365 | | if (componentSet != null) |
| | 366 | | { |
| 0 | 367 | | componentSet.Add(entityId, new List<EntityComponentData>() { newState }); |
| | 368 | | } |
| | 369 | | else |
| | 370 | | { |
| 0 | 371 | | state.setComponents.Add(componentId, new Dictionary<long, List<EntityComponentData>>()); |
| 0 | 372 | | state.setComponents[componentId].Add(entityId, new List<EntityComponentData>() { newState }); |
| | 373 | | } |
| | 374 | | } |
| | 375 | |
|
| 0 | 376 | | return true; |
| | 377 | | } |
| | 378 | |
|
| | 379 | | private bool TryGetSingleComponentState(long entityId, int componentId, out EntityComponentData entityComponentD |
| | 380 | | { |
| 187 | 381 | | if (state.singleComponents.TryGetValue(componentId, out Dictionary<long, EntityComponentData> innerDictionar |
| | 382 | | { |
| 73 | 383 | | if (innerDictionary.TryGetValue(entityId, out entityComponentData)) |
| | 384 | | { |
| 71 | 385 | | return true; |
| | 386 | | } |
| | 387 | | } |
| | 388 | |
|
| 116 | 389 | | entityComponentData = null; |
| 116 | 390 | | return false; |
| | 391 | | } |
| | 392 | |
|
| | 393 | | private bool TryGetComponentSetState(long entityId, int componentId, out List<EntityComponentData> entityCompone |
| | 394 | | { |
| 0 | 395 | | if (state.setComponents.TryGetValue(componentId, out Dictionary<long, List<EntityComponentData>> innerDictio |
| | 396 | | { |
| 0 | 397 | | if (innerDictionary.TryGetValue(entityId, out entityComponentSet)) |
| | 398 | | { |
| 0 | 399 | | return true; |
| | 400 | | } |
| | 401 | | } |
| | 402 | |
|
| 0 | 403 | | entityComponentSet = null; |
| 0 | 404 | | return false; |
| | 405 | | } |
| | 406 | |
|
| | 407 | | /** |
| | 408 | | * Compare raw data. |
| | 409 | | * @internal |
| | 410 | | * @returns 0 if is the same data, 1 if a > b, -1 if b > a |
| | 411 | | */ |
| | 412 | | public static int CompareData(object a, object b) |
| | 413 | | { |
| | 414 | | // At reference level |
| 53 | 415 | | if (a == b) return 0; |
| 53 | 416 | | if (a == null) return -1; |
| 53 | 417 | | if (b == null) return 1; |
| | 418 | |
|
| 53 | 419 | | if (a is byte[] bytesA && b is byte[] bytesB) |
| | 420 | | { |
| 3 | 421 | | int lengthDifference = bytesA.Length - bytesB.Length; |
| | 422 | |
|
| 3 | 423 | | if (lengthDifference != 0) |
| | 424 | | { |
| 0 | 425 | | return lengthDifference > 0 ? 1 : -1; |
| | 426 | | } |
| | 427 | |
|
| 246 | 428 | | for (int i = 0; i < bytesA.Length; i++) |
| | 429 | | { |
| 120 | 430 | | int res = bytesA[i] - bytesB[i]; |
| | 431 | |
|
| 120 | 432 | | if (res != 0) |
| | 433 | | { |
| 0 | 434 | | return res > 0 ? 1 : -1; |
| | 435 | | } |
| | 436 | | } |
| | 437 | |
|
| | 438 | | // the data is exactly the same |
| 3 | 439 | | return 0; |
| | 440 | | } |
| | 441 | |
|
| 50 | 442 | | if (a is string strA && b is string strB) |
| | 443 | | { |
| 50 | 444 | | int lengthDifference = strA.Length - strB.Length; |
| | 445 | |
|
| 50 | 446 | | if (lengthDifference != 0) |
| | 447 | | { |
| 2 | 448 | | return lengthDifference > 0 ? 1 : -1; |
| | 449 | | } |
| | 450 | |
|
| 48 | 451 | | return string.Compare(strA, strB, StringComparison.InvariantCulture); |
| | 452 | | } |
| | 453 | |
|
| 0 | 454 | | return Comparer.Default.Compare(a, b); |
| | 455 | | } |
| | 456 | | } |
| | 457 | | } |