2021-04-12 23:05:50 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
|
|
|
"github.com/lbryio/reflector.go/shared"
|
2021-07-20 02:09:14 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2021-04-12 23:05:50 +02:00
|
|
|
)
|
|
|
|
|
2021-07-20 02:09:14 +02:00
|
|
|
// ITTTStore performs an operation on this storage, if this fails, it attempts to run it on that
|
2021-04-12 23:05:50 +02:00
|
|
|
type ITTTStore struct {
|
|
|
|
this, that BlobStore
|
|
|
|
}
|
|
|
|
|
2021-07-20 02:09:14 +02:00
|
|
|
// NewITTTStore returns a new instance of the IF THIS THAN THAT store
|
2021-04-12 23:05:50 +02:00
|
|
|
func NewITTTStore(this, that BlobStore) *ITTTStore {
|
|
|
|
return &ITTTStore{
|
|
|
|
this: this,
|
|
|
|
that: that,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nameIttt = "ittt"
|
|
|
|
|
|
|
|
// Name is the cache type name
|
|
|
|
func (c *ITTTStore) Name() string { return nameIttt }
|
|
|
|
|
2021-07-20 02:09:14 +02:00
|
|
|
// Has checks in this for a hash, if it fails it checks in that. It returns true if either store has it.
|
2021-04-12 23:05:50 +02:00
|
|
|
func (c *ITTTStore) Has(hash string) (bool, error) {
|
|
|
|
has, err := c.this.Has(hash)
|
|
|
|
if err != nil || !has {
|
|
|
|
has, err = c.that.Has(hash)
|
|
|
|
}
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get tries to get the blob from this first, falling back to that.
|
|
|
|
func (c *ITTTStore) Get(hash string) (stream.Blob, shared.BlobTrace, error) {
|
|
|
|
start := time.Now()
|
|
|
|
blob, trace, err := c.this.Get(hash)
|
|
|
|
if err == nil {
|
|
|
|
metrics.ThisHitCount.Inc()
|
|
|
|
return blob, trace.Stack(time.Since(start), c.Name()), err
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, trace, err = c.that.Get(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, trace.Stack(time.Since(start), c.Name()), err
|
|
|
|
}
|
|
|
|
metrics.ThatHitCount.Inc()
|
|
|
|
return blob, trace.Stack(time.Since(start), c.Name()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put not implemented
|
|
|
|
func (c *ITTTStore) Put(hash string, blob stream.Blob) error {
|
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutSD not implemented
|
|
|
|
func (c *ITTTStore) PutSD(hash string, blob stream.Blob) error {
|
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete not implemented
|
|
|
|
func (c *ITTTStore) Delete(hash string) error {
|
|
|
|
return errors.Err(shared.ErrNotImplemented)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the store gracefully
|
2021-07-24 00:08:13 +02:00
|
|
|
func (c *ITTTStore) Shutdown() {}
|