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
// (c) Copyright 2025 Helsing GmbH. All rights reserved.
//! # DSON: A Delta-State CRDT for JSON-like Data Structures
//!
//! This crate provides a Rust implementation of **DSON**, a space-efficient,
//! delta-state Conflict-Free Replicated Datatype (CRDT) for JSON-like data structures.
//! It is based on the research paper ["DSON: JSON CRDT Using Delta-Mutations For Document Stores"][paper]
//! and inspired by the original author's [JavaScript implementation][js-impl].
//!
//! The primary goal of this library is to enable robust, and efficient
//! multi-writer collaboration in extremely constrained environments (high
//! latency and low bandwidth; opportunistic networking).
//!
//! Unlike other CRDT libraries that expose a single "Document" type, DSON provides a set of
//! composable primitives. This allows you to build the exact data structure you need. The most
//! common top-level structure is an [`OrMap`], which can contain other CRDTs, enabling nested,
//! JSON-like objects. The entire state is typically wrapped in a [`CausalDotStore`], which
//! tracks the causal history.
//!
//! [paper]: https://dl.acm.org/doi/10.14778/3510397.3510403
//! [oppnet]: https://hal.science/hal-03405138/document "Frédéric Guidec, Yves Mahéo, Camille Noûs. Delta-State-Based Synchronization of CRDTs in Opportunistic Networks. In 2021 IEEE 46th Conference on Local Computer Networks (LCN). doi:10.1109/LCN52139.2021.9524978"
//! [js-impl]: https://github.com/crdt-ibm-research/json-delta-crdt
//!
//! ## Core Concepts
//!
//! DSON provides three fundamental, composable CRDTs:
//!
//! - [`OrMap`]: An **Observed-Remove Map**, mapping arbitrary keys to other CRDT values.
//! - [`OrArray`]: An **Observed-Remove Array**, providing a list-like structure.
//! - [`MvReg`]: A **Multi-Value Register**, for storing primitive values. When
//! concurrent writes occur, the register holds all conflicting values. This is
//! the only CRDT in this library that can represent value conflicts.
//!
//! These primitives can be nested to create arbitrarily complex data structures, such as a map
//! containing an array of other maps.
//!
//! All modifications produce a **delta**. Instead of sending the entire state after each
//! change, only this small delta needs to be transmitted to other replicas.
//!
//! ## Observed-Remove Semantics
//!
//! DSON uses **Observed-Remove (OR)** semantics for its collections. This means
//! an element can only be removed if its addition has been observed. If an
//! element is updated concurrently with its removal, the update "wins," and the
//! element is preserved. OR-semantics are intuitive, and this is often the
//! desired behavior.
//! Consider a collaborative shopping list:
//!
//! 1. **Initial State**: Both Alice and Bob see `["apples", "bananas"]`.
//! 2. **Alice's Action**: Alice updates "bananas" to "blueberries".
//! 3. **Bob's Action**: Concurrently, Bob removes "bananas".
//!
//! With OR-semantics, the final list will be `["apples", "blueberries"]`. Bob's removal
//! is overridden by Alice's concurrent update because the update implies the continued
//! existence of the item.
//!
//! DSON can be extended with special CRDTs providing different semantics
//! for specific use cases though.
//!
//! ## Causal CRDTs and Tombstone-Free Removals
//!
//! DSON is a **causal** CRDT, meaning it uses causal history to resolve conflicts.
//! This history is tracked in a [`CausalContext`], which contains a set of "dots"—unique
//! identifiers for every operation.
//!
//! ### Dots
//!
//! A **dot** is a globally unique identifier for an operation (for example, adding or updating a
//! value). It is the fundamental unit for tracking causality.
//!
//! A [`Dot`] is a tuple `(Identifier, Sequence)`:
//!
//! - **[`Identifier`]**: A unique ID for the actor (a specific application instance on a
//! specific node) that performed the operation. It is composed of a `NodeId` and an
//! `ApplicationId`. This structure allows multiple applications on the same machine to
//! collaborate without their histories conflicting.
//! - **`Sequence`**: A monotonically increasing number (effectively a Lamport timestamp)
//! that is unique to that actor.
//!
//! When a replica makes a change, it generates a new dot. This dot is broadcast to other
//! replicas along with the **delta** describing the change.
//!
//! The collection of all dots a replica has observed forms its [`CausalContext`]. This
//! context represents the replica's knowledge of the document's history. By comparing its
//! local `CausalContext` with the context from a received delta, a replica can determine
//! which operations are new, which are concurrent, and which have already been seen. This
//! allows DSON to merge changes correctly and guarantee convergence.
//!
//! A key advantage of this model is the elimination of **tombstones**. In many other
//! CRDTs, when an item is deleted, a "tombstone" marker is left behind to signify
//! its removal. These tombstones are never garbage-collected and can cause unbounded
//! metadata growth in long-lived documents.
//!
//! DSON avoids this growth by tracking which operations are "live". A removal is simply the
//! absence of an operation's dot from the causal context. When replicas sync, they
//! can determine which items have been deleted by comparing their causal contexts,
//! without needing explicit tombstone markers. This ensures that the metadata size
//! remains proportional to the size of the live data, not the entire history of
//! operations.
//!
//! ## Scope of this Crate
//!
//! This crate provides the core data structures and algorithms for DSON. It is
//! responsible for generating deltas from mutations and merging them to ensure
//! eventual consistency. It is up to you to build your document structure by
//! composing the provided CRDT primitives, most commonly by wrapping an [`OrMap`]
//! in a [`CausalDotStore`].
//!
//! Note that this is a low-level library. You will likely want to build a
//! typed abstraction layer on top of `dson` rather than use it directly in your
//! application code.
//!
//! **It does not include any networking protocols.**
//!
//! You are responsible for implementing the transport layer to broadcast deltas
//! to other replicas. The correctness of this library, particularly its
//! **causal consistency** guarantees, relies on the transport layer delivering
//! deltas in an order that respects the causal history of events. This is typically
//! achieved with an anti-entropy algorithm that exchanges deltas and their
//! causal metadata ([`CausalContext`]).
//!
//! ## Getting Started: A Simple Conflict
//!
//! This example demonstrates how two users (Alice and Bob) concurrently edit the same
//! data, creating a conflict that DSON resolves gracefully using the transaction API.
//!
//! ```rust
//! use dson::{
//! crdts::{mvreg::MvRegValue, snapshot::ToValue},
//! CausalDotStore, Identifier, OrMap,
//! };
//!
//! // 1. SETUP: TWO REPLICAS
//! // Create two replicas, Alice and Bob, each with a unique ID.
//! let alice_id = Identifier::new(0, 0);
//! let mut alice_store = CausalDotStore::<OrMap<String>>::default();
//!
//! let bob_id = Identifier::new(1, 0);
//! let mut bob_store = CausalDotStore::<OrMap<String>>::default();
//!
//! // 2. INITIAL STATE
//! // Alice creates an initial value using the transaction API.
//! let key = "document".to_string();
//! let delta_from_alice = {
//! let mut tx = alice_store.transact(alice_id);
//! tx.write_register(&key, MvRegValue::String("initial value".to_string()));
//! tx.commit()
//! };
//!
//! // 3. SYNC
//! // Bob receives Alice's initial change.
//! bob_store.join_or_replace_with(delta_from_alice.0.store, &delta_from_alice.0.context);
//! assert_eq!(alice_store, bob_store);
//!
//! // 4. CONCURRENT EDITS
//! // Now Alice and Bob make changes without syncing.
//!
//! // Alice updates the value to "from Alice".
//! let delta_alice_edit = {
//! let mut tx = alice_store.transact(alice_id);
//! tx.write_register(&key, MvRegValue::String("from Alice".to_string()));
//! tx.commit()
//! };
//!
//! // Concurrently, Bob updates the value to "from Bob".
//! let delta_bob_edit = {
//! let mut tx = bob_store.transact(bob_id);
//! tx.write_register(&key, MvRegValue::String("from Bob".to_string()));
//! tx.commit()
//! };
//!
//! // 5. MERGE
//! // The replicas exchange their changes.
//! alice_store.join_or_replace_with(delta_bob_edit.0.store, &delta_bob_edit.0.context);
//! bob_store.join_or_replace_with(delta_alice_edit.0.store, &delta_alice_edit.0.context);
//!
//! // After merging, both stores are identical.
//! assert_eq!(alice_store, bob_store);
//!
//! // 6. VERIFY CONFLICT
//! // The concurrent writes are preserved as a conflict in the register.
//! // The transaction API exposes this through the CrdtValue enum.
//! use dson::transaction::CrdtValue;
//!
//! let tx = alice_store.transact(alice_id);
//! match tx.get(&key) {
//! Some(CrdtValue::Register(reg)) => {
//! // Read all concurrent values
//! let values: Vec<_> = reg.values().into_iter().collect();
//! assert_eq!(values.len(), 2);
//! assert!(values.contains(&&MvRegValue::String("from Alice".to_string())));
//! assert!(values.contains(&&MvRegValue::String("from Bob".to_string())));
//! }
//! _ => panic!("Expected register with conflict"),
//! }
//! ```
//!
//! For more examples of the transaction API, including nested structures and performance
//! considerations, see the [`transaction`] module documentation.
//!
//! ## Advanced Topics
//!
//! ### The Extension System
//!
//! DSON includes an extension system that allows developers to define custom CRDTs by
//! implementing the [`ExtensionType`] trait. This is for building domain-specific data
//! structures that go beyond the standard JSON-like primitives.
//!
//! By implementing the [`ExtensionType`] trait, you define how your custom type should be
//! serialized, deserialized, and merged. The system handles conflict resolution based on
//! the rules you define.
//!
//! This can be used to implement custom data structures like counters, text objects, or
//! more efficient state representation.
//!
//! ### Validation and Observation
//!
//! DSON provides a [`Sentinel`](crate::sentinel::Sentinel) trait that allows you to observe or
//! validate changes as they are applied during a merge. This can be used for implementing
//! authorization, logging, or triggering side effects.
//!
//! ## Network and Consistency
//!
//! DSON's delta-based approach minimizes the amount of data that needs to be transmitted
//! between replicas, making it efficient for low-bandwidth or high-latency networks.
//!
//! However, much of the complexity of using DSON in practice lies in the correct design and
//! implementation of the gossip protocol used to exchange deltas between replicas. An
//! efficient gossip protocol is not trivial to implement. For guidance, refer to the
//! research on [opportunistic networking (oppnet)][oppnet].
//!
//! It is also important to understand that DSON's causal consistency guarantees are provided on
//! a per-register basis. This means that while individual values are guaranteed to be causally
//! consistent, the relationships between different values are not. This can lead to very
//! unintuitive behavior.
//! For example, if you have two registers, `x` and `y`, you write to `x` and then to `y`,
//! another replica might see the write to `y` before the write to `x`.
//!
//! ## License
//!
//! This project is licensed under either of
//!
//! - Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
//! - MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
//!
//! at your option.
//!
//! ## Features
//!
//! - `json`: Enables serialization and deserialization of DSON documents to and from
//! `serde_json::Value`. This feature is enabled by default.
//! - `serde`: Provides `serde` support for all CRDT types.
//! - `arbitrary`: Implements `quickcheck::Arbitrary` for CRDT types, useful for property-based testing.
//! - `chrono`: Enables `chrono` support for `Timestamp`. This feature is enabled by default.
//! - `ulid`: Enables registers to hold ulids. This feature is enabled by default.
extern crate quickcheck_macros;
use RandomState;
use ;
// Use a constant seed for hashing to make performance benchmarks have less variance.
pub const DETERMINISTIC_HASHER: RandomState = with_seeds;
pub use ;
pub use ;
pub use ;
/// Transaction-based API for ergonomic CRDT mutations.
///
/// See [`transaction`] module documentation for details and examples.
pub use Delta;
/// Macros usable for tests and initialization
// re-export for the datetime-literal macro
pub use chrono;
// for [``] auto-linking
use TypeVariantValue;
static ENABLE_DETERMINISM: AtomicBool = new;
/// Makes all data structures behave deterministically.
///
/// This should only be enabled for testing, as it increases the odds of DoS
/// scenarios.
/// Checks if determinism is enabled.
///
/// Should be used internally and for testing.
/// Create a random state for a hashmap.
/// If `enable_determinism` has been used, this will return a deterministic
/// decidedly non-random RandomState, useful in tests.
/// This is a small wrapper around the standard RandomState.
/// This allows us to easily switch to a non-random RandomState for use in tests.
// Implement default, falling back on regular ahash::RandomState except
// when 'enable_determinism' has been called, in which case a static
// only-for-test RandomState is used.
// We implement BuildHasher for DsonRandomState, but all we do is delegate to
// the wrapped 'inner' RandomState.
//
// This construct allows us to easily use a deterministic RandomState (i.e, not random :-) ),
// for tests.
//
// Since DsonRandomState implements default, the user doesn't have to do anything more than
// specialize their hashmap using DsonRandomState instead of RandomState.
/// A type that extends [`TypeVariantValue`] and friends with additional value types.
///
/// If you are looking for an implementor of this trait to stick with the standard DSON/JSON types,
/// use [`crdts::NoExtensionTypes`].
///
/// The compiler should guide you towards all the various other traits and types you need in order
/// to satisfy this trait once you add an impl of it.
///
/// In terms of mental model, think of the type that directly implements this trait as a direct
/// analogue of [`TypeVariantValue`]. That is, it should generally be a struct with one `Option`
/// field for each possible kind of custom value type. It needs to be a struct, not an enum, so
/// that it can represent conflicts in type changes (for example, one writer sets a value to custom kind A
/// and another sets it to custom kind B concurrently). [`ExtensionType::Value`] is used in
/// situations where it is known that only a single kind is held.
/// [`ExtensionType::coerce_to_value_ref`] is the main way in which such type conflicts are
/// resolved.
///
/// The sub-types ("kinds") of a custom extension type must all be CRDTs, which in turn makes the
/// implementing type also a CRDT assuming it follows the directions above. This is represented by
/// the requirement that both `Self` and `ExtensionType::Value` implement [`DotStore`].
///
/// Implementors of this trait are generally used wherever `<Custom>` or `<C>` appears.
// NOTE: three arguments all of the same type -- big nope to have them be regular fn args.
/// Returns dots that `known_dots` has deleted (by virtue of not being in `live_dots`) that
/// are still present in `ignorant`.
///
/// Conceptually computes `(known_dots - live_dots) & ignorant`.