coreapi unixfs: layout/chunker options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 20, 2018 at 16:40 UTC
6337e69ff7e00b31c7cfe101161d652ac27c2502
3 files changed
+51
-3
core/coreapi/interface/options/unixfs.go
+27
@@ -4,6 +4,13 @@ import (
4
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
5
)
6
7
+type Layout int
8
+
9
+const (
10
+ BalancedLayout Layout = iota
11
+ TrickleLeyout
12
+)
13
+
14
type UnixfsAddSettings struct {
15
CidVersion int
16
MhType uint64
@@ -11,6 +18,9 @@ type UnixfsAddSettings struct {
18
InlineLimit int
19
RawLeaves bool
20
RawLeavesSet bool
21
+
22
+ Chunker string
23
+ Layout Layout
24
}
25
26
type UnixfsAddOption func(*UnixfsAddSettings) error
@@ -23,6 +33,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
33
InlineLimit: 0,
34
RawLeaves: false,
35
RawLeavesSet: false,
36
+
37
+ Chunker: "size-262144",
38
+ Layout: BalancedLayout,
39
}
40
41
for _, opt := range opts {
@@ -67,3 +80,17 @@ func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
80
return nil
81
}
82
}
83
+
84
+func (unixfsOpts) Chunker(chunker string) UnixfsAddOption {
85
+ return func(settings *UnixfsAddSettings) error {
86
+ settings.Chunker = chunker
87
+ return nil
88
+ }
89
+}
90
+
91
+func (unixfsOpts) Layout(layout Layout) UnixfsAddOption {
92
+ return func(settings *UnixfsAddSettings) error {
93
+ settings.Layout = layout
94
+ return nil
95
+ }
96
+}
core/coreapi/unixfs.go
+10
-2
@@ -68,10 +68,9 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
68
}
69
70
fileAdder.Out = outChan
71
- //fileAdder.Chunker = chunker
71
+ fileAdder.Chunker = settings.Chunker
72
//fileAdder.Progress = progress
73
//fileAdder.Hidden = hidden
74
- //fileAdder.Trickle = trickle
74
//fileAdder.Wrap = wrap
75
//fileAdder.Pin = dopin
76
fileAdder.Silent = false
@@ -80,6 +79,15 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
79
//fileAdder.Name = pathName
80
fileAdder.CidBuilder = prefix
81
82
+ switch settings.Layout {
83
+ case options.BalancedLayout:
84
+ // Default
85
+ case options.TrickleLeyout:
86
+ fileAdder.Trickle = true
87
+ default:
88
+ return nil, fmt.Errorf("unknown layout: %d", settings.Layout)
89
+ }
90
+
91
if settings.InlineLimit > 0 {
92
fileAdder.CidBuilder = cidutil.InlineBuilder{
93
Builder: fileAdder.CidBuilder,
core/coreapi/unixfs_test.go
+14
-1
@@ -191,6 +191,19 @@ func TestAdd(t *testing.T) {
191
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
192
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)},
193
},
194
+ // Chunker / Layout
195
+ {
196
+ name: "addChunks",
197
+ data: strings.Repeat("aoeuidhtns", 200),
198
+ path: "/ipfs/QmRo11d4QJrST47aaiGVJYwPhoNA4ihRpJ5WaxBWjWDwbX",
199
+ opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4")},
200
+ },
201
+ {
202
+ name: "addChunksTrickle",
203
+ data: strings.Repeat("aoeuidhtns", 200),
204
+ path: "/ipfs/QmNNhDGttafX3M1wKWixGre6PrLFGjnoPEDXjBYpTv93HP",
205
+ opts: []options.UnixfsAddOption{options.Unixfs.Chunker("size-4"), options.Unixfs.Layout(options.TrickleLeyout)},
206
+ },
207
}
208
209
for _, testCase := range cases {
@@ -211,7 +224,7 @@ func TestAdd(t *testing.T) {
224
}
225
226
if p.String() != testCase.path {
214
- t.Fatalf("expected path %s, got: %s", hello, p)
227
+ t.Errorf("expected path %s, got: %s", testCase.path, p)
228
}
229
230
r, err := api.Unixfs().Cat(ctx, p)