document.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/interface/main/kvo/model"
  7. "go-common/app/interface/main/kvo/model/module"
  8. "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. // Document get document
  13. func (s *Service) Document(c context.Context, mid int64, moduleKey string, timestamp int64, checkSum int64) (setting *module.Setting, err error) {
  14. var (
  15. uc *model.UserConf
  16. rm json.RawMessage
  17. moduleKeyID int
  18. )
  19. if moduleKeyID = module.VerifyModuleKey(moduleKey); moduleKeyID == 0 {
  20. err = ecode.RequestErr
  21. return
  22. }
  23. uc, err = s.userConf(c, mid, moduleKeyID)
  24. if err != nil {
  25. return
  26. }
  27. if uc.CheckSum == 0 || uc.Timestamp == 0 {
  28. err = ecode.NotModified
  29. return
  30. }
  31. // 数据没有变动
  32. if uc.CheckSum == checkSum && uc.Timestamp == timestamp {
  33. err = ecode.NotModified
  34. return
  35. }
  36. rm, err = s.document(c, uc.CheckSum)
  37. if err != nil {
  38. return
  39. }
  40. setting = &module.Setting{
  41. Timestamp: uc.Timestamp,
  42. CheckSum: uc.CheckSum,
  43. Data: rm,
  44. }
  45. return
  46. }
  47. func (s *Service) userConf(c context.Context, mid int64, moduleKeyID int) (uc *model.UserConf, err error) {
  48. uc, err = s.da.UserConfCache(c, mid, moduleKeyID)
  49. if err != nil {
  50. log.Error("service.userConf.UserConfCache(%v,%v) err:%v", mid, moduleKeyID, err)
  51. }
  52. if uc != nil {
  53. s.sp.Incr("user_conf_cached")
  54. return
  55. }
  56. uc, err = s.da.UserConf(c, mid, moduleKeyID)
  57. if err != nil {
  58. log.Error("service.userConf(%v,%v) err:%v", mid, moduleKeyID, err)
  59. return
  60. }
  61. if uc == nil {
  62. uc = &model.UserConf{
  63. Mid: mid,
  64. ModuleKey: moduleKeyID,
  65. }
  66. s.sp.Incr("default_user_conf")
  67. }
  68. s.sp.Incr("user_conf_missed")
  69. s.updateUcCache(mid, moduleKeyID)
  70. return
  71. }
  72. func (s *Service) document(c context.Context, checkSum int64) (rm json.RawMessage, err error) {
  73. var (
  74. doc *model.Document
  75. )
  76. rm, err = s.da.DocumentCache(c, checkSum)
  77. if err != nil {
  78. log.Error("service.document.DocumentCache(%v) err:%v", checkSum, err)
  79. }
  80. if rm != nil {
  81. s.sp.Incr("document_cached")
  82. return
  83. }
  84. doc, err = s.da.Document(c, checkSum)
  85. if err != nil {
  86. log.Error("service.document(%v) err:%v", checkSum, err)
  87. return
  88. }
  89. if doc == nil {
  90. err = ecode.NothingFound
  91. s.sp.Incr("user_conf_document_error")
  92. return
  93. }
  94. s.sp.Incr("document_missed")
  95. rm = json.RawMessage(doc.Doc)
  96. s.da.SetDocumentCache(c, checkSum, rm)
  97. return
  98. }
  99. // AddDocument add a user document
  100. func (s *Service) AddDocument(c context.Context, mid int64, moduleKey string, data string, timestamp int64, oldSum int64, now time.Time) (resp *model.UserConf, err error) {
  101. var (
  102. uc *model.UserConf
  103. doc *model.Document
  104. rm json.RawMessage
  105. checkSum int64
  106. tx *sql.Tx
  107. moduleKeyID int
  108. )
  109. if moduleKeyID = module.VerifyModuleKey(moduleKey); moduleKeyID == 0 {
  110. return nil, ecode.RequestErr
  111. }
  112. if rm, checkSum, err = module.Result(moduleKeyID, data); err != nil {
  113. log.Error("service.GetModule(%v,%s) err:%v", moduleKey, data, err)
  114. return nil, ecode.RequestErr
  115. }
  116. if len(rm) > s.docLimit {
  117. err = ecode.KvoDataOverLimit
  118. return
  119. }
  120. if uc, err = s.da.UserConf(c, mid, moduleKeyID); err != nil {
  121. log.Error("service.AddDocument.UserConf(%v,%v) err:%v", mid, moduleKeyID, err)
  122. return
  123. }
  124. s.updateUcCache(mid, moduleKeyID)
  125. if uc != nil {
  126. if uc.Timestamp != timestamp {
  127. err = ecode.KvoTimestampErr
  128. log.Error("service.AddDocument.CompareTimeStamp(%v,%v,%v) err:%v", mid, uc.Timestamp, timestamp, err)
  129. return
  130. }
  131. if uc.CheckSum != oldSum {
  132. err = ecode.KvoCheckSumErr
  133. return
  134. }
  135. if uc.CheckSum == checkSum {
  136. err = ecode.NotModified
  137. return
  138. }
  139. }
  140. // trans
  141. tx, err = s.da.BeginTx(c)
  142. if err != nil {
  143. log.Error("s.da.BeginTx err:%v", err)
  144. return
  145. }
  146. if err = s.da.TxUpUserConf(c, tx, mid, moduleKeyID, checkSum, now); err != nil {
  147. log.Error("s.da.TxUpUserConf(%v,%v,%v) error(%v)", mid, moduleKeyID, checkSum, err)
  148. tx.Rollback()
  149. return
  150. }
  151. doc, err = s.da.Document(c, checkSum)
  152. if err != nil {
  153. tx.Rollback()
  154. return
  155. }
  156. if doc == nil {
  157. if err = s.da.TxUpDocuement(c, tx, checkSum, string(rm), now); err != nil {
  158. log.Error("s.da.TxUpDocuement(%v,%v,%v) error(%v)", mid, moduleKeyID, checkSum, err)
  159. tx.Rollback()
  160. return
  161. }
  162. }
  163. if err = tx.Commit(); err != nil {
  164. log.Error("tx.Commit(), error(%v)", err)
  165. }
  166. resp = &model.UserConf{
  167. CheckSum: checkSum,
  168. Timestamp: now.Unix(),
  169. }
  170. s.updateUcCache(mid, moduleKeyID)
  171. return
  172. }