From 6337e69ff7e00b31c7cfe101161d652ac27c2502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 20 Sep 2018 16:40:31 +0200 Subject: [PATCH] coreapi unixfs: layout/chunker options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/options/unixfs.go | 27 ++++++++++++++++++++++++ core/coreapi/unixfs.go | 12 +++++++++-- core/coreapi/unixfs_test.go | 15 ++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/core/coreapi/interface/options/unixfs.go b/core/coreapi/interface/options/unixfs.go index 3c46ed086..fe41af9a8 100644 --- a/core/coreapi/interface/options/unixfs.go +++ b/core/coreapi/interface/options/unixfs.go @@ -4,6 +4,13 @@ import ( mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" ) +type Layout int + +const ( + BalancedLayout Layout = iota + TrickleLeyout +) + type UnixfsAddSettings struct { CidVersion int MhType uint64 @@ -11,6 +18,9 @@ type UnixfsAddSettings struct { InlineLimit int RawLeaves bool RawLeavesSet bool + + Chunker string + Layout Layout } type UnixfsAddOption func(*UnixfsAddSettings) error @@ -23,6 +33,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) { InlineLimit: 0, RawLeaves: false, RawLeavesSet: false, + + Chunker: "size-262144", + Layout: BalancedLayout, } for _, opt := range opts { @@ -67,3 +80,17 @@ func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption { return nil } } + +func (unixfsOpts) Chunker(chunker string) UnixfsAddOption { + return func(settings *UnixfsAddSettings) error { + settings.Chunker = chunker + return nil + } +} + +func (unixfsOpts) Layout(layout Layout) UnixfsAddOption { + return func(settings *UnixfsAddSettings) error { + settings.Layout = layout + return nil + } +} diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 19e4fc90c..1133964cb 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -68,10 +68,9 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options. } fileAdder.Out = outChan - //fileAdder.Chunker = chunker + fileAdder.Chunker = settings.Chunker //fileAdder.Progress = progress //fileAdder.Hidden = hidden - //fileAdder.Trickle = trickle //fileAdder.Wrap = wrap //fileAdder.Pin = dopin fileAdder.Silent = false @@ -80,6 +79,15 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options. //fileAdder.Name = pathName fileAdder.CidBuilder = prefix + switch settings.Layout { + case options.BalancedLayout: + // Default + case options.TrickleLeyout: + fileAdder.Trickle = true + default: + return nil, fmt.Errorf("unknown layout: %d", settings.Layout) + } + if settings.InlineLimit > 0 { fileAdder.CidBuilder = cidutil.InlineBuilder{ Builder: fileAdder.CidBuilder, diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index c5d483a08..bcad984a5 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -191,6 +191,19 @@ func TestAdd(t *testing.T) { path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe", opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)}, }, + // Chunker / Layout + { + name: "addChunks", + data: strings.Repeat("aoeuidhtns", 200), + path: "/ipfs/QmRo11d4QJrST47aaiGVJYwPhoNA4ihRpJ5WaxBWjWDwbX", + opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4")}, + }, + { + name: "addChunksTrickle", + data: strings.Repeat("aoeuidhtns", 200), + path: "/ipfs/QmNNhDGttafX3M1wKWixGre6PrLFGjnoPEDXjBYpTv93HP", + opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4"), options.Unixfs.Layout(options.TrickleLeyout)}, + }, } for _, testCase := range cases { @@ -211,7 +224,7 @@ func TestAdd(t *testing.T) { } if p.String() != testCase.path { - t.Fatalf("expected path %s, got: %s", hello, p) + t.Errorf("expected path %s, got: %s", testCase.path, p) } r, err := api.Unixfs().Cat(ctx, p)