refactor: rename *App to *Doc

This commit is contained in:
Zixuan Chen 2023-07-19 21:21:37 +08:00
parent 7f3bd5b0a4
commit 6abeba6849
10 changed files with 65 additions and 65 deletions

View file

@ -8,7 +8,7 @@ mod run {
use bench_utils::TextAction;
use loro_internal::fuzz::test_multi_sites;
use loro_internal::fuzz::Action;
use loro_internal::refactor::loro::LoroApp;
use loro_internal::refactor::loro::LoroDoc;
use loro_internal::LoroCore;
use loro_internal::Transact;
use rand::Rng;
@ -56,7 +56,7 @@ mod run {
b.bench_function("refactor-B4", |b| {
b.iter(|| {
let loro = LoroApp::new();
let loro = LoroDoc::new();
let mut txn = loro.txn().unwrap();
let text = txn.get_text("text");

View file

@ -4,7 +4,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
mod run {
use super::*;
use bench_utils::TextAction;
use loro_internal::refactor::loro::LoroApp;
use loro_internal::refactor::loro::LoroDoc;
pub fn b4(c: &mut Criterion) {
let actions = bench_utils::get_automerge_actions();
@ -12,7 +12,7 @@ mod run {
b.sample_size(10);
b.bench_function("B4", |b| {
b.iter(|| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
@ -24,7 +24,7 @@ mod run {
});
b.bench_function("B4 encode snapshot", |b| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
@ -47,7 +47,7 @@ mod run {
});
b.bench_function("B4 encode updates", |b| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
@ -70,7 +70,7 @@ mod run {
});
b.bench_function("B4 decode snapshot", |b| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
@ -88,13 +88,13 @@ mod run {
let data = loro.export_snapshot();
b.iter(|| {
let l = LoroApp::new();
let l = LoroDoc::new();
l.import(&data).unwrap();
});
});
b.bench_function("B4 import updates", |b| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
@ -113,14 +113,14 @@ mod run {
let data = loro.export_from(&Default::default());
b.iter(|| {
let l = LoroApp::new();
let l = LoroDoc::new();
l.import(&data).unwrap();
});
});
b.bench_function("B4 utf16", |b| {
b.iter(|| {
let loro = LoroApp::new();
let loro = LoroDoc::new();
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
@ -133,7 +133,7 @@ mod run {
b.bench_function("B4_Per100_Txn", |b| {
b.iter(|| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
@ -152,7 +152,7 @@ mod run {
b.bench_function("B4 One Op One Txn", |b| {
b.iter(|| {
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
{
for TextAction { pos, ins, del } in actions.iter() {
@ -167,8 +167,8 @@ mod run {
b.bench_function("B4DirectSync", |b| {
b.iter(|| {
let loro = LoroApp::default();
let loro_b = LoroApp::default();
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
{
@ -188,8 +188,8 @@ mod run {
let mut b = c.benchmark_group("refactored-sync");
b.bench_function("B4Parallel", |b| {
b.iter(|| {
let loro = LoroApp::default();
let loro_b = LoroApp::default();
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
let text = loro.get_text("text");
let text2 = loro_b.get_text("text");
let mut i = 0;

View file

@ -1,6 +1,6 @@
use bench_utils::TextAction;
use criterion::black_box;
use loro_internal::refactor::loro::LoroApp;
use loro_internal::refactor::loro::LoroDoc;
fn main() {
log_size();
@ -12,7 +12,7 @@ fn main() {
fn log_size() {
let actions = bench_utils::get_automerge_actions();
{
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
@ -34,7 +34,7 @@ fn log_size() {
println!("\n");
{
println!("One Transaction Per Action");
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
@ -57,7 +57,7 @@ fn log_size() {
fn bench_decode() {
let actions = bench_utils::get_automerge_actions();
{
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
#[allow(warnings)]
@ -73,7 +73,7 @@ fn bench_decode() {
// }
for _ in 0..100 {
let loro = LoroApp::new();
let loro = LoroDoc::new();
loro.import(black_box(&snapshot)).unwrap();
}
}
@ -82,7 +82,7 @@ fn bench_decode() {
#[allow(unused)]
fn bench_decode_updates() {
let actions = bench_utils::get_automerge_actions();
let loro = LoroApp::default();
let loro = LoroDoc::default();
let text = loro.get_text("text");
#[allow(warnings)]
@ -95,7 +95,7 @@ fn bench_decode_updates() {
let updates = loro.export_from(&Default::default());
for _ in 0..10 {
let loro = LoroApp::new();
let loro = LoroDoc::new();
loro.import(black_box(&updates)).unwrap();
}
}

View file

@ -8,7 +8,7 @@ pub mod recursive_refactored;
pub mod recursive_txn;
use crate::{
array_mut_ref, id::PeerID, refactor::loro::LoroApp, LoroCore, Transact, VersionVector,
array_mut_ref, id::PeerID, refactor::loro::LoroDoc, LoroCore, Transact, VersionVector,
};
#[derive(arbitrary::Arbitrary, EnumAsInner, Clone, PartialEq, Eq, Debug)]
@ -206,7 +206,7 @@ impl Actionable for Vec<LoroCore> {
}
}
impl Actionable for Vec<LoroApp> {
impl Actionable for Vec<LoroDoc> {
fn apply_action(&mut self, action: &Action) {
match action {
Action::Ins { content, pos, site } => {
@ -308,7 +308,7 @@ fn check_synced(sites: &mut [LoroCore]) {
}
}
fn check_synced_refactored(sites: &mut [LoroApp]) {
fn check_synced_refactored(sites: &mut [LoroDoc]) {
for i in 0..sites.len() - 1 {
for j in i + 1..sites.len() {
debug_log::group!("checking {} with {}", i, j);
@ -335,7 +335,7 @@ fn check_synced_refactored(sites: &mut [LoroApp]) {
}
}
fn check_eq_refactored(site_a: &mut LoroApp, site_b: &mut LoroApp) {
fn check_eq_refactored(site_a: &mut LoroDoc, site_b: &mut LoroDoc) {
let a = site_a.txn().unwrap();
let text_a = a.get_text("text");
let b = site_b.txn().unwrap();
@ -549,7 +549,7 @@ pub fn normalize(site_num: u8, actions: &mut [Action]) -> Vec<Action> {
pub fn test_multi_sites_refactored(site_num: u8, actions: &mut [Action]) {
let mut sites = Vec::new();
for i in 0..site_num {
let loro = LoroApp::new();
let loro = LoroDoc::new();
loro.set_peer_id(i as u64);
sites.push(loro);
}

View file

@ -15,7 +15,7 @@ use crate::{
};
use crate::{
container::registry::ContainerIdx,
refactor::{loro::LoroApp, ListHandler, MapHandler, TextHandler},
refactor::{loro::LoroDoc, ListHandler, MapHandler, TextHandler},
value::ToJson,
};
@ -48,7 +48,7 @@ pub enum Action {
}
struct Actor {
loro: LoroApp,
loro: LoroDoc,
// value_tracker: Arc<Mutex<LoroValue>>,
// map_tracker: Arc<Mutex<FxHashMap<String, LoroValue>>>,
// list_tracker: Arc<Mutex<Vec<LoroValue>>>,
@ -60,7 +60,7 @@ struct Actor {
impl Actor {
fn new(id: PeerID) -> Self {
let app = LoroApp::new();
let app = LoroDoc::new();
app.set_peer_id(id);
let mut actor = Actor {
loro: app,

View file

@ -1,4 +1,4 @@
use super::{state::AppState, txn::Transaction};
use super::{state::DocState, txn::Transaction};
use crate::container::{
list::list_op::{DeleteSpan, ListOp},
registry::ContainerIdx,
@ -12,21 +12,21 @@ use std::{
pub struct TextHandler {
container_idx: ContainerIdx,
state: Weak<Mutex<AppState>>,
state: Weak<Mutex<DocState>>,
}
pub struct MapHandler {
container_idx: ContainerIdx,
state: Weak<Mutex<AppState>>,
state: Weak<Mutex<DocState>>,
}
pub struct ListHandler {
container_idx: ContainerIdx,
state: Weak<Mutex<AppState>>,
state: Weak<Mutex<DocState>>,
}
impl TextHandler {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<AppState>>) -> Self {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<DocState>>) -> Self {
assert_eq!(idx.get_type(), ContainerType::Text);
Self {
container_idx: idx,
@ -129,7 +129,7 @@ impl TextHandler {
}
impl ListHandler {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<AppState>>) -> Self {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<DocState>>) -> Self {
assert_eq!(idx.get_type(), ContainerType::List);
Self {
container_idx: idx,
@ -224,7 +224,7 @@ impl ListHandler {
}
impl MapHandler {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<AppState>>) -> Self {
pub fn new(idx: ContainerIdx, state: Weak<Mutex<DocState>>) -> Self {
assert_eq!(idx.get_type(), ContainerType::Map);
Self {
container_idx: idx,
@ -302,11 +302,11 @@ impl MapHandler {
#[cfg(test)]
mod test {
use crate::refactor::loro::LoroApp;
use crate::refactor::loro::LoroDoc;
#[test]
fn test() {
let loro = LoroApp::new();
let loro = LoroDoc::new();
let mut txn = loro.txn().unwrap();
let text = txn.get_text("hello");
text.insert(&mut txn, 0, "hello");
@ -323,9 +323,9 @@ mod test {
#[test]
fn import() {
let loro = LoroApp::new();
let loro = LoroDoc::new();
loro.set_peer_id(1);
let loro2 = LoroApp::new();
let loro2 = LoroDoc::new();
loro2.set_peer_id(2);
let mut txn = loro.txn().unwrap();

View file

@ -16,7 +16,7 @@ use super::{
diff_calc::DiffCalculator,
oplog::OpLog,
snapshot_encode::{decode_app_snapshot, encode_app_snapshot},
state::{AppState, AppStateDiff, ContainerStateDiff},
state::{AppStateDiff, ContainerStateDiff, DocState},
txn::Transaction,
ListHandler, MapHandler, TextHandler,
};
@ -38,17 +38,17 @@ use super::{
/// `LoroApp::detach()` separates [AppState] from [OpLog]. In this mode,
/// updates to [OpLog] won't affect [AppState], while updates to [AppState]
/// will continue to affect [OpLog].
pub struct LoroApp {
pub struct LoroDoc {
oplog: Arc<Mutex<OpLog>>,
state: Arc<Mutex<AppState>>,
state: Arc<Mutex<DocState>>,
detached: bool,
}
impl LoroApp {
impl LoroDoc {
pub fn new() -> Self {
let oplog = OpLog::new();
// share arena
let state = Arc::new(Mutex::new(AppState::new(&oplog)));
let state = Arc::new(Mutex::new(DocState::new(&oplog)));
Self {
oplog: Arc::new(Mutex::new(oplog)),
state,
@ -60,7 +60,7 @@ impl LoroApp {
self.oplog.lock().unwrap().is_empty() && self.state.lock().unwrap().is_empty()
}
pub(super) fn from_existing(oplog: OpLog, state: AppState) -> Self {
pub(super) fn from_existing(oplog: OpLog, state: DocState) -> Self {
Self {
oplog: Arc::new(Mutex::new(oplog)),
state: Arc::new(Mutex::new(state)),
@ -104,7 +104,7 @@ impl LoroApp {
Ok(Transaction::new(self.state.clone(), self.oplog.clone()))
}
pub fn app_state(&self) -> &Arc<Mutex<AppState>> {
pub fn app_state(&self) -> &Arc<Mutex<DocState>> {
&self.state
}
@ -165,7 +165,7 @@ impl LoroApp {
decode_app_snapshot(self, &input[1..])?;
Ok(vec![]) // TODO: return diff
} else {
let app = LoroApp::new();
let app = LoroDoc::new();
decode_app_snapshot(&app, &input[1..])?;
let oplog = self.oplog.lock().unwrap();
let updates = app.export_from(oplog.vv());
@ -240,7 +240,7 @@ impl LoroApp {
}
}
impl Default for LoroApp {
impl Default for LoroDoc {
fn default() -> Self {
Self::new()
}

View file

@ -24,19 +24,19 @@ use crate::{
use super::{
arena::SharedArena,
loro::LoroApp,
loro::LoroDoc,
oplog::OpLog,
state::{AppState, ListState, MapState, State, TextState},
state::{DocState, ListState, MapState, State, TextState},
};
pub fn encode_app_snapshot(app: &LoroApp) -> Vec<u8> {
pub fn encode_app_snapshot(app: &LoroDoc) -> Vec<u8> {
let pre_encoded_state = preprocess_app_state(&app.app_state().lock().unwrap());
let f = encode_oplog(&app.oplog().lock().unwrap(), Some(pre_encoded_state));
// f.diagnose_size();
miniz_oxide::deflate::compress_to_vec(&f.encode(), 6)
}
pub fn decode_app_snapshot(app: &LoroApp, bytes: &[u8]) -> Result<(), LoroError> {
pub fn decode_app_snapshot(app: &LoroDoc, bytes: &[u8]) -> Result<(), LoroError> {
assert!(app.is_empty());
let bytes = miniz_oxide::inflate::decompress_to_vec(bytes).unwrap();
let data = FinalPhase::decode(&bytes)?;
@ -191,7 +191,7 @@ pub fn decode_oplog(
}
pub fn decode_state<'b>(
app_state: &'_ mut AppState,
app_state: &'_ mut DocState,
data: &'b FinalPhase,
) -> Result<(TempArena<'b>, CommonArena<'b>), LoroError> {
assert!(app_state.is_empty());
@ -454,7 +454,7 @@ struct PreEncodedState {
app_state: EncodedAppState,
}
fn preprocess_app_state(app_state: &AppState) -> PreEncodedState {
fn preprocess_app_state(app_state: &DocState) -> PreEncodedState {
assert!(!app_state.is_in_txn());
let mut peers = Vec::new();
let mut peer_lookup = FxHashMap::default();
@ -790,13 +790,13 @@ mod test {
#[test]
fn text_edit_snapshot_encode_decode() {
// test import snapshot directly
let app = LoroApp::new();
let app = LoroDoc::new();
let mut txn = app.txn().unwrap();
let text = txn.get_text("id");
text.insert(&mut txn, 0, "hello");
txn.commit().unwrap();
let snapshot = app.export_snapshot();
let app2 = LoroApp::new();
let app2 = LoroDoc::new();
app2.import(&snapshot).unwrap();
let actual = app2
.app_state()

View file

@ -26,7 +26,7 @@ pub(crate) use text_state::TextState;
use super::{arena::SharedArena, oplog::OpLog};
#[derive(Clone)]
pub struct AppState {
pub struct DocState {
pub(super) peer: PeerID,
pub(super) frontiers: Frontiers,
@ -107,7 +107,7 @@ impl<'a> AppStateDiff<'a> {
}
}
impl AppState {
impl DocState {
#[inline]
pub fn new(oplog: &OpLog) -> Self {
let peer = SystemRandom::new().next_u64();

View file

@ -28,7 +28,7 @@ use super::{
arena::SharedArena,
handler::{ListHandler, MapHandler, TextHandler},
oplog::OpLog,
state::{AppState, ContainerStateDiff, State},
state::{ContainerStateDiff, DocState, State},
};
pub struct Transaction {
@ -37,7 +37,7 @@ pub struct Transaction {
next_counter: Counter,
start_lamport: Lamport,
next_lamport: Lamport,
state: Arc<Mutex<AppState>>,
state: Arc<Mutex<DocState>>,
oplog: Arc<Mutex<OpLog>>,
frontiers: Frontiers,
local_ops: RleVec<[Op; 1]>,
@ -46,7 +46,7 @@ pub struct Transaction {
}
impl Transaction {
pub fn new(state: Arc<Mutex<AppState>>, oplog: Arc<Mutex<OpLog>>) -> Self {
pub fn new(state: Arc<Mutex<DocState>>, oplog: Arc<Mutex<OpLog>>) -> Self {
let mut state_lock = state.lock().unwrap();
let oplog_lock = oplog.lock().unwrap();
state_lock.start_txn();