mysql.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "go-common/app/admin/main/dm/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _pageSize = 1000
  14. _subjectSharding = 100
  15. _indexSharding = 1000
  16. _contentSharding = 1000
  17. // subject
  18. _updateSubAttr = "UPDATE dm_subject_%02d SET attr=? WHERE type=? AND oid=?"
  19. _updateSubCount = "UPDATE dm_subject_%02d SET acount=?,count=? WHERE type=? AND oid=?"
  20. _updateSubMCountSQL = "UPDATE dm_subject_%02d SET mcount=? WHERE type=? AND oid=?"
  21. _incrSubMoveCntSQL = "UPDATE dm_subject_%02d SET move_count=move_count+? WHERE type=? AND oid=?"
  22. _updateSubPoolSQL = "UPDATE dm_subject_%02d SET childpool=? WHERE type=? AND oid=?"
  23. _updateSubStateSQL = "UPDATE dm_subject_%02d SET state=? WHERE type=? AND oid=?"
  24. _updateSubMaxlimitSQL = "UPDATE dm_subject_%02d SET maxlimit=? WHERE type=? AND oid=?"
  25. _incrSubCountSQL = "UPDATE dm_subject_%02d SET count=count+? WHERE type=? AND oid=?"
  26. _selectDMCount = "SELECT count(*) from dm_index_%03d WHERE type=? AND oid=? AND state IN(%s)"
  27. _subSQL = "SELECT id,type,oid,pid,mid,state,attr,acount,count,mcount,move_count,maxlimit,childpool,ctime,mtime FROM dm_subject_%02d WHERE type=? AND oid=?"
  28. _getSubjectsSQL = "SELECT id,type,oid,pid,mid,state,attr,acount,count,mcount,move_count,maxlimit,childpool,ctime,mtime FROM dm_subject_%02d WHERE type=? AND oid IN(%s)"
  29. _contentsSQL = "SELECT dmid,fontsize,color,mode,ip,plat,msg,ctime,mtime FROM dm_content_%03d WHERE dmid IN(%s)"
  30. _contentsSpeSQL = "SELECT dmid,msg,ctime,mtime FROM dm_special_content WHERE dmid IN(%s)"
  31. _idxsByidSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE type=? AND oid=? AND id IN(%s)"
  32. _updatePoolID = "UPDATE dm_index_%03d SET pool=? WHERE type=? AND id IN (%s)"
  33. _updateDMState = "UPDATE dm_index_%03d SET state=? WHERE type=? AND id IN (%s)"
  34. _updateDMAttr = "UPDATE dm_index_%03d SET attr=? WHERE type=? AND id IN (%s)"
  35. )
  36. func (d *Dao) hitSubject(oid int64) int64 {
  37. return oid % _subjectSharding
  38. }
  39. func (d *Dao) hitIndex(oid int64) int64 {
  40. return oid % _indexSharding
  41. }
  42. func (d *Dao) hitContent(oid int64) int64 {
  43. return oid % _contentSharding
  44. }
  45. // UpSubjectAttr update subject attr.
  46. func (d *Dao) UpSubjectAttr(c context.Context, tp int32, oid int64, attr int32) (affect int64, err error) {
  47. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubAttr, d.hitSubject(oid)), attr, tp, oid)
  48. if err != nil {
  49. log.Error("dmMetaWriter.Exec() error(%v)", err)
  50. return
  51. }
  52. return res.RowsAffected()
  53. }
  54. // UpSubjectCount update acount,count of subject.
  55. func (d *Dao) UpSubjectCount(c context.Context, tp int32, oid, acount, count int64) (affect int64, err error) {
  56. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubCount, d.hitSubject(oid)), acount, count, tp, oid)
  57. if err != nil {
  58. log.Error("dmMetaWriter.Exec() error(%v)", err)
  59. return
  60. }
  61. return res.RowsAffected()
  62. }
  63. // IncrSubjectCount update count.
  64. func (d *Dao) IncrSubjectCount(c context.Context, tp int32, oid, count int64) (affect int64, err error) {
  65. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_incrSubCountSQL, d.hitSubject(oid)), count, tp, oid)
  66. if err != nil {
  67. log.Error("dmMetaWriter.Exec() error(%v)", err)
  68. return
  69. }
  70. return res.RowsAffected()
  71. }
  72. // UpSubjectMCount update monitor dm count.
  73. func (d *Dao) UpSubjectMCount(c context.Context, tp int32, oid, cnt int64) (affect int64, err error) {
  74. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubMCountSQL, d.hitSubject(oid)), cnt, tp, oid)
  75. if err != nil {
  76. log.Error("d.dmMetaWriter.Exect(%s,%d,%d,%d) error(%v)", _updateSubMCountSQL, cnt, tp, oid, err)
  77. return
  78. }
  79. return res.RowsAffected()
  80. }
  81. // IncrSubMoveCount update move_count in dm_subject.
  82. func (d *Dao) IncrSubMoveCount(c context.Context, tp int32, oid, count int64) (affect int64, err error) {
  83. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_incrSubMoveCntSQL, d.hitSubject(oid)), count, tp, oid)
  84. if err != nil {
  85. log.Error("d.dmMetaWriter.Exec(%s,%d,%d,%d) error(%v)", _incrSubMoveCntSQL, tp, oid, count, err)
  86. return
  87. }
  88. return res.RowsAffected()
  89. }
  90. // UpSubjectPool update childpool in dm subject.
  91. func (d *Dao) UpSubjectPool(c context.Context, tp int32, oid int64, childpool int32) (affect int64, err error) {
  92. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubPoolSQL, d.hitSubject(oid)), childpool, tp, oid)
  93. if err != nil {
  94. log.Error("d.dmWriter.Exect(%s,%d,%d,%d) error(%v)", _updateSubPoolSQL, childpool, tp, oid, err)
  95. return
  96. }
  97. return res.RowsAffected()
  98. }
  99. // UpSubjectState update state in dm subject.
  100. func (d *Dao) UpSubjectState(c context.Context, tp int32, oid int64, state int32) (affect int64, err error) {
  101. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubStateSQL, d.hitSubject(oid)), state, tp, oid)
  102. if err != nil {
  103. log.Error("d.dmWriter.Exect(%s,%d,%d,%d) error(%v)", _updateSubStateSQL, state, tp, oid, err)
  104. return
  105. }
  106. return res.RowsAffected()
  107. }
  108. // UpSubjectMaxlimit update maxlimit in dm subject.
  109. func (d *Dao) UpSubjectMaxlimit(c context.Context, tp int32, oid, maxlimit int64) (affect int64, err error) {
  110. res, err := d.dmMetaWriter.Exec(c, fmt.Sprintf(_updateSubMaxlimitSQL, d.hitSubject(oid)), maxlimit, tp, oid)
  111. if err != nil {
  112. log.Error("d.dmWriter.Exect(%s,%d,%d,%d) error(%v)", _updateSubMaxlimitSQL, maxlimit, tp, oid, err)
  113. return
  114. }
  115. return res.RowsAffected()
  116. }
  117. // DMCount statistics dm count by dm state.
  118. func (d *Dao) DMCount(c context.Context, tp int32, oid int64, states []int64) (count int64, err error) {
  119. row := d.dmMetaReader.QueryRow(c, fmt.Sprintf(_selectDMCount, d.hitIndex(oid), xstr.JoinInts(states)), tp, oid)
  120. if err = row.Scan(&count); err != nil {
  121. log.Error("row.Scan() error(%v)", err)
  122. }
  123. return
  124. }
  125. // Subject get subject info from db.
  126. func (d *Dao) Subject(c context.Context, tp int32, oid int64) (s *model.Subject, err error) {
  127. s = &model.Subject{}
  128. row := d.dmMetaReader.QueryRow(c, fmt.Sprintf(_subSQL, d.hitSubject(oid)), tp, oid)
  129. if err = row.Scan(&s.ID, &s.Type, &s.Oid, &s.Pid, &s.Mid, &s.State, &s.Attr, &s.ACount, &s.Count, &s.MCount, &s.MoveCnt, &s.Maxlimit, &s.Childpool, &s.Ctime, &s.Mtime); err != nil {
  130. if err == sql.ErrNoRows {
  131. s = nil
  132. err = nil
  133. } else {
  134. log.Error("row.Scan() error(%v)", err)
  135. }
  136. }
  137. return
  138. }
  139. // Subjects multi get subjects.
  140. func (d *Dao) Subjects(c context.Context, tp int32, oids []int64) (res map[int64]*model.Subject, err error) {
  141. var (
  142. oidMap = make(map[int64][]int64)
  143. mutext = &sync.Mutex{}
  144. wg, errCtx = errgroup.WithContext(c)
  145. )
  146. if len(oids) == 0 {
  147. return
  148. }
  149. res = make(map[int64]*model.Subject, len(oids))
  150. for _, oid := range oids {
  151. if _, ok := oidMap[d.hitSubject(oid)]; !ok {
  152. oidMap[d.hitSubject(oid)] = make([]int64, 0)
  153. }
  154. oidMap[d.hitSubject(oid)] = append(oidMap[d.hitSubject(oid)], oid)
  155. }
  156. for key, value := range oidMap {
  157. k := key
  158. v := value
  159. wg.Go(func() (err error) {
  160. rows, err := d.dmMetaReader.Query(errCtx, fmt.Sprintf(_getSubjectsSQL, k, xstr.JoinInts(v)), tp)
  161. if err != nil {
  162. log.Error("dmMetaReader.Query() error(%v)", err)
  163. return
  164. }
  165. defer rows.Close()
  166. for rows.Next() {
  167. s := &model.Subject{}
  168. err = rows.Scan(&s.ID, &s.Type, &s.Oid, &s.Pid, &s.Mid, &s.State, &s.Attr, &s.ACount, &s.Count, &s.MCount, &s.MoveCnt, &s.Maxlimit, &s.Childpool, &s.Ctime, &s.Mtime)
  169. if err != nil {
  170. log.Error("rows.Scan() error(%v)", err)
  171. return
  172. }
  173. mutext.Lock()
  174. res[s.Oid] = s
  175. mutext.Unlock()
  176. }
  177. return
  178. })
  179. }
  180. if err = wg.Wait(); err != nil {
  181. log.Error("d.Subjects() error(%v)", err)
  182. return
  183. }
  184. if len(res) == 0 {
  185. res = nil
  186. }
  187. return
  188. }
  189. // SetStateByIDs change danmu state in new db
  190. func (d *Dao) SetStateByIDs(c context.Context, tp int32, oid int64, ids []int64, state int32) (affect int64, err error) {
  191. sqlStr := fmt.Sprintf(_updateDMState, d.hitIndex(oid), xstr.JoinInts(ids))
  192. res, err := d.dmMetaWriter.Exec(c, sqlStr, state, tp)
  193. if err != nil {
  194. log.Error("d.dmMetaWriter.Exec(%s %d) error(%v)", sqlStr, state, err)
  195. return
  196. }
  197. return res.RowsAffected()
  198. }
  199. // SetAttrByIDs set attr by mult dmids in new db
  200. func (d *Dao) SetAttrByIDs(c context.Context, tp int32, oid int64, ids []int64, val int32) (affect int64, err error) {
  201. sqlStr := fmt.Sprintf(_updateDMAttr, d.hitIndex(oid), xstr.JoinInts(ids))
  202. res, err := d.dmMetaWriter.Exec(c, sqlStr, val, tp)
  203. if err != nil {
  204. log.Error("d.dmMetaWriter.Exec(%s %d) error(%v)", sqlStr, val, err)
  205. return
  206. }
  207. return res.RowsAffected()
  208. }
  209. // SetPoolIDByIDs change danmu poolid
  210. func (d *Dao) SetPoolIDByIDs(c context.Context, tp int32, oid int64, pool int32, dmids []int64) (affect int64, err error) {
  211. sqlStr := fmt.Sprintf(_updatePoolID, d.hitIndex(oid), xstr.JoinInts(dmids))
  212. res, err := d.dmMetaWriter.Exec(c, sqlStr, pool, tp)
  213. if err != nil {
  214. log.Error("d.dbComment.Exec(%v) error(%v)", sqlStr, err)
  215. return
  216. }
  217. return res.RowsAffected()
  218. }
  219. // IndexsByID get dm index by dmids.
  220. func (d *Dao) IndexsByID(c context.Context, tp int32, oid int64, dmids []int64) (idxMap map[int64]*model.DM, special []int64, err error) {
  221. query := fmt.Sprintf(_idxsByidSQL, d.hitIndex(oid), xstr.JoinInts(dmids))
  222. rows, err := d.dmMetaReader.Query(c, query, tp, oid)
  223. if err != nil {
  224. log.Error("db.Query() error(%v)", err)
  225. return
  226. }
  227. defer rows.Close()
  228. idxMap = make(map[int64]*model.DM, len(dmids))
  229. for rows.Next() {
  230. idx := new(model.DM)
  231. if err = rows.Scan(&idx.ID, &idx.Type, &idx.Oid, &idx.Mid, &idx.Progress, &idx.State, &idx.Pool, &idx.Attr, &idx.Ctime, &idx.Mtime); err != nil {
  232. log.Error("row.Scan() error(%v)", err)
  233. return
  234. }
  235. idxMap[idx.ID] = idx
  236. if idx.Pool == model.PoolSpecial {
  237. special = append(special, idx.ID)
  238. }
  239. }
  240. return
  241. }
  242. // Contents multi get dm content by dmids.
  243. func (d *Dao) Contents(c context.Context, oid int64, dmids []int64) (res []*model.Content, err error) {
  244. var (
  245. wg errgroup.Group
  246. lock sync.Mutex
  247. )
  248. pageNum := len(dmids) / _pageSize
  249. if len(dmids)%_pageSize > 0 {
  250. pageNum = pageNum + 1
  251. }
  252. for i := 0; i < pageNum; i++ {
  253. start := i * _pageSize
  254. end := (i + 1) * _pageSize
  255. if end > len(dmids) {
  256. end = len(dmids)
  257. }
  258. wg.Go(func() (err error) {
  259. rows, err := d.dmMetaReader.Query(c, fmt.Sprintf(_contentsSQL, d.hitContent(oid), xstr.JoinInts(dmids[start:end])))
  260. if err != nil {
  261. log.Error("db.Query(%s) error(%v)", fmt.Sprintf(_contentsSQL, d.hitContent(oid), xstr.JoinInts(dmids)), err)
  262. return
  263. }
  264. defer rows.Close()
  265. for rows.Next() {
  266. ct := &model.Content{}
  267. if err = rows.Scan(&ct.ID, &ct.FontSize, &ct.Color, &ct.Mode, &ct.IP, &ct.Plat, &ct.Msg, &ct.Ctime, &ct.Mtime); err != nil {
  268. log.Error("rows.Scan() error(%v)", err)
  269. return
  270. }
  271. lock.Lock()
  272. res = append(res, ct)
  273. lock.Unlock()
  274. }
  275. return
  276. })
  277. }
  278. err = wg.Wait()
  279. return
  280. }
  281. // SpecialContents multi get special dm content by dmids.
  282. func (d *Dao) SpecialContents(c context.Context, dmids []int64) (res map[int64]*model.ContentSpecial, err error) {
  283. res = make(map[int64]*model.ContentSpecial, len(dmids))
  284. rows, err := d.dmMetaReader.Query(c, fmt.Sprintf(_contentsSpeSQL, xstr.JoinInts(dmids)))
  285. if err != nil {
  286. log.Error("db.Query() error(%v)", err)
  287. return
  288. }
  289. defer rows.Close()
  290. for rows.Next() {
  291. content := &model.ContentSpecial{}
  292. if err = rows.Scan(&content.ID, &content.Msg, &content.Ctime, &content.Mtime); err != nil {
  293. log.Error("rows.Scan() error(%v)", err)
  294. return
  295. }
  296. res[content.ID] = content
  297. }
  298. return
  299. }