package com.mongodb.operation;
import com.mongodb.MongoNamespace;
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.client.model.Collation;
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.BsonDocument;
import org.bson.BsonString;
import org.bson.codecs.Codec;
import org.bson.codecs.Decoder;
import java.util.concurrent.TimeUnit;
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.DocumentHelper.putIfNotNullOrEmpty;
import static com.mongodb.operation.DocumentHelper.putIfNotZero;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.validateReadConcernAndCollation;
import static com.mongodb.operation.OperationReadConcernHelper.appendReadConcernToCommand;
@Deprecated
public class DistinctOperation<T> implements AsyncReadOperation<AsyncBatchCursor<T>>, ReadOperation<BatchCursor<T>> {
private static final String VALUES = "values";
private final MongoNamespace namespace;
private final String fieldName;
private final Decoder<T> decoder;
private boolean retryReads;
private BsonDocument filter;
private long maxTimeMS;
private Collation collation;
public DistinctOperation(final MongoNamespace namespace, final String fieldName, final Decoder<T> decoder) {
this.namespace = notNull("namespace", namespace);
this.fieldName = notNull("fieldName", fieldName);
this.decoder = notNull("decoder", decoder);
}
public BsonDocument getFilter() {
return filter;
}
public DistinctOperation<T> filter(final BsonDocument filter) {
this.filter = filter;
return this;
}
public DistinctOperation<T> retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
public boolean getRetryReads() {
return retryReads;
}
public long getMaxTime(final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
return timeUnit.convert(maxTimeMS, TimeUnit.MILLISECONDS);
}
public DistinctOperation<T> maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
public Collation getCollation() {
return collation;
}
public DistinctOperation<T> collation(final Collation collation) {
this.collation = collation;
return this;
}
@Override
public BatchCursor<T> execute(final ReadBinding binding) {
return executeCommand(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()), createCommandDecoder(),
transformer(), retryReads);
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
executeCommandAsync(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()), createCommandDecoder(),
asyncTransformer(), retryReads, errorHandlingCallback(callback, LOGGER));
}
private Codec<BsonDocument> createCommandDecoder() {
return CommandResultDocumentCodec.create(decoder, VALUES);
}
private QueryResult<T> createQueryResult(final BsonDocument result, final ConnectionDescription description) {
return new QueryResult<T>(namespace, BsonDocumentWrapperHelper.<T>toList(result, VALUES), 0L,
description.getServerAddress());
}
private CommandReadTransformer<BsonDocument, BatchCursor<T>> transformer() {
return new CommandReadTransformer<BsonDocument, BatchCursor<T>>() {
@Override
public BatchCursor<T> apply(final BsonDocument result, final ConnectionSource source, final Connection connection) {
QueryResult<T> queryResult = createQueryResult(result, connection.getDescription());
return new QueryBatchCursor<T>(queryResult, 0, 0, decoder, source);
}
};
}
private CommandReadTransformerAsync<BsonDocument, AsyncBatchCursor<T>> asyncTransformer() {
return new CommandReadTransformerAsync<BsonDocument, AsyncBatchCursor<T>>() {
@Override
public AsyncBatchCursor<T> apply(final BsonDocument result, final AsyncConnectionSource source,
final AsyncConnection connection) {
QueryResult<T> queryResult = createQueryResult(result, connection.getDescription());
return new AsyncSingleBatchQueryCursor<T>(queryResult);
}
};
}
private CommandCreator getCommandCreator(final SessionContext sessionContext) {
return new CommandCreator() {
@Override
public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) {
validateReadConcernAndCollation(connectionDescription, sessionContext.getReadConcern(), collation);
return getCommand(sessionContext);
}
};
}
private BsonDocument getCommand(final SessionContext sessionContext) {
BsonDocument commandDocument = new BsonDocument("distinct", new BsonString(namespace.getCollectionName()));
appendReadConcernToCommand(sessionContext, commandDocument);
commandDocument.put("key", new BsonString(fieldName));
putIfNotNullOrEmpty(commandDocument, "query", filter);
putIfNotZero(commandDocument, "maxTimeMS", maxTimeMS);
if (collation != null) {
commandDocument.put("collation", collation.asDocument());
}
return commandDocument;
}
}