From de30d691284e66a777ce13fd07bd1237171d22c2 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 5 Jun 2023 10:16:56 +0200 Subject: [PATCH] extract deltas writer --- src-tauri/src/deltas/mod.rs | 2 + src-tauri/src/deltas/writer.rs | 46 +++++++++++++++++++ .../src/gb_repository/repository_tests.rs | 17 +++---- src-tauri/src/search/searcher_test.rs | 37 ++++++--------- src-tauri/src/sessions/writer.rs | 30 +----------- .../watcher/handlers/project_file_change.rs | 10 ++-- 6 files changed, 76 insertions(+), 66 deletions(-) create mode 100644 src-tauri/src/deltas/writer.rs diff --git a/src-tauri/src/deltas/mod.rs b/src-tauri/src/deltas/mod.rs index 7a038826d..305c2315a 100644 --- a/src-tauri/src/deltas/mod.rs +++ b/src-tauri/src/deltas/mod.rs @@ -3,9 +3,11 @@ mod delta; mod document; mod operations; mod reader; +mod writer; pub use database::Database; pub use delta::Delta; pub use document::Document; pub use operations::Operation; pub use reader::DeltasReader as Reader; +pub use writer::DeltasWriter as Writer; diff --git a/src-tauri/src/deltas/writer.rs b/src-tauri/src/deltas/writer.rs new file mode 100644 index 000000000..f93a48afd --- /dev/null +++ b/src-tauri/src/deltas/writer.rs @@ -0,0 +1,46 @@ +use anyhow::{Context, Result}; + +use crate::{ + gb_repository, + writer::{self, Writer}, +}; + +use super::Delta; + +pub struct DeltasWriter<'writer> { + repository: &'writer gb_repository::Repository, + writer: writer::DirWriter, +} + +impl<'writer> DeltasWriter<'writer> { + pub fn new(repository: &'writer gb_repository::Repository) -> Result { + let writer = writer::DirWriter::open(repository.root()); + repository + .get_or_create_current_session() + .context("failed to create session")?; + Ok(Self { writer, repository }) + } + + pub fn write>(&self, path: P, deltas: &Vec) -> Result<()> { + self.repository.lock()?; + defer! { + self.repository.unlock().unwrap(); + } + + let path = path.as_ref(); + let raw_deltas = serde_json::to_string(&deltas)?; + + self.writer.write_string( + self.repository.deltas_path().join(path).to_str().unwrap(), + &raw_deltas, + )?; + + log::info!( + "{}: wrote deltas for {}", + self.repository.project_id, + path.display() + ); + + Ok(()) + } +} diff --git a/src-tauri/src/gb_repository/repository_tests.rs b/src-tauri/src/gb_repository/repository_tests.rs index 03286908e..a747f1401 100644 --- a/src-tauri/src/gb_repository/repository_tests.rs +++ b/src-tauri/src/gb_repository/repository_tests.rs @@ -1,10 +1,9 @@ use std::{thread, time}; +use crate::{deltas, gb_repository, projects, sessions, storage, users}; use anyhow::Result; use tempfile::tempdir; -use crate::{deltas, gb_repository, projects, sessions, storage, users}; - fn remote_repository() -> Result { let path = tempdir()?.path().to_str().unwrap().to_string(); let repository = git2::Repository::init_bare(path)?; @@ -200,8 +199,8 @@ fn test_list_deltas_from_current_session() -> Result<()> { gb_repository::Repository::open(gb_repo_path, project.id, project_store, user_store)?; let current_session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, ¤t_session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( "test.txt", &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "Hello World".to_string()))], @@ -235,9 +234,8 @@ fn test_list_deltas_from_flushed_session() -> Result<()> { let gb_repo = gb_repository::Repository::open(gb_repo_path, project.id, project_store, user_store)?; - let current_session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, ¤t_session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( "test.txt", &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "Hello World".to_string()))], @@ -362,9 +360,8 @@ fn test_remote_syncronization() -> Result<()> { project_store.clone(), user_store.clone(), )?; - let session_one = gb_repo_one.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo_one, &session_one)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo_one)?; + writer.write( "test.txt", &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "Hello World".to_string()))], diff --git a/src-tauri/src/search/searcher_test.rs b/src-tauri/src/search/searcher_test.rs index 45522dcb7..47c1a6332 100644 --- a/src-tauri/src/search/searcher_test.rs +++ b/src-tauri/src/search/searcher_test.rs @@ -3,7 +3,7 @@ use std::{path::Path, time}; use anyhow::Result; use tempfile::tempdir; -use crate::{bookmarks, deltas, gb_repository, projects, sessions, storage, users}; +use crate::{bookmarks, deltas, gb_repository, projects, storage, users}; fn test_repository() -> Result { let path = tempdir()?.path().to_str().unwrap().to_string(); @@ -49,9 +49,8 @@ fn test_sorted_by_timestamp() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![ deltas::Delta { @@ -100,9 +99,8 @@ fn search_by_bookmark_note() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "Hello".to_string()))], @@ -197,9 +195,8 @@ fn search_by_full_match() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "hello".to_string()))], @@ -239,9 +236,8 @@ fn search_by_diff() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![ deltas::Delta { @@ -359,9 +355,8 @@ fn test_delete_all() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![ deltas::Delta { @@ -409,9 +404,8 @@ fn search_bookmark_by_phrase() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![deltas::Delta { operations: vec![deltas::Operation::Insert((0, "Hello".to_string()))], @@ -466,9 +460,8 @@ fn search_by_filename() -> Result<()> { let index_path = tempdir()?.path().to_str().unwrap().to_string(); - let session = gb_repo.get_or_create_current_session()?; - let writer = sessions::Writer::open(&gb_repo, &session)?; - writer.write_deltas( + let writer = deltas::Writer::new(&gb_repo)?; + writer.write( Path::new("test.txt"), &vec![ deltas::Delta { diff --git a/src-tauri/src/sessions/writer.rs b/src-tauri/src/sessions/writer.rs index b8fa0db28..485addbaa 100644 --- a/src-tauri/src/sessions/writer.rs +++ b/src-tauri/src/sessions/writer.rs @@ -3,7 +3,7 @@ use std::time; use anyhow::{anyhow, Context, Result}; use crate::{ - bookmarks, deltas, gb_repository, pty, + bookmarks, gb_repository, pty, reader::{self, Reader}, writer::{self, Writer}, }; @@ -194,32 +194,4 @@ impl<'writer> SessionWriter<'writer> { Ok(()) } - - pub fn write_deltas>( - &self, - path: P, - deltas: &Vec, - ) -> Result<()> { - self.repository.lock()?; - defer! { - self.repository.unlock().expect("failed to unlock"); - } - - let path = path.as_ref(); - - let raw_deltas = serde_json::to_string(&deltas)?; - - self.writer.write_string( - self.repository.deltas_path().join(path).to_str().unwrap(), - &raw_deltas, - )?; - - log::info!( - "{}: wrote deltas for {}", - self.repository.project_id, - path.display() - ); - - Ok(()) - } } diff --git a/src-tauri/src/watcher/handlers/project_file_change.rs b/src-tauri/src/watcher/handlers/project_file_change.rs index 6730303dd..b8b7639ca 100644 --- a/src-tauri/src/watcher/handlers/project_file_change.rs +++ b/src-tauri/src/watcher/handlers/project_file_change.rs @@ -154,13 +154,13 @@ impl<'listener> Handler<'listener> { return Ok(vec![]); } - let writer = sessions::Writer::open(self.gb_repository, ¤t_session)?; - + let deltas_writer = deltas::Writer::new(self.gb_repository)?; let deltas = text_doc.get_deltas(); - - writer - .write_deltas(path, &deltas) + deltas_writer + .write(path, &deltas) .with_context(|| "failed to write deltas")?; + + let writer = sessions::Writer::open(self.gb_repository, ¤t_session)?; writer .write_session_wd_file(path, ¤t_file_content) .with_context(|| "failed to write file")?;