Skip to content

Commit

Permalink
sql: update unhealthy slabs query
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Jul 31, 2024
1 parent fbf16eb commit ea6a0d5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stores/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ type (
// Tip returns the sync height.
Tip(ctx context.Context) (types.ChainIndex, error)

// UnhealthySlabs returns up to 'limit' slabs belonging to the contract
// set 'set' with a health smaller than or equal to 'healthCutoff'
UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error)

// UnspentSiacoinElements returns all wallet outputs in the database.
UnspentSiacoinElements(ctx context.Context) ([]types.SiacoinElement, error)

Expand Down
30 changes: 30 additions & 0 deletions stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,36 @@ func Tip(ctx context.Context, tx sql.Tx) (types.ChainIndex, error) {
}, nil
}

func UnhealthySlabs(ctx context.Context, tx sql.Tx, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error) {
rows, err := tx.Query(ctx, `
SELECT sla.key, sla.health
FROM slabs sla
INNER JOIN contract_sets cs ON sla.db_contract_set_id = cs.id
WHERE sla.health <= ? AND cs.name = ? AND sla.health_valid_until > ?
ORDER BY sla.health ASC
LIMIT ?
`, healthCutoff, set, time.Now().Unix(), limit)
if err != nil {
return nil, fmt.Errorf("failed to fetch unhealthy slabs: %w", err)
}
defer rows.Close()

var slabs []api.UnhealthySlab
for rows.Next() {
var slab api.UnhealthySlab
var key SecretKey
if err := rows.Scan(&key, &slab.Health); err != nil {
return nil, fmt.Errorf("failed to scan unhealthy slab: %w", err)
}
var ec object.EncryptionKey
if err := ec.UnmarshalBinary(key); err != nil {
return nil, fmt.Errorf("failed to unmarshal encryption key: %w", err)
}
slabs = append(slabs, slab)
}
return slabs, nil
}

func UpdateBucketPolicy(ctx context.Context, tx sql.Tx, bucket string, bp api.BucketPolicy) error {
policy, err := json.Marshal(bp)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions stores/sql/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ func (tx *MainDatabaseTx) Tip(ctx context.Context) (types.ChainIndex, error) {
return ssql.Tip(ctx, tx.Tx)
}

func (tx *MainDatabaseTx) UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error) {
return ssql.UnhealthySlabs(ctx, tx, healthCutoff, set, limit)
}

func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements []types.SiacoinElement, err error) {
return ssql.UnspentSiacoinElements(ctx, tx.Tx)
}
Expand Down
4 changes: 4 additions & 0 deletions stores/sql/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ func (tx *MainDatabaseTx) Tip(ctx context.Context) (types.ChainIndex, error) {
return ssql.Tip(ctx, tx.Tx)
}

func (tx *MainDatabaseTx) UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error) {
return ssql.UnhealthySlabs(ctx, tx, healthCutoff, set, limit)
}

func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements []types.SiacoinElement, err error) {
return ssql.UnspentSiacoinElements(ctx, tx.Tx)
}
Expand Down

0 comments on commit ea6a0d5

Please sign in to comment.