app_multiple.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/job/main/search/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. // AppMultiple .
  12. type AppMultiple struct {
  13. d *Dao
  14. appid string
  15. attrs *model.Attrs
  16. db *xsql.DB
  17. offsets model.LoopOffsets
  18. mapData []model.MapData
  19. }
  20. // NewAppMultiple .
  21. func NewAppMultiple(d *Dao, appid string) (am *AppMultiple) {
  22. am = &AppMultiple{
  23. d: d,
  24. appid: appid,
  25. db: d.DBPool[d.AttrPool[appid].DBName],
  26. attrs: d.AttrPool[appid],
  27. offsets: make(map[int]*model.LoopOffset),
  28. }
  29. for i := am.attrs.Table.TableFrom; i <= am.attrs.Table.TableTo; i++ {
  30. am.offsets[i] = &model.LoopOffset{}
  31. }
  32. return
  33. }
  34. // Business return business
  35. func (am *AppMultiple) Business() string {
  36. return am.attrs.Business
  37. }
  38. // InitIndex .
  39. func (am *AppMultiple) InitIndex(c context.Context) {
  40. var (
  41. indexAliasName string
  42. indexEntityName string
  43. )
  44. aliases, err := am.d.GetAliases(am.attrs.ESName, am.attrs.Index.IndexAliasPrefix)
  45. for i := am.attrs.Index.IndexFrom; i <= am.attrs.Index.IndexTo; i++ {
  46. indexAliasName = fmt.Sprintf("%s%0"+am.attrs.Index.IndexZero+"d", am.attrs.Index.IndexAliasPrefix, i)
  47. indexEntityName = fmt.Sprintf("%s%0"+am.attrs.Index.IndexZero+"d", am.attrs.Index.IndexEntityPrefix, i)
  48. if err != nil {
  49. am.d.InitIndex(c, nil, am.attrs.ESName, indexAliasName, indexEntityName, am.attrs.Index.IndexMapping)
  50. } else {
  51. am.d.InitIndex(c, aliases, am.attrs.ESName, indexAliasName, indexEntityName, am.attrs.Index.IndexMapping)
  52. }
  53. }
  54. }
  55. // InitOffset insert init value to offset.
  56. func (am *AppMultiple) InitOffset(c context.Context) {
  57. am.d.InitOffset(c, am.offsets[0], am.attrs, []string{})
  58. }
  59. // Offset .
  60. func (am *AppMultiple) Offset(c context.Context) {
  61. for i := am.attrs.Table.TableFrom; i < am.attrs.Table.TableTo; i++ {
  62. offset, err := am.d.Offset(c, am.attrs.AppID, am.attrs.Table.TablePrefix+strconv.Itoa(i))
  63. if err != nil {
  64. log.Error("as.d.Offset error(%v)", err)
  65. time.Sleep(time.Second * 3)
  66. }
  67. am.offsets[i].SetReview(offset.ReviewID, offset.ReviewTime)
  68. am.offsets[i].SetOffset(offset.OffsetID(), offset.OffsetTime())
  69. }
  70. }
  71. // SetRecover set recover
  72. func (am *AppMultiple) SetRecover(c context.Context, recoverID int64, recoverTime string, i int) {
  73. am.offsets.SetRecoverOffsets(i, recoverID, recoverTime)
  74. }
  75. // IncrMessages .
  76. func (am *AppMultiple) IncrMessages(c context.Context) (length int, err error) {
  77. var rows *xsql.Rows
  78. am.mapData = []model.MapData{}
  79. for i := am.attrs.Table.TableFrom; i <= am.attrs.Table.TableTo; i++ {
  80. if !am.offsets[i].IsLoop {
  81. rows, err = am.db.Query(c, fmt.Sprintf(am.attrs.DataSQL.SQLByMTime, am.attrs.DataSQL.SQLFields, i), am.offsets[i].OffsetTime, am.attrs.Other.Size)
  82. } else {
  83. rows, err = am.db.Query(c, fmt.Sprintf(am.attrs.DataSQL.SQLByIDMTime, am.attrs.DataSQL.SQLFields, i), am.offsets[i].OffsetID, am.offsets[i].OffsetTime, am.attrs.Other.Size)
  84. }
  85. if err != nil {
  86. log.Error("db.Query error(%v)", err)
  87. continue
  88. }
  89. tempList := []model.MapData{}
  90. for rows.Next() {
  91. item, row := InitMapData(am.attrs.DataSQL.DataIndexFields)
  92. if err = rows.Scan(row...); err != nil {
  93. log.Error("rows.Scan() error(%v)", err)
  94. continue
  95. }
  96. tempList = append(tempList, item)
  97. am.mapData = append(am.mapData, item)
  98. }
  99. rows.Close()
  100. if len(tempList) > 0 {
  101. UpdateOffsetByMap(am.offsets[i], tempList...)
  102. }
  103. }
  104. if len(am.mapData) > 0 {
  105. //fmt.Println("before", am.mapData)
  106. am.mapData, err = am.d.ExtraData(c, am.mapData, am.attrs, "db", []string{})
  107. //fmt.Println("after", am.mapData)
  108. }
  109. length = len(am.mapData)
  110. return
  111. }
  112. // AllMessages .
  113. func (am *AppMultiple) AllMessages(c context.Context) (length int, err error) {
  114. am.mapData = []model.MapData{}
  115. for i := am.attrs.Table.TableFrom; i <= am.attrs.Table.TableTo; i++ {
  116. var rows *xsql.Rows
  117. if rows, err = am.db.Query(c, fmt.Sprintf(am.attrs.DataSQL.SQLByID, am.attrs.DataSQL.SQLFields, i), am.offsets[i].OffsetID, am.attrs.Other.Size); err != nil {
  118. log.Error("AllMessages db.Query error(%v)", err)
  119. return
  120. }
  121. tempList := []model.MapData{}
  122. for rows.Next() {
  123. item, row := InitMapData(am.attrs.DataSQL.DataIndexFields)
  124. if err = rows.Scan(row...); err != nil {
  125. log.Error("IncrMessages rows.Scan() error(%v)", err)
  126. continue
  127. }
  128. tempList = append(tempList, item)
  129. am.mapData = append(am.mapData, item)
  130. }
  131. rows.Close()
  132. tmpLength := len(tempList)
  133. if tmpLength > 0 {
  134. am.offsets[i].SetTempOffset(tempList[tmpLength-1].PrimaryID(), tempList[tmpLength-1].StrMTime())
  135. }
  136. }
  137. if len(am.mapData) > 0 {
  138. am.mapData, err = am.d.ExtraData(c, am.mapData, am.attrs, "db", []string{})
  139. }
  140. length = len(am.mapData)
  141. return
  142. }
  143. // BulkIndex .
  144. func (am *AppMultiple) BulkIndex(c context.Context, start int, end int, writeEntityIndex bool) (err error) {
  145. if len(am.mapData) >= (start+1) && len(am.mapData) >= end {
  146. partData := am.mapData[start:end]
  147. err = am.d.BulkDBData(c, am.attrs, writeEntityIndex, partData...)
  148. }
  149. return
  150. }
  151. // Commit .
  152. func (am *AppMultiple) Commit(c context.Context) (err error) {
  153. for i := am.attrs.Table.TableFrom; i <= am.attrs.Table.TableTo; i++ {
  154. if err = am.d.CommitOffset(c, am.offsets[i], am.attrs.AppID, am.attrs.Table.TablePrefix+strconv.Itoa(i)); err != nil {
  155. log.Error("Commit error(%v)", err)
  156. continue
  157. }
  158. }
  159. am.mapData = []model.MapData{}
  160. return
  161. }
  162. // Sleep .
  163. func (am *AppMultiple) Sleep(c context.Context) {
  164. time.Sleep(time.Second * time.Duration(am.attrs.Other.Sleep))
  165. }
  166. // Size .
  167. func (am *AppMultiple) Size(c context.Context) (size int) {
  168. size = am.attrs.Other.Size
  169. return
  170. }