/
filestore.go
164 lines (136 loc) · 4.88 KB
/
filestore.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package stores
import (
"context"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/ipfs/go-filestore"
bstore "github.com/ipfs/go-ipfs-blockstore"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
mh "github.com/multiformats/go-multihash"
"golang.org/x/xerrors"
)
// ReadOnlyFilestore opens the CAR in the specified path as as a read-only
// blockstore, and fronts it with a Filestore whose positional mappings are
// stored inside the CAR itself. It must be closed after done.
func ReadOnlyFilestore(path string) (ClosableBlockstore, error) {
ro, err := OpenReadOnly(path,
carv2.ZeroLengthSectionAsEOF(true),
blockstore.UseWholeCIDs(true),
)
if err != nil {
return nil, err
}
bs, err := FilestoreOf(ro)
if err != nil {
return nil, err
}
return &closableBlockstore{Blockstore: bs, closeFn: ro.Close}, nil
}
// ReadWriteFilestore opens the CAR in the specified path as as a read-write
// blockstore, and fronts it with a Filestore whose positional mappings are
// stored inside the CAR itself. It must be closed after done. Closing will
// finalize the CAR blockstore.
func ReadWriteFilestore(path string, roots ...cid.Cid) (ClosableBlockstore, error) {
rw, err := OpenReadWrite(path, roots,
carv2.ZeroLengthSectionAsEOF(true),
carv2.StoreIdentityCIDs(true),
blockstore.UseWholeCIDs(true),
)
if err != nil {
return nil, err
}
bs, err := FilestoreOf(rw)
if err != nil {
return nil, err
}
return &closableBlockstore{Blockstore: bs, closeFn: rw.Finalize}, nil
}
// FilestoreOf returns a FileManager/Filestore backed entirely by a
// blockstore without requiring a datastore. It achieves this by coercing the
// blockstore into a datastore. The resulting blockstore is suitable for usage
// with DagBuilderHelper with DagBuilderParams#NoCopy=true.
func FilestoreOf(bs bstore.Blockstore) (bstore.Blockstore, error) {
coercer := &dsCoercer{bs}
// the FileManager stores positional infos (positional mappings) in a
// datastore, which in our case is the blockstore coerced into a datastore.
//
// Passing the root dir as a base path makes me uneasy, but these filestores
// are only used locally.
fm := filestore.NewFileManager(coercer, "/")
fm.AllowFiles = true
// the Filestore sifts leaves (PosInfos) from intermediate nodes. It writes
// PosInfo leaves to the datastore (which in our case is the coerced
// blockstore), and the intermediate nodes to the blockstore proper (since
// they cannot be mapped to the file.
fstore := filestore.NewFilestore(bs, fm)
bs = bstore.NewIdStore(fstore)
return bs, nil
}
var cidBuilder = cid.V1Builder{Codec: cid.Raw, MhType: mh.SHA2_256}
// dsCoercer coerces a Blockstore to present a datastore interface, apt for
// usage with the Filestore/FileManager. Only PosInfos will be written through
// this path.
type dsCoercer struct {
bstore.Blockstore
}
var _ datastore.Batching = (*dsCoercer)(nil)
func (crcr *dsCoercer) Get(ctx context.Context, key datastore.Key) (value []byte, err error) {
c, err := cidBuilder.Sum(key.Bytes())
if err != nil {
return nil, xerrors.Errorf("failed to create cid: %w", err)
}
blk, err := crcr.Blockstore.Get(ctx, c)
if err != nil {
return nil, xerrors.Errorf("failed to get cid %s: %w", c, err)
}
return blk.RawData(), nil
}
func (crcr *dsCoercer) Put(ctx context.Context, key datastore.Key, value []byte) error {
c, err := cidBuilder.Sum(key.Bytes())
if err != nil {
return xerrors.Errorf("failed to create cid: %w", err)
}
blk, err := blocks.NewBlockWithCid(value, c)
if err != nil {
return xerrors.Errorf("failed to create block: %w", err)
}
if err := crcr.Blockstore.Put(ctx, blk); err != nil {
return xerrors.Errorf("failed to put block: %w", err)
}
return nil
}
func (crcr *dsCoercer) Has(ctx context.Context, key datastore.Key) (exists bool, err error) {
c, err := cidBuilder.Sum(key.Bytes())
if err != nil {
return false, xerrors.Errorf("failed to create cid: %w", err)
}
return crcr.Blockstore.Has(ctx, c)
}
func (crcr *dsCoercer) Batch(_ context.Context) (datastore.Batch, error) {
return datastore.NewBasicBatch(crcr), nil
}
func (crcr *dsCoercer) GetSize(_ context.Context, _ datastore.Key) (size int, err error) {
return 0, xerrors.New("operation NOT supported: GetSize")
}
func (crcr *dsCoercer) Query(_ context.Context, _ query.Query) (query.Results, error) {
return nil, xerrors.New("operation NOT supported: Query")
}
func (crcr *dsCoercer) Delete(_ context.Context, _ datastore.Key) error {
return xerrors.New("operation NOT supported: Delete")
}
func (crcr *dsCoercer) Sync(_ context.Context, _ datastore.Key) error {
return xerrors.New("operation NOT supported: Sync")
}
func (crcr *dsCoercer) Close() error {
return nil
}
type closableBlockstore struct {
bstore.Blockstore
closeFn func() error
}
func (c *closableBlockstore) Close() error {
return c.closeFn()
}