move all reimport db writes into a transaction

feat/vaults
Tomáš Mládek 2020-09-20 17:17:43 +02:00
parent e0a03b30fa
commit 9823f646cd
1 changed files with 30 additions and 27 deletions

View File

@ -5,7 +5,7 @@ use crate::database::{
};
use crate::hash::{ComputeHash, HasherWorker};
use crate::models;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Error, Result};
use log::{error, info, trace};
use serde::export::Formatter;
use serde_json::Value;
@ -363,6 +363,19 @@ async fn _process_directory_entry<C: Connection<Backend = Sqlite>, P: AsRef<Path
.collect::<Vec<Component>>();
let (filename, dir_path) = components.split_last().unwrap();
let upath = UPath(
iter::once(UDirectory {
name: "NATIVE".to_string(),
})
.chain(dir_path.iter().map(|component| UDirectory {
name: component.as_os_str().to_string_lossy().to_string(),
}))
.collect(),
);
let resolved_path = resolve_path(connection, &upath, true).await?;
let parent_dir = resolved_path.last().unwrap();
connection.transaction::<_, Error, _>(|| {
let file_address = Address::UUID(Uuid::new_v4());
let name_entry = InnerEntry {
@ -382,17 +395,6 @@ async fn _process_directory_entry<C: Connection<Backend = Sqlite>, P: AsRef<Path
let _ = insert_entry(connection, identity_entry)?;
let upath = UPath(
iter::once(UDirectory {
name: "NATIVE".to_string(),
})
.chain(dir_path.iter().map(|component| UDirectory {
name: component.as_os_str().to_string_lossy().to_string(),
}))
.collect(),
);
let resolved_path = resolve_path(connection, &upath, true).await?;
let parent_dir = resolved_path.last().unwrap();
let dir_has_entry = InnerEntry {
target: parent_dir.clone(),
key: DIR_HAS_KEY.to_string(),
@ -401,6 +403,7 @@ async fn _process_directory_entry<C: Connection<Backend = Sqlite>, P: AsRef<Path
let _ = insert_entry(connection, dir_has_entry)?;
Ok(())
})
}
#[cfg(test)]