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 reads map[cid.Cid]blockformat.Block
17}
18
19func New(base blockstore.Blockstore) *RecordingBlockstore {
20 return &RecordingBlockstore{
21 base: base,
22 inserts: make(map[cid.Cid]blockformat.Block),
23 }
24}
25
26func (bs *RecordingBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
27 return bs.base.Has(ctx, c)
28}
29
30func (bs *RecordingBlockstore) Get(ctx context.Context, c cid.Cid) (blockformat.Block, error) {
31 b, err := bs.base.Get(ctx, c)
32 if err != nil {
33 return nil, err
34 }
35 bs.reads[c] = b
36 return b, nil
37}
38
39func (bs *RecordingBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
40 return bs.base.GetSize(ctx, c)
41}
42
43func (bs *RecordingBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
44 return bs.base.DeleteBlock(ctx, c)
45}
46
47func (bs *RecordingBlockstore) Put(ctx context.Context, block blockformat.Block) error {
48 if err := bs.base.Put(ctx, block); err != nil {
49 return err
50 }
51 bs.inserts[block.Cid()] = block
52 return nil
53}
54
55func (bs *RecordingBlockstore) PutMany(ctx context.Context, blocks []blockformat.Block) error {
56 if err := bs.base.PutMany(ctx, blocks); err != nil {
57 return err
58 }
59
60 for _, b := range blocks {
61 bs.inserts[b.Cid()] = b
62 }
63
64 return nil
65}
66
67func (bs *RecordingBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
68 return nil, fmt.Errorf("iteration not allowed on recording blockstore")
69}
70
71func (bs *RecordingBlockstore) HashOnRead(enabled bool) {
72}
73
74func (bs *RecordingBlockstore) GetWriteLog() map[cid.Cid]blockformat.Block {
75 return bs.inserts
76}
77
78func (bs *RecordingBlockstore) GetReadLog() []blockformat.Block {
79 var blocks []blockformat.Block
80 for _, b := range bs.reads {
81 blocks = append(blocks, b)
82 }
83 return blocks
84}