query.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package mongo
  2. import (
  3. "time"
  4. "github.com/globalsign/mgo"
  5. "github.com/tal-tech/go-zero/core/breaker"
  6. )
  7. type (
  8. Query interface {
  9. All(result interface{}) error
  10. Apply(change mgo.Change, result interface{}) (*mgo.ChangeInfo, error)
  11. Batch(n int) Query
  12. Collation(collation *mgo.Collation) Query
  13. Comment(comment string) Query
  14. Count() (int, error)
  15. Distinct(key string, result interface{}) error
  16. Explain(result interface{}) error
  17. For(result interface{}, f func() error) error
  18. Hint(indexKey ...string) Query
  19. Iter() Iter
  20. Limit(n int) Query
  21. LogReplay() Query
  22. MapReduce(job *mgo.MapReduce, result interface{}) (*mgo.MapReduceInfo, error)
  23. One(result interface{}) error
  24. Prefetch(p float64) Query
  25. Select(selector interface{}) Query
  26. SetMaxScan(n int) Query
  27. SetMaxTime(d time.Duration) Query
  28. Skip(n int) Query
  29. Snapshot() Query
  30. Sort(fields ...string) Query
  31. Tail(timeout time.Duration) Iter
  32. }
  33. promisedQuery struct {
  34. *mgo.Query
  35. promise keepablePromise
  36. }
  37. rejectedQuery struct{}
  38. )
  39. func (q promisedQuery) All(result interface{}) error {
  40. return q.promise.keep(q.Query.All(result))
  41. }
  42. func (q promisedQuery) Apply(change mgo.Change, result interface{}) (*mgo.ChangeInfo, error) {
  43. info, err := q.Query.Apply(change, result)
  44. return info, q.promise.keep(err)
  45. }
  46. func (q promisedQuery) Batch(n int) Query {
  47. return promisedQuery{
  48. Query: q.Query.Batch(n),
  49. promise: q.promise,
  50. }
  51. }
  52. func (q promisedQuery) Collation(collation *mgo.Collation) Query {
  53. return promisedQuery{
  54. Query: q.Query.Collation(collation),
  55. promise: q.promise,
  56. }
  57. }
  58. func (q promisedQuery) Comment(comment string) Query {
  59. return promisedQuery{
  60. Query: q.Query.Comment(comment),
  61. promise: q.promise,
  62. }
  63. }
  64. func (q promisedQuery) Count() (int, error) {
  65. v, err := q.Query.Count()
  66. return v, q.promise.keep(err)
  67. }
  68. func (q promisedQuery) Distinct(key string, result interface{}) error {
  69. return q.promise.keep(q.Query.Distinct(key, result))
  70. }
  71. func (q promisedQuery) Explain(result interface{}) error {
  72. return q.promise.keep(q.Query.Explain(result))
  73. }
  74. func (q promisedQuery) For(result interface{}, f func() error) error {
  75. var ferr error
  76. err := q.Query.For(result, func() error {
  77. ferr = f()
  78. return ferr
  79. })
  80. if ferr == err {
  81. return q.promise.accept(err)
  82. }
  83. return q.promise.keep(err)
  84. }
  85. func (q promisedQuery) Hint(indexKey ...string) Query {
  86. return promisedQuery{
  87. Query: q.Query.Hint(indexKey...),
  88. promise: q.promise,
  89. }
  90. }
  91. func (q promisedQuery) Iter() Iter {
  92. return promisedIter{
  93. Iter: q.Query.Iter(),
  94. promise: q.promise,
  95. }
  96. }
  97. func (q promisedQuery) Limit(n int) Query {
  98. return promisedQuery{
  99. Query: q.Query.Limit(n),
  100. promise: q.promise,
  101. }
  102. }
  103. func (q promisedQuery) LogReplay() Query {
  104. return promisedQuery{
  105. Query: q.Query.LogReplay(),
  106. promise: q.promise,
  107. }
  108. }
  109. func (q promisedQuery) MapReduce(job *mgo.MapReduce, result interface{}) (*mgo.MapReduceInfo, error) {
  110. info, err := q.Query.MapReduce(job, result)
  111. return info, q.promise.keep(err)
  112. }
  113. func (q promisedQuery) One(result interface{}) error {
  114. return q.promise.keep(q.Query.One(result))
  115. }
  116. func (q promisedQuery) Prefetch(p float64) Query {
  117. return promisedQuery{
  118. Query: q.Query.Prefetch(p),
  119. promise: q.promise,
  120. }
  121. }
  122. func (q promisedQuery) Select(selector interface{}) Query {
  123. return promisedQuery{
  124. Query: q.Query.Select(selector),
  125. promise: q.promise,
  126. }
  127. }
  128. func (q promisedQuery) SetMaxScan(n int) Query {
  129. return promisedQuery{
  130. Query: q.Query.SetMaxScan(n),
  131. promise: q.promise,
  132. }
  133. }
  134. func (q promisedQuery) SetMaxTime(d time.Duration) Query {
  135. return promisedQuery{
  136. Query: q.Query.SetMaxTime(d),
  137. promise: q.promise,
  138. }
  139. }
  140. func (q promisedQuery) Skip(n int) Query {
  141. return promisedQuery{
  142. Query: q.Query.Skip(n),
  143. promise: q.promise,
  144. }
  145. }
  146. func (q promisedQuery) Snapshot() Query {
  147. return promisedQuery{
  148. Query: q.Query.Snapshot(),
  149. promise: q.promise,
  150. }
  151. }
  152. func (q promisedQuery) Sort(fields ...string) Query {
  153. return promisedQuery{
  154. Query: q.Query.Sort(fields...),
  155. promise: q.promise,
  156. }
  157. }
  158. func (q promisedQuery) Tail(timeout time.Duration) Iter {
  159. return promisedIter{
  160. Iter: q.Query.Tail(timeout),
  161. promise: q.promise,
  162. }
  163. }
  164. func (q rejectedQuery) All(result interface{}) error {
  165. return breaker.ErrServiceUnavailable
  166. }
  167. func (q rejectedQuery) Apply(change mgo.Change, result interface{}) (*mgo.ChangeInfo, error) {
  168. return nil, breaker.ErrServiceUnavailable
  169. }
  170. func (q rejectedQuery) Batch(n int) Query {
  171. return q
  172. }
  173. func (q rejectedQuery) Collation(collation *mgo.Collation) Query {
  174. return q
  175. }
  176. func (q rejectedQuery) Comment(comment string) Query {
  177. return q
  178. }
  179. func (q rejectedQuery) Count() (int, error) {
  180. return 0, breaker.ErrServiceUnavailable
  181. }
  182. func (q rejectedQuery) Distinct(key string, result interface{}) error {
  183. return breaker.ErrServiceUnavailable
  184. }
  185. func (q rejectedQuery) Explain(result interface{}) error {
  186. return breaker.ErrServiceUnavailable
  187. }
  188. func (q rejectedQuery) For(result interface{}, f func() error) error {
  189. return breaker.ErrServiceUnavailable
  190. }
  191. func (q rejectedQuery) Hint(indexKey ...string) Query {
  192. return q
  193. }
  194. func (q rejectedQuery) Iter() Iter {
  195. return rejectedIter{}
  196. }
  197. func (q rejectedQuery) Limit(n int) Query {
  198. return q
  199. }
  200. func (q rejectedQuery) LogReplay() Query {
  201. return q
  202. }
  203. func (q rejectedQuery) MapReduce(job *mgo.MapReduce, result interface{}) (*mgo.MapReduceInfo, error) {
  204. return nil, breaker.ErrServiceUnavailable
  205. }
  206. func (q rejectedQuery) One(result interface{}) error {
  207. return breaker.ErrServiceUnavailable
  208. }
  209. func (q rejectedQuery) Prefetch(p float64) Query {
  210. return q
  211. }
  212. func (q rejectedQuery) Select(selector interface{}) Query {
  213. return q
  214. }
  215. func (q rejectedQuery) SetMaxScan(n int) Query {
  216. return q
  217. }
  218. func (q rejectedQuery) SetMaxTime(d time.Duration) Query {
  219. return q
  220. }
  221. func (q rejectedQuery) Skip(n int) Query {
  222. return q
  223. }
  224. func (q rejectedQuery) Snapshot() Query {
  225. return q
  226. }
  227. func (q rejectedQuery) Sort(fields ...string) Query {
  228. return q
  229. }
  230. func (q rejectedQuery) Tail(timeout time.Duration) Iter {
  231. return rejectedIter{}
  232. }