-
Notifications
You must be signed in to change notification settings - Fork 387
/
consoledb.go
116 lines (91 loc) · 2.72 KB
/
consoledb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"sync"
"github.com/zeebo/errs"
"storj.io/storj/pkg/cache"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/dbx"
)
// ensures that ConsoleDB implements console.DB.
var _ console.DB = (*ConsoleDB)(nil)
// ConsoleDB contains access to different satellite databases.
type ConsoleDB struct {
apikeysLRUOptions cache.Options
db *satelliteDB
tx *dbx.Tx
methods dbx.Methods
apikeysOnce *sync.Once
apikeys *apikeys
}
// Users is getter a for Users repository.
func (db *ConsoleDB) Users() console.Users {
return &users{db.methods}
}
// Projects is a getter for Projects repository.
func (db *ConsoleDB) Projects() console.Projects {
return &projects{db: db.methods, sdb: db.db}
}
// ProjectMembers is a getter for ProjectMembers repository.
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
return &projectMembers{db.methods, db.db}
}
// APIKeys is a getter for APIKeys repository.
func (db *ConsoleDB) APIKeys() console.APIKeys {
db.apikeysOnce.Do(func() {
db.apikeys = &apikeys{
methods: db.methods,
lru: cache.New(db.apikeysLRUOptions),
db: db.db,
}
})
return db.apikeys
}
// RegistrationTokens is a getter for RegistrationTokens repository.
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
return ®istrationTokens{db.methods}
}
// ResetPasswordTokens is a getter for ResetPasswordTokens repository.
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
return &resetPasswordTokens{db.methods}
}
// WithTx is a method for executing and retrying transaction.
func (db *ConsoleDB) WithTx(ctx context.Context, fn func(context.Context, console.DBTx) error) error {
if db.db == nil {
return errs.New("DB is not initialized!")
}
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
dbTx := &DBTx{
ConsoleDB: &ConsoleDB{
apikeysLRUOptions: db.apikeysLRUOptions,
// Need to expose dbx.DB for when database methods need access to check database driver type
db: db.db,
tx: tx,
methods: tx,
apikeysOnce: db.apikeysOnce,
apikeys: db.apikeys,
},
}
return fn(ctx, dbTx)
})
}
// DBTx extends Database with transaction scope.
type DBTx struct {
*ConsoleDB
}
// Commit is a method for committing and closing transaction.
func (db *DBTx) Commit() error {
if db.tx == nil {
return errs.New("begin transaction before commit it!")
}
return db.tx.Commit()
}
// Rollback is a method for rollback and closing transaction.
func (db *DBTx) Rollback() error {
if db.tx == nil {
return errs.New("begin transaction before rollback it!")
}
return db.tx.Rollback()
}