Skip to content

Commit 09bdc7d

Browse files
committed
fix: support order/filter
1 parent c708dee commit 09bdc7d

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

s3.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,45 @@ func (s *S3Bucket) Delete(k ds.Key) error {
155155
return err
156156
}
157157

158+
func querySupported(q dsq.Query) bool {
159+
if len(q.Orders) > 0 {
160+
switch q.Orders[0].(type) {
161+
case dsq.OrderByKey, *dsq.OrderByKey:
162+
// We order by key by default.
163+
default:
164+
return false
165+
}
166+
}
167+
return len(q.Filters) == 0
168+
}
169+
158170
func (s *S3Bucket) Query(q dsq.Query) (dsq.Results, error) {
159-
if q.Orders != nil || q.Filters != nil {
160-
return nil, fmt.Errorf("s3ds: filters or orders are not supported")
171+
// Handle ordering
172+
if !querySupported(q) {
173+
// OK, time to do this the naive way.
174+
175+
// Skip the stuff we can't apply.
176+
baseQuery := q
177+
baseQuery.Filters = nil
178+
baseQuery.Orders = nil
179+
baseQuery.Limit = 0 // needs to apply after we order
180+
baseQuery.Offset = 0 // ditto.
181+
182+
// perform the base query.
183+
res, err := s.Query(baseQuery)
184+
if err != nil {
185+
return nil, err
186+
}
187+
188+
// fix the query
189+
res = dsq.ResultsReplaceQuery(res, q)
190+
191+
// Remove the prefix, S3 has already handled it.
192+
naiveQuery := q
193+
naiveQuery.Prefix = ""
194+
195+
// Apply the rest of the query
196+
return dsq.NaiveQueryApply(naiveQuery, res), nil
161197
}
162198

163199
// Normalize the path and strip the leading / as S3 stores values

0 commit comments

Comments
 (0)