fulldata.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. type KeyMapPC struct {
  117. Lock sync.Mutex
  118. Map map[string]*KeyMap
  119. }
  120. type CompareInfoPC struct {
  121. Field string //对比属性 pn/pc/pb
  122. Key string //存放rediskey
  123. Scores []*CompareOne //对比分值 pinfo索引对应分值
  124. Bfind bool //是否查找到
  125. IdArr []string
  126. K *Key
  127. KeyMapPC *KeyMapPC
  128. }
  129. func NewCompareInfoPC(field, key string, KeyMapPC *KeyMapPC) *CompareInfoPC {
  130. return &CompareInfoPC{
  131. Field: field,
  132. Key: key,
  133. Scores: []*CompareOne{},
  134. KeyMapPC: KeyMapPC,
  135. }
  136. }
  137. var PNIdMap, PCIdMap = &KeyMapPC{Map: map[string]*KeyMap{}}, &KeyMapPC{Map: map[string]*KeyMap{}}
  138. //获取对比项目数组
  139. func getComeperProjects2(p PCBV, thisinfo *Info) (res []interface{}, pncb []*CompareInfo) {
  140. newarr := []string{}
  141. repeatId := map[string]bool{}
  142. if p.PnameLen > 0 {
  143. pn := NewCompareInfoPC("pn", thisinfo.PNKey, PNIdMap)
  144. //对比全国和本省
  145. PNIdMap.Lock.Lock()
  146. km := PNIdMap.Map[thisinfo.Area]
  147. if km == nil {
  148. km = &KeyMap{Map: map[string]*Key{}}
  149. PNIdMap.Map[thisinfo.Area] = km
  150. }
  151. PNIdMap.Lock.Unlock()
  152. thisinfo.AllRelatePNKeyMap = map[string]*Key{}
  153. pn.KeyMap.Lock.Lock()
  154. for k, v := range pn.KeyMap.Map {
  155. if strings.Contains(k, pn.Key) || strings.Contains(pn.Key, k) {
  156. thisinfo.AllRelatePNKeyMap[k] = v
  157. for _, id := range *v.Arr {
  158. if !repeatId[id] {
  159. newarr = append(newarr, id)
  160. repeatId[id] = true
  161. }
  162. }
  163. }
  164. }
  165. if thisinfo.AllRelatePNKeyMap[pn.Key] == nil {
  166. K := &Key{&[]string{}, &sync.Mutex{}}
  167. thisinfo.AllRelatePNKeyMap[pn.Key] = K
  168. pn.KeyMap.Map[pn.Key] = K
  169. }
  170. pn.KeyMap.Lock.Unlock()
  171. }
  172. if p.PcodeLen > 0 {
  173. pc := NewCompareInfo("pc", thisinfo.PCKey, PCKey)
  174. pncb = append(pncb, pc)
  175. thisinfo.AllRelatePCKeyMap = map[string]*Key{}
  176. pc.KeyMap.Lock.Lock()
  177. for k, v := range pc.KeyMap.Map {
  178. if strings.Contains(k, pc.Key) || strings.Contains(pc.Key, k) {
  179. thisinfo.AllRelatePCKeyMap[k] = v
  180. for _, id := range *v.Arr {
  181. if !repeatId[id] {
  182. newarr = append(newarr, id)
  183. repeatId[id] = true
  184. }
  185. }
  186. }
  187. }
  188. if thisinfo.AllRelatePCKeyMap[pc.Key] == nil {
  189. K := &Key{&[]string{}, &sync.Mutex{}}
  190. thisinfo.AllRelatePCKeyMap[pc.Key] = K
  191. pc.KeyMap.Map[pc.Key] = K
  192. }
  193. pc.KeyMap.Lock.Unlock()
  194. }
  195. if p.BuyerLen > 0 {
  196. pb := NewCompareInfo("pb", thisinfo.PBKey, PBKey)
  197. pncb = append(pncb, pb)
  198. pb.KeyMap.Lock.Lock()
  199. K := pb.KeyMap.Map[pb.Key]
  200. if K == nil {
  201. K = &Key{&[]string{}, &sync.Mutex{}}
  202. pb.KeyMap.Map[pb.Key] = K
  203. } else {
  204. for _, id := range *K.Arr {
  205. if !repeatId[id] {
  206. newarr = append(newarr, id)
  207. repeatId[id] = true
  208. }
  209. }
  210. }
  211. pb.KeyMap.Lock.Unlock()
  212. }
  213. if len(newarr) > 0 {
  214. res = redis.Mget(REDISIDS, newarr)
  215. }
  216. return
  217. }