reflector.go/store/dbbacked.go

172 lines
3.6 KiB
Go
Raw Normal View History

package store
import (
"encoding/json"
2018-09-20 17:24:36 +02:00
"sync"
"github.com/lbryio/reflector.go/db"
2019-11-14 01:11:35 +01:00
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/lbryio/lbry.go/v2/stream"
2018-09-20 17:24:36 +02:00
log "github.com/sirupsen/logrus"
)
// DBBackedStore is a store that's backed by a DB. The DB contains data about what's in the store.
type DBBackedStore struct {
blobs BlobStore
2018-09-20 17:24:36 +02:00
db *db.SQL
blockedMu sync.RWMutex
blocked map[string]bool
}
// NewDBBackedStore returns an initialized store pointer.
func NewDBBackedStore(blobs BlobStore, db *db.SQL) *DBBackedStore {
return &DBBackedStore{blobs: blobs, db: db}
}
// Has returns true if the blob is in the store
func (d *DBBackedStore) Has(hash string) (bool, error) {
return d.db.HasBlob(hash)
}
// Get gets the blob
func (d *DBBackedStore) Get(hash string) (stream.Blob, error) {
return d.blobs.Get(hash)
}
// Put stores the blob in the S3 store and stores the blob information in the DB.
func (d *DBBackedStore) Put(hash string, blob stream.Blob) error {
err := d.blobs.Put(hash, blob)
if err != nil {
return err
}
return d.db.AddBlob(hash, len(blob), true)
}
// 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.
func (d *DBBackedStore) PutSD(hash string, blob stream.Blob) error {
var blobContents db.SdBlob
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")
}
err = d.blobs.PutSD(hash, blob)
if err != nil {
return err
}
return d.db.AddSDBlob(hash, len(blob), blobContents)
}
func (d *DBBackedStore) Delete(hash string) error {
err := d.blobs.Delete(hash)
2018-09-11 13:41:29 +02:00
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 *DBBackedStore) Block(hash string) error {
2018-09-20 17:24:36 +02:00
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.blobs.Delete(hash)
2018-09-20 17:24:36 +02:00
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 *DBBackedStore) Wants(hash string) (bool, error) {
2019-07-02 15:21:26 +02:00
blocked, err := d.isBlocked(hash)
if blocked || err != nil {
2018-09-20 17:24:36 +02:00
return false, err
}
2019-07-02 15:21:26 +02:00
has, err := d.Has(hash)
return !has, err
2018-09-20 17:24:36 +02:00
}
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 *DBBackedStore) MissingBlobsForKnownStream(sdHash string) ([]string, error) {
2018-08-16 02:17:02 +02:00
return d.db.MissingBlobsForKnownStream(sdHash)
}
2018-09-20 17:24:36 +02:00
func (d *DBBackedStore) markBlocked(hash string) error {
2018-09-20 17:24:36 +02:00
err := d.initBlocked()
if err != nil {
return err
}
d.blockedMu.Lock()
defer d.blockedMu.Unlock()
d.blocked[hash] = true
return nil
}
func (d *DBBackedStore) isBlocked(hash string) (bool, error) {
2018-09-20 17:24:36 +02:00
err := d.initBlocked()
if err != nil {
return false, err
}
d.blockedMu.RLock()
defer d.blockedMu.RUnlock()
return d.blocked[hash], nil
}
func (d *DBBackedStore) initBlocked() error {
2018-09-20 17:24:36 +02:00
// 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
}