From 5c3066767e8e5c8875fa4ddfe656362c116250ad Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Mon, 18 Dec 2023 12:37:32 +0600 Subject: [PATCH] refactor: search by artifactID returns indexes --- pkg/db/db.go | 33 +++++++++++++-------------------- pkg/db/db_test.go | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index 2dc05d7..ea40ce2 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -174,33 +174,26 @@ func (db *DB) SelectIndexByArtifactIDAndGroupID(artifactID, groupID string) (typ return index, nil } -func (db *DB) SelectGroupIDByArtifactIDVersionAndFileType(artifactID, version string, fileType types.ArchiveType) (string, - error) { - var groupID string - var count int +// SelectIndexesByArtifactIDAndFileType returns all indexes for `artifactID` + `fileType` if `version` exists for them +func (db *DB) SelectIndexesByArtifactIDAndFileType(artifactID, version string, fileType types.ArchiveType) ([]types.Index, error) { + var indexes []types.Index rows, err := db.client.Query(` - SELECT relevant.group_id, COUNT(relevant.group_id) as count + SELECT f_id.group_id, f_id.artifact_id, i.version, i.sha1, i.archive_type FROM indices i - JOIN (SELECT a.id, a.group_id - FROM indices i + JOIN (SELECT a.id, a.group_id, a.artifact_id + FROM indices i JOIN artifacts a on a.id = i.artifact_id - WHERE a.artifact_id = ? AND i.version = ? AND i.archive_type = ?) relevant ON relevant.id = i.artifact_id - GROUP BY "group_id"`, + WHERE a.artifact_id = ? AND i.version = ? AND i.archive_type = ?) f_id ON f_id.id = i.artifact_id`, artifactID, version, fileType) if err != nil && !errors.Is(err, sql.ErrNoRows) { - return "", xerrors.Errorf("select indexes error: %w", err) + return nil, xerrors.Errorf("select indexes error: %w", err) } for rows.Next() { - var indexGroupID string - var indexCount int // Number of versions for current GroupID. - if err = rows.Scan(&indexGroupID, &indexCount); err != nil { - return "", xerrors.Errorf("scan row error: %w", err) - } - // Use GroupID with maximum number of versions. - if indexCount > count { - groupID = indexGroupID - count = indexCount + var index types.Index + if err = rows.Scan(&index.GroupID, &index.ArtifactID, &index.Version, &index.SHA1, &index.ArchiveType); err != nil { + return nil, xerrors.Errorf("scan row error: %w", err) } + indexes = append(indexes, index) } - return groupID, nil + return indexes, nil } diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 2f6998c..cc883f2 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -129,27 +129,33 @@ func TestSelectIndexByArtifactIDAndGroupID(t *testing.T) { } } -func TestSelectIndexesByArtifactIDVersionAndFileType(t *testing.T) { +func TestSelectIndexesByArtifactIDAndFileType(t *testing.T) { var tests = []struct { name string artifactID string version string archiveType types.ArchiveType - wantGroupID string + wantIndexes []types.Index }{ { name: "happy path some indexes found", artifactID: "jstl", version: "1.0", archiveType: types.JarType, - wantGroupID: "javax.servlet", + wantIndexes: []types.Index{ + indexJavaxServlet10, + indexJavaxServlet11, + indexJstl, + }, }, { name: "happy path one index found", artifactID: "jstl", version: "1.2_1", archiveType: types.JarType, - wantGroupID: "org.apache.geronimo.bundles", + wantIndexes: []types.Index{ + indexBundles, + }, }, { name: "there is no required version", @@ -178,10 +184,10 @@ func TestSelectIndexesByArtifactIDVersionAndFileType(t *testing.T) { }) require.NoError(t, err) - gotGroupID, err := dbc.SelectGroupIDByArtifactIDVersionAndFileType(tt.artifactID, tt.version, tt.archiveType) + gotIndexes, err := dbc.SelectIndexesByArtifactIDAndFileType(tt.artifactID, tt.version, tt.archiveType) require.NoError(t, err) - assert.Equal(t, tt.wantGroupID, gotGroupID) + assert.Equal(t, tt.wantIndexes, gotIndexes) }) } }