2018-01-31 02:15:21 +01:00
|
|
|
package store
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-09-20 17:24:36 +02:00
|
|
|
"sync"
|
2018-02-02 22:49:20 +01:00
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/db"
|
2018-08-20 23:50:39 +02:00
|
|
|
|
2019-01-09 23:52:30 +01:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
2018-09-20 17:24:36 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-02 22:49:20 +01:00
|
|
|
)
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// DBBackedS3Store is an instance of an S3 Store that is backed by a DB for what is stored.
|
2018-01-31 02:15:21 +01:00
|
|
|
type DBBackedS3Store struct {
|
2018-09-20 17:24:36 +02:00
|
|
|
s3 *S3BlobStore
|
|
|
|
db *db.SQL
|
|
|
|
blockedMu sync.RWMutex
|
|
|
|
blocked map[string]bool
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// NewDBBackedS3Store returns an initialized store pointer.
|
2018-08-07 22:51:02 +02:00
|
|
|
func NewDBBackedS3Store(s3 *S3BlobStore, db *db.SQL) *DBBackedS3Store {
|
2018-01-31 02:15:21 +01:00
|
|
|
return &DBBackedS3Store{s3: s3, db: db}
|
|
|
|
}
|
|
|
|
|
2018-08-20 23:50:39 +02:00
|
|
|
// Has returns true if the blob is in the store
|
2018-01-31 02:15:21 +01:00
|
|
|
func (d *DBBackedS3Store) Has(hash string) (bool, error) {
|
|
|
|
return d.db.HasBlob(hash)
|
|
|
|
}
|
|
|
|
|
2018-08-20 23:50:39 +02:00
|
|
|
// Get gets the blob
|
2018-01-31 02:15:21 +01:00
|
|
|
func (d *DBBackedS3Store) Get(hash string) ([]byte, error) {
|
|
|
|
return d.s3.Get(hash)
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Put stores the blob in the S3 store and stores the blob information in the DB.
|
2018-01-31 02:15:21 +01:00
|
|
|
func (d *DBBackedS3Store) Put(hash string, blob []byte) error {
|
|
|
|
err := d.s3.Put(hash, blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
return d.db.AddBlob(hash, len(blob), true)
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// PutSD stores the SDBlob in the S3 store. It will return an error if the sd blob is missing the stream hash or if
|
|
|
|
// there is an error storing the blob information in the DB.
|
2018-02-02 22:49:20 +01:00
|
|
|
func (d *DBBackedS3Store) PutSD(hash string, blob []byte) error {
|
2018-08-07 22:51:02 +02:00
|
|
|
var blobContents db.SdBlob
|
2018-02-02 22:49:20 +01:00
|
|
|
err := json.Unmarshal(blob, &blobContents)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if blobContents.StreamHash == "" {
|
2018-03-01 22:28:06 +01:00
|
|
|
return errors.Err("sd blob is missing stream hash")
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = d.s3.PutSD(hash, blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.AddSDBlob(hash, len(blob), blobContents)
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2018-09-11 13:41:29 +02:00
|
|
|
func (d *DBBackedS3Store) Delete(hash string) error {
|
|
|
|
err := d.s3.Delete(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.Delete(hash)
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
// Block deletes the blob and prevents it from being uploaded in the future
|
|
|
|
func (d *DBBackedS3Store) Block(hash string) error {
|
|
|
|
if blocked, err := d.isBlocked(hash); blocked || err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("blocking %s", hash)
|
|
|
|
|
|
|
|
err := d.db.Block(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err := d.db.HasBlob(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
err = d.s3.Delete(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.db.Delete(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.markBlocked(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wants returns false if the hash exists or is blocked, true otherwise
|
|
|
|
func (d *DBBackedS3Store) Wants(hash string) (bool, error) {
|
|
|
|
has, err := d.Has(hash)
|
|
|
|
if has || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blocked, err := d.isBlocked(hash)
|
|
|
|
return !blocked, err
|
|
|
|
}
|
|
|
|
|
2018-08-16 02:17:02 +02:00
|
|
|
// MissingBlobsForKnownStream returns missing blobs for an existing stream
|
|
|
|
// WARNING: if the stream does NOT exist, no blob hashes will be returned, which looks
|
|
|
|
// like no blobs are missing
|
|
|
|
func (d *DBBackedS3Store) MissingBlobsForKnownStream(sdHash string) ([]string, error) {
|
|
|
|
return d.db.MissingBlobsForKnownStream(sdHash)
|
2018-07-26 16:25:47 +02:00
|
|
|
}
|
2018-09-20 17:24:36 +02:00
|
|
|
|
|
|
|
func (d *DBBackedS3Store) markBlocked(hash string) error {
|
|
|
|
err := d.initBlocked()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.Lock()
|
|
|
|
defer d.blockedMu.Unlock()
|
|
|
|
|
|
|
|
d.blocked[hash] = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBBackedS3Store) isBlocked(hash string) (bool, error) {
|
|
|
|
err := d.initBlocked()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.RLock()
|
|
|
|
defer d.blockedMu.RUnlock()
|
|
|
|
|
|
|
|
return d.blocked[hash], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBBackedS3Store) initBlocked() error {
|
|
|
|
// first check without blocking since this is the most likely scenario
|
|
|
|
if d.blocked != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.blockedMu.Lock()
|
|
|
|
defer d.blockedMu.Unlock()
|
|
|
|
|
|
|
|
// check again in case of race condition
|
|
|
|
if d.blocked != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
d.blocked, err = d.db.GetBlocked()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|