Stablize the sorting of meta.ShardGroupInfos

pull/7365/head
Joe LeGasse 2016-07-27 15:42:26 -04:00
parent 1579dd1e75
commit b3341e8f66
2 changed files with 44 additions and 3 deletions

View File

@ -1013,8 +1013,24 @@ type ShardGroupInfo struct {
type ShardGroupInfos []ShardGroupInfo
func (a ShardGroupInfos) Len() int { return len(a) }
func (a ShardGroupInfos) Less(i, j int) bool { return a[i].StartTime.Before(a[j].StartTime) }
func (a ShardGroupInfos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ShardGroupInfos) Less(i, j int) bool {
iEnd := a[i].EndTime
if a[i].Truncated() {
iEnd = a[i].TruncatedAt
}
jEnd := a[j].EndTime
if a[j].Truncated() {
jEnd = a[j].TruncatedAt
}
if iEnd.Equal(jEnd) {
return a[i].StartTime.Before(a[j].StartTime)
}
return iEnd.Before(jEnd)
}
// Contains return true if the shard group contains data for the timestamp.
func (sgi *ShardGroupInfo) Contains(timestamp time.Time) bool {

View File

@ -2,6 +2,8 @@ package meta
import (
"reflect"
"sort"
"time"
"testing"
)
@ -29,3 +31,26 @@ func TestnewShardOwner(t *testing.T) {
t.Errorf("got owner frequencies %v, expected %v", got, exp)
}
}
func TestShardGroupSort(t *testing.T) {
sg1 := ShardGroupInfo{
ID: 1,
StartTime: time.Unix(1000, 0),
EndTime: time.Unix(1100, 0),
TruncatedAt: time.Unix(1050, 0),
}
sg2 := ShardGroupInfo{
ID: 2,
StartTime: time.Unix(1000, 0),
EndTime: time.Unix(1100, 0),
}
sgs := ShardGroupInfos{sg2, sg1}
sort.Sort(sgs)
if sgs[len(sgs)-1].ID != 2 {
t.Fatal("unstable sort for ShardGroupInfos")
}
}