Skip to content

Commit

Permalink
Merge branch 'master' of github.com:josephg/diamond-types
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Nov 24, 2023
2 parents d1da820 + 9b123fd commit 05d6776
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/diamond-types-old/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ humansize = "1.1"
num_enum = "0.5"

[dev-dependencies]
criterion = "^0.3"
criterion = "0.5.1"
rand = { version = "^0.8", features = ["small_rng"] }
crdt-testdata = { path = "../crdt-testdata" }

Expand Down
38 changes: 26 additions & 12 deletions crates/diamond-types-old/examples/cloning_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use std::fmt::Write;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EditHistory {
start_content: SmartString,
end_content: String,

// num_agents: usize, // field exists but not used.
txns: Vec<HistoryEntry>,
}

Expand All @@ -33,28 +32,39 @@ pub struct SimpleTextOp(usize, usize, SmartString); // pos, del_len, ins_content
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryEntry {
id: usize,
parents: SmallVec<[usize; 2]>,
num_children: usize,
agent: String,
// op: TextOperation,
ops: SmallVec<[SimpleTextOp; 2]>,
// agent: usize, // field exists but it is not in use.
patches: SmallVec<[SimpleTextOp; 2]>,
}

// A note about agent IDs and sequence numbers:
//
// So, the input data specifies agent ids for each transaction. We could use that, but the
// challenge is that we then need a universal way (across all documents to track what sequence
// number we're up to for each agent. And then pass that sequence number in to the local_insert /
// local_del functions - since those functions don't accept a sequence number parameter. (And
// probably shouldn't for any other use case).
//
// Right now this implementation generates new agent IDs in a complex, arbitrary way as we process
// the operations such that we never accidentally reuse agents. But we could use the agents in the
// file if we wanted, so long as this problem can be solved.

fn gen_main() -> Result<(), Box<dyn Error>> {
let mut doc = ListCRDT::new();

// Make editing trace JSON data sets with this command:
// dt export-trace benchmark_data/friendsforever.dt -o ff.json

// let filename = "example_trace.json";
let filename = "node_nodecc.json";
let filename = "ff.json";
// let filename = "node_nodecc.json";
// let filename = "git_makefile.json";

let file = BufReader::new(File::open(filename)?);
let history: EditHistory = serde_json::from_reader(file)?;
// dbg!(data);

assert!(history.start_content.is_empty()); // 'cos I'm not handling this for now.

// There should be exactly one entry with no parents.
let num_roots = history.txns.iter().filter(|e| e.parents.is_empty()).count();
// assert_eq!(num_roots, 1);
Expand All @@ -78,6 +88,7 @@ fn gen_main() -> Result<(), Box<dyn Error>> {
// Fork it and take the fork.
let mut doc = parent_doc.clone();

// ** See note above about sequence numbers before changing this code.
let agent = if need_agent {
let mut agent_name = SmartString::new();
write!(agent_name, "{idx}-{retains}").unwrap();
Expand All @@ -93,7 +104,7 @@ fn gen_main() -> Result<(), Box<dyn Error>> {
// doc_at_idx.insert(usize::MAX)

// let mut root = Some(doc);
for (_i, entry) in history.txns.iter().enumerate() {
for (i, entry) in history.txns.iter().enumerate() {
// println!("Iteration {_i} / {:?}", entry);

// First we need to get the doc we're editing.
Expand Down Expand Up @@ -121,7 +132,7 @@ fn gen_main() -> Result<(), Box<dyn Error>> {
// let agent = doc.get_or_create_agent_id(&entry.agent);

// Ok, now modify the document.
for op in &entry.ops {
for op in &entry.patches {
let pos = op.0;
let del_len = op.1;
let ins_content = op.2.as_str();
Expand All @@ -137,7 +148,7 @@ fn gen_main() -> Result<(), Box<dyn Error>> {

// And deposit the result back into doc_at_idx.
if entry.num_children > 0 {
doc_at_idx.insert(entry.id, (doc, agent, entry.num_children));
doc_at_idx.insert(i, (doc, agent, entry.num_children));
} else {
println!("done!");

Expand All @@ -151,6 +162,9 @@ fn gen_main() -> Result<(), Box<dyn Error>> {
// println!("Saved to {out_filename}");

assert_eq!(result, history.end_content);

// let x: Vec<_> = doc.get_all_txns();
// dbg!(x);
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/diamond-types-old/src/list/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ impl ListCRDT {
let origin_left = self.remote_id_to_order(origin_left);
let origin_right = self.remote_id_to_order(origin_right);

if cfg!(debug_assertions) {
let left = self.get_cursor_after(origin_left, true);
let right = self.get_cursor_before(origin_right);
assert!(left <= right);
}

let item = YjsSpan {
time: order,
origin_left,
Expand Down Expand Up @@ -662,7 +668,6 @@ impl ListCRDT {
// I could break this into two loops - and here enter an inner loop,
// deleting len items. It seems a touch excessive though.

// dbg!(target_order);
let deleted_here = self.internal_mark_deleted(next_time, target_order, len, true);

// println!(" -> managed to delete {}", deleted_here);
Expand Down
8 changes: 4 additions & 4 deletions crates/diamond-types-old/src/list/time/positionmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ impl PositionRun {
// Self { val, content_len: len, final_len: 0 }
// }
pub(crate) fn new_void(len: usize) -> Self {
Self { tag: MapTag::NotInsertedYet, final_len: len, content_len: 0 }
Self { tag: NotInsertedYet, final_len: len, content_len: 0 }
}

pub(crate) fn new_ins(len: usize) -> Self {
Self { tag: MapTag::Inserted, final_len: len, content_len: len }
Self { tag: Inserted, final_len: len, content_len: len }
}

pub(crate) fn new_upstream(final_len: usize, content_len: usize) -> Self {
Self { tag: MapTag::Upstream, final_len, content_len }
Self { tag: Upstream, final_len, content_len }
}
}

Expand All @@ -71,7 +71,7 @@ impl HasLength for PositionRun {
}
impl SplitableSpanHelpers for PositionRun {
fn truncate_h(&mut self, at: usize) -> Self {
assert_ne!(self.tag, MapTag::Upstream);
assert_ne!(self.tag, Upstream);

let remainder = self.final_len - at;
self.final_len = at;
Expand Down

0 comments on commit 05d6776

Please sign in to comment.