fulldata.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. "qfw/util"
  6. "qfw/util/mongodb"
  7. "qfw/util/redis"
  8. "sync"
  9. "time"
  10. )
  11. var FullCount = 0
  12. func RunFullData() {
  13. defer util.Catch()
  14. var wg = sync.WaitGroup{}
  15. startTime := int64(1325347200) //2012-01-01
  16. ps := 3
  17. pool := make(chan *task, ps)
  18. day := 0
  19. endChan := make(chan bool, 1)
  20. go func() {
  21. now := time.Now().Unix()
  22. bComplete := false
  23. for {
  24. if startTime > now || bComplete {
  25. log.Println("任务结束")
  26. endChan <- true
  27. break
  28. }
  29. endTime := startTime + 86400
  30. q := map[string]interface{}{
  31. "publishtime": map[string]interface{}{
  32. "$gt": startTime,
  33. "$lte": endTime,
  34. },
  35. }
  36. //数据正序处理
  37. sess := MQFW.GetMgoConn()
  38. var result []map[string]interface{}
  39. sess.DB(MQFW.DbName).C(extractColl).Find(q).All(&result)
  40. MQFW.DestoryMongoConn(sess)
  41. pool <- &task{result}
  42. wg.Add(1)
  43. startTime = endTime
  44. day++
  45. log.Println("day====", day)
  46. if day > 0 && day%ps == 0 {
  47. wg.Wait()
  48. MQFW.Destory()
  49. MQFW = mongodb.MongodbSim{
  50. MongodbAddr: Sysconfig["mongodbServers"].(string),
  51. Size: 2 * ps,
  52. DbName: Sysconfig["mongodbName"].(string),
  53. }
  54. MQFW.InitPool()
  55. }
  56. }
  57. }()
  58. for {
  59. select {
  60. case t := <-pool:
  61. t.query()
  62. t.result = nil
  63. t = nil
  64. wg.Done()
  65. case <-endChan:
  66. return
  67. }
  68. }
  69. }
  70. type task struct {
  71. result []map[string]interface{}
  72. }
  73. func (t *task) query() {
  74. index := 0
  75. wg := &sync.WaitGroup{}
  76. for _, tmp := range t.result {
  77. if index%10000 == 0 {
  78. log.Println(index, tmp["_id"])
  79. }
  80. index++
  81. if util.IntAll(tmp["repeat"]) == 1 {
  82. continue
  83. }
  84. pt := util.Int64All(tmp["publishtime"])
  85. if pt > currentMegerTime {
  86. currentMegerTime = pt
  87. }
  88. currentMegerCount++
  89. if currentMegerCount > 300000 {
  90. log.Println("执行清理", currentMegerTime)
  91. clearPKey()
  92. currentMegerCount = 0
  93. }
  94. wg.Add(1)
  95. MultiThread <- true
  96. go func(tmp map[string]interface{}) {
  97. defer func() {
  98. <-MultiThread
  99. wg.Done()
  100. }()
  101. thisid := util.BsonIdToSId(tmp["_id"])
  102. info := PreThisInfo(tmp)
  103. if info != nil {
  104. lockPNCBMap(info)
  105. startProjectMerge(info, tmp)
  106. redis.Put(INFOID, thisid, 1, INFOTIMEOUT)
  107. currentMegerTime = info.Publishtime
  108. unlockPNCBMap(info)
  109. }
  110. }(tmp)
  111. }
  112. wg.Wait()
  113. FullCount += index
  114. log.Println("currentFull", FullCount)
  115. }
  116. //获取对比项目数组
  117. func getComeperProjects2(p PCBV, thisinfo *Info) (res []interface{}, pncb []*CompareInfo) {
  118. newarr := []string{}
  119. repeatId := map[string]bool{}
  120. if p.PnameLen > 0 {
  121. pn := NewCompareInfo("pn", thisinfo.PNKey, PNKey)
  122. pncb = append(pncb, pn)
  123. thisinfo.AllRelatePNKeyMap = map[string]*Key{}
  124. pn.KeyMap.Lock.Lock()
  125. for k, v := range pn.KeyMap.Map {
  126. if strings.Contains(k, pn.Key) || strings.Contains(pn.Key, k) {
  127. thisinfo.AllRelatePNKeyMap[k] = v
  128. for _, id := range *v.Arr {
  129. if !repeatId[id] {
  130. newarr = append(newarr, id)
  131. repeatId[id] = true
  132. }
  133. }
  134. }
  135. }
  136. if thisinfo.AllRelatePNKeyMap[pn.Key] == nil {
  137. K := &Key{&[]string{}, &sync.Mutex{}}
  138. thisinfo.AllRelatePNKeyMap[pn.Key] = K
  139. pn.KeyMap.Map[pn.Key] = K
  140. }
  141. pn.KeyMap.Lock.Unlock()
  142. }
  143. if p.PcodeLen > 0 {
  144. pc := NewCompareInfo("pc", thisinfo.PCKey, PCKey)
  145. pncb = append(pncb, pc)
  146. thisinfo.AllRelatePCKeyMap = map[string]*Key{}
  147. pc.KeyMap.Lock.Lock()
  148. for k, v := range pc.KeyMap.Map {
  149. if strings.Contains(k, pc.Key) || strings.Contains(pc.Key, k) {
  150. thisinfo.AllRelatePCKeyMap[k] = v
  151. for _, id := range *v.Arr {
  152. if !repeatId[id] {
  153. newarr = append(newarr, id)
  154. repeatId[id] = true
  155. }
  156. }
  157. }
  158. }
  159. if thisinfo.AllRelatePCKeyMap[pc.Key] == nil {
  160. K := &Key{&[]string{}, &sync.Mutex{}}
  161. thisinfo.AllRelatePCKeyMap[pc.Key] = K
  162. pc.KeyMap.Map[pc.Key] = K
  163. }
  164. pc.KeyMap.Lock.Unlock()
  165. }
  166. if p.BuyerLen > 0 {
  167. pb := NewCompareInfo("pb", thisinfo.PBKey, PBKey)
  168. pncb = append(pncb, pb)
  169. pb.KeyMap.Lock.Lock()
  170. K := pb.KeyMap.Map[pb.Key]
  171. if K == nil {
  172. K = &Key{&[]string{}, &sync.Mutex{}}
  173. pb.KeyMap.Map[pb.Key] = K
  174. } else {
  175. for _, id := range *K.Arr {
  176. if !repeatId[id] {
  177. newarr = append(newarr, id)
  178. repeatId[id] = true
  179. }
  180. }
  181. }
  182. pb.KeyMap.Lock.Unlock()
  183. }
  184. if len(newarr) > 0 {
  185. res = redis.Mget(REDISIDS, newarr)
  186. }
  187. return
  188. }