From 27bce8f89c0613715ea40b60db2db305feea9de6 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 20 May 2019 19:17:03 -0700 Subject: rust impl release expansion Not very DRY. --- rust/src/entity_crud.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) (limited to 'rust') diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index 58a92433..f348e8dc 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -1002,7 +1002,6 @@ impl EntityCrud for FileEntity { generic_db_get!(file_ident, file_rev); generic_db_get_rev!(file_rev); - generic_db_expand!(); generic_db_create!(file_ident, file_edit); generic_db_create_batch!(file_ident, file_edit); generic_db_update!(file_ident, file_edit); @@ -1027,6 +1026,7 @@ impl EntityCrud for FileEntity { urls: None, mimetype: None, release_ids: None, + releases: None, state: Some(ident_row.state().unwrap().shortname()), ident: Some(FatcatId::from_uuid(&ident_row.id).to_string()), revision: ident_row.rev_id.map(|u| u.to_string()), @@ -1038,6 +1038,32 @@ impl EntityCrud for FileEntity { }) } + fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()> { + // Don't expand deleted entities + if self.state == Some("deleted".to_string()) { + return Ok(()); + } + // Could potentially hit this code path when expanding a redirected entity or bare + // revision. In either case, `self.release_ids` should already be set. + if expand.releases && self.release_ids.is_some() { + if let Some(release_ids) = &self.release_ids { + self.releases = Some( + release_ids + .iter() + .map(|release_id| { + Ok(ReleaseEntity::db_get( + conn, + FatcatId::from_str(release_id)?, + HideFlags::none(), + )?) + }) + .collect::>>()?, + ); + }; + } + Ok(()) + } + fn db_from_row( conn: &DbConn, rev_row: Self::RevRow, @@ -1078,6 +1104,7 @@ impl EntityCrud for FileEntity { urls: Some(urls), mimetype: rev_row.mimetype, release_ids: Some(release_ids.iter().map(|fcid| fcid.to_string()).collect()), + releases: None, state, ident: ident_id, revision: Some(rev_row.id.to_string()), @@ -1179,7 +1206,6 @@ impl EntityCrud for FilesetEntity { generic_db_get!(fileset_ident, fileset_rev); generic_db_get_rev!(fileset_rev); - generic_db_expand!(); generic_db_create!(fileset_ident, fileset_edit); generic_db_create_batch!(fileset_ident, fileset_edit); generic_db_update!(fileset_ident, fileset_edit); @@ -1200,6 +1226,7 @@ impl EntityCrud for FilesetEntity { manifest: None, urls: None, release_ids: None, + releases: None, state: Some(ident_row.state().unwrap().shortname()), ident: Some(FatcatId::from_uuid(&ident_row.id).to_string()), revision: ident_row.rev_id.map(|u| u.to_string()), @@ -1211,6 +1238,32 @@ impl EntityCrud for FilesetEntity { }) } + fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()> { + // Don't expand deleted entities + if self.state == Some("deleted".to_string()) { + return Ok(()); + } + // Could potentially hit this code path when expanding a redirected entity or bare + // revision. In either case, `self.release_ids` should already be set. + if expand.releases && self.release_ids.is_some() { + if let Some(release_ids) = &self.release_ids { + self.releases = Some( + release_ids + .iter() + .map(|release_id| { + Ok(ReleaseEntity::db_get( + conn, + FatcatId::from_str(release_id)?, + HideFlags::none(), + )?) + }) + .collect::>>()?, + ); + }; + } + Ok(()) + } + fn db_from_row( conn: &DbConn, rev_row: Self::RevRow, @@ -1261,6 +1314,7 @@ impl EntityCrud for FilesetEntity { manifest: Some(manifest), urls: Some(urls), release_ids: Some(release_ids.iter().map(|fcid| fcid.to_string()).collect()), + releases: None, state, ident: ident_id, revision: Some(rev_row.id.to_string()), @@ -1387,7 +1441,6 @@ impl EntityCrud for WebcaptureEntity { generic_db_get!(webcapture_ident, webcapture_rev); generic_db_get_rev!(webcapture_rev); - generic_db_expand!(); generic_db_create!(webcapture_ident, webcapture_edit); generic_db_create_batch!(webcapture_ident, webcapture_edit); generic_db_update!(webcapture_ident, webcapture_edit); @@ -1410,6 +1463,7 @@ impl EntityCrud for WebcaptureEntity { original_url: None, timestamp: None, release_ids: None, + releases: None, state: Some(ident_row.state().unwrap().shortname()), ident: Some(FatcatId::from_uuid(&ident_row.id).to_string()), revision: ident_row.rev_id.map(|u| u.to_string()), @@ -1421,6 +1475,32 @@ impl EntityCrud for WebcaptureEntity { }) } + fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()> { + // Don't expand deleted entities + if self.state == Some("deleted".to_string()) { + return Ok(()); + } + // Could potentially hit this code path when expanding a redirected entity or bare + // revision. In either case, `self.release_ids` should already be set. + if expand.releases && self.release_ids.is_some() { + if let Some(release_ids) = &self.release_ids { + self.releases = Some( + release_ids + .iter() + .map(|release_id| { + Ok(ReleaseEntity::db_get( + conn, + FatcatId::from_str(release_id)?, + HideFlags::none(), + )?) + }) + .collect::>>()?, + ); + }; + } + Ok(()) + } + fn db_from_row( conn: &DbConn, rev_row: Self::RevRow, @@ -1475,6 +1555,7 @@ impl EntityCrud for WebcaptureEntity { original_url: Some(rev_row.original_url), timestamp: Some(chrono::DateTime::from_utc(rev_row.timestamp, chrono::Utc)), release_ids: Some(release_ids.iter().map(|fcid| fcid.to_string()).collect()), + releases: None, state, ident: ident_id, revision: Some(rev_row.id.to_string()), -- cgit v1.2.3