package com.mongodb.operation;
import com.mongodb.MongoNamespace;
import com.mongodb.ServerAddress;
import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncConnectionSource;
import com.mongodb.binding.AsyncReadBinding;
import com.mongodb.binding.ConnectionSource;
import com.mongodb.binding.ReadBinding;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.QueryResult;
import com.mongodb.connection.ServerDescription;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformer;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformerAsync;
import com.mongodb.session.SessionContext;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.Decoder;
import java.util.ArrayList;
import java.util.List;
import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.operation.CommandOperationHelper.executeCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.cursorDocumentToQueryResult;
import static com.mongodb.operation.OperationHelper.validateReadConcern;
import static com.mongodb.operation.OperationReadConcernHelper.appendReadConcernToCommand;
@Deprecated
public class
ParallelCollectionScanOperation<T> implements AsyncReadOperation<List<AsyncBatchCursor<T>>>,
ReadOperation<List<BatchCursor<T>>> {
private final MongoNamespace namespace;
private final int numCursors;
private boolean retryReads;
private int batchSize = 0;
private final Decoder<T> decoder;
public ParallelCollectionScanOperation(final MongoNamespace namespace, final int numCursors, final Decoder<T> decoder) {
this.namespace = notNull("namespace", namespace);
isTrue("numCursors >= 1", numCursors >= 1);
this.numCursors = numCursors;
this.decoder = notNull("decoder", decoder);
}
public int getNumCursors() {
return numCursors;
}
public int getBatchSize() {
return batchSize;
}
public ParallelCollectionScanOperation<T> batchSize(final int batchSize) {
isTrue("batchSize >= 0", batchSize >= 0);
this.batchSize = batchSize;
return this;
}
public ParallelCollectionScanOperation<T> retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
public boolean getRetryReads() {
return retryReads;
}
@Override
public List<BatchCursor<T>> execute(final ReadBinding binding) {
return executeCommand(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()),
CommandResultDocumentCodec.create(decoder, "firstBatch"),
transformer(), retryReads);
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<List<AsyncBatchCursor<T>>> callback) {
executeCommandAsync(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()),
CommandResultDocumentCodec.create(decoder, "firstBatch"),
asyncTransformer(), retryReads, errorHandlingCallback(callback, LOGGER));
}
private CommandReadTransformer<BsonDocument, List<BatchCursor<T>>> transformer() {
return new CommandReadTransformer<BsonDocument, List<BatchCursor<T>>>() {
@Override
public List<BatchCursor<T>> apply(final BsonDocument result, final ConnectionSource source, final Connection connection) {
List<BatchCursor<T>> cursors = new ArrayList<BatchCursor<T>>();
for (BsonValue cursorValue : getCursorDocuments(result)) {
cursors.add(new QueryBatchCursor<T>(createQueryResult(getCursorDocument(cursorValue.asDocument()),
source.getServerDescription().getAddress()),
0, getBatchSize(), decoder, source));
}
return cursors;
}
};
}
private CommandReadTransformerAsync<BsonDocument, List<AsyncBatchCursor<T>>> asyncTransformer() {
return new CommandReadTransformerAsync<BsonDocument, List<AsyncBatchCursor<T>>>() {
@Override
public List<AsyncBatchCursor<T>> apply(final BsonDocument result, final AsyncConnectionSource source,
final AsyncConnection connection) {
List<AsyncBatchCursor<T>> cursors = new ArrayList<AsyncBatchCursor<T>>();
for (BsonValue cursorValue : getCursorDocuments(result)) {
cursors.add(new AsyncQueryBatchCursor<T>(createQueryResult(getCursorDocument(cursorValue.asDocument()),
source.getServerDescription().getAddress()),
0, getBatchSize(), 0, decoder, source, connection, result));
}
return cursors;
}
};
}
@SuppressWarnings("unchecked")
private BsonArray getCursorDocuments(final BsonDocument result) {
return result.getArray("cursors");
}
private BsonDocument getCursorDocument(final BsonDocument cursorDocument) {
return cursorDocument.getDocument("cursor");
}
@SuppressWarnings("unchecked")
private QueryResult<T> createQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress) {
return cursorDocumentToQueryResult(cursorDocument, serverAddress);
}
private CommandCreator getCommandCreator(final SessionContext sessionContext) {
return new CommandCreator() {
@Override
public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) {
validateReadConcern(connectionDescription, sessionContext.getReadConcern());
return getCommand(sessionContext);
}
};
}
private BsonDocument getCommand(final SessionContext sessionContext) {
BsonDocument document = new BsonDocument("parallelCollectionScan", new BsonString(namespace.getCollectionName()))
.append("numCursors", new BsonInt32(getNumCursors()));
appendReadConcernToCommand(sessionContext, document);
return document;
}
}