delta.rs

Overview

This file defines the Delta abstraction used for synchronizing state updates between nodes in a distributed system. A Delta encapsulates a set of state changes related to nodes, expressed as sequences of operations that update nodes' key-value data and metadata. It supports efficient serialization and deserialization of these operations into a compressed stream for network transmission.

The core purpose of the Delta type and its related components is to represent incremental updates to node states, including key-value mutations and version tracking, and to provide mechanisms for encoding these updates into a compact binary format that can be sent to other nodes.

Key Components

Delta

A Delta is a collection of NodeDelta instances representing updates for multiple nodes. It primarily holds:

Methods

Serialization / Deserialization

Usage Example

let mut delta = Delta::default();
delta.add_node(chitchat_id, last_gc_version, from_version);
delta.add_kv(&chitchat_id, "key", "value", version, false);
let mut buf = Vec::new();
delta.serialize(&mut buf);

NodeDelta

Represents the set of changes related to a single node.

Fields

Details

Usage Example

let node_delta = NodeDelta {
    chitchat_id,
    last_gc_version,
    from_version_excluded,
    key_values: vec![...],
    max_version: Some(max_version),
};

DeltaOp and DeltaOpRef

Enumerations representing atomic operations within a delta.

Variants

Serialization


DeltaOpTag

An enum with explicit u8 discriminants to tag operation types during serialization:

It implements TryFrom<u8> for decoding tags and From<DeltaOpTag> for u8 for encoding.


DeltaBuilder

A helper struct to incrementally build a Delta from a stream of DeltaOp operations:

Fields

Methods


DeltaSerializer

A utility struct to serialize parts of a delta while respecting a maximum transmission unit (MTU) size.

Fields

Constants

Methods


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Data Structure Diagram

classDiagram
class Delta {
-node_deltas: Vec~NodeDelta~
-serialized_len: usize
+get_operations()
+serialize()
+deserialize()
}
class NodeDelta {
+chitchat_id: ChitchatId
+last_gc_version: Version
+from_version_excluded: Version
+key_values: Vec~KeyValueMutation~
+max_version: Option~Version~
}
class DeltaOp {
<<enum>>
+Node
+KeyValue
+SetMaxVersion
}
class DeltaOpRef {
<<enum>>
+Node
+KeyValue
+SetMaxVersion
}
class DeltaBuilder {
-existing_nodes: HashSet~ChitchatId~
-delta: Delta
-current_node_delta: Option~NodeDelta~
+apply_op()
+flush()
+finish()
}
class DeltaSerializer {
-mtu: usize
-delta_builder: DeltaBuilder
-compressed_stream_writer: CompressedStreamWriter
+try_add_node()
+try_add_kv()
+try_set_max_version()
+finish()
}
Delta "1" *-- "*" NodeDelta : contains
DeltaBuilder "1" o-- "1" Delta : builds
DeltaBuilder "1" *-- "0..1" NodeDelta : current node
DeltaSerializer "1" o-- "1" DeltaBuilder : uses
DeltaSerializer "1" o-- "1" CompressedStreamWriter : uses
DeltaOp <|-- DeltaOpRef : reference variant

Detailed Descriptions of Functions and Methods

Delta


DeltaBuilder


DeltaSerializer


Types Used From Other Modules

For details on these types and serialization traits, refer to ChitchatId, [Version](/version), [VersionedValue](/versioned-value), and Serialization.


Testing

The file contains comprehensive tests to validate:

These tests use the tokio crate and utilities for creating test ChitchatId instances and VersionedValues with specific deletion statuses.


Summary of Workflow

  1. Building a Delta:

    • Start with an empty Delta.

    • Add NodeDeltas for each node.

    • Add key-value mutations and optional max version per node.

  2. Serialization:

    • Convert the Delta into a sequence of DeltaOp operations.

    • Use CompressedStreamWriter to serialize these operations into a compressed stream.

  3. Transmission:

    • Send compressed delta payload over the network (respecting MTU).

  4. Deserialization:

    • Receive compressed stream.

    • Decompress and deserialize into DeltaOps.

    • Use DeltaBuilder to reconstruct the Delta structure.


End of delta.rs documentation.