-
Notifications
You must be signed in to change notification settings - Fork 0
/
changestreamopt.go
executable file
·283 lines (217 loc) · 7.24 KB
/
changestreamopt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package changestreamopt
import (
"reflect"
"time"
"github.com/Vincent20101/mongo-go-driver/bson"
"github.com/Vincent20101/mongo-go-driver/core/option"
"github.com/Vincent20101/mongo-go-driver/mongo/mongoopt"
)
var csBundle = new(ChangeStreamBundle)
// ChangeStream is options for the changeStream()() function.
type ChangeStream interface {
changeStream()
ConvertChangeStreamOption() option.ChangeStreamOptioner
}
// ChangeStreamBundle is a bundle of ChangeStream options
type ChangeStreamBundle struct {
option ChangeStream
next *ChangeStreamBundle
}
func (csb *ChangeStreamBundle) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface.
func (csb *ChangeStreamBundle) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return nil
}
// BundleChangeStream bundles ChangeStream options.
func BundleChangeStream(opts ...ChangeStream) *ChangeStreamBundle {
head := csBundle
for _, opt := range opts {
newBundle := ChangeStreamBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// BatchSize specifies the number of documents to return in a batch.
func (csb *ChangeStreamBundle) BatchSize(i int32) *ChangeStreamBundle {
bundle := &ChangeStreamBundle{
option: BatchSize(i),
next: csb,
}
return bundle
}
// Collation adds an option to specify a collation.
func (csb *ChangeStreamBundle) Collation(c *mongoopt.Collation) *ChangeStreamBundle {
bundle := &ChangeStreamBundle{
option: Collation(c),
next: csb,
}
return bundle
}
// FullDocument specifies if a copy of the whole document should be returned.
func (csb *ChangeStreamBundle) FullDocument(fd mongoopt.FullDocument) *ChangeStreamBundle {
bundle := &ChangeStreamBundle{
option: FullDocument(fd),
next: csb,
}
return bundle
}
// MaxAwaitTime specifies the maximum amount of time for the server to wait on new documents.
func (csb *ChangeStreamBundle) MaxAwaitTime(d time.Duration) *ChangeStreamBundle {
bundle := &ChangeStreamBundle{
option: MaxAwaitTime(d),
next: csb,
}
return bundle
}
// ResumeAfter specifies whether the change stream should be resumed after stopping.
func (csb *ChangeStreamBundle) ResumeAfter(d *bson.Document) *ChangeStreamBundle {
bundle := &ChangeStreamBundle{
option: ResumeAfter(d),
next: csb,
}
return bundle
}
// Unbundle transforms a bundle into a slice of options, optionally deduplicating
func (csb *ChangeStreamBundle) Unbundle(deduplicate bool) ([]option.ChangeStreamOptioner, error) {
options, err := csb.unbundle()
if err != nil {
return nil, err
}
if !deduplicate {
return options, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (csb *ChangeStreamBundle) bundleLength() int {
if csb == nil {
return 0
}
bundleLen := 0
for ; csb != nil && csb.option != nil; csb = csb.next {
if converted, ok := csb.option.(*ChangeStreamBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
bundleLen++
}
return bundleLen
}
// Helper that recursively unwraps bundle into slice of options
func (csb *ChangeStreamBundle) unbundle() ([]option.ChangeStreamOptioner, error) {
if csb == nil {
return nil, nil
}
listLen := csb.bundleLength()
options := make([]option.ChangeStreamOptioner, listLen)
index := listLen - 1
for listHead := csb; listHead != nil && listHead.option != nil; listHead = listHead.next {
// if the current option is a nested bundle, Unbundle it and add its options to the current array
if converted, ok := listHead.option.(*ChangeStreamBundle); ok {
nestedOptions, err := converted.unbundle()
if err != nil {
return nil, err
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
options[index] = listHead.option.ConvertChangeStreamOption()
index--
}
return options, nil
}
// String implements the Stringer interface
func (csb *ChangeStreamBundle) String() string {
if csb == nil {
return ""
}
str := ""
for head := csb; head != nil && head.option != nil; head = head.next {
if converted, ok := head.option.(*ChangeStreamBundle); ok {
str += converted.String()
continue
}
str += head.option.ConvertChangeStreamOption().String() + "\n"
}
return str
}
// BatchSize specifies the number of documents to return in each batch.
func BatchSize(i int32) OptBatchSize {
return OptBatchSize(i)
}
// Collation specifies a collation.
func Collation(c *mongoopt.Collation) OptCollation {
return OptCollation{Collation: c.Convert()}
}
// FullDocument specifies whether a copy of the whole document should be returned.
func FullDocument(fd mongoopt.FullDocument) OptFullDocument {
return OptFullDocument(fd)
}
// MaxAwaitTime specifies the max amount of time for the server to wait on new documents.
func MaxAwaitTime(d time.Duration) OptMaxAwaitTime {
return OptMaxAwaitTime(d)
}
// ResumeAfter specifies if a change stream should be resumed after stopping.
func ResumeAfter(d *bson.Document) OptResumeAfter {
return OptResumeAfter{
ResumeAfter: d,
}
}
// OptBatchSize specifies the number of documents to return in each batch.
type OptBatchSize option.OptBatchSize
func (OptBatchSize) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface
func (opt OptBatchSize) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return option.OptBatchSize(opt)
}
// OptCollation specifies a collation.
type OptCollation option.OptCollation
func (OptCollation) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface.
func (opt OptCollation) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return option.OptCollation(opt)
}
// OptFullDocument specifies whether a copy of the whole document should be returned.
type OptFullDocument option.OptFullDocument
func (OptFullDocument) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface.
func (opt OptFullDocument) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return option.OptFullDocument(opt)
}
// OptMaxAwaitTime specifies the max amount of time for the server to wait on new documents.
type OptMaxAwaitTime option.OptMaxAwaitTime
func (OptMaxAwaitTime) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface.
func (opt OptMaxAwaitTime) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return option.OptMaxAwaitTime(opt)
}
// OptResumeAfter specifies if the stream should be resumed after stopping.
type OptResumeAfter option.OptResumeAfter
func (OptResumeAfter) changeStream() {}
// ConvertChangeStreamOption implements the ChangeStream interface.
func (opt OptResumeAfter) ConvertChangeStreamOption() option.ChangeStreamOptioner {
return option.OptResumeAfter(opt)
}