2018-01-31 02:15:21 +01:00
|
|
|
package store
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2018-03-01 22:28:06 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-02-02 22:49:20 +01:00
|
|
|
"github.com/lbryio/reflector.go/db"
|
|
|
|
"github.com/lbryio/reflector.go/types"
|
|
|
|
)
|
2018-01-31 02:15:21 +01:00
|
|
|
|
|
|
|
type DBBackedS3Store struct {
|
|
|
|
s3 *S3BlobStore
|
|
|
|
db db.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDBBackedS3Store(s3 *S3BlobStore, db db.DB) *DBBackedS3Store {
|
|
|
|
return &DBBackedS3Store{s3: s3, db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBBackedS3Store) Has(hash string) (bool, error) {
|
|
|
|
return d.db.HasBlob(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBBackedS3Store) Get(hash string) ([]byte, error) {
|
|
|
|
return d.s3.Get(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DBBackedS3Store) PutSD(hash string, blob []byte) error {
|
|
|
|
var blobContents types.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")
|
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
|
|
|
}
|