coreapi unixfs: separate option to enable inlining
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 2, 2018 at 09:42 UTC
374fb00cc36c7c094d037188e4c082d33c5bfd63
3 files changed
+39
-9
core/coreapi/interface/options/unixfs.go
+21
-4
@@ -20,6 +20,7 @@ type UnixfsAddSettings struct {
20
CidVersion int
21
MhType uint64
22
23
+ Inline bool
24
InlineLimit int
25
RawLeaves bool
26
RawLeavesSet bool
@@ -39,7 +40,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
40
CidVersion: -1,
41
MhType: mh.SHA2_256,
42
42
- InlineLimit: 0,
43
+ Inline: false,
44
+ InlineLimit: 32,
45
RawLeaves: false,
46
RawLeavesSet: false,
47
@@ -124,11 +126,26 @@ func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption {
126
}
127
}
128
129
+// Inline tells the adder to inline small blocks into CIDs
130
+func (unixfsOpts) Inline(enable bool) UnixfsAddOption {
131
+ return func(settings *UnixfsAddSettings) error {
132
+ settings.Inline = enable
133
+ return nil
134
+ }
135
+}
136
+
137
// InlineLimit sets the amount of bytes below which blocks will be encoded
128
-// directly into CID instead of being stored and addressed by it's hash
138
+// directly into CID instead of being stored and addressed by it's hash.
139
+// Specifying this option won't enable block inlining. For that use `Inline`
140
+// option. Default: 32 bytes
141
+//
142
+// Note that while there is no hard limit on the number of bytes, it should
143
+// be kept at a reasonably low value, like 64 bytes if you intend to display
144
+// these hashes. Larger values like 256 bytes will work fine, but may affect
145
+// de-duplication of smaller blocks.
146
//
130
-// Note that while there is no hard limit on the number of bytes here, it should
131
-// be kept at something reasonably low like 32b (default for 'ipfs add')
147
+// Setting this value too high may cause various problems, such as render some
148
+// blocks unfetchable
149
func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
150
return func(settings *UnixfsAddSettings) error {
151
settings.InlineLimit = limit
core/coreapi/unixfs.go
+1
-1
@@ -84,7 +84,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
84
return nil, fmt.Errorf("unknown layout: %d", settings.Layout)
85
}
86
87
- if settings.InlineLimit > 0 {
87
+ if settings.Inline {
88
fileAdder.CidBuilder = cidutil.InlineBuilder{
89
Builder: fileAdder.CidBuilder,
90
Limit: settings.InlineLimit,
core/coreapi/unixfs_test.go
+17
-4
@@ -40,6 +40,7 @@ var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
40
var helloStr = "hello, world!"
41
42
// `echo -n | ipfs add`
43
+var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
44
45
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
46
mn := mocknet.New(ctx)
@@ -150,7 +151,7 @@ func TestAdd(t *testing.T) {
151
{
152
name: "addEmpty",
153
data: "",
153
- path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
154
+ path: emptyFile,
155
},
156
// CIDv1 version / rawLeaves
157
{
@@ -183,13 +184,25 @@ func TestAdd(t *testing.T) {
184
name: "addInline",
185
data: helloStr,
186
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
186
- opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32)},
187
+ opts: []options.UnixfsAddOption{options.Unixfs.Inline(true)},
188
+ },
189
+ {
190
+ name: "addInlineLimit",
191
+ data: helloStr,
192
+ path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
193
+ opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true)},
194
+ },
195
+ {
196
+ name: "addInlineZero",
197
+ data: "",
198
+ path: "/ipfs/z2yYDV",
199
+ opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(0), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
200
},
201
{ //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
202
name: "addInlineRaw",
203
data: helloStr,
204
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
192
- opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)},
205
+ opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
206
},
207
// Chunker / Layout
208
{
@@ -312,7 +325,7 @@ func TestCatEmptyFile(t *testing.T) {
325
t.Fatal(err)
326
}
327
315
- emptyFilePath, err := coreiface.ParsePath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH")
328
+ emptyFilePath, err := coreiface.ParsePath(emptyFile)
329
if err != nil {
330
t.Fatal(err)
331
}