Skip to content

Commit

Permalink
perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JimFuller-RedHat committed Jan 14, 2025
1 parent 88fa55c commit 8e34db1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod m0000790_alter_sbom_alter_document_id;
mod m0000800_alter_product_version_range_scheme;
mod m0000810_fix_get_purl;
mod m0000820_create_conversation;
mod m0000830_perf_indexes;

pub struct Migrator;

Expand Down Expand Up @@ -205,6 +206,7 @@ impl MigratorTrait for Migrator {
Box::new(m0000800_alter_product_version_range_scheme::Migration),
Box::new(m0000810_fix_get_purl::Migration),
Box::new(m0000820_create_conversation::Migration),
Box::new(m0000830_perf_indexes::Migration),
]
}
}
Expand Down
116 changes: 116 additions & 0 deletions migration/src/m0000830_perf_indexes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
#[allow(deprecated)]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.table(PurlStatus::Table)
.name(Indexes::PurlStatusBasePurlIdIDX.to_string())
.col(PurlStatus::BasePurlId)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(Status::Table)
.name(Indexes::StatusSlugPartialIDX.to_string())
.col(Status::Slug)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(SbomPackagePurlRef::Table)
.name(Indexes::SbomPackagePurlRefSbomIdIDX.to_string())
.col(SbomPackagePurlRef::SbomId)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(SbomPackagePurlRef::Table)
.name(Indexes::SbomPackagePurlRefNodeIdIDX.to_string())
.col(SbomPackagePurlRef::NodeId)
.to_owned(),
)
.await?;
Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(
Index::drop()
.if_exists()
.table(SbomPackagePurlRef::Table)
.name(Indexes::SbomPackagePurlRefNodeIdIDX.to_string())
.to_owned(),
)
.await?;
manager
.drop_index(
Index::drop()
.if_exists()
.table(SbomPackagePurlRef::Table)
.name(Indexes::SbomPackagePurlRefSbomIdIDX.to_string())
.to_owned(),
)
.await?;
manager
.drop_index(
Index::drop()
.if_exists()
.table(Status::Table)
.name(Indexes::StatusSlugPartialIDX.to_string())
.to_owned(),
)
.await?;
manager
.drop_index(
Index::drop()
.if_exists()
.table(PurlStatus::Table)
.name(Indexes::PurlStatusBasePurlIdIDX.to_string())
.to_owned(),
)
.await?;
Ok(())
}
}

#[allow(clippy::enum_variant_names)]
#[derive(DeriveIden)]
enum Indexes {
PurlStatusBasePurlIdIDX,
StatusSlugPartialIDX,
SbomPackagePurlRefSbomIdIDX,
SbomPackagePurlRefNodeIdIDX,
}

#[derive(DeriveIden)]
enum PurlStatus {
Table,
BasePurlId,
}

#[derive(DeriveIden)]
enum Status {
Table,
Slug,
}

#[derive(DeriveIden)]
enum SbomPackagePurlRef {
Table,
SbomId,
NodeId,
}
16 changes: 9 additions & 7 deletions modules/fundamental/src/sbom/model/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ impl SbomDetails {
JoinType::LeftJoin,
qualified_purl::Relation::VersionedPurl.def(),
)
.join(JoinType::LeftJoin, versioned_purl::Relation::BasePurl.def())
.join(JoinType::Join, base_purl::Relation::PurlStatus.def())
.join(JoinType::Join, purl_status::Relation::Status.def())
.filter(Expr::col((status::Entity, status::Column::Slug)).eq("affected"))
.join(
JoinType::LeftJoin,
purl_status::Relation::VersionRange.def(),
)
.filter(SimpleExpr::FunctionCall(
Func::cust(VersionMatches)
.arg(Expr::col((
Expand All @@ -68,13 +76,6 @@ impl SbomDetails {
)))
.arg(Expr::col((version_range::Entity, Asterisk))),
))
.join(JoinType::LeftJoin, versioned_purl::Relation::BasePurl.def())
.join(JoinType::Join, base_purl::Relation::PurlStatus.def())
.join(JoinType::Join, purl_status::Relation::Status.def())
.join(
JoinType::LeftJoin,
purl_status::Relation::VersionRange.def(),
)
.join(JoinType::LeftJoin, purl_status::Relation::ContextCpe.def())
.join(JoinType::Join, purl_status::Relation::Advisory.def())
.join(JoinType::Join, purl_status::Relation::Vulnerability.def())
Expand Down Expand Up @@ -162,6 +163,7 @@ impl SbomDetails {
JOIN "vulnerability" ON "product_status"."vulnerability_id" = "vulnerability"."id"
WHERE
"sbom"."sbom_id" = $1
AND "status"."slug" = 'affected'
"#;

let result: Vec<QueryResult> = tx
Expand Down

0 comments on commit 8e34db1

Please sign in to comment.