/
blockstore.go
58 lines (47 loc) · 1.17 KB
/
blockstore.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
package test_util
import (
"context"
"fmt"
"sync"
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)
type BlockStoreInMemory struct {
data map[cid.Cid]block.Block
}
func NewBlockStoreInMemory() *BlockStoreInMemory {
return &BlockStoreInMemory{make(map[cid.Cid]block.Block)}
}
func (mb *BlockStoreInMemory) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
d, ok := mb.data[c]
if ok {
return d, nil
}
return nil, fmt.Errorf("not found")
}
func (mb *BlockStoreInMemory) Put(ctx context.Context, b block.Block) error {
mb.data[b.Cid()] = b
return nil
}
type SyncBlockStoreInMemory struct {
bs *BlockStoreInMemory
mu sync.Mutex
}
func (ss *SyncBlockStoreInMemory) Context() context.Context {
return context.Background()
}
func NewSyncBlockStoreInMemory() *SyncBlockStoreInMemory {
return &SyncBlockStoreInMemory{
bs: NewBlockStoreInMemory(),
}
}
func (ss *SyncBlockStoreInMemory) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
ss.mu.Lock()
defer ss.mu.Unlock()
return ss.bs.Get(ctx, c)
}
func (ss *SyncBlockStoreInMemory) Put(ctx context.Context, b block.Block) error {
ss.mu.Lock()
defer ss.mu.Unlock()
return ss.bs.Put(ctx, b)
}