Skip to content

Commit

Permalink
coreapi unixfs: separate option to enable inlining
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Oct 2, 2018
1 parent 3a8d127 commit a8473f3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
25 changes: 21 additions & 4 deletions core/coreapi/interface/options/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type UnixfsAddSettings struct {
CidVersion int
MhType uint64

Inline bool
InlineLimit int
RawLeaves bool
RawLeavesSet bool
Expand All @@ -39,7 +40,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
CidVersion: -1,
MhType: mh.SHA2_256,

InlineLimit: 0,
Inline: false,
InlineLimit: 32,
RawLeaves: false,
RawLeavesSet: false,

Expand Down Expand Up @@ -124,11 +126,26 @@ func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption {
}
}

// Inline tells the adder to inline small blocks into CIDs
func (unixfsOpts) Inline(enable bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.Inline = enable
return nil
}
}

// InlineLimit sets the amount of bytes below which blocks will be encoded
// directly into CID instead of being stored and addressed by it's hash
// directly into CID instead of being stored and addressed by it's hash.
// Specifying this option won't enable block inlining. For that use `Inline`
// option. Default: 32 bytes
//
// Note that while there is no hard limit on the number of bytes, it should
// be kept at a reasonably low value, like 64 bytes if you intend to display
// these hashes. Larger values like 256 bytes will work fine, but may affect
// de-duplication of smaller blocks.
//
// Note that while there is no hard limit on the number of bytes here, it should
// be kept at something reasonably low like 32b (default for 'ipfs add')
// Setting this value too high may cause various problems, such as render some
// blocks unfetchable
func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.InlineLimit = limit
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
return nil, fmt.Errorf("unknown layout: %d", settings.Layout)
}

if settings.InlineLimit > 0 {
if settings.Inline {
fileAdder.CidBuilder = cidutil.InlineBuilder{
Builder: fileAdder.CidBuilder,
Limit: settings.InlineLimit,
Expand Down
21 changes: 17 additions & 4 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
var helloStr = "hello, world!"

// `echo -n | ipfs add`
var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"

func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
mn := mocknet.New(ctx)
Expand Down Expand Up @@ -150,7 +151,7 @@ func TestAdd(t *testing.T) {
{
name: "addEmpty",
data: "",
path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
path: emptyFile,
},
// CIDv1 version / rawLeaves
{
Expand Down Expand Up @@ -183,13 +184,25 @@ func TestAdd(t *testing.T) {
name: "addInline",
data: helloStr,
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32)},
opts: []options.UnixfsAddOption{options.Unixfs.Inline(true)},
},
{
name: "addInlineLimit",
data: helloStr,
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true)},
},
{
name: "addInlineZero",
data: "",
path: "/ipfs/z2yYDV",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(0), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
},
{ //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
name: "addInlineRaw",
data: helloStr,
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)},
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true)},
},
// Chunker / Layout
{
Expand Down Expand Up @@ -312,7 +325,7 @@ func TestCatEmptyFile(t *testing.T) {
t.Fatal(err)
}

emptyFilePath, err := coreiface.ParsePath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH")
emptyFilePath, err := coreiface.ParsePath(emptyFile)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit a8473f3

Please sign in to comment.