-
Notifications
You must be signed in to change notification settings - Fork 883
/
change_stream.go
195 lines (155 loc) · 4.03 KB
/
change_stream.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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mongo
import (
"bytes"
"context"
"errors"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/command"
"github.com/mongodb/mongo-go-driver/core/option"
)
// ErrMissingResumeToken indicates that a change stream notification from the server did not
// contain a resume token.
var ErrMissingResumeToken = errors.New("cannot provide resume functionality when the resume token is missing")
type changeStream struct {
pipeline *bson.Array
options []option.ChangeStreamOptioner
coll *Collection
cursor Cursor
resumeToken *bson.Document
err error
}
const errorCodeNotMaster int32 = 10107
const errorCodeCursorNotFound int32 = 43
func newChangeStream(ctx context.Context, coll *Collection, pipeline interface{},
opts ...option.ChangeStreamOptioner) (*changeStream, error) {
pipelineArr, err := transformAggregatePipeline(pipeline)
if err != nil {
return nil, err
}
changeStreamOptions := bson.NewDocument()
for _, opt := range opts {
err = opt.Option(changeStreamOptions)
if err != nil {
return nil, err
}
}
pipelineArr.Prepend(
bson.VC.Document(
bson.NewDocument(
bson.EC.SubDocument("$changeStream", changeStreamOptions))))
cursor, err := coll.Aggregate(ctx, pipelineArr)
if err != nil {
return nil, err
}
cs := &changeStream{
pipeline: pipelineArr,
options: opts,
coll: coll,
cursor: cursor,
}
return cs, nil
}
func (cs *changeStream) ID() int64 {
return cs.cursor.ID()
}
func (cs *changeStream) Next(ctx context.Context) bool {
if cs.cursor.Next(ctx) {
return true
}
err := cs.cursor.Err()
if err == nil {
return false
}
switch t := err.(type) {
case command.Error:
if t.Code != errorCodeNotMaster && t.Code != errorCodeCursorNotFound {
return false
}
}
resumeToken := Opt.ResumeAfter(cs.resumeToken)
found := false
for i, opt := range cs.options {
if _, ok := opt.(option.OptResumeAfter); ok {
cs.options[i] = resumeToken
found = true
break
}
}
if !found {
cs.options = append(cs.options, resumeToken)
}
oldns := cs.coll.namespace()
killCursors := command.KillCursors{
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
IDs: []int64{cs.ID()},
}
ss, err := cs.coll.client.topology.SelectServer(ctx, cs.coll.readSelector)
if err != nil {
cs.err = err
return false
}
conn, err := ss.Connection(ctx)
if err != nil {
cs.err = err
return false
}
defer conn.Close()
_, _ = killCursors.RoundTrip(ctx, ss.Description(), conn)
changeStreamOptions := bson.NewDocument()
for _, opt := range cs.options {
err = opt.Option(changeStreamOptions)
if err != nil {
cs.err = err
return false
}
}
cs.pipeline.Set(0, bson.VC.Document(
bson.NewDocument(
bson.EC.SubDocument("$changeStream", changeStreamOptions)),
),
)
oldns = cs.coll.namespace()
aggCmd := command.Aggregate{
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
Pipeline: cs.pipeline,
}
cs.cursor, cs.err = aggCmd.RoundTrip(ctx, ss.Description(), ss, conn)
if cs.err != nil {
return false
}
return cs.cursor.Next(ctx)
}
func (cs *changeStream) Decode(out interface{}) error {
br, err := cs.DecodeBytes()
if err != nil {
return err
}
return bson.NewDecoder(bytes.NewReader(br)).Decode(out)
}
func (cs *changeStream) DecodeBytes() (bson.Reader, error) {
br, err := cs.cursor.DecodeBytes()
if err != nil {
return nil, err
}
id, err := br.Lookup("_id")
if err != nil {
_ = cs.Close(context.Background())
return nil, ErrMissingResumeToken
}
cs.resumeToken = id.Value().MutableDocument()
return br, nil
}
func (cs *changeStream) Err() error {
if cs.err != nil {
return cs.err
}
return cs.cursor.Err()
}
func (cs *changeStream) Close(ctx context.Context) error {
return cs.cursor.Close(ctx)
}