
com.mongodb.async.client.MongoClientImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-async Show documentation
Show all versions of mongodb-driver-async Show documentation
The MongoDB Asynchronous Driver
/*
* 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.async.client;
import com.mongodb.ClientSessionOptions;
import com.mongodb.Function;
import com.mongodb.MongoClientException;
import com.mongodb.ReadPreference;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.connection.Cluster;
import com.mongodb.diagnostics.logging.Logger;
import com.mongodb.diagnostics.logging.Loggers;
import com.mongodb.internal.session.ServerSessionPool;
import com.mongodb.lang.Nullable;
import com.mongodb.operation.AsyncOperationExecutor;
import com.mongodb.session.ClientSession;
import org.bson.BsonDocument;
import org.bson.Document;
import java.io.Closeable;
import java.io.IOException;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
class MongoClientImpl implements MongoClient {
private static final Logger LOGGER = Loggers.getLogger("client");
private final Cluster cluster;
private final MongoClientSettings settings;
private final AsyncOperationExecutor executor;
private final Closeable externalResourceCloser;
private final ServerSessionPool serverSessionPool;
private final ClientSessionHelper clientSessionHelper;
MongoClientImpl(final MongoClientSettings settings, final Cluster cluster, @Nullable final Closeable externalResourceCloser) {
this(settings, cluster, null, externalResourceCloser);
}
MongoClientImpl(final MongoClientSettings settings, final Cluster cluster, @Nullable final AsyncOperationExecutor executor) {
this(settings, cluster, executor, null);
}
private MongoClientImpl(final MongoClientSettings settings, final Cluster cluster, @Nullable final AsyncOperationExecutor executor,
@Nullable final Closeable externalResourceCloser) {
this.settings = notNull("settings", settings);
this.cluster = notNull("cluster", cluster);
this.serverSessionPool = new ServerSessionPool(cluster);
this.clientSessionHelper = new ClientSessionHelper(this, serverSessionPool);
if (executor == null) {
this.executor = new AsyncOperationExecutorImpl(this, clientSessionHelper);
} else {
this.executor = executor;
}
this.externalResourceCloser = externalResourceCloser;
}
@Override
public void startSession(final ClientSessionOptions options, final SingleResultCallback callback) {
notNull("callback", callback);
clientSessionHelper.createClientSession(notNull("options", options), new SingleResultCallback() {
@Override
public void onResult(final ClientSession clientSession, final Throwable t) {
SingleResultCallback errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (t != null) {
errHandlingCallback.onResult(null, t);
} else if (clientSession == null) {
errHandlingCallback.onResult(null, new MongoClientException("Sessions are not supported by the MongoDB cluster to"
+ " which this client is connected"));
} else {
errHandlingCallback.onResult(clientSession, null);
}
}
});
}
@Override
public MongoDatabase getDatabase(final String name) {
return new MongoDatabaseImpl(name, settings.getCodecRegistry(), settings.getReadPreference(), settings.getWriteConcern(),
settings.getRetryWrites(), settings.getReadConcern(), executor);
}
@Override
public void close() {
serverSessionPool.close();
cluster.close();
if (externalResourceCloser != null) {
try {
externalResourceCloser.close();
} catch (IOException e) {
LOGGER.warn("Exception closing resource", e);
}
}
}
@Override
public MongoClientSettings getSettings() {
return settings;
}
@Override
public MongoIterable listDatabaseNames() {
return createListDatabaseNamesIterable(null);
}
@Override
public MongoIterable listDatabaseNames(final ClientSession clientSession) {
notNull("clientSession", clientSession);
return createListDatabaseNamesIterable(clientSession);
}
private MongoIterable createListDatabaseNamesIterable(@Nullable final ClientSession clientSession) {
return createListDatabasesIterable(clientSession, BsonDocument.class).nameOnly(true).map(new Function() {
@Override
public String apply(final BsonDocument result) {
return result.getString("name").getValue();
}
});
}
@Override
public ListDatabasesIterable listDatabases() {
return createListDatabasesIterable(null, Document.class);
}
@Override
public ListDatabasesIterable listDatabases(final ClientSession clientSession) {
return listDatabases(clientSession, Document.class);
}
@Override
public ListDatabasesIterable listDatabases(final Class resultClass) {
return createListDatabasesIterable(null, resultClass);
}
@Override
public ListDatabasesIterable listDatabases(final ClientSession clientSession, final Class resultClass) {
notNull("clientSession", clientSession);
return createListDatabasesIterable(clientSession, resultClass);
}
private ListDatabasesIterable createListDatabasesIterable(@Nullable final ClientSession clientSession, final Class clazz) {
return new ListDatabasesIterableImpl(clientSession, clazz, settings.getCodecRegistry(),
ReadPreference.primary(), executor);
}
Cluster getCluster() {
return cluster;
}
ServerSessionPool getServerSessionPool() {
return serverSessionPool;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy