11//! Implementation of Set Reconcilliation based on
22//! "Range-Based Set Reconciliation" by Aljoscha Meyer.
33
4- use std:: { cmp :: Ordering , fmt:: Debug , pin:: Pin } ;
4+ use std:: { fmt:: Debug , pin:: Pin } ;
55
66use n0_future:: StreamExt ;
77use serde:: { Deserialize , Serialize } ;
@@ -62,36 +62,33 @@ pub trait RangeValue: Sized + Debug + Ord + PartialEq + Clone + 'static {}
6262///
6363/// This means that ranges are "wrap around" conceptually.
6464#[ derive( Debug , Clone , Copy , PartialEq , Serialize , Deserialize , Default ) ]
65- pub struct Range < K > {
65+ pub ( crate ) struct Range < K > {
6666 x : K ,
6767 y : K ,
6868}
6969
7070impl < K > Range < K > {
71- pub fn x ( & self ) -> & K {
71+ pub ( crate ) fn x ( & self ) -> & K {
7272 & self . x
7373 }
7474
75- pub fn y ( & self ) -> & K {
75+ pub ( crate ) fn y ( & self ) -> & K {
7676 & self . y
7777 }
7878
79- pub fn new ( x : K , y : K ) -> Self {
80- Range { x, y }
81- }
82-
83- pub fn map < X > ( self , f : impl FnOnce ( K , K ) -> ( X , X ) ) -> Range < X > {
84- let ( x, y) = f ( self . x , self . y ) ;
79+ pub ( crate ) fn new ( x : K , y : K ) -> Self {
8580 Range { x, y }
8681 }
8782}
8883
8984impl < K : Ord > Range < K > {
90- pub fn is_all ( & self ) -> bool {
85+ pub ( crate ) fn is_all ( & self ) -> bool {
9186 self . x ( ) == self . y ( )
9287 }
9388
94- pub fn contains ( & self , t : & K ) -> bool {
89+ #[ cfg( test) ]
90+ pub ( crate ) fn contains ( & self , t : & K ) -> bool {
91+ use std:: cmp:: Ordering ;
9592 match self . x ( ) . cmp ( self . y ( ) ) {
9693 Ordering :: Equal => true ,
9794 Ordering :: Less => self . x ( ) <= t && t < self . y ( ) ,
@@ -117,13 +114,9 @@ impl Debug for Fingerprint {
117114
118115impl Fingerprint {
119116 /// The fingerprint of the empty set
120- pub fn empty ( ) -> Self {
117+ pub ( crate ) fn empty ( ) -> Self {
121118 Fingerprint ( * blake3:: hash ( & [ ] ) . as_bytes ( ) )
122119 }
123-
124- pub fn new < T : RangeEntry > ( val : T ) -> Self {
125- val. as_fingerprint ( )
126- }
127120}
128121
129122impl std:: ops:: BitXorAssign for Fingerprint {
@@ -140,9 +133,9 @@ pub struct RangeFingerprint<K> {
140133 serialize = "Range<K>: Serialize" ,
141134 deserialize = "Range<K>: Deserialize<'de>"
142135 ) ) ]
143- pub range : Range < K > ,
136+ pub ( crate ) range : Range < K > ,
144137 /// The fingerprint of `range`.
145- pub fingerprint : Fingerprint ,
138+ pub ( crate ) fingerprint : Fingerprint ,
146139}
147140
148141/// Transfers items inside a range to the other participant.
@@ -153,12 +146,12 @@ pub struct RangeItem<E: RangeEntry> {
153146 serialize = "Range<E::Key>: Serialize" ,
154147 deserialize = "Range<E::Key>: Deserialize<'de>"
155148 ) ) ]
156- pub range : Range < E :: Key > ,
149+ pub ( crate ) range : Range < E :: Key > ,
157150 #[ serde( bound( serialize = "E: Serialize" , deserialize = "E: Deserialize<'de>" ) ) ]
158- pub values : Vec < ( E , ContentStatus ) > ,
151+ pub ( crate ) values : Vec < ( E , ContentStatus ) > ,
159152 /// If false, requests to send local items in the range.
160153 /// Otherwise not.
161- pub have_local : bool ,
154+ pub ( crate ) have_local : bool ,
162155}
163156
164157#[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
@@ -176,15 +169,17 @@ pub enum MessagePart<E: RangeEntry> {
176169}
177170
178171impl < E : RangeEntry > MessagePart < E > {
179- pub fn is_range_fingerprint ( & self ) -> bool {
172+ #[ cfg( test) ]
173+ pub ( crate ) fn is_range_fingerprint ( & self ) -> bool {
180174 matches ! ( self , MessagePart :: RangeFingerprint ( _) )
181175 }
182176
183- pub fn is_range_item ( & self ) -> bool {
177+ #[ cfg( test) ]
178+ pub ( crate ) fn is_range_item ( & self ) -> bool {
184179 matches ! ( self , MessagePart :: RangeItem ( _) )
185180 }
186181
187- pub fn values ( & self ) -> Option < & [ ( E , ContentStatus ) ] > {
182+ pub ( crate ) fn values ( & self ) -> Option < & [ ( E , ContentStatus ) ] > {
188183 match self {
189184 MessagePart :: RangeFingerprint ( _) => None ,
190185 MessagePart :: RangeItem ( RangeItem { values, .. } ) => Some ( values) ,
@@ -211,15 +206,15 @@ impl<E: RangeEntry> Message<E> {
211206 Ok ( Message { parts : vec ! [ part] } )
212207 }
213208
214- pub fn parts ( & self ) -> & [ MessagePart < E > ] {
209+ pub ( crate ) fn parts ( & self ) -> & [ MessagePart < E > ] {
215210 & self . parts
216211 }
217212
218- pub fn values ( & self ) -> impl Iterator < Item = & ( E , ContentStatus ) > {
213+ pub ( crate ) fn values ( & self ) -> impl Iterator < Item = & ( E , ContentStatus ) > {
219214 self . parts ( ) . iter ( ) . filter_map ( |p| p. values ( ) ) . flatten ( )
220215 }
221216
222- pub fn value_count ( & self ) -> usize {
217+ pub ( crate ) fn value_count ( & self ) -> usize {
223218 self . values ( ) . count ( )
224219 }
225220}
@@ -241,12 +236,16 @@ pub trait Store<E: RangeEntry>: Sized {
241236 fn get_first ( & mut self ) -> Result < E :: Key , Self :: Error > ;
242237
243238 /// Get a single entry.
239+ #[ cfg( test) ]
244240 fn get ( & mut self , key : & E :: Key ) -> Result < Option < E > , Self :: Error > ;
245241
246242 /// Get the number of entries in the store.
243+ #[ cfg( test) ]
247244 fn len ( & mut self ) -> Result < usize , Self :: Error > ;
248245
249246 /// Returns `true` if the vector contains no elements.
247+ #[ cfg( test) ]
248+ #[ allow( unused) ]
250249 fn is_empty ( & mut self ) -> Result < bool , Self :: Error > ;
251250
252251 /// Calculate the fingerprint of the given range.
@@ -274,17 +273,21 @@ pub trait Store<E: RangeEntry>: Sized {
274273 }
275274
276275 /// Returns all entries whose key starts with the given `prefix`.
276+ #[ cfg( test) ]
277+ #[ allow( unused) ]
277278 fn prefixed_by ( & mut self , prefix : & E :: Key ) -> Result < Self :: RangeIterator < ' _ > , Self :: Error > ;
278279
279280 /// Returns all entries that share a prefix with `key`, including the entry for `key` itself.
280281 fn prefixes_of ( & mut self , key : & E :: Key ) -> Result < Self :: ParentIterator < ' _ > , Self :: Error > ;
281282
282283 /// Get all entries in the store
284+ #[ cfg( test) ]
283285 fn all ( & mut self ) -> Result < Self :: RangeIterator < ' _ > , Self :: Error > ;
284286
285287 /// Remove an entry from the store.
286288 ///
287289 /// This will remove just the entry with the given key, but will not perform prefix deletion.
290+ #[ cfg( test) ]
288291 fn entry_remove ( & mut self , key : & E :: Key ) -> Result < Option < E > , Self :: Error > ;
289292
290293 /// Remove all entries whose key start with a prefix and for which the `predicate` callback
@@ -602,14 +605,17 @@ impl<E: RangeEntry, S: Store<E>> Store<E> for &mut S {
602605 ( * * self ) . get_first ( )
603606 }
604607
608+ #[ cfg( test) ]
605609 fn get ( & mut self , key : & <E as RangeEntry >:: Key ) -> Result < Option < E > , Self :: Error > {
606610 ( * * self ) . get ( key)
607611 }
608612
613+ #[ cfg( test) ]
609614 fn len ( & mut self ) -> Result < usize , Self :: Error > {
610615 ( * * self ) . len ( )
611616 }
612617
618+ #[ cfg( test) ]
613619 fn is_empty ( & mut self ) -> Result < bool , Self :: Error > {
614620 ( * * self ) . is_empty ( )
615621 }
@@ -632,6 +638,7 @@ impl<E: RangeEntry, S: Store<E>> Store<E> for &mut S {
632638 ( * * self ) . get_range ( range)
633639 }
634640
641+ #[ cfg( test) ]
635642 fn prefixed_by (
636643 & mut self ,
637644 prefix : & <E as RangeEntry >:: Key ,
@@ -646,10 +653,12 @@ impl<E: RangeEntry, S: Store<E>> Store<E> for &mut S {
646653 ( * * self ) . prefixes_of ( key)
647654 }
648655
656+ #[ cfg( test) ]
649657 fn all ( & mut self ) -> Result < Self :: RangeIterator < ' _ > , Self :: Error > {
650658 ( * * self ) . all ( )
651659 }
652660
661+ #[ cfg( test) ]
653662 fn entry_remove ( & mut self , key : & <E as RangeEntry >:: Key ) -> Result < Option < E > , Self :: Error > {
654663 ( * * self ) . entry_remove ( key)
655664 }
@@ -664,7 +673,7 @@ impl<E: RangeEntry, S: Store<E>> Store<E> for &mut S {
664673}
665674
666675#[ derive( Debug , Clone , Copy ) ]
667- pub struct SyncConfig {
676+ pub ( crate ) struct SyncConfig {
668677 /// Up to how many values to send immediately, before sending only a fingerprint.
669678 max_set_size : usize ,
670679 /// `k` in the protocol, how many splits to generate. at least 2
@@ -682,7 +691,7 @@ impl Default for SyncConfig {
682691
683692/// The outcome of a [`Store::put`] operation.
684693#[ derive( Debug ) ]
685- pub enum InsertOutcome {
694+ pub ( crate ) enum InsertOutcome {
686695 /// The entry was not inserted because a newer entry for its key or a
687696 /// prefix of its key exists.
688697 NotInserted ,
@@ -865,7 +874,7 @@ mod tests {
865874 }
866875
867876 #[ derive( Debug ) ]
868- pub struct SimpleRangeIterator < ' a , K , V > {
877+ pub ( crate ) struct SimpleRangeIterator < ' a , K , V > {
869878 iter : std:: collections:: btree_map:: Iter < ' a , K , V > ,
870879 filter : SimpleFilter < K > ,
871880 }
@@ -874,6 +883,8 @@ mod tests {
874883 enum SimpleFilter < K > {
875884 None ,
876885 Range ( Range < K > ) ,
886+ // TODO(Frando): Add test for this.
887+ #[ allow( unused) ]
877888 Prefix ( K ) ,
878889 }
879890
0 commit comments