coreapi: asunc ls option
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@72006bfe2d78cd6cb507ff0265cd1844521d190e This commit was moved from ipfs/boxo@db66d03977366d25aa6b91f30a6ec7a9fcae9037
Łukasz Magiera committed
Feb 1, 2019 at 20:12 UTC
9f4c14741c76ab5c65de43a7871bbb24609b72d6
3 files changed
+53
-5
core/coreiface/options/unixfs.go
+30
@@ -42,7 +42,12 @@ type UnixfsAddSettings struct {
42
Progress bool
43
}
44
45
+type UnixfsLsSettings struct {
46
+ Async bool
47
+}
48
+
49
type UnixfsAddOption func(*UnixfsAddSettings) error
50
+type UnixfsLsOption func(*UnixfsLsSettings) error
51
52
func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, error) {
53
options := &UnixfsAddSettings{
@@ -122,6 +127,21 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
127
return options, prefix, nil
128
}
129
130
+func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) {
131
+ options := &UnixfsLsSettings{
132
+ Async: true,
133
+ }
134
+
135
+ for _, opt := range opts {
136
+ err := opt(options)
137
+ if err != nil {
138
+ return nil, err
139
+ }
140
+ }
141
+
142
+ return options, nil
143
+}
144
+
145
type unixfsOpts struct{}
146
147
var Unixfs unixfsOpts
@@ -290,3 +310,13 @@ func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption {
310
return nil
311
}
312
}
313
+
314
+// Async tells ls to return results as soon as they are available, which can be
315
+// useful for listing HAMT directories. When this option is set to true returned
316
+// results won't be returned in order
317
+func (unixfsOpts) Async(async bool) UnixfsLsOption {
318
+ return func(settings *UnixfsLsSettings) error {
319
+ settings.Async = async
320
+ return nil
321
+ }
322
+}
core/coreiface/tests/unixfs.go
+20
-2
@@ -749,12 +749,12 @@ func (tp *provider) TestLs(t *testing.T) {
749
t.Error(err)
750
}
751
752
- links, err := api.Unixfs().Ls(ctx, p)
752
+ links, err := api.Unixfs().Ls(ctx, p, options.Unixfs.Async(false))
753
if err != nil {
754
t.Error(err)
755
}
756
757
- link := <- links
757
+ link := (<-links).Link
758
if link.Size != 23 {
759
t.Fatalf("expected size = 23, got %d", link.Size)
760
}
@@ -768,6 +768,24 @@ func (tp *provider) TestLs(t *testing.T) {
768
t.Errorf("didn't expect a second link")
769
}
770
771
+ links, err = api.Unixfs().Ls(ctx, p, options.Unixfs.Async(true))
772
+ if err != nil {
773
+ t.Error(err)
774
+ }
775
+
776
+ link = (<-links).Link
777
+ if link.Size != 23 {
778
+ t.Fatalf("expected size = 23, got %d", link.Size)
779
+ }
780
+ if link.Name != "name-of-file" {
781
+ t.Fatalf("expected name = name-of-file, got %s", link.Name)
782
+ }
783
+ if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
784
+ t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid)
785
+ }
786
+ if _, ok := <-links; ok {
787
+ t.Errorf("didn't expect a second link")
788
+ }
789
}
790
791
func (tp *provider) TestEntriesExpired(t *testing.T) {
core/coreiface/unixfs.go
+3
-3
@@ -5,8 +5,8 @@ import (
5
6
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
7
8
- ipld "gx/ipfs/QmRL22E4paat7ky7vx9MLpR97JHHbFPrg3ytFQw6qp1y1s/go-ipld-format"
9
- files "gx/ipfs/QmaXvvAVAQ5ABqM5xtjYmV85xmN5MkWAZsX9H9Fwo4FVXp/go-ipfs-files"
8
+ ft "gx/ipfs/QmQ1JnYpnzkaurjW1yxkQxC2w3K1PorNE1nv1vaP5Le7sq/go-unixfs"
9
+ "gx/ipfs/QmaXvvAVAQ5ABqM5xtjYmV85xmN5MkWAZsX9H9Fwo4FVXp/go-ipfs-files"
10
)
11
12
type AddEvent struct {
@@ -31,5 +31,5 @@ type UnixfsAPI interface {
31
Get(context.Context, Path) (files.Node, error)
32
33
// Ls returns the list of links in a directory
34
- Ls(context.Context, Path) (<-chan *ipld.Link, error)
34
+ Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan ft.LinkResult, error)
35
}