1package recording_blockstore
2
3import (
4 "context"
5 "fmt"
6
7 blockformat "github.com/ipfs/go-block-format"
8 "github.com/ipfs/go-cid"
9 blockstore "github.com/ipfs/go-ipfs-blockstore"
10)
11
12type RecordingBlockstore struct {
13 base blockstore.Blockstore
14
15 inserts map[cid.Cid]blockformat.Block
16}
17
18func New(base blockstore.Blockstore) *RecordingBlockstore {
19 return &RecordingBlockstore{
20 base: base,
21 inserts: make(map[cid.Cid]blockformat.Block),
22 }
23}
24
25func (bs *RecordingBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
26 return bs.base.Has(ctx, c)
27}
28
29func (bs *RecordingBlockstore) Get(ctx context.Context, c cid.Cid) (blockformat.Block, error) {
30 return bs.base.Get(ctx, c)
31}
32
33func (bs *RecordingBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
34 return bs.base.GetSize(ctx, c)
35}
36
37func (bs *RecordingBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
38 return bs.base.DeleteBlock(ctx, c)
39}
40
41func (bs *RecordingBlockstore) Put(ctx context.Context, block blockformat.Block) error {
42 if err := bs.base.Put(ctx, block); err != nil {
43 return err
44 }
45 bs.inserts[block.Cid()] = block
46 return nil
47}
48
49func (bs *RecordingBlockstore) PutMany(ctx context.Context, blocks []blockformat.Block) error {
50 if err := bs.base.PutMany(ctx, blocks); err != nil {
51 return err
52 }
53
54 for _, b := range blocks {
55 bs.inserts[b.Cid()] = b
56 }
57
58 return nil
59}
60
61func (bs *RecordingBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
62 return nil, fmt.Errorf("iteration not allowed on recording blockstore")
63}
64
65func (bs *RecordingBlockstore) HashOnRead(enabled bool) {
66}
67
68func (bs *RecordingBlockstore) GetLogMap() map[cid.Cid]blockformat.Block {
69 return bs.inserts
70}
71
72func (bs *RecordingBlockstore) GetLogArray() []blockformat.Block {
73 var blocks []blockformat.Block
74 for _, b := range bs.inserts {
75 blocks = append(blocks, b)
76 }
77 return blocks
78}