Stablize the sorting of meta.ShardGroupInfos
parent
1579dd1e75
commit
b3341e8f66
|
@ -1012,9 +1012,25 @@ type ShardGroupInfo struct {
|
|||
// on the StartTime field.
|
||||
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) Len() int { return len(a) }
|
||||
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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue