-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from ipfs/fix/datastore-order
query: make datastore ordering act like a user would expect
- Loading branch information
Showing
5 changed files
with
54 additions
and
48 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.4.1: Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM | ||
3.5.0: QmPGYyi1DtuWyUkG3PtvLz1xb4ScjnUvwJMCoX3cxeyxNr |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,48 @@ | ||
package query | ||
|
||
import ( | ||
"sort" | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
// Order is an object used to order objects | ||
type Order interface { | ||
|
||
// Sort sorts the Entry slice according to | ||
// the Order criteria. | ||
Sort([]Entry) | ||
Compare(a, b Entry) int | ||
} | ||
|
||
// OrderByValue is used to signal to datastores they | ||
// should apply internal orderings. unfortunately, there | ||
// is no way to apply order comparisons to interface{} types | ||
// in Go, so if the datastore doesnt have a special way to | ||
// handle these comparisons, you must provide an Order | ||
// implementation that casts to the correct type. | ||
type OrderByValue struct { | ||
TypedOrder Order | ||
// OrderByFunction orders the results based on the result of the given function. | ||
type OrderByFunction func(a, b Entry) int | ||
|
||
func (o OrderByFunction) Compare(a, b Entry) int { | ||
return o(a, b) | ||
} | ||
|
||
func (o OrderByValue) Sort(res []Entry) { | ||
if o.TypedOrder == nil { | ||
panic("cannot order interface{} by value. see query docs.") | ||
} | ||
o.TypedOrder.Sort(res) | ||
// OrderByValue is used to signal to datastores they should apply internal | ||
// orderings. | ||
type OrderByValue struct{} | ||
|
||
func (o OrderByValue) Compare(a, b Entry) int { | ||
return bytes.Compare(a.Value, b.Value) | ||
} | ||
|
||
// OrderByValueDescending is used to signal to datastores they | ||
// should apply internal orderings. unfortunately, there | ||
// is no way to apply order comparisons to interface{} types | ||
// in Go, so if the datastore doesnt have a special way to | ||
// handle these comparisons, you are SOL. | ||
type OrderByValueDescending struct { | ||
TypedOrder Order | ||
} | ||
// should apply internal orderings. | ||
type OrderByValueDescending struct{} | ||
|
||
func (o OrderByValueDescending) Sort(res []Entry) { | ||
if o.TypedOrder == nil { | ||
panic("cannot order interface{} by value. see query docs.") | ||
} | ||
o.TypedOrder.Sort(res) | ||
func (o OrderByValueDescending) Compare(a, b Entry) int { | ||
return -bytes.Compare(a.Value, b.Value) | ||
} | ||
|
||
// OrderByKey | ||
type OrderByKey struct{} | ||
|
||
func (o OrderByKey) Sort(res []Entry) { | ||
sort.Stable(reByKey(res)) | ||
func (o OrderByKey) Compare(a, b Entry) int { | ||
return strings.Compare(a.Key, b.Key) | ||
} | ||
|
||
// OrderByKeyDescending | ||
type OrderByKeyDescending struct{} | ||
|
||
func (o OrderByKeyDescending) Sort(res []Entry) { | ||
sort.Stable(sort.Reverse(reByKey(res))) | ||
func (o OrderByKeyDescending) Compare(a, b Entry) int { | ||
return -strings.Compare(a.Key, b.Key) | ||
} | ||
|
||
type reByKey []Entry | ||
|
||
func (s reByKey) Len() int { return len(s) } | ||
func (s reByKey) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s reByKey) Less(i, j int) bool { return s[i].Key < s[j].Key } |
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
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