/
db.go
59 lines (47 loc) · 1.2 KB
/
db.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
package core
import (
"context"
"fmt"
"github.com/pingcap/tipocket/pkg/cluster"
)
// DB allows to set up and tear down database.
type DB interface {
// SetUp initializes the database.
SetUp(ctx context.Context, nodes []cluster.Node, node cluster.Node) error
// TearDown tears down the database.
TearDown(ctx context.Context, nodes []cluster.Node, node cluster.Node) error
// Name returns the unique name for the database
Name() string
}
// NoopDB is a DB but does nothing
type NoopDB struct {
}
// SetUp initializes the database.
func (NoopDB) SetUp(ctx context.Context, nodes []cluster.Node, node cluster.Node) error {
return nil
}
// TearDown tears down the database.
func (NoopDB) TearDown(ctx context.Context, nodes []cluster.Node, node cluster.Node) error {
return nil
}
// Name returns the unique name for the database
func (NoopDB) Name() string {
return ""
}
var dbs = map[string]DB{}
// RegisterDB registers db. Not thread-safe
func RegisterDB(db DB) {
name := db.Name()
_, ok := dbs[name]
if ok {
panic(fmt.Sprintf("db %s is already registered", name))
}
dbs[name] = db
}
// GetDB gets the registered db.
func GetDB(name string) DB {
return dbs[name]
}
func init() {
RegisterDB(NoopDB{})
}