Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.9 kB
2
Indexable
Never
package main

import (
	"fmt"
	"runtime"
	"sort"
	"sync"
)

const (
	empty           = 0
	arraySingleSize = 2
	arrayMultiSize  = 6
)

var mutexOnMD5Count = sync.Mutex{}

// сюда писать код
func ExecutePipeline(jobs ...job) {
	runtime.GOMAXPROCS(0)
	inChan := make(chan interface{})
	waiter := &sync.WaitGroup{}
	for _, curJob := range jobs {
		outChan := make(chan interface{})
		waiter.Add(1)
		go func(currentJob job, in chan interface{}, out chan interface{}, waiter *sync.WaitGroup) {
			defer waiter.Done()
			currentJob(in, out)
			close(out)
		}(curJob, inChan, outChan, waiter)
		inChan = outChan
	}
	waiter.Wait()
}

func SingleHash(in, out chan interface{}) {
	waiter := &sync.WaitGroup{}
	for data := range in {
		var dataString = fmt.Sprint(data)
		waiter.Add(1)
		go proccssSingleHash(dataString, waiter, out)
	}
	waiter.Wait()
}

func proccssSingleHash(data string, waiter *sync.WaitGroup, out chan interface{}) {
	defer waiter.Done()
	resultArray := make([]string, arraySingleSize)
	wg := &sync.WaitGroup{}
	wg.Add(1)
	go SingleFirstPart(data, resultArray, wg)
	wg.Add(1)
	go SingleSecondPart(data, resultArray, wg)
	wg.Wait()
	out <- resultArray[0] + "~" + resultArray[1]
}

func SingleFirstPart(data string, arr []string, wg *sync.WaitGroup) {
	defer wg.Done()
	result := DataSignerCrc32(data)
	WriteInArray(result, arr, 0)
}

func WriteInArray(data string, arr []string, i int) {
	arr[i] = data
}

func SingleSecondPart(data string, arr []string, wg *sync.WaitGroup) {
	defer wg.Done()
	dataMD5 := SingleMD5(data)
	result := DataSignerCrc32(dataMD5)
	WriteInArray(result, arr, 1)
}

func SingleMD5(data string) string {
	mutexOnMD5Count.Lock()
	result := DataSignerMd5(data)
	mutexOnMD5Count.Unlock()
	return result
}

func MultiHash(in, out chan interface{}) {
	waiter := &sync.WaitGroup{}
	for data := range in {
		var dataString = data.(string)
		waiter.Add(1)
		go proccssMultiHash(dataString, waiter, out)
	}
	waiter.Wait()
}

func proccssMultiHash(data string, waiter *sync.WaitGroup, out chan interface{}) {
	defer waiter.Done()
	wg := &sync.WaitGroup{}
	resultArray := make([]string, arrayMultiSize)
	for i := 0; i < arrayMultiSize; i++ {
		wg.Add(1)
		go DoCRC32inMulti(data, resultArray, i, wg)
	}
	wg.Wait()
	resultString := resultArray[0]
	for i := 1; i < arrayMultiSize; i++ {
		resultString += resultArray[i]
	}
	out <- resultString
}

func DoCRC32inMulti(data string, arr []string, i int, wg *sync.WaitGroup) {
	defer wg.Done()
	concatenateData := fmt.Sprint(i) + data
	result := DataSignerCrc32(concatenateData)
	WriteInArray(result, arr, i)
}

func CombineResults(in, out chan interface{}) {
	resultArray := make([]string, empty)
	for el := range in {
		resultArray = append(resultArray, fmt.Sprint(el))
	}
	sort.Strings(resultArray)
	resultString := resultArray[0]
	for i := 1; i < len(resultArray); i++ {
		resultString += "_" + resultArray[i]
	}
	out <- resultString
}
Leave a Comment