Improve order of execution for adding tags

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/5/head
trivernis 3 years ago
parent 31addcda87
commit e9641e604e
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Debug; use std::fmt::Debug;
use std::iter::FromIterator;
use std::path::PathBuf; use std::path::PathBuf;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
@ -9,14 +9,14 @@ use sea_orm::DatabaseConnection;
use mediarepo_core::error::RepoResult; use mediarepo_core::error::RepoResult;
use mediarepo_core::fs::file_hash_store::FileHashStore; use mediarepo_core::fs::file_hash_store::FileHashStore;
use mediarepo_core::fs::thumbnail_store::ThumbnailStore; use mediarepo_core::fs::thumbnail_store::ThumbnailStore;
use mediarepo_core::itertools::Itertools;
use mediarepo_core::utils::parse_namespace_and_tag; use mediarepo_core::utils::parse_namespace_and_tag;
use mediarepo_database::get_database; use mediarepo_database::get_database;
use mediarepo_database::queries::analysis::{get_all_counts, Counts}; use mediarepo_database::queries::analysis::{get_all_counts, Counts};
use crate::dao::tag::by_name::TagByNameQuery;
use crate::dao::{DaoContext, DaoProvider}; use crate::dao::{DaoContext, DaoProvider};
use crate::namespace::Namespace; use crate::namespace::Namespace;
use crate::tag::Tag; use crate::tag::Tag;

@ -21,8 +21,13 @@ impl TagDao {
let existing_tag_map: HashMap<String, TagDto> = let existing_tag_map: HashMap<String, TagDto> =
HashMap::from_iter(existing_tags.into_iter().map(|t| (t.normalized_name(), t))); HashMap::from_iter(existing_tags.into_iter().map(|t| (t.normalized_name(), t)));
let namespace_map = add_or_get_all_namespaces(&trx, namespaces).await?;
tags.retain(|dto| !existing_tag_map.contains_key(&dto.normalized_name())); tags.retain(|dto| !existing_tag_map.contains_key(&dto.normalized_name()));
let namespace_map = add_or_get_all_namespaces(&trx, namespaces).await?;
if tags.is_empty() {
return Ok(existing_tag_map.into_values().collect());
}
let tag_models: Vec<tag::ActiveModel> = tags let tag_models: Vec<tag::ActiveModel> = tags
.iter() .iter()
.map(|t| tag::ActiveModel { .map(|t| tag::ActiveModel {
@ -38,7 +43,7 @@ impl TagDao {
tag::Entity::insert_many(tag_models).exec(&trx).await?; tag::Entity::insert_many(tag_models).exec(&trx).await?;
let mut tag_dtos = tags_by_name(&trx, tags).await?; let mut tag_dtos = tags_by_name(&trx, tags).await?;
trx.commit().await?; trx.commit().await?;
tag_dtos.append(&mut existing_tag_map.into_iter().map(|(_, dto)| dto).collect()); tag_dtos.append(&mut existing_tag_map.into_values().collect());
Ok(tag_dtos) Ok(tag_dtos)
} }
@ -48,6 +53,9 @@ async fn add_or_get_all_namespaces(
trx: &DatabaseTransaction, trx: &DatabaseTransaction,
mut namespaces: Vec<String>, mut namespaces: Vec<String>,
) -> RepoResult<HashMap<String, NamespaceDto>> { ) -> RepoResult<HashMap<String, NamespaceDto>> {
if namespaces.is_empty() {
return Ok(HashMap::with_capacity(0));
}
let existing_namespaces = namespaces_by_name(trx, namespaces.clone()).await?; let existing_namespaces = namespaces_by_name(trx, namespaces.clone()).await?;
let mut namespace_map = HashMap::from_iter( let mut namespace_map = HashMap::from_iter(
existing_namespaces existing_namespaces
@ -58,6 +66,9 @@ async fn add_or_get_all_namespaces(
return Ok(namespace_map); return Ok(namespace_map);
} }
namespaces.retain(|nsp| !namespace_map.contains_key(nsp)); namespaces.retain(|nsp| !namespace_map.contains_key(nsp));
if namespaces.is_empty() {
return Ok(namespace_map);
}
let namespace_models: Vec<namespace::ActiveModel> = namespaces let namespace_models: Vec<namespace::ActiveModel> = namespaces
.iter() .iter()
.map(|nsp| namespace::ActiveModel { .map(|nsp| namespace::ActiveModel {
@ -81,6 +92,9 @@ async fn namespaces_by_name(
trx: &DatabaseTransaction, trx: &DatabaseTransaction,
names: Vec<String>, names: Vec<String>,
) -> RepoResult<Vec<NamespaceDto>> { ) -> RepoResult<Vec<NamespaceDto>> {
if names.is_empty() {
return Ok(vec![]);
}
let namespaces: Vec<NamespaceDto> = namespace::Entity::find() let namespaces: Vec<NamespaceDto> = namespace::Entity::find()
.filter(namespace::Column::Name.is_in(names)) .filter(namespace::Column::Name.is_in(names))
.all(trx) .all(trx)
@ -93,6 +107,9 @@ async fn namespaces_by_name(
} }
async fn tags_by_name(trx: &DatabaseTransaction, tags: Vec<AddTagDto>) -> RepoResult<Vec<TagDto>> { async fn tags_by_name(trx: &DatabaseTransaction, tags: Vec<AddTagDto>) -> RepoResult<Vec<TagDto>> {
if tags.is_empty() {
return Ok(vec![]);
}
let condition = tags let condition = tags
.into_iter() .into_iter()
.map(build_tag_condition) .map(build_tag_condition)

@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::iter::FromIterator; use std::iter::FromIterator;
use mediarepo_core::error::RepoResult; use mediarepo_core::error::RepoResult;
use mediarepo_core::mediarepo_api::types::filtering::TagQuery;
use mediarepo_core::utils::parse_namespace_and_tag; use mediarepo_core::utils::parse_namespace_and_tag;
use mediarepo_database::entities::{content_descriptor, content_descriptor_tag, namespace, tag}; use mediarepo_database::entities::{content_descriptor, content_descriptor_tag, namespace, tag};

Loading…
Cancel
Save