storj/satellite/satellitedb/irreparabledb.go
Bill Thorp ba5f71810e
added tally to capt run.go (#751)
* added tally to capt run.go, ParseURL -> SplitDBURL
2018-12-12 08:15:34 -05:00

79 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"storj.io/storj/pkg/datarepair/irreparable"
"storj.io/storj/pkg/utils"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
type irreparableDB struct {
db *dbx.DB
}
// IncrementRepairAttempts a db entry for to increment the repair attempts field
func (db *irreparableDB) IncrementRepairAttempts(ctx context.Context, segmentInfo *irreparable.RemoteSegmentInfo) (err error) {
tx, err := db.db.Begin()
if err != nil {
return Error.Wrap(err)
}
dbxInfo, err := db.Get(ctx, segmentInfo.EncryptedSegmentPath)
if err != nil {
// no rows err, so create/insert an entry
_, err = db.db.Create_Irreparabledb(
ctx,
dbx.Irreparabledb_Segmentpath(segmentInfo.EncryptedSegmentPath),
dbx.Irreparabledb_Segmentdetail(segmentInfo.EncryptedSegmentDetail),
dbx.Irreparabledb_PiecesLostCount(segmentInfo.LostPiecesCount),
dbx.Irreparabledb_SegDamagedUnixSec(segmentInfo.RepairUnixSec),
dbx.Irreparabledb_RepairAttemptCount(segmentInfo.RepairAttemptCount),
)
if err != nil {
return Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
}
} else {
// row exits increment the attempt counter
dbxInfo.RepairAttemptCount++
updateFields := dbx.Irreparabledb_Update_Fields{}
updateFields.RepairAttemptCount = dbx.Irreparabledb_RepairAttemptCount(dbxInfo.RepairAttemptCount)
_, err = db.db.Update_Irreparabledb_By_Segmentpath(
ctx,
dbx.Irreparabledb_Segmentpath(dbxInfo.EncryptedSegmentPath),
updateFields,
)
if err != nil {
return Error.Wrap(utils.CombineErrors(err, tx.Rollback()))
}
}
return Error.Wrap(tx.Commit())
}
// Get a irreparable's segment info from the db
func (db *irreparableDB) Get(ctx context.Context, segmentPath []byte) (resp *irreparable.RemoteSegmentInfo, err error) {
dbxInfo, err := db.db.Get_Irreparabledb_By_Segmentpath(ctx, dbx.Irreparabledb_Segmentpath(segmentPath))
if err != nil {
return &irreparable.RemoteSegmentInfo{}, Error.Wrap(err)
}
return &irreparable.RemoteSegmentInfo{
EncryptedSegmentPath: dbxInfo.Segmentpath,
EncryptedSegmentDetail: dbxInfo.Segmentdetail,
LostPiecesCount: dbxInfo.PiecesLostCount,
RepairUnixSec: dbxInfo.SegDamagedUnixSec,
RepairAttemptCount: dbxInfo.RepairAttemptCount,
}, nil
}
// Delete a irreparable's segment info from the db
func (db *irreparableDB) Delete(ctx context.Context, segmentPath []byte) (err error) {
_, err = db.db.Delete_Irreparabledb_By_Segmentpath(ctx, dbx.Irreparabledb_Segmentpath(segmentPath))
return Error.Wrap(err)
}