2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-05 09:35:50 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-12-14 14:27:21 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-12-10 19:08:45 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/irreparable"
|
2018-12-21 15:11:19 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/queue"
|
2019-01-15 16:08:45 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2018-12-14 20:17:30 +00:00
|
|
|
"storj.io/storj/pkg/statdb"
|
2018-12-05 09:35:50 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2019-01-16 20:23:28 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2018-12-05 09:35:50 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
var (
|
|
|
|
// Error is the default satellitedb errs class
|
|
|
|
Error = errs.Class("satellitedb")
|
|
|
|
)
|
|
|
|
|
2019-01-11 16:07:26 +00:00
|
|
|
//go:generate go run ../../scripts/lockedgen.go -o locked.go -p satellitedb -i storj.io/storj/satellite.DB
|
2019-01-02 17:53:27 +00:00
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
// DB contains access to different database tables
|
|
|
|
type DB struct {
|
2019-01-31 13:01:13 +00:00
|
|
|
db *dbx.DB
|
|
|
|
driver string
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
// New creates instance of database (supports: postgres, sqlite3)
|
2018-12-27 09:56:25 +00:00
|
|
|
func New(databaseURL string) (satellite.DB, error) {
|
2018-12-12 13:15:34 +00:00
|
|
|
driver, source, err := utils.SplitDBURL(databaseURL)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2018-12-12 13:15:34 +00:00
|
|
|
db, err := dbx.Open(driver, source)
|
2018-12-05 09:35:50 +00:00
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.New("failed opening database %q, %q: %v",
|
2018-12-12 13:15:34 +00:00
|
|
|
driver, source, err)
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
2018-12-27 09:56:25 +00:00
|
|
|
|
2019-01-31 13:01:13 +00:00
|
|
|
core := &DB{db: db, driver: driver}
|
2018-12-27 09:56:25 +00:00
|
|
|
if driver == "sqlite3" {
|
2019-01-02 17:53:27 +00:00
|
|
|
return newLocked(core), nil
|
2018-12-27 09:56:25 +00:00
|
|
|
}
|
|
|
|
return core, nil
|
2018-12-05 09:35:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 09:30:09 +00:00
|
|
|
// NewInMemory creates instance of Sqlite in memory satellite database
|
2018-12-27 09:56:25 +00:00
|
|
|
func NewInMemory() (satellite.DB, error) {
|
2019-01-15 16:08:45 +00:00
|
|
|
return New("sqlite3://file::memory:?mode=memory")
|
2018-12-11 09:30:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 19:17:12 +00:00
|
|
|
// SetSchema sets the schema
|
|
|
|
func (db *DB) SetSchema(schema string) error {
|
|
|
|
switch db.driver {
|
|
|
|
case "postgres":
|
|
|
|
// TODO: proper escaping
|
|
|
|
_, err := db.db.Exec("create schema " + schema + "; set search_path to " + schema + ";")
|
|
|
|
return err
|
2019-01-31 13:01:13 +00:00
|
|
|
}
|
2019-01-31 19:17:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2019-01-31 19:17:12 +00:00
|
|
|
// DropSchema drops the named schema
|
|
|
|
func (db *DB) DropSchema(schema string) error {
|
2019-01-31 13:01:13 +00:00
|
|
|
switch db.driver {
|
|
|
|
case "postgres":
|
2019-01-31 19:17:12 +00:00
|
|
|
_, err := db.db.Exec("drop schema " + schema + " cascade;")
|
|
|
|
return err
|
2019-01-31 13:01:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-07 09:59:31 +00:00
|
|
|
// BandwidthAgreement is a getter for bandwidth agreement repository
|
|
|
|
func (db *DB) BandwidthAgreement() bwagreement.DB {
|
|
|
|
return &bandwidthagreement{db: db.db}
|
|
|
|
}
|
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
// // PointerDB is a getter for PointerDB repository
|
|
|
|
// func (db *DB) PointerDB() pointerdb.DB {
|
|
|
|
// return &pointerDB{db: db.db}
|
|
|
|
// }
|
|
|
|
|
2018-12-14 20:17:30 +00:00
|
|
|
// StatDB is a getter for StatDB repository
|
|
|
|
func (db *DB) StatDB() statdb.DB {
|
|
|
|
return &statDB{db: db.db}
|
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-17 20:14:16 +00:00
|
|
|
// OverlayCache is a getter for overlay cache repository
|
2019-01-15 16:08:45 +00:00
|
|
|
func (db *DB) OverlayCache() overlay.DB {
|
2018-12-27 09:56:25 +00:00
|
|
|
return &overlaycache{db: db.db}
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-21 15:11:19 +00:00
|
|
|
// RepairQueue is a getter for RepairQueue repository
|
|
|
|
func (db *DB) RepairQueue() queue.RepairQueue {
|
2018-12-27 09:56:25 +00:00
|
|
|
return &repairQueue{db: db.db}
|
2018-12-21 15:11:19 +00:00
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
// Accounting returns database for tracking bandwidth agreements over time
|
|
|
|
func (db *DB) Accounting() accounting.DB {
|
|
|
|
return &accountingDB{db: db.db}
|
|
|
|
}
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-10 19:08:45 +00:00
|
|
|
// Irreparable returns database for storing segments that failed repair
|
|
|
|
func (db *DB) Irreparable() irreparable.DB {
|
|
|
|
return &irreparableDB{db: db.db}
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:23:28 +00:00
|
|
|
// Console returns database for storing users, projects and api keys
|
|
|
|
func (db *DB) Console() console.DB {
|
|
|
|
return &ConsoleDB{
|
|
|
|
db: db.db,
|
|
|
|
methods: db.db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 09:35:50 +00:00
|
|
|
// CreateTables is a method for creating all tables for database
|
|
|
|
func (db *DB) CreateTables() error {
|
|
|
|
return migrate.Create("database", db.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close db connection
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|