Skip to content

Commit

Permalink
do not make noise if entries are already in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbasit committed Dec 6, 2024
1 parent a8ca9ea commit 1c69957
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ func (c *Cache) putNarInfoInStore(hash string, narInfo *narinfo.NarInfo) error {
}

func (c *Cache) storeInDatabase(hash string, narInfo *narinfo.NarInfo) error {
c.logger.Info("storing narinfo and nar record in the database", "hash", hash, "nar-url", narInfo.URL)
log := c.logger.New("hash", hash, "nar-url", narInfo.URL)

log.Info("storing narinfo and nar record in the database")

tx, err := c.db.Begin()
if err != nil {
Expand All @@ -523,6 +525,11 @@ func (c *Cache) storeInDatabase(hash string, narInfo *narinfo.NarInfo) error {

res, err := c.db.InsertNarInfoRecord(tx, hash)
if err != nil {
if errors.Is(err, database.ErrAlreadyExists) {
log.Warn("narinfo record was not added to database because it already exists")
return nil

Check failure on line 530 in pkg/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

return fmt.Errorf("error inserting the narinfo record for hash %q in the database: %w", hash, err)
}

Expand All @@ -537,6 +544,11 @@ func (c *Cache) storeInDatabase(hash string, narInfo *narinfo.NarInfo) error {
}

if _, err := c.db.InsertNarRecord(tx, lid, narHash, compression, narInfo.FileSize); err != nil {
if errors.Is(err, database.ErrAlreadyExists) {
log.Warn("nar record was not added to database because it already exists")
return nil

Check failure on line 549 in pkg/cache/cache.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

return fmt.Errorf("error inserting the nar record in the database: %w", err)
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/inconshreveable/log15/v3"

// Import the SQLite driver.
"github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"

Check failure on line 13 in pkg/database/sqlite.go

View workflow job for this annotation

GitHub Actions / lint

blank-imports: a blank import should be only in a main or test package, or have a comment justifying it (revive)
)

Expand Down Expand Up @@ -77,6 +78,9 @@ const (
var (
// ErrNotFound is returned if record is not found in the database.
ErrNotFound = errors.New("not found")

// ErrAlreadyExists is returned if record insertion failed due to uniqueness constraint.
ErrAlreadyExists = errors.New("error already exists")
)

type (
Expand Down Expand Up @@ -178,6 +182,11 @@ func (db *DB) InsertNarInfoRecord(tx *sql.Tx, hash string) (sql.Result, error) {

res, err := stmt.Exec(hash)
if err != nil {
sqliteErr, ok := errors.Unwrap(err).(sqlite3.Error)
if ok && sqliteErr.Code == sqlite3.ErrConstraint {
return nil, ErrAlreadyExists
}

return nil, fmt.Errorf("error executing the statement: %w", err)
}

Expand Down Expand Up @@ -250,6 +259,11 @@ func (db *DB) InsertNarRecord(tx *sql.Tx, narInfoID int64,

res, err := stmt.Exec(narInfoID, hash, compression, fileSize)
if err != nil {
sqliteErr, ok := errors.Unwrap(err).(sqlite3.Error)
if ok && sqliteErr.Code == sqlite3.ErrConstraint {
return nil, ErrAlreadyExists
}

return nil, fmt.Errorf("error executing the statement: %w", err)
}

Expand Down

0 comments on commit 1c69957

Please sign in to comment.