inverted_index.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/log"
  6. "math/rand"
  7. "sort"
  8. "time"
  9. )
  10. const (
  11. _nblocks = 5
  12. _redisPrefix = "RECALL:NEWPUB:%d"
  13. )
  14. // GenRealTimeInvertedIndex 实时倒排标签
  15. func (s *Service) GenRealTimeInvertedIndex() {
  16. svids, err := s.dao.FetchNewincomeVideo()
  17. if err != nil || svids == nil || len(svids) == 0 {
  18. log.Error("GenRealTimeInvertedIndex FetchNewincomeVideo err[%v] svids[%v]", err, svids)
  19. return
  20. }
  21. // svid 乱序
  22. rand.Seed(time.Now().Unix())
  23. sort.Slice(svids, func(i int, j int) bool {
  24. return rand.Float32() > 0.5
  25. })
  26. // 平均分为5份
  27. offset := 0
  28. blocks := len(svids) / _nblocks
  29. invertedIndex := make([][]int64, _nblocks)
  30. for i := 0; i < _nblocks; i++ {
  31. invertedIndex[i] = make([]int64, 0)
  32. for j := 0; j < blocks; j++ {
  33. invertedIndex[i] = append(invertedIndex[i], svids[offset+j])
  34. }
  35. offset = offset + blocks
  36. }
  37. if blocks*_nblocks < len(svids) {
  38. invertedIndex[_nblocks-1] = append(invertedIndex[_nblocks-1], svids[len(svids)-1])
  39. }
  40. log.Info("GenRealTimeInvertedIndex invertedIndex[%v]", invertedIndex)
  41. // 序列化后写入redis
  42. for i, v := range invertedIndex {
  43. key := fmt.Sprintf(_redisPrefix, i)
  44. err = s.dao.SetInvertedIndex(context.Background(), key, v)
  45. if err != nil {
  46. log.Error("GenRealTimeInvertedIndex SetInvertedIndex err[%v]", err)
  47. }
  48. }
  49. log.Info("finish [GenRealTimeInvertedIndex]")
  50. }