126450f7dd
Change-Id: I327afa1b4bde44f7c2f0d4df10a0792dd5c588a3
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright (C) 2020 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package mutlinodedb
|
|
|
|
import (
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
|
|
"storj.io/storj/multinode"
|
|
"storj.io/storj/multinode/console"
|
|
"storj.io/storj/multinode/mutlinodedb/dbx"
|
|
"storj.io/storj/private/dbutil"
|
|
"storj.io/storj/private/dbutil/pgutil"
|
|
)
|
|
|
|
var (
|
|
// ensures that multinodeDB implements multinode.DB.
|
|
_ multinode.DB = (*multinodeDB)(nil)
|
|
|
|
mon = monkit.Package()
|
|
|
|
// Error is the default multinodedb errs class.
|
|
Error = errs.Class("multinodedb internal error")
|
|
)
|
|
|
|
// multinodeDB combines access to different database tables with a record
|
|
// of the db driver, db implementation, and db source URL.
|
|
// Implementation of multinode.DB interface.
|
|
//
|
|
// architecture: Master Database
|
|
type multinodeDB struct {
|
|
*dbx.DB
|
|
|
|
log *zap.Logger
|
|
driver string
|
|
implementation dbutil.Implementation
|
|
source string
|
|
}
|
|
|
|
// New creates instance of database supports postgres.
|
|
func New(log *zap.Logger, databaseURL string) (multinode.DB, error) {
|
|
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// TODO: do we need cockroach implementation?
|
|
if implementation != dbutil.Postgres && implementation != dbutil.Cockroach {
|
|
return nil, Error.New("unsupported driver %q", driver)
|
|
}
|
|
|
|
source = pgutil.CheckApplicationName(source)
|
|
|
|
// dbxDB, err := dbx.Open(driver, source)
|
|
// if err != nil {
|
|
// return nil, Error.New("failed opening database via DBX at %q: %v",
|
|
// source, err)
|
|
// }
|
|
// log.Debug("Connected to:", zap.String("db source", source))
|
|
|
|
// dbutil.Configure(dbxDB.DB, "multinodedb", mon)
|
|
|
|
core := &multinodeDB{
|
|
// DB: dbxDB,
|
|
|
|
log: log,
|
|
driver: driver,
|
|
implementation: implementation,
|
|
source: source,
|
|
}
|
|
|
|
return core, nil
|
|
}
|
|
|
|
// Nodes returns nodes database.
|
|
func (db *multinodeDB) Nodes() console.Nodes {
|
|
return &nodes{
|
|
methods: db,
|
|
db: db,
|
|
}
|
|
}
|