-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs-storage
: Implement simple conflict resolution using Monoids (#26)
--------- Signed-off-by: Tarek <[email protected]> Signed-off-by: Pushkar Mishra <[email protected]> Co-authored-by: Kirill Taran <[email protected]> Co-authored-by: Tarek <[email protected]>
- Loading branch information
1 parent
2ce5ee7
commit 07cbdd0
Showing
4 changed files
with
163 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod base_storage; | ||
pub mod file_storage; | ||
pub mod monoid; | ||
mod utils; | ||
pub const ARK_FOLDER: &str = ".ark"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Currently, we have three structures: Tags (HashSet), Properties (HashSet), Score (int). | ||
// In fact, HashSet already implements a union function, | ||
// so only a special function for integers is needed. | ||
// CRDTs can be considered later when we need to add structures that require | ||
// more powerful combine semantics. | ||
|
||
// Trait defining a Monoid, which represents a mathematical structure with an identity element and an associative binary operation. | ||
pub trait Monoid<V> { | ||
// Returns the neutral element of the monoid. | ||
fn neutral() -> V; | ||
|
||
// Combines two elements of the monoid into a single element. | ||
fn combine(a: &V, b: &V) -> V; | ||
|
||
// Combines multiple elements of the monoid into a single element. | ||
// Default implementation uses `neutral()` as the initial accumulator and `combine()` for folding. | ||
fn combine_all<I: IntoIterator<Item = V>>(values: I) -> V { | ||
values | ||
.into_iter() | ||
.fold(Self::neutral(), |acc, val| Self::combine(&acc, &val)) | ||
} | ||
} | ||
|
||
impl Monoid<i32> for i32 { | ||
fn neutral() -> i32 { | ||
0 | ||
} | ||
|
||
fn combine(a: &i32, b: &i32) -> i32 { | ||
if a > b { | ||
*a | ||
} else { | ||
*b | ||
} | ||
} | ||
} | ||
|
||
impl Monoid<String> for String { | ||
fn neutral() -> String { | ||
String::new() | ||
} | ||
|
||
fn combine(a: &String, b: &String) -> String { | ||
let mut result = a.clone(); | ||
result.push_str(b); | ||
result | ||
} | ||
} |