dm_seg.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package service
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "context"
  6. "math"
  7. "go-common/app/job/main/dm2/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. func (s *Service) gzip(input []byte) ([]byte, error) {
  12. buf := new(bytes.Buffer)
  13. zw := gzip.NewWriter(buf)
  14. if _, err := zw.Write(input); err != nil {
  15. return nil, err
  16. }
  17. if err := zw.Close(); err != nil {
  18. return nil, err
  19. }
  20. return buf.Bytes(), nil
  21. }
  22. func (s *Service) dmsByid(c context.Context, tp int32, oid int64, missed []int64) (dms []*model.DM, err error) {
  23. idxMap, special, err := s.dao.IndexsByid(c, tp, oid, missed)
  24. if err != nil || len(idxMap) == 0 {
  25. return
  26. }
  27. ctsMap, err := s.dao.Contents(c, oid, missed)
  28. if err != nil {
  29. return
  30. }
  31. ctsSpeMap := make(map[int64]*model.ContentSpecial)
  32. if len(special) > 0 {
  33. if ctsSpeMap, err = s.dao.ContentsSpecial(c, special); err != nil {
  34. return
  35. }
  36. }
  37. for _, content := range ctsMap {
  38. if idx, ok := idxMap[content.ID]; ok {
  39. dm := &model.DM{
  40. ID: idx.ID,
  41. Type: idx.Type,
  42. Oid: idx.Oid,
  43. Mid: idx.Mid,
  44. Progress: idx.Progress,
  45. Pool: idx.Pool,
  46. Attr: idx.Attr,
  47. State: idx.State,
  48. Ctime: idx.Ctime,
  49. Mtime: idx.Mtime,
  50. Content: content,
  51. }
  52. if idx.Pool == model.PoolSpecial {
  53. if _, ok = ctsSpeMap[dm.ID]; ok {
  54. dm.ContentSpe = ctsSpeMap[dm.ID]
  55. }
  56. }
  57. dms = append(dms, dm)
  58. }
  59. }
  60. return
  61. }
  62. func (s *Service) dmSeg(c context.Context, tp int32, oid, limit int64, childpool int32, p *model.Page) (res *model.DMSeg, err error) {
  63. var (
  64. ids []int64
  65. cache = true
  66. dmids = make([]int64, 0, limit)
  67. elems = make([]*model.Elem, 0, limit)
  68. ps = (p.Num - 1) * p.Size
  69. pe = p.Num * p.Size
  70. )
  71. res = new(model.DMSeg)
  72. if ids, err = s.dmidsSeg(c, tp, oid, p.Total, p.Num, ps, pe, limit); err != nil {
  73. return
  74. }
  75. dmids = append(dmids, ids...)
  76. if childpool > 0 {
  77. if ids, err = s.dmidSubtitle(c, tp, oid, ps, pe, limit); err != nil {
  78. return
  79. }
  80. dmids = append(dmids, ids...)
  81. }
  82. if len(dmids) <= 0 {
  83. return
  84. }
  85. elemsCache, missed, err := s.dao.IdxContentCacheV2(c, tp, oid, dmids)
  86. if err != nil {
  87. missed = dmids
  88. cache = false
  89. } else {
  90. elems = append(elems, elemsCache...)
  91. }
  92. if len(missed) == 0 {
  93. res.Elems = elems
  94. return
  95. }
  96. dms, err := s.dmsByid(c, tp, oid, missed)
  97. if err != nil {
  98. return
  99. }
  100. for _, dm := range dms {
  101. if e := dm.ToElem(); e != nil {
  102. elems = append(elems, e)
  103. }
  104. }
  105. res.Elems = elems
  106. if cache && len(dms) > 0 {
  107. s.cache.Do(c, func(ctx context.Context) {
  108. s.dao.AddIdxContentCaches(ctx, tp, oid, dms...)
  109. })
  110. }
  111. return
  112. }
  113. func (s *Service) dmidsSeg(c context.Context, tp int32, oid, total, num, ps, pe, limit int64) (dmids []int64, err error) {
  114. if dmids, err = s.dao.DMIDCache(c, tp, oid, total, num, limit); err != nil || len(dmids) == 0 {
  115. if dmids, err = s.dao.IndexsSegID(c, tp, oid, ps, pe, limit, model.PoolNormal); err != nil {
  116. return
  117. }
  118. if len(dmids) > 0 {
  119. s.cache.Do(c, func(ctx context.Context) {
  120. s.dao.AddDMIDCache(ctx, tp, oid, total, num, dmids...)
  121. })
  122. }
  123. }
  124. return
  125. }
  126. func (s *Service) dmidSubtitle(c context.Context, tp int32, oid, ps, pe, limit int64) (dmids []int64, err error) {
  127. if dmids, err = s.dao.DMIDSubtitleCache(c, tp, oid, ps, pe, limit); err != nil || len(dmids) == 0 {
  128. var dms []*model.DM
  129. if dms, dmids, err = s.dao.IndexsSeg(c, tp, oid, ps, pe, limit, model.PoolSubtitle); err != nil {
  130. return
  131. }
  132. if len(dms) > 0 {
  133. s.cache.Do(c, func(ctx context.Context) {
  134. s.dao.AddDMIDSubtitleCache(ctx, tp, oid, dms...)
  135. })
  136. }
  137. }
  138. return
  139. }
  140. // add flush dm segment action to flush channel.
  141. func (s *Service) asyncAddFlushDMSeg(c context.Context, fc *model.FlushDMSeg) (err error) {
  142. select {
  143. case s.flushSegChan[fc.Oid%int64(s.routineSize)] <- fc:
  144. default:
  145. log.Warn("segment flush merge channel is full,fc:%+v page:%+v", fc, fc.Page)
  146. }
  147. return
  148. }
  149. func (s *Service) pageinfo(c context.Context, pid int64, dm *model.DM) (p *model.Page, err error) {
  150. duration, err := s.videoDuration(c, pid, dm.Oid)
  151. if err != nil {
  152. return
  153. }
  154. if duration != 0 {
  155. p = &model.Page{
  156. Num: int64(math.Ceil(float64(dm.Progress) / float64(model.DefaultPageSize))),
  157. Size: model.DefaultPageSize,
  158. Total: int64(math.Ceil(float64(duration) / float64(model.DefaultPageSize))),
  159. }
  160. if p.Num == 0 { // fix progress == 0
  161. p.Num = 1
  162. }
  163. } else { // duration not exist
  164. p = model.DefaultPage
  165. }
  166. // NOTE PoolSpecial store in the first segment
  167. if dm.Pool == model.PoolSpecial {
  168. p.Num = 1
  169. }
  170. return
  171. }
  172. func (s *Service) dmSegXML(c context.Context, sub *model.Subject, seg *model.Segment) (res []byte, err error) {
  173. var (
  174. cache = true
  175. buf = new(bytes.Buffer)
  176. dms []*model.DM
  177. dmids, normalIds, subtitleIds []int64
  178. )
  179. buf.WriteString(seg.ToXMLHeader(sub.Oid, sub.State, 0))
  180. defer func() {
  181. if err == nil {
  182. buf.WriteString(`</i>`)
  183. res, err = s.gzip(buf.Bytes())
  184. }
  185. }()
  186. if normalIds, err = s.dmidsSeg(c, sub.Type, sub.Oid, seg.Cnt, seg.Num, seg.Start, seg.End, 2*sub.Maxlimit); err != nil {
  187. return
  188. }
  189. dmids = append(dmids, normalIds...)
  190. if sub.Childpool > 0 {
  191. if subtitleIds, err = s.dmidSubtitle(c, sub.Type, sub.Oid, seg.Start, seg.End, 2*sub.Maxlimit); err != nil {
  192. return
  193. }
  194. dmids = append(dmids, subtitleIds...)
  195. }
  196. if len(dmids) <= 0 {
  197. return
  198. }
  199. content, missed, err := s.dao.IdxContentCache(c, sub.Type, sub.Oid, dmids)
  200. if err != nil {
  201. missed = dmids
  202. cache = false
  203. } else {
  204. buf.Write(content)
  205. }
  206. if len(missed) > 0 {
  207. if dms, err = s.dmsByid(c, sub.Type, sub.Oid, missed); err != nil {
  208. return
  209. }
  210. for _, dm := range dms {
  211. buf.WriteString(dm.ToXMLSeg())
  212. }
  213. }
  214. if cache && len(dms) > 0 {
  215. s.cache.Do(c, func(ctx context.Context) {
  216. s.dao.AddIdxContentCaches(ctx, sub.Type, sub.Oid, dms...)
  217. })
  218. }
  219. return
  220. }
  221. // segmentInfo get segment info of oid.
  222. func (s *Service) segmentInfo(c context.Context, aid, oid, ps int64, duration int64) (seg *model.Segment, err error) {
  223. if duration != 0 && ps >= duration {
  224. log.Warn("oid:%d ps:%d larger than duration:%d", oid, ps, duration)
  225. err = ecode.NotModified
  226. return
  227. }
  228. seg = model.SegmentInfo(ps, duration)
  229. return
  230. }