You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

changestreamoptions.go 4.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package options
  7. import (
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "time"
  10. )
  11. // ChangeStreamOptions represents all possible options to a change stream
  12. type ChangeStreamOptions struct {
  13. BatchSize *int32 // The number of documents to return per batch
  14. Collation *Collation // Specifies a collation
  15. FullDocument *FullDocument // When set to ‘updateLookup’, the change notification for partial updates will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
  16. MaxAwaitTime *time.Duration // The maximum amount of time for the server to wait on new documents to satisfy a change stream query
  17. ResumeAfter interface{} // Specifies the logical starting point for the new change stream
  18. StartAtOperationTime *primitive.Timestamp // Ensures that a change stream will only provide changes that occurred after a timestamp.
  19. StartAfter interface{} // Specifies a resume token. The started change stream will return the first notification after the token.
  20. }
  21. // ChangeStream returns a pointer to a new ChangeStreamOptions
  22. func ChangeStream() *ChangeStreamOptions {
  23. cso := &ChangeStreamOptions{}
  24. cso.SetFullDocument(Default)
  25. return cso
  26. }
  27. // SetBatchSize specifies the number of documents to return per batch
  28. func (cso *ChangeStreamOptions) SetBatchSize(i int32) *ChangeStreamOptions {
  29. cso.BatchSize = &i
  30. return cso
  31. }
  32. // SetCollation specifies a collation
  33. func (cso *ChangeStreamOptions) SetCollation(c Collation) *ChangeStreamOptions {
  34. cso.Collation = &c
  35. return cso
  36. }
  37. // SetFullDocument specifies the fullDocument option.
  38. // When set to ‘updateLookup’, the change notification for partial updates will
  39. // include both a delta describing the changes to the document, as well as a
  40. // copy of the entire document that was changed from some time after the change
  41. // occurred.
  42. func (cso *ChangeStreamOptions) SetFullDocument(fd FullDocument) *ChangeStreamOptions {
  43. cso.FullDocument = &fd
  44. return cso
  45. }
  46. // SetMaxAwaitTime specifies the maximum amount of time for the server to wait on new documents to satisfy a change stream query
  47. func (cso *ChangeStreamOptions) SetMaxAwaitTime(d time.Duration) *ChangeStreamOptions {
  48. cso.MaxAwaitTime = &d
  49. return cso
  50. }
  51. // SetResumeAfter specifies the logical starting point for the new change stream
  52. func (cso *ChangeStreamOptions) SetResumeAfter(rt interface{}) *ChangeStreamOptions {
  53. cso.ResumeAfter = rt
  54. return cso
  55. }
  56. // SetStartAtOperationTime ensures that a change stream will only provide changes that occurred after a specified timestamp.
  57. func (cso *ChangeStreamOptions) SetStartAtOperationTime(t *primitive.Timestamp) *ChangeStreamOptions {
  58. cso.StartAtOperationTime = t
  59. return cso
  60. }
  61. // SetStartAfter specifies a resume token. The resulting change stream will return the first notification after the token.
  62. // Cannot be used in conjunction with ResumeAfter.
  63. func (cso *ChangeStreamOptions) SetStartAfter(sa interface{}) *ChangeStreamOptions {
  64. cso.StartAfter = sa
  65. return cso
  66. }
  67. // MergeChangeStreamOptions combines the argued ChangeStreamOptions into a single ChangeStreamOptions in a last-one-wins fashion
  68. func MergeChangeStreamOptions(opts ...*ChangeStreamOptions) *ChangeStreamOptions {
  69. csOpts := ChangeStream()
  70. for _, cso := range opts {
  71. if cso == nil {
  72. continue
  73. }
  74. if cso.BatchSize != nil {
  75. csOpts.BatchSize = cso.BatchSize
  76. }
  77. if cso.Collation != nil {
  78. csOpts.Collation = cso.Collation
  79. }
  80. if cso.FullDocument != nil {
  81. csOpts.FullDocument = cso.FullDocument
  82. }
  83. if cso.MaxAwaitTime != nil {
  84. csOpts.MaxAwaitTime = cso.MaxAwaitTime
  85. }
  86. if cso.ResumeAfter != nil {
  87. csOpts.ResumeAfter = cso.ResumeAfter
  88. }
  89. if cso.StartAtOperationTime != nil {
  90. csOpts.StartAtOperationTime = cso.StartAtOperationTime
  91. }
  92. if cso.StartAfter != nil {
  93. csOpts.StartAfter = cso.StartAfter
  94. }
  95. }
  96. return csOpts
  97. }