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 15, 2025
1 parent 88fa55c commit 4feb467
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 8 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
141 changes: 141 additions & 0 deletions migration/src/m0000830_perf_indexes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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(VersionedPurl::Table)
.name(Indexes::VersionedPurlBasePurlIdIDX.to_string())
.col(VersionedPurl::BasePurlId)
.to_owned(),
)
.await?;
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?;
manager
.drop_index(
Index::drop()
.if_exists()
.table(VersionedPurl::Table)
.name(Indexes::VersionedPurlBasePurlIdIDX.to_string())
.to_owned(),
)
.await?;
Ok(())
}
}

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

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

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

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

#[derive(DeriveIden)]
enum SbomPackagePurlRef {
Table,
SbomId,
NodeId,
}
18 changes: 10 additions & 8 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 @@ -149,7 +150,7 @@ impl SbomDetails {
JOIN "version_range" ON "product_version_range"."version_range_id" = "version_range"."id" AND version_matches("product_version"."version", "version_range".*)
-- now find matching purls in these statuses
JOIN base_purl ON "product_status"."package" LIKE CONCAT("base_purl"."namespace", '/', "base_purl"."name") OR "product_status"."package" = "base_purl"."name"
JOIN base_purl ON product_status.package = base_purl.name OR product_status.package LIKE CONCAT(base_purl.namespace, '/%')
JOIN "versioned_purl" ON "versioned_purl"."base_purl_id" = "base_purl"."id"
JOIN "qualified_purl" ON "qualified_purl"."versioned_purl_id" = "versioned_purl"."id"
join sbom_package_purl_ref ON sbom_package_purl_ref.qualified_purl_id = qualified_purl.id AND sbom_package_purl_ref.sbom_id = sbom.sbom_id
Expand All @@ -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 4feb467

Please sign in to comment.