Skip to content

Commit

Permalink
chore: db optimization to get GAV faster by Artifact_id and file_type (
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen authored Feb 17, 2023
1 parent 7cddb14 commit 9a2c141
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ func (db *DB) Init() error {
return xerrors.Errorf("unable to create 'indices' table: %w", err)
}

if _, err := db.client.Exec("CREATE UNIQUE INDEX artifacts_idx ON artifacts(group_id, artifact_id)"); err != nil {
if _, err := db.client.Exec("CREATE UNIQUE INDEX artifacts_idx ON artifacts(artifact_id, group_id)"); err != nil {
return xerrors.Errorf("unable to create 'artifacts_idx' index: %w", err)
}
if _, err := db.client.Exec("CREATE INDEX indices_artifact_idx ON indices(artifact_id)"); err != nil {
return xerrors.Errorf("unable to create 'indices_artifact_idx' index: %w", err)
}
if _, err := db.client.Exec("CREATE UNIQUE INDEX indices_sha1_idx ON indices(sha1)"); err != nil {
return xerrors.Errorf("unable to create 'indices_sha1_idx' index: %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db_test

import (
"encoding/hex"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -124,8 +125,8 @@ func TestSelectIndexesByArtifactIDAndFileType(t *testing.T) {
artifactID: "jstl",
archiveType: types.JarType,
want: []types.Index{
indexJstl,
indexJavaxServlet,
indexJstl,
},
},
{
Expand All @@ -148,6 +149,10 @@ func TestSelectIndexesByArtifactIDAndFileType(t *testing.T) {
require.NoError(t, err)

got, err := dbc.SelectIndexesByArtifactIDAndFileType(tt.artifactID, tt.archiveType)
sort.Slice(got, func(i, j int) bool {
return got[i].GroupID < got[j].GroupID
})

require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
Expand Down

0 comments on commit 9a2c141

Please sign in to comment.