remove unused code

This commit is contained in:
Nikita Galaiko 2023-05-17 14:21:43 +02:00
parent 9ae8f7f4ea
commit e083922114
8 changed files with 4 additions and 185 deletions

1
src-tauri/Cargo.lock generated
View File

@ -1476,7 +1476,6 @@ dependencies = [
"futures", "futures",
"futures-util", "futures-util",
"git2", "git2",
"lazy_static",
"log", "log",
"md5", "md5",
"notify", "notify",

View File

@ -50,7 +50,6 @@ scopeguard = "1.1.0"
sentry-anyhow = "0.31.0" sentry-anyhow = "0.31.0"
sentry-rust-minidump = "0.5.1" sentry-rust-minidump = "0.5.1"
sentry-debug-images = "0.31.0" sentry-debug-images = "0.31.0"
lazy_static = "1.4.0"
zip = "0.6.5" zip = "0.6.5"
rusqlite = { version = "0.28.0", features = [ "bundled", "blob", "hooks" ] } rusqlite = { version = "0.28.0", features = [ "bundled", "blob", "hooks" ] }
refinery = { version = "0.8", features = [ "rusqlite" ] } refinery = { version = "0.8", features = [ "rusqlite" ] }

View File

@ -52,44 +52,6 @@ impl Database {
Ok(()) Ok(())
} }
pub fn get_by_project_id_session_id_file_path(
&self,
project_id: &str,
session_id: &str,
file_path: &str,
) -> Result<Option<delta::Delta>> {
let mut delta: Option<delta::Delta> = None;
self.database.transaction(|tx| -> Result<()> {
let mut stmt = get_by_project_id_session_id_file_path_stmt(tx)
.context("Failed to prepare get_by_session_id_file_path statement")?;
let mut rows = stmt
.query(rusqlite::named_params! {
":project_id": project_id,
":session_id": session_id,
":file_path": file_path,
})
.context("Failed to execute get_by_session_id_file_path statement")?;
while let Some(row) = rows
.next()
.context("Failed to iterate over get_by_session_id_file_path results")?
{
let timestamp_ms: String = row.get(0).context("Failed to get timestamp_ms")?;
let operations: Vec<u8> = row.get(1).context("Failed to get operations")?;
let operations: Vec<operations::Operation> = serde_json::from_slice(&operations)
.context("Failed to deserialize operations")?;
let timestamp_ms: u128 = timestamp_ms
.parse()
.context("Failed to parse timestamp_ms")?;
delta = Some(delta::Delta {
timestamp_ms,
operations,
});
}
Ok(())
})?;
Ok(delta)
}
pub fn on<F>(&self, callback: F) -> Result<()> pub fn on<F>(&self, callback: F) -> Result<()>
where where
F: Fn(&str, &str, delta::Delta) + Send + 'static, F: Fn(&str, &str, delta::Delta) + Send + 'static,
@ -190,14 +152,6 @@ impl Database {
} }
} }
fn get_by_project_id_session_id_file_path_stmt<'conn>(
tx: &'conn rusqlite::Transaction,
) -> Result<rusqlite::CachedStatement<'conn>> {
Ok(tx.prepare_cached(
"SELECT `timestamp_ms`, `operations` FROM `deltas` WHERE `project_id` = :project_id AND `session_id` = :session_id AND `file_path` = :file_path",
)?)
}
fn get_by_rowid_stmt<'conn>( fn get_by_rowid_stmt<'conn>(
tx: &'conn rusqlite::Transaction, tx: &'conn rusqlite::Transaction,
) -> Result<rusqlite::CachedStatement<'conn>> { ) -> Result<rusqlite::CachedStatement<'conn>> {
@ -256,20 +210,6 @@ mod tests {
.collect() .collect()
); );
assert_eq!(
database.get_by_project_id_session_id_file_path(project_id, session_id, file_path)?,
Some(delta1)
);
assert_eq!(
database.get_by_project_id_session_id_file_path(
project_id,
session_id,
"other_file_path"
)?,
None
);
Ok(()) Ok(())
} }
@ -303,11 +243,6 @@ mod tests {
.collect() .collect()
); );
assert_eq!(
database.get_by_project_id_session_id_file_path(project_id, session_id, file_path)?,
Some(delta2)
);
Ok(()) Ok(())
} }
} }

View File

