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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usedSerials struct {
|
2019-04-02 08:54:09 +01:00
|
|
|
*InfoDB
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UsedSerials returns certificate database.
|
|
|
|
func (db *DB) UsedSerials() piecestore.UsedSerials { return db.info.UsedSerials() }
|
|
|
|
|
|
|
|
// UsedSerials returns certificate database.
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) UsedSerials() piecestore.UsedSerials { return &usedSerials{db} }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Add adds a serial to the database.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *usedSerials) Add(ctx context.Context, satelliteID storj.NodeID, serialNumber storj.SerialNumber, expiration time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
_, err = db.db.Exec(`
|
|
|
|
INSERT INTO
|
|
|
|
used_serial(satellite_id, serial_number, expiration)
|
2019-03-18 10:55:06 +00:00
|
|
|
VALUES(?, ?, ?)`, satelliteID, serialNumber, expiration)
|
|
|
|
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteExpired deletes expired serial numbers
|
2019-06-04 13:31:39 +01:00
|
|
|
func (db *usedSerials) DeleteExpired(ctx context.Context, now time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
_, err = db.db.Exec(`DELETE FROM used_serial WHERE expiration < ?`, now)
|
2019-03-18 10:55:06 +00:00
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAll iterates all serials.
|
|
|
|
// Note, this will lock the database and should only be used during startup.
|
|
|
|
func (db *usedSerials) 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
|
|
|
defer db.locked()()
|
|
|
|
|
|
|
|
rows, err := db.db.Query(`SELECT satellite_id, serial_number, expiration FROM used_serial`)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, ErrInfo.Wrap(rows.Close())) }()
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(satelliteID, serialNumber, expiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrInfo.Wrap(rows.Err())
|
|
|
|
}
|