/
multiblockstore.go
97 lines (83 loc) · 2.06 KB
/
multiblockstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"sync"
"github.com/cockroachdb/errors"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
ipld "github.com/ipfs/go-ipld-format"
)
var _ blockstore.Blockstore = &multiBlockstore{}
type multiBlockstore struct {
bss []blockstore.Blockstore
mu sync.RWMutex
}
var ErrNotImplemented = errors.New("not implemented")
func (m *multiBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
return ErrNotImplemented
}
func (m *multiBlockstore) AddBlockstore(bs blockstore.Blockstore) {
m.mu.Lock()
defer m.mu.Unlock()
m.bss = append(m.bss, bs)
}
func (m *multiBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, bs := range m.bss {
has, err := bs.Has(ctx, c)
if err != nil {
return false, errors.WithStack(err)
}
if has {
return true, nil
}
}
return false, nil
}
func (m *multiBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, bs := range m.bss {
block, err := bs.Get(ctx, c)
if errors.Is(err, ipld.ErrNotFound{}) {
continue
}
if err != nil {
return nil, errors.WithStack(err)
}
if block != nil {
return block, nil
}
}
return nil, ipld.ErrNotFound{Cid: c}
}
func (m *multiBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, bs := range m.bss {
size, err := bs.GetSize(ctx, c)
if errors.Is(err, ipld.ErrNotFound{}) {
continue
}
if err != nil {
return 0, errors.WithStack(err)
}
if size > 0 {
return size, nil
}
}
return 0, ipld.ErrNotFound{Cid: c}
}
func (m *multiBlockstore) Put(ctx context.Context, block blocks.Block) error {
return ErrNotImplemented
}
func (m *multiBlockstore) PutMany(ctx context.Context, blocks []blocks.Block) error {
return ErrNotImplemented
}
func (m *multiBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, ErrNotImplemented
}
func (m *multiBlockstore) HashOnRead(enabled bool) {
}