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
//! Kitsune2 arcs represent a range of hash locations on the DHT.
//!
//! When used in the context of an agent, it represents the range of hash locations
//! that agent is responsible for.
use serde::{Deserialize, Serialize};
/// The definition of a storage arc compatible with the concept of
/// storage and querying of items in a store that fall within that arc.
#[derive(
Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq, PartialEq, Default,
)]
#[serde(untagged)]
pub enum DhtArc {
/// No DHT locations are contained within this arc.
#[default]
Empty,
/// A specific range of DHT locations are contained within this arc.
///
/// The lower and upper bounds are inclusive.
Arc(u32, u32),
}
impl DhtArc {
/// A full arc that contains all DHT locations.
pub const FULL: DhtArc = DhtArc::Arc(0, u32::MAX);
/// Get the min distance from a location to an arc in a wrapping u32 space.
/// This function will only return 0 if the location is covered by the arc.
/// This function will return u32::MAX if the arc is empty.
///
/// All possible cases:
///
/// ```text
/// s = arc_start
/// e = arc_end
/// l = location
///
/// Arc wraps around, loc >= arc_start
///
/// |----e-----------s--l--|
/// 0 u32::MAX
///
/// Arc wraps around, loc <= arc_end
/// |-l--e-----------s-----|
/// 0 u32::MAX
///
/// Arc wraps around, loc outside of arc
/// |----e----l------s-----|
/// 0 u32::MAX
///
/// Arc does not wrap around, loc inside of arc
/// |---------s--l---e-----|
/// 0 u32::MAX
///
/// Arc does not wrap around, loc < arc_start
/// |-----l---s------e-----|
/// 0 u32::MAX
///
/// Arc does not wrap around, loc > arc_end
/// |---------s------e--l--|
/// 0 u32::MAX
/// ```
pub fn dist(&self, loc: u32) -> u32 {
match self {
DhtArc::Empty => u32::MAX,
DhtArc::Arc(arc_start, arc_end) => {
let (d1, d2) = if arc_start > arc_end {
// this arc wraps around the end of u32::MAX
if loc >= *arc_start || loc <= *arc_end {
return 0;
} else {
(
// Here we know that location is less than arc_start
arc_start - loc,
loc - arc_end,
)
}
} else {
// this arc does not wrap, arc_start <= arc_end
if loc >= *arc_start && loc <= *arc_end {
return 0;
} else if loc < *arc_start {
(
// Here we know that location is less than arc_start
arc_start - loc,
// Add one to account for the wrap.
// Here we know that location is less than arc_end, but we need to
// compute the wrapping distance between them. Adding 1 is safe and
// cannot overflow.
u32::MAX - arc_end + loc + 1,
)
} else {
(u32::MAX - loc + arc_start + 1, loc - arc_end)
}
};
std::cmp::min(d1, d2)
}
}
}
/// Convenience function to determine if a location is contained within the arc.
///
/// Simply checks whether the distance from the location to the arc is 0.
pub fn contains(&self, loc: u32) -> bool {
self.dist(loc) == 0
}
/// Determine if any part of two arcs overlap.
///
/// All possible cases (though note the arcs can also wrap around u32::MAX):
///
/// ```text
/// a = a_start
/// A = a_end
/// b = b_start
/// B = b_end
///
/// The tail of a..A overlaps the head of b..B
///
/// |---a--b-A--B---|
///
/// The tail of b..B overlaps the head of a..A
///
/// |---b--a-B--A---|
///
/// b..B is fully contained by a..A
///
/// |---a--b-B--A---|
///
/// a..A is fully contained by b..B
///
/// |---b--a-A--B---|
/// ```
pub fn overlaps(&self, other: &DhtArc) -> bool {
match (&self, &other) {
(DhtArc::Empty, _) | (_, DhtArc::Empty) => false,
(
this @ DhtArc::Arc(a_beg, a_end),
other @ DhtArc::Arc(b_beg, b_end),
) => {
// The only way for there to be overlap is if
// either of a's start or end points are within b
// or either of b's start or end points are within a
this.dist(*b_beg) == 0
|| this.dist(*b_end) == 0
|| other.dist(*a_beg) == 0
|| other.dist(*a_end) == 0
}
}
}
/// The distance from the inclusive beginning of the arc
/// to the exclusive end of the arc. (Or length minus one.)
/// Note: DhtArc::Empty and DhtArc::Arc(0, 0) will both
/// return arc_span() == 0. To address this, first match on
/// the enum variants.
pub fn arc_span(&self) -> u32 {
match self {
DhtArc::Empty => 0,
DhtArc::Arc(start, end) => {
if start > end {
u32::MAX - start + end + 1
} else {
end - start
}
}
}
}
/// Determine if the arc is empty.
pub fn is_empty(&self) -> bool {
matches!(self, DhtArc::Empty)
}
}
#[cfg(test)]
mod tests {
use crate::DhtArc;
#[test]
fn arc_span_fixtures() {
for (expected, arc) in [
(0, DhtArc::Empty),
(0, DhtArc::Arc(0, 0)),
(1, DhtArc::Arc(0, 1)),
(1, DhtArc::Arc(u32::MAX, 0)),
(2, DhtArc::Arc(42, 44)),
(u32::MAX, DhtArc::Arc(42, 41)),
(u32::MAX, DhtArc::Arc(0, u32::MAX)),
(u32::MAX, DhtArc::Arc(u32::MAX, u32::MAX - 1)),
] {
assert_eq!(expected, arc.arc_span());
}
}
#[test]
fn contains_full_arc_all_values() {
let arc = DhtArc::FULL;
// Contains bounds
assert!(arc.contains(0));
assert!(arc.contains(u32::MAX));
// and a value in the middle somewhere
assert!(arc.contains(u32::MAX / 2));
}
#[test]
fn contains_includes_bounds() {
let arc = DhtArc::Arc(32, 64);
assert!(arc.contains(32));
assert!(arc.contains(64));
}
#[test]
fn contains_wraps_around() {
let arc = DhtArc::Arc(u32::MAX - 32, 32);
assert!(!arc.contains(u32::MAX - 33));
assert!(arc.contains(u32::MAX - 32));
assert!(arc.contains(u32::MAX - 1));
assert!(arc.contains(u32::MAX));
assert!(arc.contains(0));
assert!(arc.contains(20));
assert!(arc.contains(32));
assert!(!arc.contains(33));
}
#[test]
fn contains_empty_arc_no_locations() {
let arc = DhtArc::Empty;
assert!(!arc.contains(0));
assert!(!arc.contains(u32::MAX));
assert!(!arc.contains(u32::MAX / 2));
}
#[test]
fn arc_dist_edge_cases() {
type Dist = u32;
type Loc = u32;
const F: &[(Dist, Loc, DhtArc)] = &[
// Empty arcs contain no values, distance is always u32::MAX
(u32::MAX, 0, DhtArc::Empty),
(u32::MAX, u32::MAX / 2, DhtArc::Empty),
(u32::MAX, u32::MAX, DhtArc::Empty),
// Unit length arcs at max value
(1, 0, DhtArc::Arc(u32::MAX, u32::MAX)),
(
u32::MAX / 2 + 1,
u32::MAX / 2,
DhtArc::Arc(u32::MAX, u32::MAX),
),
// Unit length arcs at min value
(1, u32::MAX, DhtArc::Arc(0, 0)),
(u32::MAX / 2, u32::MAX / 2, DhtArc::Arc(0, 0)),
// Lower bound is inclusive
(0, 0, DhtArc::Arc(0, 1)),
(0, u32::MAX - 1, DhtArc::Arc(u32::MAX - 1, u32::MAX)),
// Distance from lower bound, non-wrapping
(1, 0, DhtArc::Arc(1, 2)),
(1, u32::MAX, DhtArc::Arc(0, 1)),
// Distance from upper bound, non-wrapping
(1, 0, DhtArc::Arc(u32::MAX - 1, u32::MAX)),
// Distance from upper bound, wrapping
(0, 0, DhtArc::Arc(u32::MAX, 0)),
(1, 1, DhtArc::Arc(u32::MAX, 0)),
// Distance from lower bound, wrapping
(1, u32::MAX - 1, DhtArc::Arc(u32::MAX, 0)),
(1, u32::MAX - 1, DhtArc::Arc(u32::MAX, 1)),
// Contains, wrapping
(0, 0, DhtArc::Arc(u32::MAX, 1)),
];
for (dist, loc, arc) in F.iter() {
assert_eq!(
*dist,
arc.dist(*loc),
"While checking the distance from {loc} to arc {arc:?}"
);
}
}
#[test]
fn arcs_overlap_edge_cases() {
type DoOverlap = bool;
const F: &[(DoOverlap, DhtArc, DhtArc)] = &[
(false, DhtArc::Arc(0, 0), DhtArc::Arc(1, 1)),
(false, DhtArc::Arc(0, 0), DhtArc::Arc(u32::MAX, u32::MAX)),
(true, DhtArc::Arc(0, 0), DhtArc::Arc(0, 0)),
(
true,
DhtArc::Arc(u32::MAX, u32::MAX),
DhtArc::Arc(u32::MAX, u32::MAX),
),
(true, DhtArc::Arc(u32::MAX, 0), DhtArc::Arc(0, 0)),
(
true,
DhtArc::Arc(u32::MAX, 0),
DhtArc::Arc(u32::MAX, u32::MAX),
),
(
true,
DhtArc::Arc(u32::MAX, 0),
DhtArc::Arc(u32::MAX, u32::MAX),
),
(true, DhtArc::Arc(0, 3), DhtArc::Arc(1, 2)),
(true, DhtArc::Arc(1, 2), DhtArc::Arc(0, 3)),
(true, DhtArc::Arc(1, 3), DhtArc::Arc(2, 4)),
(true, DhtArc::Arc(2, 4), DhtArc::Arc(1, 3)),
(true, DhtArc::Arc(u32::MAX - 1, 1), DhtArc::Arc(u32::MAX, 0)),
(true, DhtArc::Arc(u32::MAX, 0), DhtArc::Arc(u32::MAX - 1, 1)),
(true, DhtArc::Arc(u32::MAX - 1, 0), DhtArc::Arc(u32::MAX, 1)),
(true, DhtArc::Arc(u32::MAX, 1), DhtArc::Arc(u32::MAX - 1, 0)),
];
for (do_overlap, a, b) in F.iter() {
assert_eq!(
*do_overlap,
a.overlaps(b),
"While checking that {a:?} overlaps {b:?}"
);
}
}
}