com.mongodb.operation.ParallelCollectionScanOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-core Show documentation
Show all versions of mongodb-driver-core Show documentation
The Java operations layer for the MongoDB Java Driver. Third parties can ' +
'wrap this layer to provide custom higher-level APIs
/*
* 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.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.QueryResult;
import com.mongodb.operation.CommandOperationHelper.CommandTransformer;
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.executeWrappedCommandProtocol;
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
import static com.mongodb.operation.OperationHelper.AsyncCallableWithConnectionAndSource;
import static com.mongodb.operation.OperationHelper.CallableWithConnectionAndSource;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.cursorDocumentToQueryResult;
import static com.mongodb.operation.OperationHelper.releasingCallback;
import static com.mongodb.operation.OperationHelper.validateReadConcern;
import static com.mongodb.operation.OperationHelper.withConnection;
import static com.mongodb.operation.OperationReadConcernHelper.appendReadConcernToCommand;
/**
* Return a list of cursors over the collection that can be used to scan it in parallel.
*
* Note: As of MongoDB 2.6, this operation will work against a mongod, but not a mongos.
*
* @param the operations result type.
* @mongodb.driver.manual reference/command/parallelCollectionScan/ parallelCollectionScan
* @mongodb.server.release 2.6
* @since 3.0
*/
@Deprecated
public class
ParallelCollectionScanOperation implements AsyncReadOperation>>,
ReadOperation>> {
private final MongoNamespace namespace;
private final int numCursors;
private int batchSize = 0;
private final Decoder decoder;
/**
* Construct a new instance.
*
* @param namespace the database and collection namespace for the operation.
* @param numCursors The maximum number of cursors to return. Must be between 1 and 10000, inclusive.
* @param decoder the decoder for the result documents.
*/
public ParallelCollectionScanOperation(final MongoNamespace namespace, final int numCursors, final Decoder decoder) {
this.namespace = notNull("namespace", namespace);
isTrue("numCursors >= 1", numCursors >= 1);
this.numCursors = numCursors;
this.decoder = notNull("decoder", decoder);
}
/**
* Gets the number of cursors requested.
*
* @return number of cursors requested.
*/
public int getNumCursors() {
return numCursors;
}
/**
* Gets the batch size to use for each cursor. The default value is 0, which tells the server to use its own default batch size.
*
* @return batch size
* @mongodb.driver.manual core/cursors/#cursor-batches BatchSize
*/
public int getBatchSize() {
return batchSize;
}
/**
* The batch size to use for each cursor.
*
* @param batchSize the batch size, which must be greater than or equal to 0
* @return this
* @mongodb.driver.manual core/cursors/#cursor-batches BatchSize
*/
public ParallelCollectionScanOperation batchSize(final int batchSize) {
isTrue("batchSize >= 0", batchSize >= 0);
this.batchSize = batchSize;
return this;
}
@Override
public List> execute(final ReadBinding binding) {
return withConnection(binding, new CallableWithConnectionAndSource>>() {
@Override
public List> call(final ConnectionSource source, final Connection connection) {
validateReadConcern(connection, binding.getSessionContext().getReadConcern());
return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(binding.getSessionContext()),
CommandResultDocumentCodec.create(decoder, "firstBatch"), connection,
transformer(source));
}
});
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback>> callback) {
withConnection(binding, new AsyncCallableWithConnectionAndSource() {
@Override
public void call(final AsyncConnectionSource source, final AsyncConnection connection, final Throwable t) {
SingleResultCallback>> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (t != null) {
errHandlingCallback.onResult(null, t);
} else {
final SingleResultCallback>> wrappedCallback = releasingCallback(
errHandlingCallback, source, connection);
validateReadConcern(source, connection, binding.getSessionContext().getReadConcern(),
new AsyncCallableWithConnectionAndSource() {
@Override
public void call(final AsyncConnectionSource source, final AsyncConnection connection, final Throwable t) {
if (t != null) {
wrappedCallback.onResult(null, t);
} else {
executeWrappedCommandProtocolAsync(binding, namespace.getDatabaseName(),
getCommand(binding.getSessionContext()),
CommandResultDocumentCodec.create(decoder, "firstBatch"), connection,
asyncTransformer(source, connection), wrappedCallback);
}
}
});
}
}
});
}
private CommandTransformer>> transformer(final ConnectionSource source) {
return new CommandTransformer>>() {
@Override
public List> apply(final BsonDocument result, final ServerAddress serverAddress) {
List> cursors = new ArrayList>();
for (BsonValue cursorValue : getCursorDocuments(result)) {
cursors.add(new QueryBatchCursor(createQueryResult(getCursorDocument(cursorValue.asDocument()),
source.getServerDescription().getAddress()),
0, getBatchSize(), decoder, source));
}
return cursors;
}
};
}
private CommandTransformer>> asyncTransformer(final AsyncConnectionSource source,
final AsyncConnection connection) {
return new CommandTransformer>>() {
@Override
public List> apply(final BsonDocument result, final ServerAddress serverAddress) {
List> cursors = new ArrayList>();
for (BsonValue cursorValue : getCursorDocuments(result)) {
cursors.add(new AsyncQueryBatchCursor(createQueryResult(getCursorDocument(cursorValue.asDocument()),
source.getServerDescription().getAddress()),
0, getBatchSize(), 0, decoder, source, connection));
}
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 createQueryResult(final BsonDocument cursorDocument, final ServerAddress serverAddress) {
return cursorDocumentToQueryResult(cursorDocument, serverAddress);
}
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;
}
}