Skip to content

Commit

Permalink
chore: sync octobase's update (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Sep 15, 2023
2 parents 645c9c4 + aa7d89e commit e74a132
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 157 deletions.
1 change: 1 addition & 0 deletions y-octo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ nom = "7.1"
ordered-float = "3.8"
rand = "0.8"
rand_chacha = "0.3"
rand_distr = "0.4.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
Expand Down
15 changes: 10 additions & 5 deletions y-octo/src/doc/codec/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,22 @@ impl Item {
}
}

pub fn resolve_parent(&self) -> Option<(Option<Parent>, Option<String>)> {
if let Some(item) = self.left.get() {
// find a note that has parent info
// in crdt tree, not all node has parent info
// so we need to check left and right node if they have parent info
pub fn find_node_with_parent_info(&self) -> Option<Item> {
if self.parent.is_some() {
return Some(self.clone());
} else if let Some(item) = self.left.get() {
if item.parent.is_none() {
if let Some(item) = item.right.get() {
return Some((item.parent.clone(), item.parent_sub.clone()));
return Some(item.clone());
}
} else {
return Some((item.parent.clone(), item.parent_sub.clone()));
return Some(item.clone());
}
} else if let Some(item) = self.right.get() {
return Some((item.parent.clone(), item.parent_sub.clone()));
return Some(item.clone());
}
None
}
Expand Down
38 changes: 31 additions & 7 deletions y-octo/src/doc/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use super::{publisher::DocPublisher, store::StoreRef, *};
use super::{history::StoreHistory, publisher::DocPublisher, store::StoreRef, *};
use crate::sync::{Arc, RwLock};

#[cfg(feature = "debug")]
Expand Down Expand Up @@ -42,8 +42,26 @@ impl Default for DocOptions {
gc: true,
}
} else {
/// It tends to generate small numbers.
/// Since the client id will be included in all crdt items, the
/// small client helps to reduce the binary size.
///
/// NOTE: The probability of 36% of the random number generated by
/// this function is greater than [u32::MAX]
fn prefer_small_random() -> u64 {
use rand::{distributions::Distribution, thread_rng};
use rand_distr::Exp;

let scale_factor = u16::MAX as f64;
let v: f64 = Exp::new(1.0 / scale_factor)
.map(|exp| exp.sample(&mut thread_rng()))
.unwrap_or_else(|_| rand::random());

(v * scale_factor) as u64
}

Self {
client_id: rand::random(),
client_id: prefer_small_random(),
guid: nanoid::nanoid!(),
gc: true,
}
Expand Down Expand Up @@ -164,8 +182,10 @@ impl Doc {
self.store.read().unwrap().clients()
}

pub fn history(&self, client: u64) -> Option<Vec<RawHistory>> {
self.store.read().unwrap().history(client)
pub fn history(&self) -> StoreHistory {
let history = StoreHistory::new(&self.store);
history.resolve();
history
}

#[cfg(feature = "debug")]
Expand Down Expand Up @@ -343,14 +363,18 @@ impl Doc {
self.store.read().unwrap().get_state_vector()
}

pub fn subscribe(&self, cb: impl Fn(&[u8]) + Sync + Send + 'static) {
pub fn subscribe(&self, cb: impl Fn(&[u8], &[History]) + Sync + Send + 'static) {
self.publisher.subscribe(cb);
}

pub fn unsubscribe_all(&self) {
self.publisher.unsubscribe_all();
}

pub fn subscribe_count(&self) -> usize {
self.publisher.count()
}

pub fn gc(&self) -> JwstCodecResult<()> {
self.store.write().unwrap().optimize()
}
Expand Down Expand Up @@ -504,11 +528,11 @@ mod tests {
let count = Arc::new(AtomicU8::new(0));
let count_clone1 = count.clone();
let count_clone2 = count.clone();
doc.subscribe(move |_| {
doc.subscribe(move |_, _| {
count_clone1.fetch_add(1, Ordering::SeqCst);
});

doc_clone.subscribe(move |_| {
doc_clone.subscribe(move |_, _| {
count_clone2.fetch_add(1, Ordering::SeqCst);
});

Expand Down
Loading

0 comments on commit e74a132

Please sign in to comment.