Example #1
0
func (d *Dataset) MergeSorted(f interface{}) (ret *Dataset) {
	ret = d.context.newNextDataset(1, d.Type)
	step := d.context.AddAllToOneStep(d, ret)
	step.Name = "MergeSorted"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan

		var pq *util.PriorityQueue
		// enqueue one item to the pq from each shard
		isFirst := true
		for shardId, shardChan := range task.InputChans {
			if x, ok := <-shardChan; ok {
				if isFirst {
					isFirst = false
					v := guessKey(x)
					comparator := getLessThanComparator(d.Type, v, f)
					pq = util.NewPriorityQueue(comparator)
				}
				pq.Enqueue(x.Interface(), shardId)
			}
		}
		for pq.Len() > 0 {
			t, shardId := pq.Dequeue()
			outChan.Send(reflect.ValueOf(t))
			if x, ok := <-task.InputChans[shardId]; ok {
				pq.Enqueue(x.Interface(), shardId)
			}
		}
	}
	return ret
}
Example #2
0
func NewMasterResource() *MasterResource {
	l := &MasterResource{
		Topology:  *resource.NewTopology(),
		EventChan: make(chan interface{}, 1),
		EvictionQueue: util.NewPriorityQueue(func(a, b interface{}) bool {
			x, y := a.(*resource.AgentInformation), b.(*resource.AgentInformation)
			return x.LastHeartBeat.Before(y.LastHeartBeat)
		}),
	}

	go l.BackgroundEventLoop()
	go l.BackgroundEvictionLoop()

	return l
}