package com.mongodb.internal.operation;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.ServerCursor;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ServerDescription;
import com.mongodb.internal.binding.AsyncConnectionSource;
import com.mongodb.internal.binding.AsyncReadBinding;
import com.mongodb.internal.binding.ConnectionSource;
import com.mongodb.internal.binding.ReadBinding;
import com.mongodb.internal.connection.AsyncConnection;
import com.mongodb.internal.connection.Connection;
import com.mongodb.internal.connection.QueryResult;
import com.mongodb.internal.operation.CommandOperationHelper.CommandReadTransformer;
import com.mongodb.internal.operation.CommandOperationHelper.CommandReadTransformerAsync;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonDocumentReader;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
import org.bson.BsonRegularExpression;
import org.bson.BsonString;
import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.Codec;
import org.bson.codecs.Decoder;
import org.bson.codecs.DecoderContext;
import java.util.ArrayList;
import java.util.List;
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.internal.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.internal.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.internal.operation.CommandOperationHelper.executeCommandWithConnection;
import static com.mongodb.internal.operation.CommandOperationHelper.isNamespaceError;
import static com.mongodb.internal.operation.CommandOperationHelper.rethrowIfNotNamespaceError;
import static com.mongodb.internal.operation.CursorHelper.getCursorDocumentFromBatchSize;
import static com.mongodb.internal.operation.OperationHelper.AsyncCallableWithConnectionAndSource;
import static com.mongodb.internal.operation.OperationHelper.CallableWithSource;
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
import static com.mongodb.internal.operation.OperationHelper.createEmptyAsyncBatchCursor;
import static com.mongodb.internal.operation.OperationHelper.createEmptyBatchCursor;
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToAsyncBatchCursor;
import static com.mongodb.internal.operation.OperationHelper.cursorDocumentToBatchCursor;
import static com.mongodb.internal.operation.OperationHelper.releasingCallback;
import static com.mongodb.internal.operation.OperationHelper.withAsyncReadConnection;
import static com.mongodb.internal.operation.OperationHelper.withReadConnectionSource;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotZero;
import static java.lang.String.format;
import static java.util.Arrays.asList;
public class ListCollectionsOperation<T> implements AsyncReadOperation<AsyncBatchCursor<T>>, ReadOperation<BatchCursor<T>> {
private final String databaseName;
private final Decoder<T> decoder;
private boolean retryReads;
private BsonDocument filter;
private int batchSize;
private long maxTimeMS;
private boolean nameOnly;
public ListCollectionsOperation(final String databaseName, final Decoder<T> decoder) {
this.databaseName = notNull("databaseName", databaseName);
this.decoder = notNull("decoder", decoder);
}
public BsonDocument getFilter() {
return filter;
}
public boolean isNameOnly() {
return nameOnly;
}
public ListCollectionsOperation<T> filter(final BsonDocument filter) {
this.filter = filter;
return this;
}
public ListCollectionsOperation<T> nameOnly(final boolean nameOnly) {
this.nameOnly = nameOnly;
return this;
}
public Integer getBatchSize() {
return batchSize;
}
public ListCollectionsOperation<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 ListCollectionsOperation<T> maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
public ListCollectionsOperation<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, databaseName, getCommandCreator(), createCommandDecoder(),
commandTransformer(), retryReads, connection);
} catch (MongoCommandException e) {
return rethrowIfNotNamespaceError(e, createEmptyBatchCursor(createNamespace(), decoder,
source.getServerDescription().getAddress(), batchSize));
}
} else {
try {
return new ProjectingBatchCursor(new QueryBatchCursor<BsonDocument>(connection.query(getNamespace(),
asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize,
binding.getReadPreference().isSlaveOk(), false, false, false, false, false,
new BsonDocumentCodec()), 0, batchSize, new BsonDocumentCodec(), 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) {
SingleResultCallback<AsyncBatchCursor<T>> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (t != null) {
errHandlingCallback.onResult(null, t);
} else {
final SingleResultCallback<AsyncBatchCursor<T>> wrappedCallback = releasingCallback(errHandlingCallback,
source, connection);
if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
executeCommandAsync(binding, databaseName, getCommandCreator(), createCommandDecoder(),
asyncTransformer(), retryReads,
new SingleResultCallback<AsyncBatchCursor<T>>() {
@Override
public void onResult(final AsyncBatchCursor<T> result, final Throwable t) {
if (t != null && !isNamespaceError(t)) {
wrappedCallback.onResult(null, t);
} else {
wrappedCallback.onResult(result != null ? result : emptyAsyncCursor(source), null);
}
}
});
} else {
connection.queryAsync(getNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()),
null, 0, 0, batchSize, binding.getReadPreference().isSlaveOk(), false, false, false, false, false,
new BsonDocumentCodec(), new SingleResultCallback<QueryResult<BsonDocument>>() {
@Override
public void onResult(final QueryResult<BsonDocument> result, final Throwable t) {
if (t != null) {
wrappedCallback.onResult(null, t);
} else {
wrappedCallback.onResult(new ProjectingAsyncBatchCursor(
new AsyncQueryBatchCursor<BsonDocument>(result, 0,
batchSize, 0, new BsonDocumentCodec(), source, connection)
), null);
}
}
});
}
}
}
});
}
private AsyncBatchCursor<T> emptyAsyncCursor(final AsyncConnectionSource source) {
return createEmptyAsyncBatchCursor(createNamespace(), source.getServerDescription().getAddress());
}
private MongoNamespace createNamespace() {
return new MongoNamespace(databaseName, "$cmd.listCollections");
}
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 CommandReadTransformer<BsonDocument, BatchCursor<T>> commandTransformer() {
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 MongoNamespace getNamespace() {
return new MongoNamespace(databaseName, "system.namespaces");
}
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("listCollections", new BsonInt32(1))
.append("cursor", getCursorDocumentFromBatchSize(batchSize == 0 ? null : batchSize));
if (filter != null) {
command.append("filter", filter);
}
if (nameOnly) {
command.append("nameOnly", BsonBoolean.TRUE);
}
if (maxTimeMS > 0) {
command.put("maxTimeMS", new BsonInt64(maxTimeMS));
}
return command;
}
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
BsonDocument document = new BsonDocument();
BsonDocument transformedFilter = null;
if (filter != null) {
if (filter.containsKey("name")) {
if (!filter.isString("name")) {
throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field "
+ "must be a string");
}
transformedFilter = new BsonDocument();
transformedFilter.putAll(filter);
transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
} else {
transformedFilter = filter;
}
}
BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
BsonDocument query = transformedFilter == null ? indexExcludingRegex
: new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex,
transformedFilter)));
document.put("$query", query);
if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
document.put("$readPreference", readPreference.toDocument());
}
if (maxTimeMS > 0) {
document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
}
return document;
}
private Codec<BsonDocument> createCommandDecoder() {
return CommandResultDocumentCodec.create(decoder, "firstBatch");
}
private final class ProjectingBatchCursor implements BatchCursor<T> {
private final BatchCursor<BsonDocument> delegate;
private ProjectingBatchCursor(final BatchCursor<BsonDocument> delegate) {
this.delegate = delegate;
}
@Override
public void remove() {
delegate.remove();
}
@Override
public void close() {
delegate.close();
}
@Override
public boolean hasNext() {
return delegate.hasNext();
}
@Override
public List<T> next() {
return projectFromFullNamespaceToCollectionName(delegate.next());
}
@Override
public void setBatchSize(final int batchSize) {
delegate.setBatchSize(batchSize);
}
@Override
public int getBatchSize() {
return delegate.getBatchSize();
}
@Override
public List<T> tryNext() {
return projectFromFullNamespaceToCollectionName(delegate.tryNext());
}
@Override
public ServerCursor getServerCursor() {
return delegate.getServerCursor();
}
@Override
public ServerAddress getServerAddress() {
return delegate.getServerAddress();
}
}
private final class ProjectingAsyncBatchCursor implements AsyncBatchCursor<T> {
private final AsyncBatchCursor<BsonDocument> delegate;
private ProjectingAsyncBatchCursor(final AsyncBatchCursor<BsonDocument> delegate) {
this.delegate = delegate;
}
@Override
public void next(final SingleResultCallback<List<T>> callback) {
delegate.next(new SingleResultCallback<List<BsonDocument>>() {
@Override
public void onResult(final List<BsonDocument> result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(projectFromFullNamespaceToCollectionName(result), null);
}
}
});
}
@Override
public void tryNext(final SingleResultCallback<List<T>> callback) {
delegate.tryNext(new SingleResultCallback<List<BsonDocument>>() {
@Override
public void onResult(final List<BsonDocument> result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
callback.onResult(projectFromFullNamespaceToCollectionName(result), null);
}
}
});
}
@Override
public void setBatchSize(final int batchSize) {
delegate.setBatchSize(batchSize);
}
@Override
public int getBatchSize() {
return delegate.getBatchSize();
}
@Override
public boolean isClosed() {
return delegate.isClosed();
}
@Override
public void close() {
delegate.close();
}
}
private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) {
if (unstripped == null) {
return null;
}
List<T> stripped = new ArrayList<T>(unstripped.size());
String prefix = databaseName + ".";
for (BsonDocument cur : unstripped) {
String name = cur.getString("name").getValue();
String collectionName = name.substring(prefix.length());
cur.put("name", new BsonString(collectionName));
stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build()));
}
return stripped;
}
}