2018-02-07 21:21:20 +01:00
|
|
|
package store
|
|
|
|
|
2018-02-22 19:48:46 +01:00
|
|
|
import "github.com/lbryio/lbry.go/errors"
|
2018-02-07 21:21:20 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// MemoryBlobStore is an in memory only blob store with no persistence.
|
2018-02-07 21:21:20 +01:00
|
|
|
type MemoryBlobStore struct {
|
|
|
|
blobs map[string][]byte
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Has returns T/F if the blob is currently stored. It will never error.
|
2018-02-07 21:21:20 +01:00
|
|
|
func (m *MemoryBlobStore) Has(hash string) (bool, error) {
|
|
|
|
if m.blobs == nil {
|
|
|
|
m.blobs = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
_, ok := m.blobs[hash]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Get returns the blob byte slice if present and errors if the blob is not found.
|
2018-02-07 21:21:20 +01:00
|
|
|
func (m *MemoryBlobStore) Get(hash string) ([]byte, error) {
|
|
|
|
if m.blobs == nil {
|
|
|
|
m.blobs = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
blob, ok := m.blobs[hash]
|
|
|
|
if !ok {
|
|
|
|
return []byte{}, errors.Err(ErrBlobNotFound)
|
|
|
|
}
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// Put stores the blob in memory
|
2018-02-07 21:21:20 +01:00
|
|
|
func (m *MemoryBlobStore) Put(hash string, blob []byte) error {
|
|
|
|
if m.blobs == nil {
|
|
|
|
m.blobs = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
m.blobs[hash] = blob
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// PutSD stores the sd blob in memory
|
2018-02-07 21:21:20 +01:00
|
|
|
func (m *MemoryBlobStore) PutSD(hash string, blob []byte) error {
|
|
|
|
return m.Put(hash, blob)
|
|
|
|
}
|