1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-21 19:50:56 +08:00

coreapi unixfs: layout/chunker options

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-09-20 16:40:31 +02:00
parent 041e55d706
commit 6337e69ff7
3 changed files with 51 additions and 3 deletions

View File

@ -4,6 +4,13 @@ import (
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
) )
type Layout int
const (
BalancedLayout Layout = iota
TrickleLeyout
)
type UnixfsAddSettings struct { type UnixfsAddSettings struct {
CidVersion int CidVersion int
MhType uint64 MhType uint64
@ -11,6 +18,9 @@ type UnixfsAddSettings struct {
InlineLimit int InlineLimit int
RawLeaves bool RawLeaves bool
RawLeavesSet bool RawLeavesSet bool
Chunker string
Layout Layout
} }
type UnixfsAddOption func(*UnixfsAddSettings) error type UnixfsAddOption func(*UnixfsAddSettings) error
@ -23,6 +33,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
InlineLimit: 0, InlineLimit: 0,
RawLeaves: false, RawLeaves: false,
RawLeavesSet: false, RawLeavesSet: false,
Chunker: "size-262144",
Layout: BalancedLayout,
} }
for _, opt := range opts { for _, opt := range opts {
@ -67,3 +80,17 @@ func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
return nil 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
}
}

View File

@ -68,10 +68,9 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
} }
fileAdder.Out = outChan fileAdder.Out = outChan
//fileAdder.Chunker = chunker fileAdder.Chunker = settings.Chunker
//fileAdder.Progress = progress //fileAdder.Progress = progress
//fileAdder.Hidden = hidden //fileAdder.Hidden = hidden
//fileAdder.Trickle = trickle
//fileAdder.Wrap = wrap //fileAdder.Wrap = wrap
//fileAdder.Pin = dopin //fileAdder.Pin = dopin
fileAdder.Silent = false fileAdder.Silent = false
@ -80,6 +79,15 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
//fileAdder.Name = pathName //fileAdder.Name = pathName
fileAdder.CidBuilder = prefix 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 { if settings.InlineLimit > 0 {
fileAdder.CidBuilder = cidutil.InlineBuilder{ fileAdder.CidBuilder = cidutil.InlineBuilder{
Builder: fileAdder.CidBuilder, Builder: fileAdder.CidBuilder,

View File

@ -191,6 +191,19 @@ func TestAdd(t *testing.T) {
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe", path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)}, 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 { for _, testCase := range cases {
@ -211,7 +224,7 @@ func TestAdd(t *testing.T) {
} }
if p.String() != testCase.path { 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) r, err := api.Unixfs().Cat(ctx, p)