2020-04-10 15:03:14 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2020-04-10 15:03:14 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/storj/storagenode/pricing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that pricingDB implements pricing.DB interface.
|
|
|
|
var _ pricing.DB = (*pricingDB)(nil)
|
|
|
|
|
|
|
|
// ErrPricing represents errors from the pricing database.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrPricing = errs.Class("pricing")
|
2020-04-10 15:03:14 +01:00
|
|
|
|
|
|
|
// PricingDBName represents the database name.
|
|
|
|
const PricingDBName = "pricing"
|
|
|
|
|
|
|
|
// pricing works with node pricing DB.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type pricingDB struct {
|
|
|
|
dbContainerImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store inserts or updates pricing model into the db.
|
|
|
|
func (db *pricingDB) Store(ctx context.Context, pricing pricing.Pricing) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
query := `INSERT OR REPLACE INTO pricing (
|
|
|
|
satellite_id,
|
|
|
|
egress_bandwidth_price,
|
|
|
|
repair_bandwidth_price,
|
|
|
|
audit_bandwidth_price,
|
|
|
|
disk_space_price
|
|
|
|
) VALUES(?,?,?,?,?)`
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, query,
|
|
|
|
pricing.SatelliteID,
|
|
|
|
pricing.EgressBandwidth,
|
|
|
|
pricing.RepairBandwidth,
|
|
|
|
pricing.AuditBandwidth,
|
|
|
|
pricing.DiskSpace,
|
|
|
|
)
|
|
|
|
|
|
|
|
return ErrPricing.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves pricing model for specific satellite.
|
|
|
|
func (db *pricingDB) Get(ctx context.Context, satelliteID storj.NodeID) (_ *pricing.Pricing, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
pricingModel := pricing.Pricing{
|
|
|
|
SatelliteID: satelliteID,
|
|
|
|
}
|
|
|
|
|
|
|
|
row := db.QueryRowContext(ctx,
|
|
|
|
`SELECT egress_bandwidth_price,
|
|
|
|
repair_bandwidth_price,
|
|
|
|
audit_bandwidth_price,
|
|
|
|
disk_space_price
|
|
|
|
FROM pricing WHERE satellite_id = ?`,
|
|
|
|
satelliteID,
|
|
|
|
)
|
|
|
|
|
|
|
|
err = row.Scan(
|
|
|
|
&pricingModel.EgressBandwidth,
|
|
|
|
&pricingModel.RepairBandwidth,
|
|
|
|
&pricingModel.AuditBandwidth,
|
|
|
|
&pricingModel.DiskSpace,
|
|
|
|
)
|
|
|
|
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-04-10 15:03:14 +01:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pricingModel, ErrPricing.Wrap(err)
|
|
|
|
}
|