Skip to content

Commit

Permalink
refactor: search by artifactID returns indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Dec 18, 2023
1 parent 68c706b commit 5c30667
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
33 changes: 13 additions & 20 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 12 additions & 6 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 5c30667

Please sign in to comment.