share repo for sessions storage

This commit is contained in:
Nikita Galaiko 2023-03-21 11:28:58 +01:00
parent e79d377e3e
commit 7bee70ac80
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
9 changed files with 87 additions and 90 deletions

View File

@ -28,8 +28,9 @@ fn test_project() -> Result<(git2::Repository, projects::Project)> {
#[test]
fn test_read_none() {
let (repo, project) = test_project().unwrap();
let sessions_store = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project, sessions_store);
let repo = Arc::new(Mutex::new(repo));
let sessions_store = sessions::Store::new(repo.clone(), project.clone());
let store = super::Store::new(repo, project, sessions_store);
let file_path = Path::new("test.txt");
let deltas = store.read(file_path);
assert!(deltas.is_ok());
@ -39,8 +40,9 @@ fn test_read_none() {
#[test]
fn test_read_invalid() {
let (repo, project) = test_project().unwrap();
let sessions_store = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone(), sessions_store);
let repo = Arc::new(Mutex::new(repo));
let sessions_store = sessions::Store::new(repo.clone(), project.clone());
let store = super::Store::new(repo, project.clone(), sessions_store);
let file_path = Path::new("test.txt");
let full_file_path = project.deltas_path().join(file_path);
@ -54,8 +56,9 @@ fn test_read_invalid() {
#[test]
fn test_write_read() {
let (repo, project) = test_project().unwrap();
let sessions_store = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project, sessions_store);
let repo = Arc::new(Mutex::new(repo));
let sessions_store = sessions::Store::new(repo.clone(), project.clone());
let store = super::Store::new(repo, project, sessions_store);
let file_path = Path::new("test.txt");
let deltas = vec![Delta {
@ -73,8 +76,9 @@ fn test_write_read() {
#[test]
fn test_write_must_create_session() {
let (repo, project) = test_project().unwrap();
let sessions_store = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project, sessions_store.clone());
let repo = Arc::new(Mutex::new(repo));
let sessions_store = sessions::Store::new(repo.clone(), project.clone());
let store = super::Store::new(repo, project, sessions_store.clone());
let file_path = Path::new("test.txt");
let deltas = vec![Delta {
@ -98,8 +102,9 @@ fn clone_repo(repo: &git2::Repository) -> git2::Repository {
#[test]
fn test_write_must_not_override_session() {
let (repo, project) = test_project().unwrap();
let sessions_store = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project, sessions_store.clone());
let repo = Arc::new(Mutex::new(repo));
let sessions_store = sessions::Store::new(repo.clone(), project.clone());
let store = super::Store::new(repo, project, sessions_store.clone());
let file_path = Path::new("test.txt");
let session_before_write = sessions_store.create_current();

View File

@ -31,18 +31,14 @@ impl Clone for Repository {
impl Repository {
pub fn new(project: projects::Project, user: Option<users::User>) -> Result<Self> {
let git_repository = git2::Repository::open(&project.path)?;
let sessions_storage =
sessions::Store::new(git2::Repository::open(&project.path)?, project.clone())?;
let arepo = Arc::new(Mutex::new(git2::Repository::open(&project.path)?));
let sessions_storage = sessions::Store::new(arepo.clone(), project.clone());
init(&git_repository, &project, user, &sessions_storage)
.with_context(|| "failed to init repository")?;
Ok(Repository {
project: project.clone(),
git_repository,
deltas_storage: deltas::Store::new(
Arc::new(Mutex::new(git2::Repository::open(&project.path)?)),
project,
sessions_storage.clone(),
),
deltas_storage: deltas::Store::new(arepo, project, sessions_storage.clone()),
sessions_storage,
})
}

View File

@ -38,12 +38,9 @@ fn test_filter_by_timestamp() {
let (repo, project) = test_project().unwrap();
let index_path = tempdir().unwrap().path().to_str().unwrap().to_string();
let sessions_storage = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let deltas_storage = deltas::Store::new(
Arc::new(Mutex::new(repo)),
project.clone(),
sessions_storage.clone(),
);
let repo = Arc::new(Mutex::new(repo));
let sessions_storage = sessions::Store::new(repo.clone(), project.clone());
let deltas_storage = deltas::Store::new(repo, project.clone(), sessions_storage.clone());
let mut session = sessions_storage.create_current().unwrap();
deltas_storage
.write(
@ -113,13 +110,10 @@ fn test_filter_by_timestamp() {
fn test_sorted_by_timestamp() {
let (repo, project) = test_project().unwrap();
let index_path = tempdir().unwrap().path().to_str().unwrap().to_string();
let repo = Arc::new(Mutex::new(repo));
let sessions_storage = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let deltas_storage = deltas::Store::new(
Arc::new(Mutex::new(repo)),
project.clone(),
sessions_storage.clone(),
);
let sessions_storage = sessions::Store::new(repo.clone(), project.clone());
let deltas_storage = deltas::Store::new(repo, project.clone(), sessions_storage.clone());
let mut session = sessions_storage.create_current().unwrap();
deltas_storage
.write(
@ -163,13 +157,10 @@ fn test_sorted_by_timestamp() {
fn test_simple() {
let (repo, project) = test_project().unwrap();
let index_path = tempdir().unwrap().path().to_str().unwrap().to_string();
let repo = Arc::new(Mutex::new(repo));
let sessions_storage = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let deltas_storage = deltas::Store::new(
Arc::new(Mutex::new(repo)),
project.clone(),
sessions_storage.clone(),
);
let sessions_storage = sessions::Store::new(repo.clone(), project.clone());
let deltas_storage = deltas::Store::new(repo, project.clone(), sessions_storage.clone());
let mut session = sessions_storage.create_current().unwrap();
deltas_storage
.write(

View File

@ -1,4 +1,8 @@
use std::{collections::HashMap, path::Path};
use std::{
collections::HashMap,
path::Path,
sync::{Arc, Mutex},
};
use crate::{projects, users};
use anyhow::Result;
@ -44,7 +48,7 @@ fn test_project() -> Result<(git2::Repository, projects::Project)> {
#[test]
fn test_current_none() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(repo, project).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let current_session = store.get_current();
assert!(current_session.is_ok());
assert!(current_session.unwrap().is_none());
@ -53,7 +57,7 @@ fn test_current_none() {
#[test]
fn test_create_current_fails_when_meta_path_exists() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(repo, project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let meta_path = project.session_path().join("meta");
std::fs::create_dir_all(&meta_path).unwrap();
@ -65,7 +69,7 @@ fn test_create_current_fails_when_meta_path_exists() {
#[test]
fn test_create_current_when_session_dir_exists() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(repo, project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let session_dir = project.session_path();
std::fs::create_dir_all(&session_dir).unwrap();
@ -77,7 +81,7 @@ fn test_create_current_when_session_dir_exists() {
#[test]
fn test_create_current_empty() {
let (repo, project) = test_project_empty().unwrap();
let store = super::Store::new(repo, project).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let current_session = store.create_current();
assert!(current_session.is_ok());
assert!(current_session.as_ref().unwrap().id.len() > 0);
@ -95,7 +99,7 @@ fn test_create_current_empty() {
#[test]
fn test_create_current() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(clone_repo(&repo))), project.clone());
let current_session = store.create_current();
assert!(current_session.is_ok());
assert!(current_session.as_ref().unwrap().id.len() > 0);
@ -139,7 +143,7 @@ fn test_create_current() {
#[test]
fn test_get_current() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let created_session = store.create_current();
assert!(created_session.is_ok());
let created_session = created_session.unwrap();
@ -155,7 +159,7 @@ fn test_get_current() {
#[test]
fn test_flush() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(clone_repo(&repo))), project.clone());
let created_session = store.create_current();
assert!(created_session.is_ok());
let created_session = created_session.unwrap();
@ -186,7 +190,7 @@ fn test_flush() {
#[test]
fn test_flush_with_user() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(clone_repo(&repo))), project.clone());
let created_session = store.create_current();
assert!(created_session.is_ok());
let created_session = created_session.unwrap();
@ -217,7 +221,7 @@ fn test_flush_with_user() {
#[test]
fn test_get_persistent() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(clone_repo(&repo))), project.clone());
let created_session = store.create_current();
assert!(created_session.is_ok());
let mut created_session = created_session.unwrap();
@ -241,7 +245,7 @@ fn clone_repo(repo: &git2::Repository) -> git2::Repository {
#[test]
fn test_list() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let first = store.create_current();
assert!(first.is_ok());
let mut first = first.unwrap();
@ -272,7 +276,7 @@ fn test_list() {
fn test_list_files_from_first_presistent_session() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
@ -297,7 +301,7 @@ fn test_list_files_from_first_presistent_session() {
fn test_list_files_from_second_current_session() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
@ -325,7 +329,7 @@ fn test_list_files_from_second_current_session() {
#[test]
fn test_list_files_from_second_presistent_session() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(repo)), project.clone());
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
@ -358,7 +362,7 @@ fn test_list_files_from_second_presistent_session() {
#[test]
fn test_flush_ensure_wd_structure() {
let (repo, project) = test_project().unwrap();
let store = super::Store::new(clone_repo(&repo), project.clone()).unwrap();
let store = super::Store::new(Arc::new(Mutex::new(clone_repo(&repo))), project.clone());
// create file inside a directory
let file_dir = Path::new(&project.path).join("dir1").join("dir2");

View File

@ -14,11 +14,11 @@ pub struct Store {
}
impl Store {
pub fn new(git_repository: git2::Repository, project: projects::Project) -> Result<Self> {
Ok(Self {
project: project.clone(),
git_repository: Arc::new(Mutex::new(git_repository)),
})
pub fn new(git_repository: Arc<Mutex<git2::Repository>>, project: projects::Project) -> Self {
Self {
project,
git_repository,
}
}
pub fn create(&self) -> Result<sessions::Session> {

View File

@ -1,4 +1,8 @@
use std::{collections::HashMap, time};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
time,
};
use crate::{projects, sessions, users};
use anyhow::Result;
@ -14,15 +18,12 @@ pub struct Store {
}
impl Store {
pub fn new(git_repository: git2::Repository, project: projects::Project) -> Result<Self> {
Ok(Self {
project: project.clone(),
current: current::Store::new(git_repository, project.clone())?,
persistent: persistent::Store::new(
git2::Repository::open(&project.path)?,
project.clone(),
)?,
})
pub fn new(git_repository: Arc<Mutex<git2::Repository>>, project: projects::Project) -> Self {
Self {
current: current::Store::new(git_repository.clone(), project.clone()),
persistent: persistent::Store::new(git_repository, project.clone()),
project,
}
}
pub fn create_current(&self) -> Result<sessions::Session> {

View File

@ -21,13 +21,13 @@ pub struct Store {
}
impl Store {
pub fn new(git_repository: git2::Repository, project: projects::Project) -> Result<Self> {
Ok(Self {
project: project.clone(),
git_repository: Arc::new(Mutex::new(git_repository)),
pub fn new(git_repository: Arc<Mutex<git2::Repository>>, project: projects::Project) -> Self {
Self {
project,
git_repository,
files_cache: Arc::new(Mutex::new(HashMap::new())),
sessions_cache: Arc::new(Mutex::new(None)),
})
}
}
pub fn get_by_id(&self, session_id: &str) -> Result<Option<sessions::Session>> {

View File

@ -33,16 +33,13 @@ fn clone_repo(repo: &git2::Repository) -> git2::Repository {
#[test]
fn test_register_file_change_must_create_session() {
let (repo, project) = test_project().unwrap();
let arepo = Arc::new(Mutex::new(clone_repo(&repo)));
let relative_file_path = Path::new("test.txt");
std::fs::write(Path::new(&project.path).join(relative_file_path), "test").unwrap();
let sessions_storage = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let deltas_storage = deltas::Store::new(
Arc::new(Mutex::new(clone_repo(&repo))),
project.clone(),
sessions_storage,
);
let sessions_storage = sessions::Store::new(arepo.clone(), project.clone());
let deltas_storage = deltas::Store::new(arepo, project.clone(), sessions_storage);
let result =
super::delta::register_file_change(&project, &repo, &deltas_storage, &relative_file_path);
println!("{:?}", result);
@ -57,16 +54,13 @@ fn test_register_file_change_must_create_session() {
#[test]
fn test_register_file_change_must_not_change_session() {
let (repo, project) = test_project().unwrap();
let arepo = Arc::new(Mutex::new(clone_repo(&repo)));
let relative_file_path = Path::new("test.txt");
std::fs::write(Path::new(&project.path).join(relative_file_path), "test").unwrap();
let sessions_storage = sessions::Store::new(clone_repo(&repo), project.clone()).unwrap();
let deltas_storage = deltas::Store::new(
Arc::new(Mutex::new(clone_repo(&repo))),
project.clone(),
sessions_storage,
);
let sessions_storage = sessions::Store::new(arepo.clone(), project.clone());
let deltas_storage = deltas::Store::new(arepo, project.clone(), sessions_storage);
let result =
super::delta::register_file_change(&project, &repo, &deltas_storage, &relative_file_path);
assert!(result.is_ok());

View File

@ -1,6 +1,9 @@
use crate::{deltas::Operation, projects, repositories, sessions};
use anyhow::Result;
use std::path::Path;
use std::{
path::Path,
sync::{Arc, Mutex},
};
use tempfile::tempdir;
fn test_project() -> Result<repositories::Repository> {
@ -25,8 +28,10 @@ fn test_project() -> Result<repositories::Repository> {
#[test]
fn test_flush_session() {
let repo = test_project().unwrap();
let sessions_store =
sessions::Store::new(clone_repo(&repo.git_repository), repo.project.clone()).unwrap();
let sessions_store = sessions::Store::new(
Arc::new(Mutex::new(clone_repo(&repo.git_repository))),
repo.project.clone(),
);
let relative_file_path = Path::new("test.txt");
std::fs::write(
@ -34,11 +39,10 @@ fn test_flush_session() {
"hello",
)
.unwrap();
let git_repo = repo.git_repository;
let result = super::delta::register_file_change(
&repo.project,
&git_repo,
&repo.git_repository,
&repo.deltas_storage,
&relative_file_path,
);
@ -60,7 +64,7 @@ fn test_flush_session() {
let result = super::delta::register_file_change(
&repo.project,
&git_repo,
&repo.git_repository,
&repo.deltas_storage,
&relative_file_path,
);
@ -83,8 +87,10 @@ fn clone_repo(repo: &git2::Repository) -> git2::Repository {
#[test]
fn test_flow() {
let repo = test_project().unwrap();
let sessions_store =
sessions::Store::new(clone_repo(&repo.git_repository), repo.project.clone()).unwrap();
let sessions_store = sessions::Store::new(
Arc::new(Mutex::new(clone_repo(&repo.git_repository))),
repo.project.clone(),
);
let size = 10;
let relative_file_path = Path::new("one/two/test.txt");