Skip to content

Commit

Permalink
Ent - HasMetadata: applied concurrent approach (#1458)
Browse files Browse the repository at this point in the history
Signed-off-by: mrizzi <[email protected]>
  • Loading branch information
mrizzi authored Nov 2, 2023
1 parent 9faa6de commit 2a9a787
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
25 changes: 17 additions & 8 deletions pkg/assembler/backends/ent/backend/hasMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]}
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/assembler/backends/ent/backend/hasMetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
16 changes: 8 additions & 8 deletions pkg/assembler/backends/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/assembler/backends/ent/schema/hasmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
}
}

0 comments on commit 2a9a787

Please sign in to comment.