2018-01-31 02:15:21 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2018-02-22 19:48:46 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-03-01 22:28:06 +01:00
|
|
|
"github.com/lbryio/lbry.go/querytools"
|
2018-03-01 22:12:53 +01:00
|
|
|
"github.com/lbryio/reflector.go/types"
|
2018-05-30 03:38:55 +02:00
|
|
|
// blank import for db driver
|
2018-01-31 02:15:21 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// DB interface communicates to a backend database with a simple set of methods that supports tracking blobs that are
|
|
|
|
// used together with a BlobStore. The DB tracks pointers and the BlobStore stores the data.
|
2018-01-31 02:15:21 +01:00
|
|
|
type DB interface {
|
|
|
|
Connect(string) error
|
|
|
|
HasBlob(string) (bool, error)
|
2018-02-02 22:49:20 +01:00
|
|
|
AddBlob(string, int, bool) error
|
|
|
|
AddSDBlob(string, int, types.SdBlob) error
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// SQL is the container for the supporting MySQL database connection.
|
2018-01-31 02:15:21 +01:00
|
|
|
type SQL struct {
|
|
|
|
conn *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func logQuery(query string, args ...interface{}) {
|
2018-03-01 22:28:06 +01:00
|
|
|
s, err := querytools.InterpolateParams(query, args...)
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
} else {
|
|
|
|
log.Debugln(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Connect will create a connection to the database
|
2018-01-31 02:15:21 +01:00
|
|
|
func (s *SQL) Connect(dsn string) error {
|
|
|
|
var err error
|
|
|
|
dsn += "?parseTime=1&collation=utf8mb4_unicode_ci"
|
|
|
|
s.conn, err = sql.Open("mysql", dsn)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Err(s.conn.Ping())
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// AddBlob adds a blobs information to the database.
|
2018-02-02 22:49:20 +01:00
|
|
|
func (s *SQL) AddBlob(hash string, length int, stored bool) error {
|
2018-01-31 02:15:21 +01:00
|
|
|
if s.conn == nil {
|
|
|
|
return errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
return withTx(s.conn, func(tx *sql.Tx) error {
|
|
|
|
return addBlob(tx, hash, length, stored)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func addBlob(tx *sql.Tx, hash string, length int, stored bool) error {
|
2018-01-31 02:15:21 +01:00
|
|
|
if length <= 0 {
|
|
|
|
return errors.Err("length must be positive")
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
query := "INSERT INTO blob_ (hash, stored, length) VALUES (?,?,?) ON DUPLICATE KEY UPDATE stored = (stored or VALUES(stored))"
|
|
|
|
args := []interface{}{hash, stored, length}
|
2018-01-31 02:15:21 +01:00
|
|
|
|
|
|
|
logQuery(query, args...)
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
stmt, err := tx.Prepare(query)
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HasBlob checks if the database contains the blob information.
|
2018-01-31 02:15:21 +01:00
|
|
|
func (s *SQL) HasBlob(hash string) (bool, error) {
|
|
|
|
if s.conn == nil {
|
|
|
|
return false, errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
query := "SELECT EXISTS(SELECT 1 FROM blob_ WHERE hash = ? AND stored = ?)"
|
|
|
|
args := []interface{}{hash, true}
|
2018-01-31 02:15:21 +01:00
|
|
|
|
|
|
|
logQuery(query, args...)
|
|
|
|
|
|
|
|
row := s.conn.QueryRow(query, args...)
|
|
|
|
|
|
|
|
exists := false
|
|
|
|
err := row.Scan(&exists)
|
|
|
|
|
|
|
|
return exists, errors.Err(err)
|
|
|
|
}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// HasBlobs checks if the database contains the set of blobs and returns a bool map.
|
2018-05-15 02:55:45 +02:00
|
|
|
func (s *SQL) HasBlobs(hashes []string) (map[string]bool, error) {
|
|
|
|
if s.conn == nil {
|
|
|
|
return nil, errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
var hash string
|
|
|
|
exists := make(map[string]bool)
|
|
|
|
maxBatchSize := 100
|
|
|
|
doneIndex := 0
|
|
|
|
|
|
|
|
for len(hashes) > doneIndex {
|
|
|
|
sliceEnd := doneIndex + maxBatchSize
|
|
|
|
if sliceEnd > len(hashes) {
|
|
|
|
sliceEnd = len(hashes)
|
|
|
|
}
|
|
|
|
log.Debugf("getting hashes[%d:%d] of %d", doneIndex, sliceEnd, len(hashes))
|
|
|
|
batch := hashes[doneIndex:sliceEnd]
|
|
|
|
|
|
|
|
query := "SELECT hash FROM blob_ WHERE stored = ? && hash IN (" + querytools.Qs(len(batch)) + ")"
|
|
|
|
args := make([]interface{}, len(batch)+1)
|
|
|
|
args[0] = true
|
|
|
|
for i := range batch {
|
|
|
|
args[i+1] = batch[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
logQuery(query, args...)
|
|
|
|
|
|
|
|
rows, err := s.conn.Query(query, args...)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
closeRows(rows)
|
2018-05-15 02:55:45 +02:00
|
|
|
return exists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&hash)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
closeRows(rows)
|
2018-05-15 02:55:45 +02:00
|
|
|
return exists, err
|
|
|
|
}
|
|
|
|
exists[hash] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
closeRows(rows)
|
2018-05-15 02:55:45 +02:00
|
|
|
return exists, err
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
closeRows(rows)
|
2018-05-15 02:55:45 +02:00
|
|
|
doneIndex += len(batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// AddSDBlob takes the SD Hash number of blobs and the set of blobs. In a single db tx it inserts the sdblob information
|
|
|
|
// into a stream, and inserts the associated blobs' information in the database. If a blob fails the transaction is
|
|
|
|
// rolled back and error(s) are returned.
|
2018-02-02 22:49:20 +01:00
|
|
|
func (s *SQL) AddSDBlob(sdHash string, sdBlobLength int, sdBlob types.SdBlob) error {
|
|
|
|
if s.conn == nil {
|
|
|
|
return errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
return withTx(s.conn, func(tx *sql.Tx) error {
|
|
|
|
// insert sd blob
|
|
|
|
err := addBlob(tx, sdHash, sdBlobLength, true)
|
2018-02-02 22:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
// insert stream
|
|
|
|
query := "INSERT IGNORE INTO stream (hash, sd_hash) VALUES (?,?)"
|
|
|
|
args := []interface{}{sdBlob.StreamHash, sdHash}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
|
|
|
logQuery(query, args...)
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
stmt, err := tx.Prepare(query)
|
2018-02-02 22:49:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2018-03-01 22:12:53 +01:00
|
|
|
|
|
|
|
// insert content blobs and connect them to stream
|
|
|
|
for _, contentBlob := range sdBlob.Blobs {
|
|
|
|
if contentBlob.BlobHash == "" {
|
|
|
|
// null terminator blob
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := addBlob(tx, contentBlob.BlobHash, contentBlob.Length, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "INSERT IGNORE INTO stream_blob (stream_hash, blob_hash, num) VALUES (?,?,?)"
|
|
|
|
args := []interface{}{sdBlob.StreamHash, contentBlob.BlobHash, contentBlob.BlobNum}
|
|
|
|
|
|
|
|
logQuery(query, args...)
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// txFunc is a function that can be wrapped in a transaction
|
|
|
|
type txFunc func(tx *sql.Tx) error
|
|
|
|
|
|
|
|
// withTx wraps a function in an sql transaction. the transaction is committed if there's no error, or rolled back if there is one.
|
|
|
|
// if dbOrTx is an sql.DB, a new transaction is started
|
|
|
|
func withTx(dbOrTx interface{}, f txFunc) (err error) {
|
|
|
|
var tx *sql.Tx
|
|
|
|
|
|
|
|
switch t := dbOrTx.(type) {
|
|
|
|
case *sql.Tx:
|
|
|
|
tx = t
|
|
|
|
case *sql.DB:
|
|
|
|
tx, err = t.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
if rollBackError := tx.Rollback(); rollBackError != nil {
|
|
|
|
log.Error("failed to rollback tx on panic - ", rollBackError)
|
|
|
|
}
|
2018-03-01 22:12:53 +01:00
|
|
|
panic(p)
|
|
|
|
} else if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
if rollBackError := tx.Rollback(); rollBackError != nil {
|
|
|
|
log.Error("failed to rollback tx on panic - ", rollBackError)
|
|
|
|
}
|
2018-03-01 22:12:53 +01:00
|
|
|
} else {
|
|
|
|
err = errors.Err(tx.Commit())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
return errors.Err("db or tx required")
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
return f(tx)
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
func closeRows(rows *sql.Rows) {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
log.Error("error closing rows: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*// func to generate schema. SQL below that.
|
2018-02-02 22:49:20 +01:00
|
|
|
func schema() {
|
|
|
|
_ = `
|
|
|
|
CREATE TABLE blob_ (
|
|
|
|
hash char(96) NOT NULL,
|
|
|
|
stored TINYINT(1) NOT NULL DEFAULT 0,
|
|
|
|
length bigint(20) unsigned DEFAULT NULL,
|
|
|
|
last_announced_at datetime DEFAULT NULL,
|
|
|
|
PRIMARY KEY (hash),
|
|
|
|
KEY last_announced_at_idx (last_announced_at)
|
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
|
|
|
|
CREATE TABLE stream (
|
|
|
|
hash char(96) NOT NULL,
|
|
|
|
sd_hash char(96) NOT NULL,
|
|
|
|
PRIMARY KEY (hash),
|
|
|
|
KEY sd_hash_idx (sd_hash),
|
2018-02-07 21:21:20 +01:00
|
|
|
FOREIGN KEY (sd_hash) REFERENCES blob_ (hash) ON DELETE RESTRICT ON UPDATE CASCADE
|
2018-02-02 22:49:20 +01:00
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
|
|
|
|
CREATE TABLE stream_blob (
|
|
|
|
stream_hash char(96) NOT NULL,
|
|
|
|
blob_hash char(96) NOT NULL,
|
|
|
|
num int NOT NULL,
|
|
|
|
PRIMARY KEY (stream_hash, blob_hash),
|
2018-02-07 21:21:20 +01:00
|
|
|
FOREIGN KEY (stream_hash) REFERENCES stream (hash) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
FOREIGN KEY (blob_hash) REFERENCES blob_ (hash) ON DELETE CASCADE ON UPDATE CASCADE
|
2018-02-02 22:49:20 +01:00
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
|
|
|
|
`
|
2018-05-30 03:38:55 +02:00
|
|
|
}*/
|
|
|
|
|
|
|
|
/* SQL script to create schema
|
|
|
|
CREATE TABLE `reflector`.`blob_`
|
|
|
|
(
|
|
|
|
`hash` char(96) NOT NULL,
|
|
|
|
`stored` TINYINT(1) NOT NULL DEFAULT 0,
|
|
|
|
`length` bigint(20) unsigned DEFAULT NULL,
|
|
|
|
`last_announced_at` datetime DEFAULT NULL,
|
|
|
|
PRIMARY KEY (`hash`),
|
|
|
|
KEY `last_announced_at_idx` (`last_announced_at`)
|
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
|
|
|
|
CREATE TABLE `reflector`.`stream`
|
|
|
|
(
|
|
|
|
`hash` char(96) NOT NULL,
|
|
|
|
`sd_hash` char(96) NOT NULL,
|
|
|
|
PRIMARY KEY (hash),
|
|
|
|
KEY `sd_hash_idx` (`sd_hash`),
|
|
|
|
FOREIGN KEY (`sd_hash`) REFERENCES `blob_` (`hash`) ON DELETE RESTRICT ON UPDATE CASCADE
|
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
|
|
|
|
CREATE TABLE `reflector`.`stream_blob`
|
|
|
|
(
|
|
|
|
`stream_hash` char(96) NOT NULL,
|
|
|
|
`blob_hash` char(96) NOT NULL,
|
|
|
|
`num` int NOT NULL,
|
|
|
|
PRIMARY KEY (`stream_hash`, `blob_hash`),
|
|
|
|
FOREIGN KEY (`stream_hash`) REFERENCES `stream` (`hash`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
|
|
FOREIGN KEY (`blob_hash`) REFERENCES `blob_` (`hash`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
|
|
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
|
|
*/
|