open-nomad/scheduler/select.go

82 lines
1.7 KiB
Go
Raw Normal View History

2015-08-12 01:27:54 +00:00
package scheduler
// LimitIterator is a RankIterator used to limit the number of options
// that are returned before we artifically end the stream.
type LimitIterator struct {
ctx Context
source RankIterator
limit int
seen int
}
// NewLimitIterator is returns a LimitIterator with a fixed limit of returned options
func NewLimitIterator(ctx Context, source RankIterator, limit int) *LimitIterator {
iter := &LimitIterator{
ctx: ctx,
source: source,
limit: limit,
}
return iter
}
func (iter *LimitIterator) Next() *RankedNode {
if iter.seen == iter.limit {
return nil
}
option := iter.source.Next()
if option == nil {
return nil
}
iter.seen += 1
return option
}
2015-08-13 22:01:02 +00:00
func (iter *LimitIterator) Reset() {
iter.source.Reset()
iter.seen = 0
}
2015-08-12 01:27:54 +00:00
// MaxScoreIterator is a RankIterator used to return only a single result
// of the item with the highest score. This iterator will consume all of the
// possible inputs and only returns the highest ranking result.
type MaxScoreIterator struct {
ctx Context
source RankIterator
max *RankedNode
}
2015-08-13 17:05:54 +00:00
// MaxScoreIterator returns a MaxScoreIterator over the given source
func NewMaxScoreIterator(ctx Context, source RankIterator) *MaxScoreIterator {
iter := &MaxScoreIterator{
ctx: ctx,
source: source,
}
return iter
}
2015-08-12 01:27:54 +00:00
func (iter *MaxScoreIterator) Next() *RankedNode {
2015-08-13 17:05:54 +00:00
// Check if we've found the max, return nil
if iter.max != nil {
return nil
}
// Consume and determine the max
2015-08-12 01:27:54 +00:00
for {
option := iter.source.Next()
if option == nil {
2015-08-13 17:05:54 +00:00
return iter.max
2015-08-12 01:27:54 +00:00
}
2015-08-13 17:05:54 +00:00
if iter.max == nil || option.Score > iter.max.Score {
2015-08-12 01:27:54 +00:00
iter.max = option
}
}
}
2015-08-13 22:01:02 +00:00
func (iter *MaxScoreIterator) Reset() {
iter.source.Reset()
iter.max = nil
}