extract deltas writer

This commit is contained in:
Nikita Galaiko 2023-06-05 10:16:56 +02:00
parent ede366f13e
commit de30d69128
6 changed files with 76 additions and 66 deletions

View File

@ -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;

View File

@ -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<Self> {
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<P: AsRef<std::path::Path>>(&self, path: P, deltas: &Vec<Delta>) -> 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(())
}
}

View File

@ -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<git2::Repository> {
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, &current_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, &current_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()))],

View File

@ -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<git2::Repository> {
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 {

View File

@ -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<P: AsRef<std::path::Path>>(
&self,
path: P,
deltas: &Vec<deltas::Delta>,
) -> 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(())
}
}

View File

@ -154,13 +154,13 @@ impl<'listener> Handler<'listener> {
return Ok(vec![]);
}
let writer = sessions::Writer::open(self.gb_repository, &current_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, &current_session)?;
writer
.write_session_wd_file(path, &current_file_content)
.with_context(|| "failed to write file")?;