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,25 +363,6 @@ async fn _process_directory_entry<C: Connection<Backend = Sqlite>, P: AsRef<Path
.collect::<Vec<Component>>();
let (filename, dir_path) = components.split_last().unwrap();
let file_address = Address::UUID(Uuid::new_v4());
let name_entry = InnerEntry {
target: file_address.clone(),
key: FILENAME_KEY.to_string(),
value: EntryValue::Value(Value::String(
filename.as_os_str().to_string_lossy().to_string(),
)),
};
let _ = insert_entry(connection, name_entry)?;
let identity_entry = InnerEntry {
target: file_address.clone(),
key: FILE_IDENTITY_KEY.to_string(),
value: EntryValue::Address(Address::Hash(digest.clone())),
};
let _ = insert_entry(connection, identity_entry)?;
let upath = UPath(
iter::once(UDirectory {
name: "NATIVE".to_string(),
@ -393,14 +374,36 @@ async fn _process_directory_entry<C: Connection<Backend = Sqlite>, P: AsRef<Path
);
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(),
value: EntryValue::Address(file_address),
};
let _ = insert_entry(connection, dir_has_entry)?;
Ok(())
connection.transaction::<_, Error, _>(|| {
let file_address = Address::UUID(Uuid::new_v4());
let name_entry = InnerEntry {
target: file_address.clone(),
key: FILENAME_KEY.to_string(),
value: EntryValue::Value(Value::String(
filename.as_os_str().to_string_lossy().to_string(),
)),
};
let _ = insert_entry(connection, name_entry)?;
let identity_entry = InnerEntry {
target: file_address.clone(),
key: FILE_IDENTITY_KEY.to_string(),
value: EntryValue::Address(Address::Hash(digest.clone())),
};
let _ = insert_entry(connection, identity_entry)?;
let dir_has_entry = InnerEntry {
target: parent_dir.clone(),
key: DIR_HAS_KEY.to_string(),
value: EntryValue::Address(file_address),
};
let _ = insert_entry(connection, dir_has_entry)?;
Ok(())
})
}
#[cfg(test)]