-
Notifications
You must be signed in to change notification settings - Fork 178
/
blob_store.go
83 lines (63 loc) · 2.18 KB
/
blob_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
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
package blobs
import (
"context"
"errors"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
ipld "github.com/ipfs/go-ipld-format"
)
type Blobstore interface {
DeleteBlob(context.Context, cid.Cid) error
Has(context.Context, cid.Cid) (bool, error)
Get(context.Context, cid.Cid) (Blob, error)
// GetSize returns the CIDs mapped BlobSize
GetSize(context.Context, cid.Cid) (int, error)
// Put puts a given blob to the underlying datastore
Put(context.Context, Blob) error
// PutMany puts a slice of blobs at the same time using batching
// capabilities of the underlying datastore whenever possible.
PutMany(context.Context, []Blob) error
// AllKeysChan returns a channel from which
// the CIDs in the Blobstore can be read. It should respect
// the given context, closing the channel if it becomes Done.
AllKeysChan(ctx context.Context) (<-chan cid.Cid, error)
// HashOnRead specifies if every read blob should be
// rehashed to make sure it matches its CID.
HashOnRead(enabled bool)
}
var ErrNotFound = errors.New("blobstore: blob not found")
type blobstoreImpl struct {
bs blockstore.Blockstore
}
func NewBlobstore(ds datastore.Batching) *blobstoreImpl {
return &blobstoreImpl{bs: blockstore.NewBlockstore(ds)}
}
func (bs *blobstoreImpl) DeleteBlob(ctx context.Context, c cid.Cid) error {
return bs.bs.DeleteBlock(ctx, c)
}
func (bs *blobstoreImpl) Has(ctx context.Context, c cid.Cid) (bool, error) {
return bs.bs.Has(ctx, c)
}
func (bs *blobstoreImpl) Get(ctx context.Context, c cid.Cid) (Blob, error) {
blob, err := bs.bs.Get(ctx, c)
if ipld.IsNotFound(err) {
return nil, ErrNotFound
}
return blob, err
}
func (bs *blobstoreImpl) GetSize(ctx context.Context, c cid.Cid) (int, error) {
return bs.bs.GetSize(ctx, c)
}
func (bs *blobstoreImpl) Put(ctx context.Context, blob Blob) error {
return bs.bs.Put(ctx, blob)
}
func (bs *blobstoreImpl) PutMany(ctx context.Context, blobs []Blob) error {
return bs.bs.PutMany(ctx, blobs)
}
func (bs *blobstoreImpl) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return bs.bs.AllKeysChan(ctx)
}
func (bs *blobstoreImpl) HashOnRead(enabled bool) {
bs.bs.HashOnRead(enabled)
}