Guest User

Untitled

a guest
Jan 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11.  
  12. // myCRC32 write 3 first chars into return string -
  13. // id of calling gorutine for subsequent sort
  14. func myCRC32(id int, a string, out chan<- string) {
  15. ret := fmt.Sprintf("%03d", id) + DataSignerCrc32(a)
  16. out <- ret
  17. }
  18.  
  19. func myMD5(a string, out chan<- string, mutex *sync.Mutex) {
  20. mutex.Lock()
  21. ret := DataSignerMd5(a)
  22. mutex.Unlock()
  23.  
  24. out <- ret
  25. }
  26.  
  27. // SingleHash return crc32(data) + "~" + crc32(md5(data))
  28. // Where data is a input number from first function
  29. func SingleHash(in int, out chan<- string, mutex *sync.Mutex) {
  30. chCRC32_1 := make(chan string)
  31. chCRC32_2 := make(chan string)
  32. chMD5 := make(chan string)
  33.  
  34. instr := strconv.Itoa(in)
  35.  
  36. go myCRC32(0, instr, chCRC32_1)
  37. go myMD5(instr, chMD5, mutex)
  38. go myCRC32(0, <-chMD5, chCRC32_2)
  39. // Cut of 3 first chars of gorutine id - they don't needed anymore
  40. a := <-chCRC32_1
  41. a = a[3:]
  42. b := <-chCRC32_2
  43. b = b[3:]
  44.  
  45. ret := a + "~" + b
  46. out <- ret
  47. }
  48.  
  49. // MultiHash is calculating crc32(th+data)), where th=0..5
  50. // then concatenates results. Data is input data from SingleHash
  51. func MultiHash(wid int, out chan<- string, mutex *sync.Mutex) {
  52. chSingle := make(chan string)
  53. chCRC32 := make(chan string)
  54.  
  55. go SingleHash(wid, chSingle, mutex)
  56. widHash := <-chSingle
  57.  
  58. const iters int = 6
  59. for i := 0; i < iters; i++ {
  60. istr := strconv.Itoa(i)
  61. go myCRC32(i, istr+widHash, chCRC32)
  62. }
  63.  
  64. var hashResults []string
  65. for hash := range chCRC32 {
  66. hashResults = append(hashResults, hash)
  67. if len(hashResults) >= iters {
  68. break
  69. }
  70. }
  71.  
  72. // Sort strings by gorutine id and cut them off (first free chars)
  73. sort.Strings(hashResults)
  74. var sortResults []string
  75. for _, str := range hashResults {
  76. sortResults = append(sortResults, str[3:])
  77. }
  78.  
  79. ret := strings.Join(sortResults, "")
  80. out <- ret
  81. }
  82.  
  83. // CombineResults is combining and sorting resultst together
  84. // Merging them with _ symbol
  85. func CombineResults(iterations int, out chan<- string, mutex *sync.Mutex) {
  86. var hashResults []string
  87. chHash := make(chan string)
  88.  
  89. for i := 0; i < iterations; i++ {
  90. go MultiHash(i, chHash, mutex)
  91. }
  92.  
  93. for hash := range chHash {
  94. hashResults = append(hashResults, hash)
  95. if len(hashResults) == iterations {
  96. break
  97. }
  98. }
  99.  
  100. sort.Strings(hashResults)
  101. out <- strings.Join(hashResults, "_")
  102. }
  103.  
  104. func ExecutePipeline() {
  105. ch1 := make(chan string)
  106. var mutex = &sync.Mutex{}
  107.  
  108. const iterations int = 100
  109. go CombineResults(iterations, ch1, mutex)
  110.  
  111. ret := <-ch1
  112. fmt.Println("")
  113. // fmt.Println(ret)
  114. }
  115.  
  116. func main() {
  117.  
  118. start := time.Now()
  119. {
  120. ExecutePipeline()
  121. }
  122. end := time.Since(start)
  123. fmt.Println("Execution took %s", end)
  124. }
Add Comment
Please, Sign In to add comment