2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-08 14:19:42 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2018-12-10 12:29:01 +00:00
|
|
|
"context"
|
2020-01-10 01:12:27 +00:00
|
|
|
"sync"
|
2018-12-10 12:29:01 +00:00
|
|
|
|
2019-01-08 14:05:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-01-10 01:12:27 +00:00
|
|
|
"storj.io/storj/pkg/cache"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-08 14:19:42 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that ConsoleDB implements console.DB.
|
|
|
|
var _ console.DB = (*ConsoleDB)(nil)
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ConsoleDB contains access to different satellite databases.
|
2019-01-15 13:03:24 +00:00
|
|
|
type ConsoleDB struct {
|
2020-01-10 01:12:27 +00:00
|
|
|
apikeysLRUOptions cache.Options
|
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-10 12:29:01 +00:00
|
|
|
tx *dbx.Tx
|
|
|
|
|
|
|
|
methods dbx.Methods
|
2020-01-10 01:12:27 +00:00
|
|
|
|
|
|
|
apikeysOnce *sync.Once
|
|
|
|
apikeys *apikeys
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Users is getter a for Users repository.
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) Users() console.Users {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &users{db.methods}
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Projects is a getter for Projects repository.
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) Projects() console.Projects {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &projects{db.methods}
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ProjectMembers is a getter for ProjectMembers repository.
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
|
2018-12-28 12:07:35 +00:00
|
|
|
return &projectMembers{db.methods, db.db}
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// APIKeys is a getter for APIKeys repository.
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) APIKeys() console.APIKeys {
|
2020-01-10 01:12:27 +00:00
|
|
|
db.apikeysOnce.Do(func() {
|
|
|
|
db.apikeys = &apikeys{
|
|
|
|
methods: db.methods,
|
|
|
|
lru: cache.New(db.apikeysLRUOptions),
|
|
|
|
db: db.db,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return db.apikeys
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// RegistrationTokens is a getter for RegistrationTokens repository.
|
2019-03-19 17:55:43 +00:00
|
|
|
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
|
|
|
|
return ®istrationTokens{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ResetPasswordTokens is a getter for ResetPasswordTokens repository.
|
2019-05-13 16:53:52 +01:00
|
|
|
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
|
|
|
|
return &resetPasswordTokens{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// UserCredits is a getter for console.UserCredits repository.
|
2019-06-18 16:55:47 +01:00
|
|
|
func (db *ConsoleDB) UserCredits() console.UserCredits {
|
2019-07-30 14:21:00 +01:00
|
|
|
return &usercredits{db.db, db.tx}
|
2019-06-18 16:55:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// WithTx is a method for executing and retrying transaction.
|
|
|
|
func (db *ConsoleDB) WithTx(ctx context.Context, fn func(context.Context, console.DBTx) error) error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
2019-12-19 10:07:56 +00:00
|
|
|
return errs.New("DB is not initialized!")
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
|
|
dbTx := &DBTx{
|
|
|
|
ConsoleDB: &ConsoleDB{
|
2020-01-10 01:12:27 +00:00
|
|
|
apikeysLRUOptions: db.apikeysLRUOptions,
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// Need to expose dbx.DB for when database methods need access to check database driver type
|
|
|
|
db: db.db,
|
|
|
|
tx: tx,
|
|
|
|
methods: tx,
|
2020-01-10 01:12:27 +00:00
|
|
|
|
|
|
|
apikeysOnce: db.apikeysOnce,
|
|
|
|
apikeys: db.apikeys,
|
2019-12-19 10:07:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return fn(ctx, dbTx)
|
|
|
|
})
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// DBTx extends Database with transaction scope.
|
2018-12-10 12:29:01 +00:00
|
|
|
type DBTx struct {
|
2019-01-15 13:03:24 +00:00
|
|
|
*ConsoleDB
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Commit is a method for committing and closing transaction.
|
2018-12-10 12:29:01 +00:00
|
|
|
func (db *DBTx) Commit() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before commit it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Commit()
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Rollback is a method for rollback and closing transaction.
|
2018-12-10 12:29:01 +00:00
|
|
|
func (db *DBTx) Rollback() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before rollback it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Rollback()
|
|
|
|
}
|