Skip to content

Commit

Permalink
etcdserver: set sort ASCEND for empty sort order
Browse files Browse the repository at this point in the history
when target is not key
  • Loading branch information
gyuho committed Oct 18, 2016
1 parent 20bdb31 commit e1fde10
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions etcdserver/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ func (a *applierV3backend) Range(txnID int64, r *pb.RangeRequest) (*pb.RangeResp
pruneKVs(rr, f)
}

if r.SortOrder != pb.RangeRequest_NONE {
sortOrder := r.SortOrder
if r.SortTarget != pb.RangeRequest_KEY && sortOrder == pb.RangeRequest_NONE {
// Since current mvcc.Range implementation returns results
// sorted by keys in lexiographically ascending order,
// sort ASCEND by default only when target is not 'KEY'
sortOrder = pb.RangeRequest_ASCEND
}
if sortOrder != pb.RangeRequest_NONE {
var sorter sort.Interface
switch {
case r.SortTarget == pb.RangeRequest_KEY:
Expand All @@ -319,13 +326,12 @@ func (a *applierV3backend) Range(txnID int64, r *pb.RangeRequest) (*pb.RangeResp
sorter = &kvSortByValue{&kvSort{rr.KVs}}
}
switch {
case r.SortOrder == pb.RangeRequest_ASCEND:
case sortOrder == pb.RangeRequest_ASCEND:
sort.Sort(sorter)
case r.SortOrder == pb.RangeRequest_DESCEND:
case sortOrder == pb.RangeRequest_DESCEND:
sort.Sort(sort.Reverse(sorter))
}
}

if r.Limit > 0 && len(rr.KVs) > int(r.Limit) {
rr.KVs = rr.KVs[:r.Limit]
resp.More = true
Expand Down

0 comments on commit e1fde10

Please sign in to comment.