This commit is contained in:
Antonio Scandurra 2021-04-08 16:57:46 +02:00
parent 5e0d5b7f9a
commit c20935de91
3 changed files with 105 additions and 37 deletions

7
Cargo.lock generated
View file

@ -1674,6 +1674,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "seahash"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.9.0" version = "0.9.0"
@ -2251,6 +2257,7 @@ dependencies = [
"parking_lot", "parking_lot",
"rand 0.8.3", "rand 0.8.3",
"rust-embed", "rust-embed",
"seahash",
"serde_json", "serde_json",
"simplelog", "simplelog",
"smallvec", "smallvec",

View file

@ -28,6 +28,7 @@ num_cpus = "1.13.0"
parking_lot = "0.11.1" parking_lot = "0.11.1"
rand = "0.8.3" rand = "0.8.3"
rust-embed = "5.9.0" rust-embed = "5.9.0"
seahash = "4.1"
simplelog = "0.9" simplelog = "0.9"
smallvec = "1.6.1" smallvec = "1.6.1"
smol = "1.2.5" smol = "1.2.5"

View file

@ -5,6 +5,7 @@ mod text;
pub use anchor::*; pub use anchor::*;
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
pub use point::*; pub use point::*;
use seahash::SeaHasher;
pub use text::*; pub use text::*;
use crate::{ use crate::{
@ -20,7 +21,7 @@ use lazy_static::lazy_static;
use rand::prelude::*; use rand::prelude::*;
use std::{ use std::{
cmp::{self, Ordering}, cmp::{self, Ordering},
collections::{HashMap, HashSet}, hash::BuildHasher,
iter::{self, Iterator}, iter::{self, Iterator},
mem, mem,
ops::{AddAssign, Range}, ops::{AddAssign, Range},
@ -32,6 +33,29 @@ use std::{
pub type SelectionSetId = time::Lamport; pub type SelectionSetId = time::Lamport;
pub type SelectionsVersion = usize; pub type SelectionsVersion = usize;
#[derive(Clone, Default)]
struct DeterministicState;
impl BuildHasher for DeterministicState {
type Hasher = SeaHasher;
fn build_hasher(&self) -> Self::Hasher {
SeaHasher::new()
}
}
#[cfg(test)]
type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
#[cfg(test)]
type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
#[cfg(not(test))]
type HashMap<K, V> = std::collections::HashMap<K, V>;
#[cfg(not(test))]
type HashSet<T> = std::collections::HashSet<T>;
#[derive(Clone, Default)] #[derive(Clone, Default)]
struct UndoMap(HashMap<time::Local, Vec<UndoOperation>>); struct UndoMap(HashMap<time::Local, Vec<UndoOperation>>);
@ -40,14 +64,14 @@ impl UndoMap {
self.0.entry(undo.edit_id).or_default().push(undo); self.0.entry(undo.edit_id).or_default().push(undo);
} }
fn is_undone(&self, op_id: time::Local) -> bool { fn is_undone(&self, edit_id: time::Local) -> bool {
self.undo_count(op_id) % 2 == 1 self.undo_count(edit_id) % 2 == 1
} }
fn was_undone(&self, op_id: time::Local, version: &time::Global) -> bool { fn was_undone(&self, edit_id: time::Local, version: &time::Global) -> bool {
let undo_count = self let undo_count = self
.0 .0
.get(&op_id) .get(&edit_id)
.unwrap_or(&Vec::new()) .unwrap_or(&Vec::new())
.iter() .iter()
.filter(|undo| version.observed(undo.id)) .filter(|undo| version.observed(undo.id))
@ -57,9 +81,9 @@ impl UndoMap {
undo_count % 2 == 1 undo_count % 2 == 1
} }
fn undo_count(&self, op_id: time::Local) -> u32 { fn undo_count(&self, edit_id: time::Local) -> u32 {
self.0 self.0
.get(&op_id) .get(&edit_id)
.unwrap_or(&Vec::new()) .unwrap_or(&Vec::new())
.iter() .iter()
.map(|undo| undo.count) .map(|undo| undo.count)
@ -76,7 +100,7 @@ pub struct Buffer {
pub version: time::Global, pub version: time::Global,
saved_version: time::Global, saved_version: time::Global,
last_edit: time::Local, last_edit: time::Local,
undos: UndoMap, undo_map: UndoMap,
selections: HashMap<SelectionSetId, Vec<Selection>>, selections: HashMap<SelectionSetId, Vec<Selection>>,
pub selections_last_update: SelectionsVersion, pub selections_last_update: SelectionsVersion,
deferred_ops: OperationQueue<Operation>, deferred_ops: OperationQueue<Operation>,
@ -225,7 +249,7 @@ impl Buffer {
} }
fn build(replica_id: ReplicaId, file: Option<FileHandle>, base_text: String) -> Self { fn build(replica_id: ReplicaId, file: Option<FileHandle>, base_text: String) -> Self {
let mut insertion_splits = HashMap::new(); let mut insertion_splits = HashMap::default();
let mut fragments = SumTree::new(); let mut fragments = SumTree::new();
let base_insertion = Insertion { let base_insertion = Insertion {
@ -247,7 +271,7 @@ impl Buffer {
id: FragmentId::min_value().clone(), id: FragmentId::min_value().clone(),
insertion: base_insertion.clone(), insertion: base_insertion.clone(),
text: base_insertion.text.slice(0..0), text: base_insertion.text.slice(0..0),
deletions: HashSet::new(), deletions: HashSet::default(),
visible: true, visible: true,
}); });
@ -266,7 +290,7 @@ impl Buffer {
id: base_fragment_id, id: base_fragment_id,
text: base_insertion.text.clone(), text: base_insertion.text.clone(),
insertion: base_insertion, insertion: base_insertion,
deletions: HashSet::new(), deletions: HashSet::default(),
visible: true, visible: true,
}); });
} }
@ -275,15 +299,15 @@ impl Buffer {
file, file,
fragments, fragments,
insertion_splits, insertion_splits,
edit_ops: HashMap::new(), edit_ops: HashMap::default(),
version: time::Global::new(), version: time::Global::new(),
saved_version: time::Global::new(), saved_version: time::Global::new(),
last_edit: time::Local::default(), last_edit: time::Local::default(),
undos: Default::default(), undo_map: Default::default(),
selections: HashMap::default(), selections: HashMap::default(),
selections_last_update: 0, selections_last_update: 0,
deferred_ops: OperationQueue::new(), deferred_ops: OperationQueue::new(),
deferred_replicas: HashSet::new(), deferred_replicas: HashSet::default(),
replica_id, replica_id,
local_clock: time::Local::new(replica_id), local_clock: time::Local::new(replica_id),
lamport_clock: time::Lamport::new(replica_id), lamport_clock: time::Lamport::new(replica_id),
@ -451,7 +475,7 @@ impl Buffer {
Edits { Edits {
cursor, cursor,
undos: &self.undos, undos: &self.undo_map,
since, since,
delta: 0, delta: 0,
} }
@ -911,7 +935,7 @@ impl Buffer {
let undo = UndoOperation { let undo = UndoOperation {
id: self.local_clock.tick(), id: self.local_clock.tick(),
edit_id, edit_id,
count: self.undos.undo_count(edit_id) + 1, count: self.undo_map.undo_count(edit_id) + 1,
}; };
self.apply_undo(undo)?; self.apply_undo(undo)?;
self.version.observe(undo.id); self.version.observe(undo.id);
@ -931,15 +955,26 @@ impl Buffer {
} }
fn apply_undo(&mut self, undo: UndoOperation) -> Result<()> { fn apply_undo(&mut self, undo: UndoOperation) -> Result<()> {
// let mut new_fragments = SumTree::new();
// self.undos.insert(undo);
// let edit = &self.edit_ops[&undo.edit_id];
// let start_fragment_id = self.resolve_fragment_id(edit.start_id, edit.start_offset)?;
// let end_fragment_id = self.resolve_fragment_id(edit.end_id, edit.end_offset)?;
// let mut cursor = self.fragments.cursor::<FragmentIdRef, ()>();
// for fragment in cursor {}
// self.fragments = new_fragments;
let mut new_fragments; let mut new_fragments;
self.undos.insert(undo); self.undo_map.insert(undo);
let edit = &self.edit_ops[&undo.edit_id]; let edit = &self.edit_ops[&undo.edit_id];
let start_fragment_id = self.resolve_fragment_id(edit.start_id, edit.start_offset)?; let start_fragment_id = self.resolve_fragment_id(edit.start_id, edit.start_offset)?;
let end_fragment_id = self.resolve_fragment_id(edit.end_id, edit.end_offset)?; let end_fragment_id = self.resolve_fragment_id(edit.end_id, edit.end_offset)?;
let mut cursor = self.fragments.cursor::<FragmentIdRef, ()>(); let mut cursor = self.fragments.cursor::<FragmentIdRef, ()>();
if start_fragment_id == end_fragment_id { if edit.start_id == edit.end_id && edit.start_offset == edit.end_offset {
let splits = &self.insertion_splits[&undo.edit_id]; let splits = &self.insertion_splits[&undo.edit_id];
let mut insertion_splits = splits.cursor::<(), ()>().map(|s| &s.fragment_id).peekable(); let mut insertion_splits = splits.cursor::<(), ()>().map(|s| &s.fragment_id).peekable();
@ -948,7 +983,7 @@ impl Buffer {
loop { loop {
let mut fragment = cursor.item().unwrap().clone(); let mut fragment = cursor.item().unwrap().clone();
fragment.visible = fragment.is_visible(&self.undos); fragment.visible = fragment.is_visible(&self.undo_map);
new_fragments.push(fragment); new_fragments.push(fragment);
cursor.next(); cursor.next();
if let Some(split_id) = insertion_splits.next() { if let Some(split_id) = insertion_splits.next() {
@ -968,7 +1003,7 @@ impl Buffer {
if edit.version_in_range.observed(fragment.insertion.id) if edit.version_in_range.observed(fragment.insertion.id)
|| fragment.insertion.id == undo.edit_id || fragment.insertion.id == undo.edit_id
{ {
fragment.visible = fragment.is_visible(&self.undos); fragment.visible = fragment.is_visible(&self.undo_map);
} }
new_fragments.push(fragment); new_fragments.push(fragment);
cursor.next(); cursor.next();
@ -1559,7 +1594,7 @@ impl Clone for Buffer {
version: self.version.clone(), version: self.version.clone(),
saved_version: self.saved_version.clone(), saved_version: self.saved_version.clone(),
last_edit: self.last_edit.clone(), last_edit: self.last_edit.clone(),
undos: self.undos.clone(), undo_map: self.undo_map.clone(),
selections: self.selections.clone(), selections: self.selections.clone(),
selections_last_update: self.selections_last_update.clone(), selections_last_update: self.selections_last_update.clone(),
deferred_ops: self.deferred_ops.clone(), deferred_ops: self.deferred_ops.clone(),
@ -1891,7 +1926,7 @@ impl Fragment {
id, id,
text: insertion.text.clone(), text: insertion.text.clone(),
insertion, insertion,
deletions: HashSet::new(), deletions: HashSet::default(),
visible: true, visible: true,
} }
} }
@ -2810,9 +2845,9 @@ mod tests {
fn test_random_concurrent_edits() { fn test_random_concurrent_edits() {
use crate::test::Network; use crate::test::Network;
const PEERS: usize = 3; const PEERS: usize = 2;
for seed in 0..50 { for seed in 0..1000 {
println!("{:?}", seed); println!("{:?}", seed);
let mut rng = &mut StdRng::seed_from_u64(seed); let mut rng = &mut StdRng::seed_from_u64(seed);
@ -2830,19 +2865,29 @@ mod tests {
network.add_peer(i as u16); network.add_peer(i as u16);
} }
let mut mutation_count = 10; let mut mutation_count = 3;
loop { loop {
let replica_index = rng.gen_range(0..PEERS); let replica_index = rng.gen_range(0..PEERS);
let replica_id = replica_ids[replica_index]; let replica_id = replica_ids[replica_index];
let buffer = &mut buffers[replica_index]; let buffer = &mut buffers[replica_index];
if mutation_count > 0 && rng.gen() {
let (_, _, ops) = buffer.randomly_mutate(&mut rng, None); match rng.gen_range(0..=100) {
network.broadcast(replica_id, ops, &mut rng); 0..=50 if mutation_count != 0 => {
mutation_count -= 1; let (_, _, ops) = buffer.randomly_mutate(&mut rng, None);
} else if network.has_unreceived(replica_id) { network.broadcast(replica_id, ops, &mut rng);
buffer mutation_count -= 1;
.apply_ops(network.receive(replica_id, &mut rng), None) }
.unwrap(); 51..=70 if mutation_count != 0 => {
let ops = buffer.randomly_undo_redo(&mut rng, None);
network.broadcast(replica_id, ops, &mut rng);
mutation_count -= 1;
}
71..=100 if network.has_unreceived(replica_id) => {
buffer
.apply_ops(network.receive(replica_id, &mut rng), None)
.unwrap();
}
_ => {}
} }
if mutation_count == 0 && network.is_idle() { if mutation_count == 0 && network.is_idle() {
@ -2868,13 +2913,14 @@ mod tests {
pub fn randomly_mutate<T>( pub fn randomly_mutate<T>(
&mut self, &mut self,
rng: &mut T, rng: &mut T,
ctx: Option<&mut ModelContext<Self>>, mut ctx: Option<&mut ModelContext<Self>>,
) -> (Vec<Range<usize>>, String, Vec<Operation>) ) -> (Vec<Range<usize>>, String, Vec<Operation>)
where where
T: Rng, T: Rng,
{ {
// Randomly edit // Randomly edit
let (old_ranges, new_text, mut operations) = self.randomly_edit(rng, 5, ctx); let (old_ranges, new_text, mut operations) =
self.randomly_edit(rng, 5, ctx.as_deref_mut());
// Randomly add, remove or mutate selection sets. // Randomly add, remove or mutate selection sets.
let replica_selection_sets = &self let replica_selection_sets = &self
@ -2907,6 +2953,20 @@ mod tests {
(old_ranges, new_text, operations) (old_ranges, new_text, operations)
} }
pub fn randomly_undo_redo(
&mut self,
rng: &mut impl Rng,
mut ctx: Option<&mut ModelContext<Self>>,
) -> Vec<Operation> {
let mut ops = Vec::new();
for _ in 0..rng.gen_range(0..5) {
if let Some(edit_id) = self.edit_ops.keys().choose(rng).copied() {
ops.push(self.undo_or_redo(edit_id, ctx.as_deref_mut()).unwrap());
}
}
ops
}
} }
impl Operation { impl Operation {
@ -2924,11 +2984,11 @@ mod tests {
for (row, line) in buffer.text()[range].lines().enumerate() { for (row, line) in buffer.text()[range].lines().enumerate() {
lengths lengths
.entry(line.len() as u32) .entry(line.len() as u32)
.or_insert(HashSet::new()) .or_insert(HashSet::default())
.insert(row as u32); .insert(row as u32);
} }
if lengths.is_empty() { if lengths.is_empty() {
let mut rows = HashSet::new(); let mut rows = HashSet::default();
rows.insert(0); rows.insert(0);
lengths.insert(0, rows); lengths.insert(0, rows);
} }