package com.mongodb.operation;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.ReadPreference;
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.operation.OperationHelper.CallableWithSource;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.BsonString;
import org.bson.codecs.Codec;
import org.bson.codecs.Decoder;
import java.util.concurrent.TimeUnit;
import static com.mongodb.ReadPreference.primary;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.connection.ServerType.SHARD_ROUTER;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.operation.CommandOperationHelper.executeCommandWithConnection;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsyncWithConnection;
import static com.mongodb.operation.CommandOperationHelper.isNamespaceError;
import static com.mongodb.operation.CommandOperationHelper.rethrowIfNotNamespaceError;
import static com.mongodb.operation.CursorHelper.getCursorDocumentFromBatchSize;
import static com.mongodb.operation.OperationHelper.AsyncCallableWithConnectionAndSource;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.createEmptyAsyncBatchCursor;
import static com.mongodb.operation.OperationHelper.createEmptyBatchCursor;
import static com.mongodb.operation.OperationHelper.cursorDocumentToAsyncBatchCursor;
import static com.mongodb.operation.OperationHelper.cursorDocumentToBatchCursor;
import static com.mongodb.operation.OperationHelper.releasingCallback;
import static com.mongodb.operation.OperationHelper.withAsyncReadConnection;
import static com.mongodb.operation.OperationHelper.withReadConnectionSource;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotZero;
@Deprecated
public class ListIndexesOperation<T> implements AsyncReadOperation<AsyncBatchCursor<T>>, ReadOperation<BatchCursor<T>> {
private final MongoNamespace namespace;
private final Decoder<T> decoder;
private boolean retryReads;
private int batchSize;
private long maxTimeMS;
public ListIndexesOperation(final MongoNamespace namespace, final Decoder<T> decoder) {
this.namespace = notNull("namespace", namespace);
this.decoder = notNull("decoder", decoder);
}
public Integer getBatchSize() {
return batchSize;
}
public ListIndexesOperation<T> batchSize(final int batchSize) {
this.batchSize = batchSize;
return this;
}
public long getMaxTime(final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
return timeUnit.convert(maxTimeMS, TimeUnit.MILLISECONDS);
}
public ListIndexesOperation<T> maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
public ListIndexesOperation<T> retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
public boolean getRetryReads() {
return retryReads;
}
@Override
public BatchCursor<T> execute(final ReadBinding binding) {
return withReadConnectionSource(binding, new CallableWithSource<BatchCursor<T>>() {
@Override
public BatchCursor<T> call(final ConnectionSource source) {
Connection connection = source.getConnection();
if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
try {
return executeCommandWithConnection(binding, source, namespace.getDatabaseName(), getCommandCreator(),
createCommandDecoder(), transformer(), retryReads, connection);
} catch (MongoCommandException e) {
return rethrowIfNotNamespaceError(e, createEmptyBatchCursor(namespace, decoder,
source.getServerDescription().getAddress(), batchSize));
}
} else {
try {
return new QueryBatchCursor<T>(connection.query(getIndexNamespace(),
asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize,
binding.getReadPreference().isSlaveOk(), false, false, false, false, false, decoder), 0, batchSize, decoder,
source);
} finally {
connection.release();
}
}
}
});
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
withAsyncReadConnection(binding, new AsyncCallableWithConnectionAndSource() {
@Override
public void call(final AsyncConnectionSource source, final AsyncConnection connection, final Throwable t) {
final SingleResultCallback<AsyncBatchCursor<T>> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (t != null) {
errHandlingCallback.onResult(null, t);
} else {
if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
executeCommandAsyncWithConnection(binding, source, namespace.getDatabaseName(), getCommandCreator(),
createCommandDecoder(), asyncTransformer(), retryReads, connection,
new SingleResultCallback<AsyncBatchCursor<T>>() {
@Override
public void onResult(final AsyncBatchCursor<T> result,
final Throwable t) {
if (t != null && !isNamespaceError(t)) {
errHandlingCallback.onResult(null, t);
} else {
errHandlingCallback.onResult(result != null ? result : emptyAsyncCursor(source), null);
}
}
});
} else {
final SingleResultCallback<AsyncBatchCursor<T>> wrappedCallback = releasingCallback(errHandlingCallback,
source, connection);
connection.queryAsync(getIndexNamespace(),
asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize,
binding.getReadPreference().isSlaveOk(), false, false, false, false, false, decoder,
new SingleResultCallback<QueryResult<T>>() {
@Override
public void onResult(final QueryResult<T> result, final Throwable t) {
if (t != null) {
wrappedCallback.onResult(null, t);
} else {
wrappedCallback.onResult(new AsyncQueryBatchCursor<T>(result, 0, batchSize, 0, decoder, source,
connection),
null);
}
}
});
}
}
}
});
}
private AsyncBatchCursor<T> emptyAsyncCursor(final AsyncConnectionSource source) {
return createEmptyAsyncBatchCursor(namespace, source.getServerDescription().getAddress());
}
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
BsonDocument document = new BsonDocument("$query", new BsonDocument("ns", new BsonString(namespace.getFullName())));
if (maxTimeMS > 0) {
document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
}
if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
document.put("$readPreference", readPreference.toDocument());
}
return document;
}
private MongoNamespace getIndexNamespace() {
return new MongoNamespace(namespace.getDatabaseName(), "system.indexes");
}
private CommandCreator getCommandCreator() {
return new CommandCreator() {
@Override
public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) {
return getCommand();
}
};
}
private BsonDocument getCommand() {
BsonDocument command = new BsonDocument("listIndexes", new BsonString(namespace.getCollectionName()))
.append("cursor", getCursorDocumentFromBatchSize(batchSize == 0 ? null : batchSize));
if (maxTimeMS > 0) {
command.put("maxTimeMS", new BsonInt64(maxTimeMS));
}
return command;
}
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) {
return cursorDocumentToBatchCursor(result.getDocument("cursor"), decoder, source, batchSize);
}
};
}
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) {
return cursorDocumentToAsyncBatchCursor(result.getDocument("cursor"), decoder, source, connection, batchSize);
}
};
}
private Codec<BsonDocument> createCommandDecoder() {
return CommandResultDocumentCodec.create(decoder, "firstBatch");
}
}