Skip to content

Commit

Permalink
StateVector - add partial comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Horusiath committed Aug 29, 2024
1 parent 831c806 commit 47b083f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion yrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ pub use crate::out::Out;
pub use crate::state_vector::Snapshot;
pub use crate::state_vector::StateVector;
pub use crate::store::Store;
pub use crate::transact::{AsyncTransact, Transact, TransactionAcqError};
pub use crate::transact::{
AcquireTransaction, AcquireTransactionMut, AsyncTransact, Transact, TransactionAcqError,
};
pub use crate::transaction::Origin;
pub use crate::transaction::ReadTxn;
pub use crate::transaction::RootRefs;
Expand Down
29 changes: 29 additions & 0 deletions yrs/src/state_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::updates::decoder::{Decode, Decoder};
use crate::updates::encoder::{Encode, Encoder};
use crate::utils::client_hasher::ClientHasher;
use crate::{DeleteSet, ID};
use std::cmp::Ordering;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
Expand Down Expand Up @@ -138,6 +139,34 @@ impl Encode for StateVector {
}
}

impl PartialOrd for StateVector {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let mut result = Some(Ordering::Equal);

for (client, clock) in self.iter() {
let other_clock = other.get(client);
match clock.cmp(&other_clock) {
Ordering::Less if result == Some(Ordering::Greater) => return None,
Ordering::Greater if result == Some(Ordering::Less) => return None,
Ordering::Equal => { /* unchanged */ }
other => result = Some(other),
}
}

for (other_client, other_clock) in other.iter() {
let clock = self.get(other_client);
match clock.cmp(&other_clock) {
Ordering::Less if result == Some(Ordering::Greater) => return None,
Ordering::Greater if result == Some(Ordering::Less) => return None,
Ordering::Equal => { /* unchanged */ }
other => result = Some(other),
}
}

result
}
}

/// Snapshot describes a state of a document store at a given point in (logical) time. In practice
/// it's a combination of [StateVector] (a summary of all observed insert/update operations)
/// and a [DeleteSet] (a summary of all observed deletions).
Expand Down

0 comments on commit 47b083f

Please sign in to comment.