From 98d846827d32f8dc0fa2f1a31df66e70e5961813 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 18 Apr 2023 01:26:27 +0800 Subject: [PATCH] job parent id fix (#714) --- core/src/job/job_manager.rs | 17 ++++++--- core/src/location/file_path_helper.rs | 54 +++++++++++++-------------- core/src/util/db.rs | 14 +++++++ 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/core/src/job/job_manager.rs b/core/src/job/job_manager.rs index fe9106bea..7067094fc 100644 --- a/core/src/job/job_manager.rs +++ b/core/src/job/job_manager.rs @@ -17,6 +17,7 @@ use crate::{ validation::validator_job::ObjectValidatorJob, }, prisma::{job, node}, + util, }; use std::{ @@ -455,12 +456,16 @@ impl JobReport { self.name.clone(), JobStatus::Running as i32, node::id::equals(library.node_local_id), - vec![ - job::data::set(self.data.clone()), - job::parent_id::set(self.parent_id.map(|id| id.as_bytes().to_vec())), - job::date_created::set(now.into()), - job::date_modified::set(now.into()), - ], + util::db::chain_optional_iter( + [ + job::data::set(self.data.clone()), + job::date_created::set(now.into()), + job::date_modified::set(now.into()), + ], + [self + .parent_id + .map(|id| job::parent::connect(job::id::equals(id.as_bytes().to_vec())))], + ), ) .exec() .await?; diff --git a/core/src/location/file_path_helper.rs b/core/src/location/file_path_helper.rs index 3d2ac4ff3..7670c838d 100644 --- a/core/src/location/file_path_helper.rs +++ b/core/src/location/file_path_helper.rs @@ -330,7 +330,7 @@ impl LastFilePathIdManager { ) -> Result { // Keeping a reference in that map for the entire duration of the function, so we keep it locked - use crate::sync; + use crate::{sync, util}; use serde_json::json; let mut last_id_ref = match self.last_id_by_location.entry(location_id) { @@ -351,33 +351,31 @@ impl LastFilePathIdManager { let next_id = *last_id_ref + 1; - let params = [ - ("cas_id", json!(cas_id)), - ("materialized_path", json!(materialized_path)), - ("name", json!(name)), - ("extension", json!(extension)), - ("size_in_bytes", json!(metadata.size_in_bytes.to_string())), - ("inode", json!(metadata.inode.to_le_bytes())), - ("device", json!(metadata.device.to_le_bytes())), - ("is_dir", json!(is_dir)), - ("date_created", json!(metadata.created_at)), - ("date_modified", json!(metadata.modified_at)), - ] - .into_iter() - .map(Some) - .chain([parent_id.map(|parent_id| { - ( - "parent_id", - json!(sync::file_path::SyncId { - location: sync::location::SyncId { - pub_id: location.pub_id.clone() - }, - id: parent_id - }), - ) - })]) - .flatten() - .collect::>(); + let params = util::db::chain_optional_iter( + [ + ("cas_id", json!(cas_id)), + ("materialized_path", json!(materialized_path)), + ("name", json!(name)), + ("extension", json!(extension)), + ("size_in_bytes", json!(metadata.size_in_bytes.to_string())), + ("inode", json!(metadata.inode.to_le_bytes())), + ("device", json!(metadata.device.to_le_bytes())), + ("is_dir", json!(is_dir)), + ("date_created", json!(metadata.created_at)), + ("date_modified", json!(metadata.modified_at)), + ], + [parent_id.map(|parent_id| { + ( + "parent_id", + json!(sync::file_path::SyncId { + location: sync::location::SyncId { + pub_id: location.pub_id.clone() + }, + id: parent_id + }), + ) + })], + ); let created_path = sync .write_op( diff --git a/core/src/util/db.rs b/core/src/util/db.rs index fab8524d5..aaab50785 100644 --- a/core/src/util/db.rs +++ b/core/src/util/db.rs @@ -71,3 +71,17 @@ pub async fn write_storedkey_to_db( Ok(()) } + +/// Combines an iterator of `T` and an iterator of `Option`, +/// removing any `None` values in the process +pub fn chain_optional_iter( + required: impl IntoIterator, + optional: impl IntoIterator>, +) -> Vec { + required + .into_iter() + .map(Some) + .chain(optional) + .flatten() + .collect() +}