1package util
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 LoggingBstore struct {
13 base blockstore.Blockstore
14
15 set map[cid.Cid]blockformat.Block
16}
17
18func NewLoggingBstore(base blockstore.Blockstore) *LoggingBstore {
19 return &LoggingBstore{
20 base: base,
21 set: make(map[cid.Cid]blockformat.Block),
22 }
23}
24
25var _ blockstore.Blockstore = (*LoggingBstore)(nil)
26
27func (bs *LoggingBstore) GetLoggedBlocks() []blockformat.Block {
28 var out []blockformat.Block
29 for _, v := range bs.set {
30 out = append(out, v)
31 }
32 return out
33}
34
35func (bs *LoggingBstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
36 return bs.base.Has(ctx, c)
37}
38
39func (bs *LoggingBstore) Get(ctx context.Context, c cid.Cid) (blockformat.Block, error) {
40 blk, err := bs.base.Get(ctx, c)
41 if err != nil {
42 return nil, err
43 }
44
45 bs.set[c] = blk
46
47 return blk, nil
48}
49
50func (bs *LoggingBstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
51 return bs.base.GetSize(ctx, c)
52}
53
54func (bs *LoggingBstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
55 return fmt.Errorf("deletes not allowed on logging blockstore")
56}
57
58func (bs *LoggingBstore) Put(context.Context, blockformat.Block) error {
59 return fmt.Errorf("writes not allowed on logging blockstore")
60}
61
62func (bs *LoggingBstore) PutMany(context.Context, []blockformat.Block) error {
63 return fmt.Errorf("writes not allowed on logging blockstore")
64}
65
66func (bs *LoggingBstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
67 return nil, fmt.Errorf("iteration not allowed on logging blockstore")
68}
69
70func (bs *LoggingBstore) HashOnRead(enabled bool) {
71
72}