forked from willscott/go-selfish-bitswap-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
56 lines (46 loc) · 1.15 KB
/
store.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
package util
import (
"context"
"errors"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multicodec"
bitswapserver "github.com/willscott/go-selfish-bitswap-client/server"
)
var ErrNotHave = errors.New("not found")
func NewMemStore(of map[cid.Cid][]byte) bitswapserver.Blockstore {
return &store{of}
}
type store struct {
db map[cid.Cid][]byte
}
func (s *store) Has(ctx context.Context, c cid.Cid) (bool, error) {
_, ok := s.db[c]
if ok {
return true, nil
}
return false, nil
}
func (s *store) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
blk, ok := s.db[c]
if ok {
return blocks.NewBlockWithCid(blk, c)
}
return nil, ErrNotHave
}
// TODO: To encode the blcoks here, take as input an encoder callback function to run on each array item.
func (s *store) GetAll() map[cid.Cid][]byte {
return s.db
}
func Add(s bitswapserver.Blockstore, blk []byte) cid.Cid {
st, ok := s.(*store)
if !ok {
return cid.Undef
}
name, err := cid.V1Builder{Codec: uint64(multicodec.Raw), MhType: uint64(multicodec.Sha2_256)}.Sum(blk)
if err != nil {
return cid.Undef
}
st.db[name] = blk
return name
}