Skip to content

Commit 8a3991e

Browse files
committed
reorder get fileobj
1 parent e4558de commit 8a3991e

File tree

5 files changed

+49
-37
lines changed

5 files changed

+49
-37
lines changed

drivers/alias/driver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ func (d *Alias) Drop(ctx context.Context) error {
6666
return nil
6767
}
6868

69+
func (d *Alias) GetRoot(ctx context.Context) (model.Obj, error) {
70+
return &model.Object{
71+
Name: "Root",
72+
IsFolder: true,
73+
Path: "/",
74+
}, nil
75+
}
76+
6977
func (d *Alias) Get(ctx context.Context, path string) (model.Obj, error) {
70-
if utils.PathEqual(path, "/") {
71-
return &model.Object{
72-
Name: "Root",
73-
IsFolder: true,
74-
Path: "/",
75-
}, nil
76-
}
7778
root, sub := d.getRootAndPath(path)
7879
dsts, ok := d.pathMap[root]
7980
if !ok {

drivers/crypt/driver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,15 @@ func (d *Crypt) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
186186
return result, nil
187187
}
188188

189+
func (d *Crypt) GetRoot(ctx context.Context) (model.Obj, error) {
190+
return &model.Object{
191+
Name: "Root",
192+
IsFolder: true,
193+
Path: "/",
194+
}, nil
195+
}
196+
189197
func (d *Crypt) Get(ctx context.Context, path string) (model.Obj, error) {
190-
if utils.PathEqual(path, "/") {
191-
return &model.Object{
192-
Name: "Root",
193-
IsFolder: true,
194-
Path: "/",
195-
}, nil
196-
}
197198
remoteFullPath := ""
198199
var remoteObj model.Obj
199200
var err, err2 error

drivers/netease_music/driver.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func (d *NeteaseMusic) Drop(ctx context.Context) error {
4242
return nil
4343
}
4444

45-
func (d *NeteaseMusic) Get(ctx context.Context, path string) (model.Obj, error) {
46-
if path == "/" {
47-
return &model.Object{
48-
IsFolder: true,
49-
Path: path,
50-
}, nil
51-
}
45+
func (d *NeteaseMusic) GetRoot(ctx context.Context) (model.Obj, error) {
46+
return &model.Object{
47+
IsFolder: true,
48+
Path: "/",
49+
}, nil
50+
}
5251

52+
func (d *NeteaseMusic) Get(ctx context.Context, path string) (model.Obj, error) {
5353
fragments := strings.Split(path, "/")
5454
if len(fragments) > 1 {
5555
fileName := fragments[1]

drivers/strm/driver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ func (d *Strm) Drop(ctx context.Context) error {
8989
return nil
9090
}
9191

92+
func (d *Strm) GetRoot(ctx context.Context) (model.Obj, error) {
93+
return &model.Object{
94+
Name: "Root",
95+
IsFolder: true,
96+
Path: "/",
97+
}, nil
98+
}
99+
92100
func (d *Strm) Get(ctx context.Context, path string) (model.Obj, error) {
93-
if utils.PathEqual(path, "/") {
94-
return &model.Object{
95-
Name: "Root",
96-
IsFolder: true,
97-
Path: "/",
98-
}, nil
99-
}
100101
root, sub := d.getRootAndPath(path)
101102
dsts, ok := d.pathMap[root]
102103
if !ok {

internal/op/fs.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,6 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
177177
path = utils.FixAndCleanPath(path)
178178
log.Debugf("op.Get %s", path)
179179

180-
// get the obj directly without list so that we can reduce the io
181-
if g, ok := storage.(driver.Getter); ok {
182-
obj, err := g.Get(ctx, path)
183-
if err == nil {
184-
return model.WrapObjName(obj), nil
185-
}
186-
}
187-
188180
// is root folder
189181
if utils.PathEqual(path, "/") {
190182
var rootObj model.Obj
@@ -227,7 +219,24 @@ func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, er
227219

228220
// not root folder
229221
dir, name := stdpath.Split(path)
230-
files, err := List(ctx, storage, dir, model.ListArgs{})
222+
key := Key(storage, dir)
223+
if files, ok := listCache.Get(key); ok {
224+
log.Debugf("use cache when list %s", dir)
225+
for _, f := range files {
226+
if f.GetName() == name {
227+
return f, nil
228+
}
229+
}
230+
}
231+
// get the obj directly without list so that we can reduce the io
232+
if g, ok := storage.(driver.Getter); ok {
233+
obj, err := g.Get(ctx, path)
234+
if err == nil {
235+
return model.WrapObjName(obj), nil
236+
}
237+
}
238+
239+
files, err := List(ctx, storage, dir, model.ListArgs{Refresh: true})
231240
if err != nil {
232241
return nil, errors.WithMessage(err, "failed get parent list")
233242
}

0 commit comments

Comments
 (0)