reflector.go/store/memory.go
2020-11-04 16:03:44 -05:00

54 lines
1.3 KiB
Go

package store
import (
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/lbryio/lbry.go/v2/stream"
)
// MemoryStore is an in memory only blob store with no persistence.
type MemoryStore struct {
blobs map[string]stream.Blob
}
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
blobs: make(map[string]stream.Blob),
}
}
// Has returns T/F if the blob is currently stored. It will never error.
func (m *MemoryStore) Has(hash string) (bool, error) {
_, ok := m.blobs[hash]
return ok, nil
}
// Get returns the blob byte slice if present and errors if the blob is not found.
func (m *MemoryStore) Get(hash string) (stream.Blob, error) {
blob, ok := m.blobs[hash]
if !ok {
return nil, errors.Err(ErrBlobNotFound)
}
return blob, nil
}
// Put stores the blob in memory
func (m *MemoryStore) Put(hash string, blob stream.Blob) error {
m.blobs[hash] = blob
return nil
}
// PutSD stores the sd blob in memory
func (m *MemoryStore) PutSD(hash string, blob stream.Blob) error {
return m.Put(hash, blob)
}
// Delete deletes the blob from the store
func (m *MemoryStore) Delete(hash string) error {
delete(m.blobs, hash)
return nil
}
// Debug returns the blobs in memory. It's useful for testing and debugging.
func (m *MemoryStore) Debug() map[string]stream.Blob {
return m.blobs
}