Skip to content

Commit a8473f3

Browse files
committed
coreapi unixfs: separate option to enable inlining
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
1 parent 3a8d127 commit a8473f3

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

core/coreapi/interface/options/unixfs.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type UnixfsAddSettings struct {
2020
CidVersion int
2121
MhType uint64
2222

23+
Inline bool
2324
InlineLimit int
2425
RawLeaves bool
2526
RawLeavesSet bool
@@ -39,7 +40,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
3940
CidVersion: -1,
4041
MhType: mh.SHA2_256,
4142

42-
InlineLimit: 0,
43+
Inline: false,
44+
InlineLimit: 32,
4345
RawLeaves: false,
4446
RawLeavesSet: false,
4547

@@ -124,11 +126,26 @@ func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption {
124126
}
125127
}
126128

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+
127137
// 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.
129146
//
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
132149
func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
133150
return func(settings *UnixfsAddSettings) error {
134151
settings.InlineLimit = limit

core/coreapi/unixfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
8484
return nil, fmt.Errorf("unknown layout: %d", settings.Layout)
8585
}
8686

87-
if settings.InlineLimit > 0 {
87+
if settings.Inline {
8888
fileAdder.CidBuilder = cidutil.InlineBuilder{
8989
Builder: fileAdder.CidBuilder,
9090
Limit: settings.InlineLimit,

core/coreapi/unixfs_test.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
4040
var helloStr = "hello, world!"
4141

4242
// `echo -n | ipfs add`
43+
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
4344

4445
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
4546
mn := mocknet.New(ctx)
@@ -150,7 +151,7 @@ func TestAdd(t *testing.T) {
150151
{
151152
name: "addEmpty",
152153
data: "",
153-
path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
154+
path: emptyFile,
154155
},
155156
// CIDv1 version / rawLeaves
156157
{
@@ -183,13 +184,25 @@ func TestAdd(t *testing.T) {
183184
name: "addInline",
184185
data: helloStr,
185186
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)},
187200
},
188201
{ //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
189202
name: "addInlineRaw",
190203
data: helloStr,
191204
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)},
193206
},
194207
// Chunker / Layout
195208
{
@@ -312,7 +325,7 @@ func TestCatEmptyFile(t *testing.T) {
312325
t.Fatal(err)
313326
}
314327

315-
emptyFilePath, err := coreiface.ParsePath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH")
328+
emptyFilePath, err := coreiface.ParsePath(emptyFile)
316329
if err != nil {
317330
t.Fatal(err)
318331
}

0 commit comments

Comments
 (0)