/
ChangeStreamBatchCursor.java
202 lines (177 loc) · 6.91 KB
/
ChangeStreamBatchCursor.java
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
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.operation;
import com.mongodb.Function;
import com.mongodb.MongoChangeStreamException;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.ServerCursor;
import com.mongodb.internal.binding.ConnectionSource;
import com.mongodb.internal.binding.ReadBinding;
import com.mongodb.internal.operation.OperationHelper.CallableWithSource;
import org.bson.BsonDocument;
import org.bson.BsonTimestamp;
import org.bson.RawBsonDocument;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.internal.operation.ChangeStreamBatchCursorHelper.isRetryableError;
import static com.mongodb.internal.operation.OperationHelper.withReadConnectionSource;
final class ChangeStreamBatchCursor<T> implements AggregateResponseBatchCursor<T> {
private final ReadBinding binding;
private final ChangeStreamOperation<T> changeStreamOperation;
private final int maxWireVersion;
private AggregateResponseBatchCursor<RawBsonDocument> wrapped;
private BsonDocument resumeToken;
private volatile boolean closed;
ChangeStreamBatchCursor(final ChangeStreamOperation<T> changeStreamOperation,
final AggregateResponseBatchCursor<RawBsonDocument> wrapped,
final ReadBinding binding,
final BsonDocument resumeToken,
final int maxWireVersion) {
this.changeStreamOperation = changeStreamOperation;
this.binding = binding.retain();
this.wrapped = wrapped;
this.resumeToken = resumeToken;
this.maxWireVersion = maxWireVersion;
}
AggregateResponseBatchCursor<RawBsonDocument> getWrapped() {
return wrapped;
}
@Override
public boolean hasNext() {
return resumeableOperation(new Function<AggregateResponseBatchCursor<RawBsonDocument>, Boolean>() {
@Override
public Boolean apply(final AggregateResponseBatchCursor<RawBsonDocument> queryBatchCursor) {
try {
return queryBatchCursor.hasNext();
} catch (RuntimeException e) {
cachePostBatchResumeToken(queryBatchCursor);
throw e;
}
}
});
}
@Override
public List<T> next() {
return resumeableOperation(new Function<AggregateResponseBatchCursor<RawBsonDocument>, List<T>>() {
@Override
public List<T> apply(final AggregateResponseBatchCursor<RawBsonDocument> queryBatchCursor) {
try {
return convertResults(queryBatchCursor.next());
} finally {
cachePostBatchResumeToken(queryBatchCursor);
}
}
});
}
@Override
public List<T> tryNext() {
return resumeableOperation(new Function<AggregateResponseBatchCursor<RawBsonDocument>, List<T>>() {
@Override
public List<T> apply(final AggregateResponseBatchCursor<RawBsonDocument> queryBatchCursor) {
try {
return convertResults(queryBatchCursor.tryNext());
} finally {
cachePostBatchResumeToken(queryBatchCursor);
}
}
});
}
@Override
public void close() {
if (!closed) {
closed = true;
wrapped.close();
binding.release();
}
}
@Override
public void setBatchSize(final int batchSize) {
wrapped.setBatchSize(batchSize);
}
@Override
public int getBatchSize() {
return wrapped.getBatchSize();
}
@Override
public ServerCursor getServerCursor() {
return wrapped.getServerCursor();
}
@Override
public ServerAddress getServerAddress() {
return wrapped.getServerAddress();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not implemented!");
}
@Override
public BsonDocument getPostBatchResumeToken() {
return wrapped.getPostBatchResumeToken();
}
@Override
public BsonTimestamp getOperationTime() {
return changeStreamOperation.getStartAtOperationTime();
}
@Override
public boolean isFirstBatchEmpty() {
return wrapped.isFirstBatchEmpty();
}
@Override
public int getMaxWireVersion() {
return maxWireVersion;
}
private void cachePostBatchResumeToken(final AggregateResponseBatchCursor<RawBsonDocument> queryBatchCursor) {
if (queryBatchCursor.getPostBatchResumeToken() != null) {
resumeToken = queryBatchCursor.getPostBatchResumeToken();
}
}
private List<T> convertResults(final List<RawBsonDocument> rawDocuments) {
List<T> results = null;
if (rawDocuments != null) {
results = new ArrayList<T>();
for (RawBsonDocument rawDocument : rawDocuments) {
if (!rawDocument.containsKey("_id")) {
throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
}
results.add(rawDocument.decode(changeStreamOperation.getDecoder()));
}
resumeToken = rawDocuments.get(rawDocuments.size() - 1).getDocument("_id");
}
return results;
}
<R> R resumeableOperation(final Function<AggregateResponseBatchCursor<RawBsonDocument>, R> function) {
while (true) {
try {
return function.apply(wrapped);
} catch (Throwable t) {
if (!isRetryableError(t, maxWireVersion)) {
throw MongoException.fromThrowableNonNull(t);
}
}
wrapped.close();
withReadConnectionSource(binding, new CallableWithSource<Void>() {
@Override
public Void call(final ConnectionSource source) {
changeStreamOperation.setChangeStreamOptionsForResume(resumeToken, source.getServerDescription().getMaxWireVersion());
return null;
}
});
wrapped = ((ChangeStreamBatchCursor<T>) changeStreamOperation.execute(binding)).getWrapped();
binding.release(); // release the new change stream batch cursor's reference to the binding
}
}
}