Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- // myCRC32 write 3 first chars into return string -
- // id of calling gorutine for subsequent sort
- func myCRC32(id int, a string, out chan<- string) {
- ret := fmt.Sprintf("%03d", id) + DataSignerCrc32(a)
- out <- ret
- }
- func myMD5(a string, out chan<- string, mutex *sync.Mutex) {
- mutex.Lock()
- ret := DataSignerMd5(a)
- mutex.Unlock()
- out <- ret
- }
- // SingleHash return crc32(data) + "~" + crc32(md5(data))
- // Where data is a input number from first function
- func SingleHash(in int, out chan<- string, mutex *sync.Mutex) {
- chCRC32_1 := make(chan string)
- chCRC32_2 := make(chan string)
- chMD5 := make(chan string)
- instr := strconv.Itoa(in)
- go myCRC32(0, instr, chCRC32_1)
- go myMD5(instr, chMD5, mutex)
- go myCRC32(0, <-chMD5, chCRC32_2)
- // Cut of 3 first chars of gorutine id - they don't needed anymore
- a := <-chCRC32_1
- a = a[3:]
- b := <-chCRC32_2
- b = b[3:]
- ret := a + "~" + b
- out <- ret
- }
- // MultiHash is calculating crc32(th+data)), where th=0..5
- // then concatenates results. Data is input data from SingleHash
- func MultiHash(wid int, out chan<- string, mutex *sync.Mutex) {
- chSingle := make(chan string)
- chCRC32 := make(chan string)
- go SingleHash(wid, chSingle, mutex)
- widHash := <-chSingle
- const iters int = 6
- for i := 0; i < iters; i++ {
- istr := strconv.Itoa(i)
- go myCRC32(i, istr+widHash, chCRC32)
- }
- var hashResults []string
- for hash := range chCRC32 {
- hashResults = append(hashResults, hash)
- if len(hashResults) >= iters {
- break
- }
- }
- // Sort strings by gorutine id and cut them off (first free chars)
- sort.Strings(hashResults)
- var sortResults []string
- for _, str := range hashResults {
- sortResults = append(sortResults, str[3:])
- }
- ret := strings.Join(sortResults, "")
- out <- ret
- }
- // CombineResults is combining and sorting resultst together
- // Merging them with _ symbol
- func CombineResults(iterations int, out chan<- string, mutex *sync.Mutex) {
- var hashResults []string
- chHash := make(chan string)
- for i := 0; i < iterations; i++ {
- go MultiHash(i, chHash, mutex)
- }
- for hash := range chHash {
- hashResults = append(hashResults, hash)
- if len(hashResults) == iterations {
- break
- }
- }
- sort.Strings(hashResults)
- out <- strings.Join(hashResults, "_")
- }
- func ExecutePipeline() {
- ch1 := make(chan string)
- var mutex = &sync.Mutex{}
- const iterations int = 100
- go CombineResults(iterations, ch1, mutex)
- ret := <-ch1
- fmt.Println("")
- // fmt.Println(ret)
- }
- func main() {
- start := time.Now()
- {
- ExecutePipeline()
- }
- end := time.Since(start)
- fmt.Println("Execution took %s", end)
- }
Add Comment
Please, Sign In to add comment