com.mongodb.operation.AggregateExplainOperation 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
The newest version!
/*
* 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.async.SingleResultCallback;
import com.mongodb.binding.AsyncReadBinding;
import com.mongodb.binding.ReadBinding;
import com.mongodb.client.model.Collation;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ServerDescription;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.BsonString;
import org.bson.BsonValue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.isTrueArgument;
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.IdentityTransformerAsync;
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.validateCollation;
// an operation that executes an explain on an aggregation pipeline
class AggregateExplainOperation implements AsyncReadOperation, ReadOperation {
private final MongoNamespace namespace;
private final List pipeline;
private boolean retryReads;
private Boolean allowDiskUse;
private long maxTimeMS;
private Collation collation;
private BsonValue hint;
AggregateExplainOperation(final MongoNamespace namespace, final List pipeline) {
this.namespace = notNull("namespace", namespace);
this.pipeline = notNull("pipeline", pipeline);
}
/**
* Enables writing to temporary files. A null value indicates that it's unspecified.
*
* @param allowDiskUse true if writing to temporary files is enabled
* @return this
* @mongodb.driver.manual reference/command/aggregate/ Aggregation
* @mongodb.server.release 2.6
*/
public AggregateExplainOperation allowDiskUse(final Boolean allowDiskUse) {
this.allowDiskUse = allowDiskUse;
return this;
}
/**
* Sets the maximum execution time on the server for this operation.
*
* @param maxTime the max time
* @param timeUnit the time unit, which may not be null
* @return this
* @mongodb.driver.manual reference/method/cursor.maxTimeMS/#cursor.maxTimeMS Max Time
*/
public AggregateExplainOperation maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
/**
* Enables retryable reads if a read fails due to a network error. A null value indicates that it's unspecified.
*
* @param retryReads true if retryable reads is enabled
* @return this
* @mongodb.driver.manual reference/command/aggregate/ Aggregation
* @mongodb.server.release 3.6
* @since 3.11
*/
public AggregateExplainOperation retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
/**
* Gets the value for retryable reads. The default is true.
*
* @return the retryable reads value
* @since 3.11
*/
public boolean getRetryReads() {
return retryReads;
}
/**
* Sets the collation options
*
* A null value represents the server default.
* @param collation the collation options to use
* @return this
* @since 3.4
* @mongodb.driver.manual reference/command/aggregate/ Aggregation
* @mongodb.server.release 3.4
*/
public AggregateExplainOperation collation(final Collation collation) {
this.collation = collation;
return this;
}
/**
* Returns the hint for which index to use. The default is not to set a hint.
*
* @return the hint
* @since 3.6
* @mongodb.server.release 3.6
*/
public BsonDocument getHint() {
if (hint == null) {
return null;
}
if (!hint.isDocument()) {
throw new IllegalArgumentException("Hint is not a BsonDocument please use the #getHintBsonValue() method. ");
}
return hint.asDocument();
}
/**
* Returns the hint BsonValue for which index to use. The default is not to set a hint.
*
* Hints can either be a BsonString or a BsonDocument.
*
* @return the hint
* @since 3.8
* @mongodb.server.release 3.6
*/
public BsonValue getHintBsonValue() {
return hint;
}
/**
* Sets the hint for which index to use. A null value means no hint is set.
*
* @param hint the hint
* @return this
* @since 3.6
* @mongodb.server.release 3.6
*/
public AggregateExplainOperation hint(final BsonValue hint) {
isTrueArgument("BsonString or BsonDocument", hint == null || hint.isDocument() || hint.isString());
this.hint = hint;
return this;
}
@Override
public BsonDocument execute(final ReadBinding binding) {
return executeCommand(binding, namespace.getDatabaseName(), getCommandCreator(), retryReads);
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback callback) {
SingleResultCallback errHandlingCallback = errorHandlingCallback(callback, LOGGER);
executeCommandAsync(binding, namespace.getDatabaseName(), getCommandCreator(),
new IdentityTransformerAsync(), retryReads, errHandlingCallback);
}
private CommandCreator getCommandCreator() {
return new CommandCreator() {
@Override
public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) {
validateCollation(connectionDescription, collation);
return getCommand();
}
};
}
private BsonDocument getCommand() {
BsonDocument commandDocument = new BsonDocument("aggregate", new BsonString(namespace.getCollectionName()));
commandDocument.put("pipeline", new BsonArray(pipeline));
commandDocument.put("explain", BsonBoolean.TRUE);
if (maxTimeMS > 0) {
commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS));
}
if (allowDiskUse != null) {
commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse));
}
if (collation != null) {
commandDocument.put("collation", collation.asDocument());
}
if (hint != null) {
commandDocument.put("hint", hint);
}
return commandDocument;
}
}