2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/storagenode/piecestore"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrUsedSerials represents errors from the used serials database.
|
|
|
|
var ErrUsedSerials = errs.Class("usedserialsdb error")
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// UsedSerialsDBName represents the database name.
|
|
|
|
const UsedSerialsDBName = "used_serial"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
type usedSerialsDB struct {
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-21 15:32:25 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Add adds a serial to the database.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *usedSerialsDB) Add(ctx context.Context, satelliteID storj.NodeID, serialNumber storj.SerialNumber, expiration time.Time) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.Exec(`
|
2019-06-04 13:31:39 +01:00
|
|
|
INSERT INTO
|
2019-07-16 17:31:29 +01:00
|
|
|
used_serial_(satellite_id, serial_number, expiration)
|
2019-07-15 18:38:08 +01:00
|
|
|
VALUES(?, ?, ?)`, satelliteID, serialNumber, expiration.UTC())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrUsedSerials.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteExpired deletes expired serial numbers
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *usedSerialsDB) DeleteExpired(ctx context.Context, now time.Time) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.Exec(`DELETE FROM used_serial_ WHERE expiration < ?`, now.UTC())
|
|
|
|
return ErrUsedSerials.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAll iterates all serials.
|
|
|
|
// Note, this will lock the database and should only be used during startup.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *usedSerialsDB) IterateAll(ctx context.Context, fn piecestore.SerialNumberFn) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.Query(`SELECT satellite_id, serial_number, expiration FROM used_serial_`)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrUsedSerials.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
defer func() { err = errs.Combine(err, ErrUsedSerials.Wrap(rows.Close())) }()
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var satelliteID storj.NodeID
|
2019-03-18 13:08:24 +00:00
|
|
|
var serialNumber storj.SerialNumber
|
2019-03-18 10:55:06 +00:00
|
|
|
var expiration time.Time
|
|
|
|
|
|
|
|
err := rows.Scan(&satelliteID, &serialNumber, &expiration)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrUsedSerials.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn(satelliteID, serialNumber, expiration)
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrUsedSerials.Wrap(rows.Err())
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|