2018-02-07 15:21:20 -05:00
|
|
|
package store
|
|
|
|
|
2019-10-03 16:34:57 -04:00
|
|
|
import (
|
2019-11-13 19:11:35 -05:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2019-10-03 16:34:57 -04:00
|
|
|
)
|
2018-02-07 15:21:20 -05:00
|
|
|
|
2018-05-29 21:38:55 -04:00
|
|
|
// MemoryBlobStore is an in memory only blob store with no persistence.
|
2018-02-07 15:21:20 -05:00
|
|
|
type MemoryBlobStore struct {
|
2019-10-03 16:47:23 -04:00
|
|
|
blobs map[string]stream.Blob
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemoryBlobStore() *MemoryBlobStore {
|
|
|
|
return &MemoryBlobStore{
|
|
|
|
blobs: make(map[string]stream.Blob),
|
|
|
|
}
|
2018-02-07 15:21:20 -05:00
|
|
|
}
|
|
|
|
|
2018-05-29 21:38:55 -04:00
|
|
|
// Has returns T/F if the blob is currently stored. It will never error.
|
2018-02-07 15:21:20 -05:00
|
|
|
func (m *MemoryBlobStore) Has(hash string) (bool, error) {
|
|
|
|
_, ok := m.blobs[hash]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
2018-05-29 21:38:55 -04:00
|
|
|
// Get returns the blob byte slice if present and errors if the blob is not found.
|
2019-10-03 16:34:57 -04:00
|
|
|
func (m *MemoryBlobStore) Get(hash string) (stream.Blob, error) {
|
2018-02-07 15:21:20 -05:00
|
|
|
blob, ok := m.blobs[hash]
|
|
|
|
if !ok {
|
2019-10-03 16:47:23 -04:00
|
|
|
return nil, errors.Err(ErrBlobNotFound)
|
2018-02-07 15:21:20 -05:00
|
|
|
}
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:25:47 -04:00
|
|
|
// Put stores the blob in memory
|
2019-10-03 16:34:57 -04:00
|
|
|
func (m *MemoryBlobStore) Put(hash string, blob stream.Blob) error {
|
2018-02-07 15:21:20 -05:00
|
|
|
m.blobs[hash] = blob
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:25:47 -04:00
|
|
|
// PutSD stores the sd blob in memory
|
2019-10-03 16:34:57 -04:00
|
|
|
func (m *MemoryBlobStore) PutSD(hash string, blob stream.Blob) error {
|
2018-02-07 15:21:20 -05:00
|
|
|
return m.Put(hash, blob)
|
|
|
|
}
|
2018-09-11 07:41:29 -04:00
|
|
|
|
|
|
|
// Delete deletes the blob from the store
|
|
|
|
func (m *MemoryBlobStore) Delete(hash string) error {
|
|
|
|
delete(m.blobs, hash)
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-29 14:42:45 -05:00
|
|
|
|
|
|
|
// Debug returns the blobs in memory. It's useful for testing and debugging.
|
2019-10-03 16:47:23 -04:00
|
|
|
func (m *MemoryBlobStore) Debug() map[string]stream.Blob {
|
2019-01-29 14:42:45 -05:00
|
|
|
return m.blobs
|
|
|
|
}
|