com.mongodb.operation.AsyncChangeStreamBatchCursor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
/*
* 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.operation;
import com.mongodb.MongoChangeStreamException;
import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncReadBinding;
import org.bson.BsonDocument;
import org.bson.RawBsonDocument;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.ChangeStreamBatchCursorHelper.isRetryableError;
import static com.mongodb.operation.OperationHelper.LOGGER;
final class AsyncChangeStreamBatchCursor implements AsyncBatchCursor {
private final AsyncReadBinding binding;
private final ChangeStreamOperation changeStreamOperation;
private volatile BsonDocument resumeToken;
private volatile AsyncBatchCursor wrapped;
AsyncChangeStreamBatchCursor(final ChangeStreamOperation changeStreamOperation,
final AsyncBatchCursor wrapped,
final AsyncReadBinding binding) {
changeStreamOperation.startOperationTimeForResume(binding.getSessionContext().getOperationTime());
this.changeStreamOperation = changeStreamOperation;
this.resumeToken = changeStreamOperation.getResumeToken();
this.wrapped = wrapped;
this.binding = binding;
binding.retain();
}
AsyncBatchCursor getWrapped() {
return wrapped;
}
@Override
public void next(final SingleResultCallback> callback) {
resumeableOperation(new AsyncBlock() {
@Override
public void apply(final AsyncBatchCursor cursor, final SingleResultCallback> callback) {
cursor.next(callback);
}
}, convertResultsCallback(callback));
}
@Override
public void tryNext(final SingleResultCallback> callback) {
resumeableOperation(new AsyncBlock() {
@Override
public void apply(final AsyncBatchCursor cursor, final SingleResultCallback> callback) {
cursor.tryNext(callback);
}
}, convertResultsCallback(callback));
}
@Override
public void setBatchSize(final int batchSize) {
wrapped.setBatchSize(batchSize);
}
@Override
public int getBatchSize() {
return wrapped.getBatchSize();
}
@Override
public boolean isClosed() {
return wrapped.isClosed();
}
@Override
public void close() {
wrapped.close();
binding.release();
}
private interface AsyncBlock {
void apply(AsyncBatchCursor cursor, SingleResultCallback> callback);
}
private void resumeableOperation(final AsyncBlock asyncBlock, final SingleResultCallback> callback) {
asyncBlock.apply(wrapped, new SingleResultCallback>() {
@Override
public void onResult(final List result, final Throwable t) {
if (t == null) {
callback.onResult(result, null);
} else if (isRetryableError(t)) {
wrapped.close();
retryOperation(asyncBlock, callback);
} else {
callback.onResult(null, t);
}
}
});
}
private void retryOperation(final AsyncBlock asyncBlock, final SingleResultCallback> callback) {
if (resumeToken != null) {
changeStreamOperation.startOperationTimeForResume(null);
changeStreamOperation.resumeAfter(resumeToken);
}
changeStreamOperation.executeAsync(binding, new SingleResultCallback>() {
@Override
public void onResult(final AsyncBatchCursor result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
wrapped = ((AsyncChangeStreamBatchCursor) result).getWrapped();
binding.release(); // release the new change stream batch cursor's reference to the binding
resumeableOperation(asyncBlock, callback);
}
}
});
}
private SingleResultCallback> convertResultsCallback(final SingleResultCallback> callback) {
return errorHandlingCallback(new SingleResultCallback>() {
@Override
public void onResult(final List rawDocuments, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else if (rawDocuments != null) {
List results = new ArrayList();
for (RawBsonDocument rawDocument : rawDocuments) {
if (!rawDocument.containsKey("_id")) {
callback.onResult(null,
new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.")
);
return;
}
resumeToken = rawDocument.getDocument("_id");
results.add(rawDocument.decode(changeStreamOperation.getDecoder()));
}
callback.onResult(results, null);
} else {
callback.onResult(null, null);
}
}
}, LOGGER);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy