2020-07-08 16:34:56 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2020 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ipfsethdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2021-12-29 06:17:09 +00:00
|
|
|
blocks "github.com/ipfs/go-block-format"
|
2020-07-08 16:34:56 +00:00
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
2021-12-29 06:17:09 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
exchange "github.com/ipfs/go-ipfs-exchange-interface"
|
2020-07-08 16:34:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
blockNotFoundErr = errors.New("block not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockBlockservice struct {
|
|
|
|
blockStore *MockBlockstore
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockBlockservice() blockservice.BlockService {
|
|
|
|
return &MockBlockservice{
|
|
|
|
blockStore: &MockBlockstore{
|
|
|
|
blocks: make(map[string]blocks.Block),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) Blockstore() blockstore.Blockstore {
|
|
|
|
return mbs.blockStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) Exchange() exchange.Interface {
|
|
|
|
panic("Exchange: implement me")
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockservice) AddBlock(ctx context.Context, b blocks.Block) error {
|
|
|
|
return mbs.blockStore.Put(ctx, b)
|
2020-07-08 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockservice) AddBlocks(ctx context.Context, bs []blocks.Block) error {
|
|
|
|
return mbs.blockStore.PutMany(ctx, bs)
|
2020-07-08 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockservice) DeleteBlock(ctx context.Context, c cid.Cid) error {
|
|
|
|
return mbs.blockStore.DeleteBlock(ctx, c)
|
2020-07-08 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
2022-08-01 21:05:18 +00:00
|
|
|
return mbs.blockStore.Get(ctx, c)
|
2020-07-08 16:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) GetBlocks(ctx context.Context, cs []cid.Cid) <-chan blocks.Block {
|
|
|
|
blockChan := make(chan blocks.Block)
|
|
|
|
go func() {
|
|
|
|
for _, c := range cs {
|
2022-08-01 21:05:18 +00:00
|
|
|
if b, err := mbs.blockStore.Get(ctx, c); err == nil {
|
2020-07-08 16:34:56 +00:00
|
|
|
blockChan <- b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return blockChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) Close() error {
|
|
|
|
return mbs.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockservice) SetError(err error) {
|
|
|
|
mbs.err = err
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockBlockstore struct {
|
|
|
|
blocks map[string]blocks.Block
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
|
2020-07-08 16:34:56 +00:00
|
|
|
delete(mbs.blocks, c.String())
|
|
|
|
return mbs.err
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
|
2020-07-08 16:34:56 +00:00
|
|
|
_, ok := mbs.blocks[c.String()]
|
|
|
|
return ok, mbs.err
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
2020-07-08 16:34:56 +00:00
|
|
|
obj, ok := mbs.blocks[c.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, blockNotFoundErr
|
|
|
|
}
|
|
|
|
return obj, mbs.err
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
|
2020-07-08 16:34:56 +00:00
|
|
|
obj, ok := mbs.blocks[c.String()]
|
|
|
|
if !ok {
|
|
|
|
return 0, blockNotFoundErr
|
|
|
|
}
|
|
|
|
return len(obj.RawData()), mbs.err
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) Put(ctx context.Context, b blocks.Block) error {
|
2020-07-08 16:34:56 +00:00
|
|
|
mbs.blocks[b.Cid().String()] = b
|
|
|
|
return mbs.err
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:05:18 +00:00
|
|
|
func (mbs *MockBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
|
2020-07-08 16:34:56 +00:00
|
|
|
for _, b := range bs {
|
|
|
|
mbs.blocks[b.Cid().String()] = b
|
|
|
|
}
|
|
|
|
return mbs.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
panic("AllKeysChan: implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockstore) HashOnRead(enabled bool) {
|
|
|
|
panic("HasOnRead: implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbs *MockBlockstore) SetError(err error) {
|
|
|
|
mbs.err = err
|
|
|
|
}
|