diff --git a/pkg/assembler/backends/ent/backend/hasMetadata.go b/pkg/assembler/backends/ent/backend/hasMetadata.go index dc7860086f..fac61010ff 100644 --- a/pkg/assembler/backends/ent/backend/hasMetadata.go +++ b/pkg/assembler/backends/ent/backend/hasMetadata.go @@ -28,6 +28,7 @@ import ( "github.com/guacsec/guac/pkg/assembler/graphql/model" "github.com/pkg/errors" "github.com/vektah/gqlparser/v2/gqlerror" + "golang.org/x/sync/errgroup" ) func (b *EntBackend) HasMetadata(ctx context.Context, filter *model.HasMetadataSpec) ([]*model.HasMetadata, error) { @@ -59,8 +60,11 @@ func (b *EntBackend) IngestHasMetadata(ctx context.Context, subject model.Packag } func (b *EntBackend) IngestBulkHasMetadata(ctx context.Context, subjects model.PackageSourceOrArtifactInputs, pkgMatchType *model.MatchFlags, hasMetadataList []*model.HasMetadataInputSpec) ([]string, error) { - var results []string + var results = make([]string, len(hasMetadataList)) + eg, ctx := errgroup.WithContext(ctx) for i := range hasMetadataList { + index := i + hmSpec := *hasMetadataList[i] var subject model.PackageSourceOrArtifactInput if len(subjects.Packages) > 0 { subject = model.PackageSourceOrArtifactInput{Package: subjects.Packages[i]} @@ -69,11 +73,18 @@ func (b *EntBackend) IngestBulkHasMetadata(ctx context.Context, subjects model.P } else { subject = model.PackageSourceOrArtifactInput{Source: subjects.Sources[i]} } - hm, err := b.IngestHasMetadata(ctx, subject, pkgMatchType, *hasMetadataList[i]) - if err != nil { - return nil, gqlerror.Errorf("IngestBulkHasMetadata failed with element #%v with err: %v", i, err) - } - results = append(results, hm.ID) + concurrently(eg, func() error { + hm, err := b.IngestHasMetadata(ctx, subject, pkgMatchType, hmSpec) + if err == nil { + results[index] = hm.ID + return err + } else { + return gqlerror.Errorf("IngestBulkHasMetadata failed with element #%v %+v with err: %v", i, *subject.Package, err) + } + }) + } + if err := eg.Wait(); err != nil { + return nil, err } return results, nil } @@ -115,7 +126,6 @@ func upsertHasMetadata(ctx context.Context, client *ent.Tx, subject model.Packag SetCollector(spec.Collector) conflictColumns := []string{ - hasmetadata.FieldTimestamp, hasmetadata.FieldKey, hasmetadata.FieldValue, hasmetadata.FieldJustification, @@ -253,7 +263,6 @@ func hasMetadataInputPredicate(subject model.PackageSourceOrArtifactInput, pkgMa } return hasMetadataPredicate(&model.HasMetadataSpec{ Subject: subjectSpec, - Since: &filter.Timestamp, Key: &filter.Key, Value: &filter.Value, Justification: &filter.Justification, diff --git a/pkg/assembler/backends/ent/backend/hasMetadata_test.go b/pkg/assembler/backends/ent/backend/hasMetadata_test.go index c0c793c6cc..04aab9194f 100644 --- a/pkg/assembler/backends/ent/backend/hasMetadata_test.go +++ b/pkg/assembler/backends/ent/backend/hasMetadata_test.go @@ -992,7 +992,7 @@ func (s *Suite) TestIngestBulkHasMetadata() { if err != nil { return } - if diff := cmp.Diff(test.ExpHM, got, ignoreID); diff != "" { + if diff := cmp.Diff(test.ExpHM, got, IngestPredicatesCmpOpts...); diff != "" { t.Errorf("Unexpected results. (-want +got):\n%s", diff) } }) diff --git a/pkg/assembler/backends/ent/migrate/schema.go b/pkg/assembler/backends/ent/migrate/schema.go index cb50258a2d..adddb8e519 100644 --- a/pkg/assembler/backends/ent/migrate/schema.go +++ b/pkg/assembler/backends/ent/migrate/schema.go @@ -468,33 +468,33 @@ var ( }, Indexes: []*schema.Index{ { - Name: "hasmetadata_timestamp_key_value_justification_origin_collector_source_id", + Name: "hasmetadata_key_value_justification_origin_collector_source_id", Unique: true, - Columns: []*schema.Column{HasMetadataColumns[1], HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[7]}, + Columns: []*schema.Column{HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[7]}, Annotation: &entsql.IndexAnnotation{ Where: "source_id IS NOT NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NULL", }, }, { - Name: "hasmetadata_timestamp_key_value_justification_origin_collector_package_version_id", + Name: "hasmetadata_key_value_justification_origin_collector_package_version_id", Unique: true, - Columns: []*schema.Column{HasMetadataColumns[1], HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[8]}, + Columns: []*schema.Column{HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[8]}, Annotation: &entsql.IndexAnnotation{ Where: "source_id IS NULL AND package_version_id IS NOT NULL AND package_name_id IS NULL AND artifact_id IS NULL", }, }, { - Name: "hasmetadata_timestamp_key_value_justification_origin_collector_package_name_id", + Name: "hasmetadata_key_value_justification_origin_collector_package_name_id", Unique: true, - Columns: []*schema.Column{HasMetadataColumns[1], HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[9]}, + Columns: []*schema.Column{HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[9]}, Annotation: &entsql.IndexAnnotation{ Where: "source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NOT NULL AND artifact_id IS NULL", }, }, { - Name: "hasmetadata_timestamp_key_value_justification_origin_collector_artifact_id", + Name: "hasmetadata_key_value_justification_origin_collector_artifact_id", Unique: true, - Columns: []*schema.Column{HasMetadataColumns[1], HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[10]}, + Columns: []*schema.Column{HasMetadataColumns[2], HasMetadataColumns[3], HasMetadataColumns[4], HasMetadataColumns[5], HasMetadataColumns[6], HasMetadataColumns[10]}, Annotation: &entsql.IndexAnnotation{ Where: "source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NOT NULL", }, diff --git a/pkg/assembler/backends/ent/schema/hasmetadata.go b/pkg/assembler/backends/ent/schema/hasmetadata.go index 47c3c18f63..f19e17c0c9 100644 --- a/pkg/assembler/backends/ent/schema/hasmetadata.go +++ b/pkg/assembler/backends/ent/schema/hasmetadata.go @@ -56,9 +56,9 @@ func (HasMetadata) Edges() []ent.Edge { func (HasMetadata) Indexes() []ent.Index { return []ent.Index{ - index.Fields("timestamp", "key", "value", "justification", "origin", "collector", "source_id").Unique().Annotations(entsql.IndexWhere("source_id IS NOT NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NULL")), - index.Fields("timestamp", "key", "value", "justification", "origin", "collector", "package_version_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NOT NULL AND package_name_id IS NULL AND artifact_id IS NULL")), - index.Fields("timestamp", "key", "value", "justification", "origin", "collector", "package_name_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NOT NULL AND artifact_id IS NULL")), - index.Fields("timestamp", "key", "value", "justification", "origin", "collector", "artifact_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NOT NULL")), + index.Fields("key", "value", "justification", "origin", "collector", "source_id").Unique().Annotations(entsql.IndexWhere("source_id IS NOT NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NULL")), + index.Fields("key", "value", "justification", "origin", "collector", "package_version_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NOT NULL AND package_name_id IS NULL AND artifact_id IS NULL")), + index.Fields("key", "value", "justification", "origin", "collector", "package_name_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NOT NULL AND artifact_id IS NULL")), + index.Fields("key", "value", "justification", "origin", "collector", "artifact_id").Unique().Annotations(entsql.IndexWhere("source_id IS NULL AND package_version_id IS NULL AND package_name_id IS NULL AND artifact_id IS NOT NULL")), } }