Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query: add support for filters/orders #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ func (s *S3Bucket) Delete(k ds.Key) error {
}

func (s *S3Bucket) Query(q dsq.Query) (dsq.Results, error) {
if q.Orders != nil || q.Filters != nil {
return nil, fmt.Errorf("s3ds: filters or orders are not supported")
}

limit := q.Limit + q.Offset
if limit == 0 || limit > listMax {
limit = listMax
Expand Down Expand Up @@ -198,13 +194,26 @@ func (s *S3Bucket) Query(q dsq.Query) (dsq.Results, error) {
index++
return dsq.Result{Entry: entry}, true
}

return dsq.ResultsFromIterator(q, dsq.Iterator{
results := dsq.ResultsFromIterator(q, dsq.Iterator{
Close: func() error {
return nil
},
Next: nextValue,
}), nil
})

// Apply everything else (filters and orders).
naiveQuery := q
naiveQuery.Prefix = ""
naiveQuery.Offset = 0
naiveQuery.Limit = 0
// Results are pre-ordered by key.
if len(naiveQuery.Orders) > 0 {
switch naiveQuery.Orders[0].(type) {
case dsq.OrderByKey, *dsq.OrderByKey:
naiveQuery.Orders = nil
}
}
return dsq.NaiveQueryApply(naiveQuery, results), nil
}

func (s *S3Bucket) Batch() (ds.Batch, error) {
Expand Down
15 changes: 3 additions & 12 deletions s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,16 @@ func TestSuite(t *testing.T) {

s3ds, err := NewS3Datastore(config)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if hasLocalS3 {
err = devMakeBucket(s3ds.S3, "localBucketName")
if err != nil {
t.Error(err)
t.Fatal(err)
}
}

t.Run("basic operations", func(t *testing.T) {
dstest.SubtestBasicPutGet(t, s3ds)
})
t.Run("not found operations", func(t *testing.T) {
dstest.SubtestNotFounds(t, s3ds)
})
t.Run("many puts and gets, query", func(t *testing.T) {
dstest.SubtestManyKeysAndQuery(t, s3ds)
})
dstest.SubtestAll(t, s3ds)
}

func devMakeBucket(s3obj *s3.S3, bucketName string) error {
Expand Down