Skip to content

Commit

Permalink
Local fs start end offset (#101)
Browse files Browse the repository at this point in the history
* Add TestList with empty case.

* TestList: add "one" case.

* TestList: add "many" case.

* Support StartOffset.

* Support EndOffset

* Include localfs in StartOffset/EndOffset GoDoc.

* Move offset filtering inside of filepath.Walk closure.
  • Loading branch information
snargleplax authored Aug 16, 2022
1 parent f2213e5 commit 3f40c94
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
7 changes: 6 additions & 1 deletion localfs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,13 @@ func (l *LocalStore) List(ctx context.Context, query cloudstorage.Query) (*cloud
mdkey := strings.Replace(obj, ".metadata", "", 1)
metadatas[mdkey] = md
} else {

oname := strings.TrimPrefix(obj, "/")

if (query.StartOffset != "" && oname < query.StartOffset) ||
(query.EndOffset != "" && oname >= query.EndOffset) {
return nil
}

objects[obj] = &object{
name: oname,
updated: f.ModTime(),
Expand Down
100 changes: 98 additions & 2 deletions localfs/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/lytics/cloudstorage"
"github.com/lytics/cloudstorage/localfs"
"github.com/lytics/cloudstorage/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAll(t *testing.T) {
Expand Down Expand Up @@ -111,3 +111,99 @@ func TestGetDir(t *testing.T) {
err = store.Delete(context.Background(), "test/index.html")
require.NoError(t, err)
}

func TestList(t *testing.T) {
t.Parallel()

for name, tt := range map[string]struct {
objs map[string]string
q cloudstorage.Query
startOffset string
want []string
}{
"empty": {
objs: nil,
want: nil,
},
"one": {
objs: map[string]string{
"nimi": "ijo",
},
want: []string{"nimi"},
},
"many": {
objs: map[string]string{
"wan": "loje",
"tu": "jelo",
"tu wan": "laso",
},
want: []string{"wan", "tu", "tu wan"},
},
"start-offset-inclusive": {
objs: map[string]string{
"a": "ijo",
"b": "ijo",
"c": "ijo",
},
q: cloudstorage.Query{
StartOffset: "b",
},
want: []string{"b", "c"},
},
"end-offset-exclusive": {
objs: map[string]string{
"a": "ijo",
"b": "ijo",
"c": "ijo",
},
q: cloudstorage.Query{
EndOffset: "b",
},
want: []string{"a"},
},
"start-and-end-offsets-together": {
objs: map[string]string{
"a": "ijo",
"b": "ijo",
"c": "ijo",
},
q: cloudstorage.Query{
StartOffset: "b",
EndOffset: "c",
},
want: []string{"b"},
},
} {
t.Run(name, func(t *testing.T) {
ctx := context.Background()

tmpDir, err := ioutil.TempDir("/tmp", "getdir")
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, os.RemoveAll(tmpDir)) })

store, err := localfs.NewLocalStore(
"list",
filepath.Join(tmpDir, "mockcloud"),
filepath.Join(tmpDir, "localcache"),
)
require.NoError(t, err)

for k, v := range tt.objs {
w, err := store.NewWriterWithContext(ctx, k, nil)
require.NoError(t, err)
_, err = w.Write([]byte(v))
require.NoError(t, err)
err = w.Close()
require.NoError(t, err)
}

got, err := store.List(ctx, tt.q)
require.NoError(t, err)
var names []string
for _, o := range got.Objects {
names = append(names, o.Name())
}
assert.ElementsMatch(t, tt.want, names)
})
}
}
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Filter func(objects Objects) Objects
type Query struct {
Delimiter string // Delimiter is most likely "/"
Prefix string // prefix (directory) to search for or object name if one file
StartOffset string // (gcs only) "bar/", Only list objects lexicographically >= "bar/"
EndOffset string // (gcs only) "foo/", Only list objects lexicographically < "foo/"
StartOffset string // (gcs/localfs only) "bar/", Only list objects lexicographically >= "bar/"
EndOffset string // (gcs/localfs only) "foo/", Only list objects lexicographically < "foo/"
Marker string // Next Page Marker if provided is a start next page fetch bookmark.
ShowHidden bool // Show hidden files?
Filters []Filter // Applied to the result sets to filter out Objects (i.e. remove objects by extension)
Expand Down

0 comments on commit 3f40c94

Please sign in to comment.