2019-10-03 22:10:29 +02:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
2021-05-21 05:49:02 +02:00
|
|
|
"strings"
|
2019-11-22 12:53:42 +01:00
|
|
|
"time"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2021-01-09 05:08:20 +01:00
|
|
|
"github.com/lbryio/reflector.go/shared"
|
2021-05-21 05:49:02 +02:00
|
|
|
"github.com/lbryio/reflector.go/store"
|
2019-10-03 22:10:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store is a blob store that gets blobs from a peer.
|
|
|
|
// It satisfies the store.BlobStore interface but cannot put or delete blobs.
|
|
|
|
type Store struct {
|
2020-05-12 03:32:11 +02:00
|
|
|
opts StoreOpts
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 12:53:42 +01:00
|
|
|
// StoreOpts allows to set options for a new Store.
|
|
|
|
type StoreOpts struct {
|
|
|
|
Address string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:10:29 +02:00
|
|
|
// NewStore makes a new peer store.
|
2019-11-22 12:53:42 +01:00
|
|
|
func NewStore(opts StoreOpts) *Store {
|
2020-05-12 03:32:11 +02:00
|
|
|
return &Store{opts: opts}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Store) getClient() (*Client, error) {
|
|
|
|
c := &Client{Timeout: p.opts.Timeout}
|
|
|
|
err := c.Connect(p.opts.Address)
|
|
|
|
return c, errors.Prefix("connection error", err)
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
func (p *Store) Name() string { return "peer" }
|
|
|
|
|
2019-10-03 22:10:29 +02:00
|
|
|
// Has asks the peer if they have a hash
|
|
|
|
func (p *Store) Has(hash string) (bool, error) {
|
2020-05-12 03:32:11 +02:00
|
|
|
c, err := p.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
2020-05-12 03:32:11 +02:00
|
|
|
defer c.Close()
|
|
|
|
return c.HasBlob(hash)
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get downloads the blob from the peer
|
2021-01-09 05:08:20 +01:00
|
|
|
func (p *Store) Get(hash string) (stream.Blob, shared.BlobTrace, error) {
|
|
|
|
start := time.Now()
|
2020-05-12 03:32:11 +02:00
|
|
|
c, err := p.getClient()
|
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), p.Name()), err
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
2020-05-12 03:32:11 +02:00
|
|
|
defer c.Close()
|
2021-05-21 05:49:02 +02:00
|
|
|
blob, trace, err := c.GetBlob(hash)
|
|
|
|
if err != nil && strings.Contains(err.Error(), "blob not found") {
|
|
|
|
return nil, trace, store.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return blob, trace, err
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put is not supported
|
2019-10-03 22:34:57 +02:00
|
|
|
func (p *Store) Put(hash string, blob stream.Blob) error {
|
2021-02-23 15:23:46 +01:00
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutSD is not supported
|
2019-10-03 22:34:57 +02:00
|
|
|
func (p *Store) PutSD(hash string, blob stream.Blob) error {
|
2021-02-23 15:23:46 +01:00
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is not supported
|
|
|
|
func (p *Store) Delete(hash string) error {
|
2021-02-23 15:23:46 +01:00
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
2019-10-03 22:10:29 +02:00
|
|
|
}
|
2020-12-23 06:04:42 +01:00
|
|
|
|
|
|
|
// Delete is not supported
|
|
|
|
func (p *Store) Shutdown() {
|
|
|
|
return
|
|
|
|
}
|