Created
March 18, 2019 19:49
-
-
Save Stebalien/5e59cf3a2837438cba31e6befbd210f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/mount/mount.go b/mount/mount.go | |
index 9d26f46..561a943 100644 | |
--- a/mount/mount.go | |
+++ b/mount/mount.go | |
@@ -122,22 +122,36 @@ func (d *Datastore) Delete(key ds.Key) error { | |
} | |
func (d *Datastore) Query(q query.Query) (query.Results, error) { | |
- if len(q.Filters) > 0 || | |
- len(q.Orders) > 0 || | |
- q.Limit > 0 || | |
- q.Offset > 0 { | |
- // TODO this is still overly simplistic, but the only callers are | |
- // `ipfs refs local` and ipfs-ds-convert. | |
- return nil, errors.New("mount only supports listing all prefixed keys in random order") | |
+ | |
+ if q.Offset > 0 { | |
+ // Kind of hard to handle, we can skip this for now | |
+ return some error | |
} | |
+ | |
prefix := ds.NewKey(q.Prefix) | |
dses, mounts, rests := d.lookupAll(prefix) | |
+ var order query.Order | |
+ if len(q.Orders) > 0 { | |
+ switch q.Orders[0] { | |
+ case query.OrderByKey: | |
+ // sort `dses` by `mounts` ascending | |
+ case query.OrderByKeyDscending: | |
+ // sort `dses` by `mounts` descending | |
+ default: | |
+ return nil, fmt.Errorf("bad") | |
+ } | |
+ order = q.Orders[0] | |
+ // Don't care about the rest of the orders. Order by key gives us a total ordering. | |
+ } | |
+ | |
// current itorator state | |
var res query.Results | |
var mount ds.Key | |
i := 0 | |
+ limit := q.Limit | |
+ | |
return query.ResultsFromIterator(q, query.Iterator{ | |
Next: func() (query.Result, bool) { | |
var r query.Result | |
@@ -157,6 +171,7 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) { | |
q2 := q | |
q2.Prefix = rest.String() | |
r, err := dst.Query(q2) | |
+ // Apply order, offset, limit. | |
if err != nil { | |
return query.Result{Error: err}, false | |
} | |
@@ -188,6 +203,8 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) { | |
return nil | |
}, | |
}), nil | |
+ | |
+ // Wrap with a naive filter (query.NaiveFilter) if necessary... | |
} | |
func (d *Datastore) Close() error { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment