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