2020-10-12 22:52:38 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2023-01-26 15:41:28 +00:00
|
|
|
blocks "github.com/ipfs/go-libipfs/blocks"
|
2020-10-12 22:52:38 +00:00
|
|
|
)
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
// NewMemorySync returns a thread-safe in-memory blockstore.
|
|
|
|
func NewMemorySync() *SyncBlockstore {
|
2021-01-29 20:01:00 +00:00
|
|
|
return &SyncBlockstore{bs: make(MemBlockstore)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncBlockstore is a terminal blockstore that is a synchronized version
|
|
|
|
// of MemBlockstore.
|
|
|
|
type SyncBlockstore struct {
|
2020-10-12 22:52:38 +00:00
|
|
|
mu sync.RWMutex
|
2021-01-29 20:01:00 +00:00
|
|
|
bs MemBlockstore // specifically use a memStore to save indirection overhead.
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 07:11:28 +00:00
|
|
|
func (*SyncBlockstore) Flush(context.Context) error { return nil }
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.DeleteBlock(ctx, k)
|
2021-03-02 14:45:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) DeleteMany(ctx context.Context, ks []cid.Cid) error {
|
2021-03-02 14:45:45 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.DeleteMany(ctx, ks)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
2020-11-10 12:40:32 +00:00
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.Has(ctx, k)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
2020-11-10 12:40:32 +00:00
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
|
2020-11-10 12:40:32 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.View(ctx, k, callback)
|
2020-11-10 12:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.Get(ctx, k)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.GetSize(ctx, k)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) Put(ctx context.Context, b blocks.Block) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.Put(ctx, b)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m *SyncBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return m.bs.PutMany(ctx, bs)
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
func (m *SyncBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
// this blockstore implementation doesn't do any async work.
|
|
|
|
return m.bs.AllKeysChan(ctx)
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (m *SyncBlockstore) HashOnRead(enabled bool) {
|
2020-10-12 22:52:38 +00:00
|
|
|
// noop
|
|
|
|
}
|