2020-10-12 22:52:38 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
// NewMemory returns a temporary memory-backed blockstore.
|
|
|
|
func NewMemory() MemBlockstore {
|
|
|
|
return make(MemBlockstore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemBlockstore is a terminal blockstore that keeps blocks in memory.
|
|
|
|
type MemBlockstore map[cid.Cid]blocks.Block
|
2020-10-12 22:52:38 +00:00
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
delete(m, k)
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-10 12:40:32 +00:00
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) DeleteMany(ctx context.Context, ks []cid.Cid) error {
|
2021-03-02 14:45:45 +00:00
|
|
|
for _, k := range ks {
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
_, ok := m[k]
|
|
|
|
return ok, nil
|
|
|
|
}
|
2020-11-10 12:40:32 +00:00
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
|
2020-11-10 12:40:32 +00:00
|
|
|
b, ok := m[k]
|
|
|
|
if !ok {
|
2020-11-01 20:58:01 +00:00
|
|
|
return ErrNotFound
|
2020-11-10 12:40:32 +00:00
|
|
|
}
|
|
|
|
return callback(b.RawData())
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
b, ok := m[k]
|
|
|
|
if !ok {
|
2020-11-01 20:58:01 +00:00
|
|
|
return nil, ErrNotFound
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSize returns the CIDs mapped BlockSize
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
b, ok := m[k]
|
|
|
|
if !ok {
|
2020-11-01 20:58:01 +00:00
|
|
|
return 0, ErrNotFound
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
return len(b.RawData()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts a given block to the underlying datastore
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) Put(ctx context.Context, b blocks.Block) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
// Convert to a basic block for safety, but try to reuse the existing
|
|
|
|
// block if it's already a basic block.
|
|
|
|
k := b.Cid()
|
|
|
|
if _, ok := b.(*blocks.BasicBlock); !ok {
|
|
|
|
// If we already have the block, abort.
|
|
|
|
if _, ok := m[k]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// the error is only for debugging.
|
|
|
|
b, _ = blocks.NewBlockWithCid(b.RawData(), b.Cid())
|
|
|
|
}
|
|
|
|
m[b.Cid()] = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutMany puts a slice of blocks at the same time using batching
|
|
|
|
// capabilities of the underlying datastore whenever possible.
|
2021-11-19 01:50:25 +00:00
|
|
|
func (m MemBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
|
2020-10-12 22:52:38 +00:00
|
|
|
for _, b := range bs {
|
2021-11-19 01:50:25 +00:00
|
|
|
_ = m.Put(ctx, b) // can't fail
|
2020-10-12 22:52:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllKeysChan returns a channel from which
|
|
|
|
// the CIDs in the Blockstore can be read. It should respect
|
|
|
|
// the given context, closing the channel if it becomes Done.
|
2021-01-29 20:01:00 +00:00
|
|
|
func (m MemBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
2020-10-12 22:52:38 +00:00
|
|
|
ch := make(chan cid.Cid, len(m))
|
|
|
|
for k := range m {
|
|
|
|
ch <- k
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashOnRead specifies if every read block should be
|
|
|
|
// rehashed to make sure it matches its CID.
|
2021-12-11 21:03:00 +00:00
|
|
|
func (m MemBlockstore) HashOnRead(enabled bool) {
|
2020-10-12 22:52:38 +00:00
|
|
|
// no-op
|
|
|
|
}
|