mysql.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "go-common/app/interface/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. _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=?"
  18. _idxSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE id=? AND oid=? AND type=?"
  19. _idxsByIDSQL = "SELECT id,type,oid,mid,progress,state,pool,attr,ctime,mtime FROM dm_index_%03d WHERE id IN(%s) AND oid=? AND type=?"
  20. _contentSQL = "SELECT dmid,fontsize,color,mode,ip,plat,msg,ctime,mtime FROM dm_content_%03d WHERE dmid=?"
  21. _contentsSQL = "SELECT dmid,fontsize,color,mode,ip,plat,msg,ctime,mtime FROM dm_content_%03d WHERE dmid IN(%s)"
  22. _contentSpeSQL = "SELECT dmid,msg,ctime,mtime FROM dm_special_content WHERE dmid=?"
  23. _contentsSpeSQL = "SELECT dmid,msg,ctime,mtime FROM dm_special_content WHERE dmid IN(%s)"
  24. // dm transfer
  25. _addTransferJob = "INSERT INTO dm_transfer_job SET from_cid=?,to_cid=?,mid=?,offset=?,state=?"
  26. _selTransferJob = "SELECT id,from_cid,to_cid,mid,offset,state,ctime,mtime from dm_transfer_job WHERE from_cid=? AND to_cid=?"
  27. _transferListSQL = "SELECT id,from_cid,state,ctime FROM dm_transfer_job where to_cid=?"
  28. _uptTransferSQL = "UPDATE dm_transfer_job SET state=? WHERE id=?"
  29. _selTransferID = "SELECT id,from_cid,to_cid,mid,offset,state,ctime,mtime from dm_transfer_job WHERE id=?"
  30. //dm state update
  31. _updateIdxStatSQL = "UPDATE dm_index_%03d SET state=? WHERE type=? AND oid=? AND id IN(%s)"
  32. )
  33. func (d *Dao) hitSubject(oid int64) int64 {
  34. return oid % _subjectSharding
  35. }
  36. func (d *Dao) hitIndex(oid int64) int64 {
  37. return oid % _indexSharding
  38. }
  39. func (d *Dao) hitContent(dmid int64) int64 {
  40. return dmid % _contentSharding
  41. }
  42. // Subject get subject info from db.
  43. func (d *Dao) Subject(c context.Context, tp int32, oid int64) (s *model.Subject, err error) {
  44. s = &model.Subject{}
  45. row := d.dmMetaReader.QueryRow(c, fmt.Sprintf(_subSQL, d.hitSubject(oid)), tp, oid)
  46. 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 {
  47. if err == sql.ErrNoRows {
  48. s = nil
  49. err = nil
  50. } else {
  51. log.Error("row.Scan() error(%v)", err)
  52. }
  53. }
  54. return
  55. }
  56. // Index get index by dmid
  57. func (d *Dao) Index(c context.Context, tp int32, oid, dmid int64) (dm *model.DM, err error) {
  58. dm = &model.DM{}
  59. row := d.dmMetaReader.QueryRow(c, fmt.Sprintf(_idxSQL, d.hitIndex(oid)), dmid, oid, tp)
  60. if err = row.Scan(&dm.ID, &dm.Type, &dm.Oid, &dm.Mid, &dm.Progress, &dm.State, &dm.Pool, &dm.Attr, &dm.Ctime, &dm.Mtime); err != nil {
  61. if err == sql.ErrNoRows {
  62. dm = nil
  63. err = nil
  64. } else {
  65. log.Error("row.Scan() error(%v)", err)
  66. }
  67. }
  68. return
  69. }
  70. // IndexsByID get dm index by dmids.
  71. func (d *Dao) IndexsByID(c context.Context, tp int32, oid int64, dmids []int64) (res map[int64]*model.DM, special []int64, err error) {
  72. rows, err := d.dmMetaReader.Query(c, fmt.Sprintf(_idxsByIDSQL, d.hitIndex(oid), xstr.JoinInts(dmids)), oid, tp)
  73. if err != nil {
  74. log.Error("db.Query() error(%v)", err)
  75. return
  76. }
  77. defer rows.Close()
  78. res = make(map[int64]*model.DM, len(dmids))
  79. for rows.Next() {
  80. dm := &model.DM{}
  81. if err = rows.Scan(&dm.ID, &dm.Type, &dm.Oid, &dm.Mid, &dm.Progress, &dm.State, &dm.Pool, &dm.Attr, &dm.Ctime, &dm.Mtime); err != nil {
  82. log.Error("row.Scan() error(%v)", err)
  83. return
  84. }
  85. res[dm.ID] = dm
  86. if dm.Pool == model.PoolSpecial {
  87. special = append(special, dm.ID)
  88. }
  89. }
  90. if err = rows.Err(); err != nil {
  91. log.Error("rows.Err() error(%v)", err)
  92. }
  93. return
  94. }
  95. // Content dm content by dmid
  96. func (d *Dao) Content(c context.Context, oid, dmid int64) (ct *model.Content, err error) {
  97. ct = &model.Content{}
  98. row := d.dmMetaReader.QueryRow(c, fmt.Sprintf(_contentSQL, d.hitContent(oid)), dmid)
  99. if err = row.Scan(&ct.ID, &ct.FontSize, &ct.Color, &ct.Mode, &ct.IP, &ct.Plat, &ct.Msg, &ct.Ctime, &ct.Mtime); err != nil {
  100. if err == sql.ErrNoRows {
  101. ct = nil
  102. err = nil
  103. } else {
  104. log.Error("row.Scan() error(%v)", err)
  105. }
  106. }
  107. return
  108. }
  109. // Contents multi get dm content by dmids.
  110. func (d *Dao) Contents(c context.Context, oid int64, dmids []int64) (res []*model.Content, err error) {
  111. var (
  112. wg errgroup.Group
  113. lock sync.Mutex
  114. )
  115. pageNum := len(dmids) / _pagesize
  116. if len(dmids)%_pagesize > 0 {
  117. pageNum = pageNum + 1
  118. }
  119. for i := 0; i < pageNum; i++ {
  120. start := i * _pagesize
  121. end := (i + 1) * _pagesize
  122. if end > len(dmids) {
  123. end = len(dmids)
  124. }
  125. wg.Go(func() (err error) {
  126. rows, err := d.dmMetaReader.Query(c, fmt.Sprintf(_contentsSQL, d.hitContent(oid), xstr.JoinInts(dmids[start:end])))
  127. if err != nil {
  128. log.Error("db.Query(%s) error(%v)", fmt.Sprintf(_contentsSQL, d.hitContent(oid), xstr.JoinInts(dmids)), err)
  129. return
  130. }
  131. defer rows.Close()
  132. for rows.Next() {
  133. ct := &model.Content{}
  134. if err = rows.Scan(&ct.ID, &ct.FontSize, &ct.Color, &ct.Mode, &ct.IP, &ct.Plat, &ct.Msg, &ct.Ctime, &ct.Mtime); err != nil {
  135. log.Error("rows.Scan() error(%v)", err)
  136. return
  137. }
  138. lock.Lock()
  139. res = append(res, ct)
  140. lock.Unlock()
  141. }
  142. err = rows.Err()
  143. return
  144. })
  145. }
  146. if err = wg.Wait(); err != nil {
  147. log.Error("wg.Wait() error(%v)", err)
  148. }
  149. return
  150. }
  151. // ContentSpecial get special dm content by dmids.
  152. func (d *Dao) ContentSpecial(c context.Context, dmid int64) (contentSpe *model.ContentSpecial, err error) {
  153. contentSpe = &model.ContentSpecial{}
  154. row := d.dmMetaReader.QueryRow(c, _contentSpeSQL, dmid)
  155. if err = row.Scan(&contentSpe.ID, &contentSpe.Msg, &contentSpe.Ctime, &contentSpe.Mtime); err != nil {
  156. if err == sql.ErrNoRows {
  157. contentSpe = nil
  158. err = nil
  159. } else {
  160. log.Error("rows.Scan() error(%v)", err)
  161. }
  162. }
  163. return
  164. }
  165. // ContentsSpecial multi get special dm content by dmids.
  166. func (d *Dao) ContentsSpecial(c context.Context, dmids []int64) (res map[int64]*model.ContentSpecial, err error) {
  167. res = make(map[int64]*model.ContentSpecial, len(dmids))
  168. rows, err := d.dmMetaReader.Query(c, fmt.Sprintf(_contentsSpeSQL, xstr.JoinInts(dmids)))
  169. if err != nil {
  170. log.Error("db.Query() error(%v)", err)
  171. return
  172. }
  173. defer rows.Close()
  174. for rows.Next() {
  175. content := &model.ContentSpecial{}
  176. if err = rows.Scan(&content.ID, &content.Msg, &content.Ctime, &content.Mtime); err != nil {
  177. log.Error("rows.Scan() error(%v)", err)
  178. return
  179. }
  180. res[content.ID] = content
  181. }
  182. if err = rows.Err(); err != nil {
  183. log.Error("rows.Err() error(%v)", err)
  184. }
  185. return
  186. }
  187. // AddTransferJob add transfer job.
  188. func (d *Dao) AddTransferJob(c context.Context, fromCid, toCid, mid int64, offset float64, state int8) (affect int64, err error) {
  189. row, err := d.biliDM.Exec(c, _addTransferJob, fromCid, toCid, mid, offset, model.TransferJobStatInit)
  190. if err != nil {
  191. log.Error("d.biliDM.Exec(fromCid:%d,toCid:%d,mid:%d,offset:%v) error(%v)", fromCid, toCid, mid, offset, err)
  192. }
  193. return row.LastInsertId()
  194. }
  195. // CheckTransferJob check transfer job state.
  196. func (d *Dao) CheckTransferJob(c context.Context, fromCid, toCid int64) (job *model.TransferJob, err error) {
  197. job = new(model.TransferJob)
  198. row := d.biliDM.QueryRow(c, _selTransferJob, fromCid, toCid)
  199. if err = row.Scan(&job.ID, &job.FromCID, &job.ToCID, &job.MID, &job.Offset, &job.State, &job.Ctime, &job.Mtime); err != nil {
  200. if err == sql.ErrNoRows {
  201. job = nil
  202. err = nil
  203. } else {
  204. log.Error("row.Scan error(%v)", err)
  205. }
  206. }
  207. return
  208. }
  209. // CheckTransferID check transfer job state by id
  210. func (d *Dao) CheckTransferID(c context.Context, id int64) (job *model.TransferJob, err error) {
  211. job = new(model.TransferJob)
  212. row := d.biliDM.QueryRow(c, _selTransferID, id)
  213. if err = row.Scan(&job.ID, &job.FromCID, &job.ToCID, &job.MID, &job.Offset, &job.State, &job.Ctime, &job.Mtime); err != nil {
  214. if err == sql.ErrNoRows {
  215. job = nil
  216. err = nil
  217. } else {
  218. log.Error("row.Scan error(%v)", err)
  219. }
  220. }
  221. return
  222. }
  223. // TransferList cid 的转移历史
  224. func (d *Dao) TransferList(c context.Context, cid int64) (l []*model.TransferHistory, err error) {
  225. l = make([]*model.TransferHistory, 0)
  226. rows, err := d.biliDM.Query(c, _transferListSQL, cid)
  227. if err != nil {
  228. log.Error("d.biliDM.Query(%s,%d) error(%v)", _transferListSQL, cid, err)
  229. return
  230. }
  231. defer rows.Close()
  232. for rows.Next() {
  233. hs := new(model.TransferHistory)
  234. if err = rows.Scan(&hs.ID, &hs.CID, &hs.State, &hs.CTime); err != nil {
  235. log.Error("TransfrerList: rows.Scan() error(%v)", err)
  236. return
  237. }
  238. l = append(l, hs)
  239. }
  240. if err = rows.Err(); err != nil {
  241. log.Error("rows.Err() error(%v)", err)
  242. }
  243. return
  244. }
  245. // SetTransferState change transfer state
  246. func (d *Dao) SetTransferState(c context.Context, id int64, state int8) (affect int64, err error) {
  247. row, err := d.biliDM.Exec(c, _uptTransferSQL, state, id)
  248. if err != nil {
  249. log.Error("d.biliDM.Exec(%s,%d) error(%v)", _uptTransferSQL, id, err)
  250. return
  251. }
  252. return row.RowsAffected()
  253. }
  254. // UpdateDMStat edit dm state
  255. func (d *Dao) UpdateDMStat(c context.Context, tp int32, oid int64, state int32, dmids []int64) (affect int64, err error) {
  256. res, err := d.dmWriter.Exec(c, fmt.Sprintf(_updateIdxStatSQL, d.hitIndex(oid), xstr.JoinInts(dmids)), state, tp, oid)
  257. if err != nil {
  258. log.Error("dmWriter.Exec(%s %d) error(%v)", _updateIdxStatSQL, oid, err)
  259. return
  260. }
  261. return res.RowsAffected()
  262. }