@ -424,36 +424,6 @@ impl Repository {
Ok(sessions::SessionsIterator::new(&self.git_repository)?) Ok(sessions::SessionsIterator::new(&self.git_repository)?)
} }
pub fn get_session(&self, session_id: &str) -> Result<sessions::Session> {
if let Some(oid) = sessions::get_hash_mapping(session_id) {
let commit = self.git_repository.find_commit(oid)?;
let reader = reader::CommitReader::from_commit(&self.git_repository, commit)?;
return Ok(sessions::Session::try_from(reader)?);
}
if let Some(session) = self.get_current_session()? {
if session.id == session_id {
return Ok(session);
}
}
let mut session_ids_iterator = sessions::SessionsIdsIterator::new(&self.git_repository)?;
while let Some(ids) = session_ids_iterator.next() {
match ids {
Result::Ok((oid, sid)) => {
if sid == session_id {
let commit = self.git_repository.find_commit(oid)?;
let reader =
reader::CommitReader::from_commit(&self.git_repository, commit)?;
return Ok(sessions::Session::try_from(reader)?);
}
}
Err(e) => return Err(e),
}
}
Err(anyhow!("session not found"))
}
pub fn get_current_session(&self) -> Result<Option<sessions::Session>> { pub fn get_current_session(&self) -> Result<Option<sessions::Session>> {
let reader = reader::DirReader::open(self.root()); let reader = reader::DirReader::open(self.root());
match sessions::Session::try_from(reader) { match sessions::Session::try_from(reader) {

View File

@ -1,16 +0,0 @@
use std::{collections::HashMap, sync::RwLock};
lazy_static! {
static ref MAPPING: RwLock<HashMap<String, git2::Oid>> = RwLock::new(HashMap::new());
}
pub fn set_hash_mapping(session_id: &str, hash: &git2::Oid) {
MAPPING
.write()
.unwrap()
.insert(session_id.to_string(), hash.clone());
}
pub fn get_hash_mapping(hash: &str) -> Option<git2::Oid> {
MAPPING.read().unwrap().get(hash).cloned()
}

View File

@ -1,8 +1,8 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use crate::reader::{CommitReader, Reader}; use crate::reader::CommitReader;
use super::{cache, Session, SessionError}; use super::{Session, SessionError};
pub struct SessionsIterator<'iterator> { pub struct SessionsIterator<'iterator> {
git_repository: &'iterator git2::Repository, git_repository: &'iterator git2::Repository,
@ -58,7 +58,6 @@ impl<'iterator> Iterator for SessionsIterator<'iterator> {
Err(SessionError::NoSession) => return None, Err(SessionError::NoSession) => return None,
Err(err) => return Some(Err(err.into())), Err(err) => return Some(Err(err.into())),
}; };
cache::set_hash_mapping(&session.id, &oid);
Some(Ok(session)) Some(Ok(session))
} }
Some(Err(err)) => Some(Err(err.into())), Some(Err(err)) => Some(Err(err.into())),
@ -66,66 +65,3 @@ impl<'iterator> Iterator for SessionsIterator<'iterator> {
} }
} }
} }
pub struct SessionsIdsIterator<'iterator> {
git_repository: &'iterator git2::Repository,
iter: git2::Revwalk<'iterator>,
}
impl<'iterator> SessionsIdsIterator<'iterator> {
pub(crate) fn new(git_repository: &'iterator git2::Repository) -> Result<Self> {
let mut iter = git_repository
.revwalk()
.context("failed to create revwalk")?;
iter.set_sorting(git2::Sort::TOPOLOGICAL | git2::Sort::TIME)
.context("failed to set sorting")?;
let mut branches = git_repository.branches(None)?;
while let Some(branch) = branches.next() {
let (branch, _) = branch.context("failed to get branch")?;
iter.push(branch.get().peel_to_commit()?.id())
.with_context(|| format!("failed to push branch {:?}", branch.name()))?;
}
Ok(Self {
git_repository,
iter,
})
}
}
impl<'iterator> Iterator for SessionsIdsIterator<'iterator> {
type Item = Result<(git2::Oid, String)>;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
Some(Result::Ok(oid)) => {
let commit = match self.git_repository.find_commit(oid) {
Result::Ok(commit) => commit,
Err(err) => return Some(Err(err.into())),
};
if commit.parent_count() == 0 {
// skip initial commit, as it's impossible to get a list of files from it
// it's only used to bootstrap the history
return self.next();
}
let commit_reader = match CommitReader::from_commit(self.git_repository, commit) {
Result::Ok(commit_reader) => commit_reader,
Err(err) => return Some(Err(err)),
};
match commit_reader.read_to_string("session/meta/id") {
Ok(sid) => {
cache::set_hash_mapping(&sid, &oid);
Some(Ok((oid, sid)))
}
Err(e) => Some(Err(e.into())),
}
}
Some(Err(err)) => Some(Err(err.into())),
None => None,
}
}
}

View File

@ -1,4 +1,3 @@
mod cache;
mod database; mod database;
mod iterator; mod iterator;
mod reader; mod reader;
@ -8,9 +7,8 @@ mod writer;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub use cache::get_hash_mapping; pub use database::Database;
pub use iterator::{SessionsIdsIterator, SessionsIterator}; pub use iterator::SessionsIterator;
pub use reader::SessionReader as Reader; pub use reader::SessionReader as Reader;
pub use session::{Meta, Session, SessionError}; pub use session::{Meta, Session, SessionError};
pub use writer::SessionWriter as Writer; pub use writer::SessionWriter as Writer;
pub use database::Database;

View File

@ -1,7 +1,5 @@
#[macro_use(defer)] #[macro_use(defer)]
extern crate scopeguard; extern crate scopeguard;
#[macro_use]
extern crate lazy_static;
mod app; mod app;
mod database; mod database;