kitsune2_dht/
lib.rs

1#![deny(missing_docs)]
2//! A distributed hash table (DHT) implementation for use in Kitsune2.
3//!
4//! The DHT model partitions data by location and time. The location is derived from the hash of the data, and is a 32-bit
5//! number. The time is a 64-bit number, representing a number of microseconds since the Unix epoch.
6//!
7//! The model first organises by location, then within a set of locations, it organises by time. The way that the model is
8//! partitioned is designed to permit computing a diff against another DHT model. This is the primary purpose of this crate,
9//! to enable efficient syncing of DHT models between agents.
10//!
11//! You can think of the DHT as a graph, where the vertical axis represents location and the horizontal axis represents time.
12//! Every piece of data can be represented as a point on this graph:
13//!
14//! <details style="cursor:pointer">
15//!     <summary>DHT model (graph representation)</summary>
16#![doc = include_str!("../art/dht-graph.svg")]
17//! </details>
18//!
19//! Notice that the partitioning introduces some new terminology:
20//! - **Sector**: The location space is split into equally-sized chunks called sectors. A sector spans the entire time axis.
21//! - **Time slice**: The horizontal axis is split into time slices. These come in two flavours, full and partial. The light
22//!   blue slices are full, and the dark blue slices are partial. The full time slices are fixed-size, while the partial
23//!   time slices start at half the size of a full time slice and halve in size towards the current time.
24//! - **Disc**: The disc is the light blue area of the graph. It is defined by the set of full time slices and the full set
25//!   of sectors.
26//! - **Ring**: A ring is one vertical section in the dark blue area of the graph. It is defined by a partial time slice and
27//!   the full set of sectors.
28//!
29//! The reason for the geometric naming is that the locations are thought of as a circle, with the highest location being
30//! adjacent to the lowest location. This is not a perfect analogy, but it is useful to have terminology that describes
31//! areas of the graph in a consistent way. See the following diagram:
32//!
33//! <details style="cursor:pointer">
34//!     <summary>DHT model (circle representation)</summary>
35#![doc = include_str!("../art/dht-circle.svg")]
36//! </details>
37//!
38//! With time starting at the center of the circle and moving outwards, and location segments delimited by straight lines
39//! radiating from the center. Then we can see the definition of the terms above more clearly. The disc is the light blue
40//! area, which is a disc in the geometric sense, bounded by the end of the last full time slice. The rings are the dark
41//! blue area, delimited by the end of the last partial time slice and the current time. The time slices are the concentric
42//! circles, delimited by white lines, with the light blue ones being full and the dark blue ones being partial.
43//!
44//! Each contained area within this shape can be represented by a single "combined hash". A "combined hash" is simply a
45//! combination of all the hashes of data within that area. A "combined hash" is generally pre-computed or updated
46//! incrementally, as opposed to a "top hash". The term "top hash" is used to refer to a combination of "combined hash"es.
47//! For example, computing a "top hash" of the disc would involve taking all the combined hashes for full time slices in
48//! each sector and combining them, then combining all of those across the sectors.
49//!
50//! There are detailed explanations of the pieces of the model in each module. To aid with navigation, here is a brief
51//! overview of the modules:
52//! - The model starts with the [`TimePartition`] type. This defines time slices, and handles combining hashes
53//!   within time slices. It can also combine hashes to yield top hashes.
54//! - The next piece of the model is the [`HashPartition`] type. This defines the partition over location, and
55//!   uses [`TimePartition`]s for its inner state.
56//! - The [`ArcSet`] type defines a set of agent arcs. This provides a simple representation of the
57//!   overlap between the storage [DhtArc](kitsune2_api::DhtArc)s' of two agents.
58//! - The top level of the model is the [`Dht`] type. This uses a [`HashPartition`] as its inner state, and adds
59//!   the logic for computing a diff between two DHT models. This is a multistep process, and is designed to balance the
60//!   amount of data that must be sent, with the number of round-trips required to sync the models.
61
62mod arc_set;
63mod constant;
64mod dht;
65mod hash;
66mod time;
67
68mod combine;
69#[cfg(test)]
70mod test;
71
72pub use arc_set::*;
73pub use constant::*;
74pub use dht::{snapshot::*, *};
75pub use hash::*;
76pub use time::*;