2018-02-07 21:21:20 +01:00
|
|
|
package store
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
import (
|
2020-11-02 20:48:56 +01:00
|
|
|
"sync"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2019-10-03 22:34:57 +02:00
|
|
|
)
|
2018-02-07 21:21:20 +01:00
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
// MemStore is an in memory only blob store with no persistence.
|
|
|
|
type MemStore struct {
|
2019-10-03 22:47:23 +02:00
|
|
|
blobs map[string]stream.Blob
|
2020-11-02 20:48:56 +01:00
|
|
|
mu *sync.RWMutex
|
2019-10-03 22:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
func NewMemStore() *MemStore {
|
|
|
|
return &MemStore{
|
2019-10-03 22:47:23 +02:00
|
|
|
blobs: make(map[string]stream.Blob),
|
2020-11-02 20:48:56 +01:00
|
|
|
mu: &sync.RWMutex{},
|
2019-10-03 22:47:23 +02:00
|
|
|
}
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:49:02 +02:00
|
|
|
const nameMem = "mem"
|
|
|
|
|
|
|
|
// Name is the cache type name
|
|
|
|
func (m *MemStore) Name() string { return nameMem }
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Has returns T/F if the blob is currently stored. It will never error.
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) Has(hash string) (bool, error) {
|
2020-11-02 20:48:56 +01:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2018-02-07 21:21:20 +01:00
|
|
|
_, 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.
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) Get(hash string) (stream.Blob, error) {
|
2020-11-02 20:48:56 +01:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2018-02-07 21:21:20 +01:00
|
|
|
blob, ok := m.blobs[hash]
|
|
|
|
if !ok {
|
2019-10-03 22:47:23 +02:00
|
|
|
return nil, errors.Err(ErrBlobNotFound)
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// Put stores the blob in memory
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) Put(hash string, blob stream.Blob) error {
|
2020-11-02 20:48:56 +01:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-02-07 21:21:20 +01:00
|
|
|
m.blobs[hash] = blob
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// PutSD stores the sd blob in memory
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) PutSD(hash string, blob stream.Blob) error {
|
2018-02-07 21:21:20 +01:00
|
|
|
return m.Put(hash, blob)
|
|
|
|
}
|
2018-09-11 13:41:29 +02:00
|
|
|
|
|
|
|
// Delete deletes the blob from the store
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) Delete(hash string) error {
|
2020-11-02 20:48:56 +01:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-09-11 13:41:29 +02:00
|
|
|
delete(m.blobs, hash)
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-29 20:42:45 +01:00
|
|
|
|
|
|
|
// Debug returns the blobs in memory. It's useful for testing and debugging.
|
2020-10-22 19:49:02 +02:00
|
|
|
func (m *MemStore) Debug() map[string]stream.Blob {
|
2020-11-02 20:48:56 +01:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2019-01-29 20:42:45 +01:00
|
|
|
return m.blobs
|
|
|
|
}
|