Пример #1
0
// f(V, V) bool : less than function
// New Dataset contains K,V
func (d *Dataset) LocalSort(f interface{}) *Dataset {
	if f == nil && d.IsKeyLocalSorted {
		return d
	}
	ret, step := add1ShardTo1Step(d, d.Type)
	ret.IsKeyPartitioned = d.IsKeyPartitioned
	if f == nil {
		ret.IsKeyLocalSorted = true
	}
	step.Name = "LocalSort"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan
		var kvs []interface{}
		for input := range task.InputChan() {
			kvs = append(kvs, input.Interface())
		}
		if len(kvs) == 0 {
			return
		}
		comparator := getLessThanComparator(d.Type, reflect.ValueOf(kvs[0]), f)
		timsort.Sort(kvs, comparator)

		for _, kv := range kvs {
			outChan.Send(reflect.ValueOf(kv))
			// println(task.Name(), "sent kv index:", i)
		}
	}
	return ret
}
Пример #2
0
// f(V, V) bool : less than function
// New Dataset contains K,V
func (d *Dataset) LocalSort(f interface{}) *Dataset {
	ret, step := add1ShardTo1Step(d, d.Type)
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan
		var kvs []interface{}
		for input := range task.InputChan() {
			kvs = append(kvs, input.Interface())
		}
		if len(kvs) == 0 {
			return
		}
		v := guessKey(reflect.ValueOf(kvs[0]))
		comparator := getLessThanComparator(d.Type, v, f)
		timsort.Sort(kvs, comparator)

		for _, kv := range kvs {
			outChan.Send(reflect.ValueOf(kv))
		}
	}
	return ret
}