All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.connection.DefaultServerConnection Maven / Gradle / Ivy

Go to download

The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson

The newest version!
/*
 * Copyright (c) 2008-2015 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.connection;

import com.mongodb.MongoNamespace;
import com.mongodb.WriteConcern;
import com.mongodb.WriteConcernResult;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.bulk.DeleteRequest;
import com.mongodb.bulk.InsertRequest;
import com.mongodb.bulk.UpdateRequest;
import org.bson.BsonDocument;
import org.bson.FieldNameValidator;
import org.bson.codecs.Decoder;

import java.util.List;

import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.connection.ServerType.SHARD_ROUTER;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;

@SuppressWarnings("deprecation")  // because this class implements deprecated methods
class DefaultServerConnection extends AbstractReferenceCounted implements Connection, AsyncConnection {
    private final InternalConnection wrapped;
    private final ProtocolExecutor protocolExecutor;
    private final ClusterConnectionMode clusterConnectionMode;

    public DefaultServerConnection(final InternalConnection wrapped, final ProtocolExecutor protocolExecutor,
                                   final ClusterConnectionMode clusterConnectionMode) {
        this.wrapped = wrapped;
        this.protocolExecutor = protocolExecutor;
        this.clusterConnectionMode = clusterConnectionMode;
    }

    @Override
    public DefaultServerConnection retain() {
        super.retain();
        return this;
    }

    @Override
    public void release() {
        super.release();
        if (getCount() == 0) {
            wrapped.close();
        }
    }

    @Override
    public ConnectionDescription getDescription() {
        isTrue("open", getCount() > 0);
        return wrapped.getDescription();
    }

    @Override
    public WriteConcernResult insert(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                     final List inserts) {
        return executeProtocol(new InsertProtocol(namespace, ordered, writeConcern, inserts));
    }

    @Override
    public void insertAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                            final List inserts, final SingleResultCallback callback) {
        executeProtocolAsync(new InsertProtocol(namespace, ordered, writeConcern, inserts), callback);
    }

    @Override
    public WriteConcernResult update(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                     final List updates) {
        return executeProtocol(new UpdateProtocol(namespace, ordered, writeConcern, updates));
    }

    @Override
    public void updateAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                            final List updates, final SingleResultCallback callback) {
        executeProtocolAsync(new UpdateProtocol(namespace, ordered, writeConcern, updates), callback);
    }

    @Override
    public WriteConcernResult delete(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                     final List deletes) {
        return executeProtocol(new DeleteProtocol(namespace, ordered, writeConcern, deletes));
    }

    @Override
    public void deleteAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                            final List deletes, final SingleResultCallback callback) {
        executeProtocolAsync(new DeleteProtocol(namespace, ordered, writeConcern, deletes), callback);
    }

    @Override
    public BulkWriteResult insertCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                         final List inserts) {
        return executeProtocol(new InsertCommandProtocol(namespace, ordered, writeConcern, inserts));
    }

    @Override
    public void insertCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                   final List inserts, final SingleResultCallback callback) {
        executeProtocolAsync(new InsertCommandProtocol(namespace, ordered, writeConcern, inserts), callback);
    }

    @Override
    public BulkWriteResult updateCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                         final List updates) {
        return executeProtocol(new UpdateCommandProtocol(namespace, ordered, writeConcern, updates));
    }

    @Override
    public void updateCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                   final List updates, final SingleResultCallback callback) {
        executeProtocolAsync(new UpdateCommandProtocol(namespace, ordered, writeConcern, updates), callback);
    }

    @Override
    public BulkWriteResult deleteCommand(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                         final List deletes) {
        return executeProtocol(new DeleteCommandProtocol(namespace, ordered, writeConcern, deletes));

    }

    @Override
    public void deleteCommandAsync(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
                                   final List deletes, final SingleResultCallback callback) {
        executeProtocolAsync(new DeleteCommandProtocol(namespace, ordered, writeConcern, deletes), callback);
    }

    @Override
    public  T command(final String database, final BsonDocument command, final boolean slaveOk,
                         final FieldNameValidator fieldNameValidator,
                         final Decoder commandResultDecoder) {
        return executeProtocol(new CommandProtocol(database, command, fieldNameValidator, commandResultDecoder)
                               .slaveOk(getSlaveOk(slaveOk)));
    }

    @Override
    public  void commandAsync(final String database, final BsonDocument command, final boolean slaveOk,
                                           final FieldNameValidator fieldNameValidator,
                                           final Decoder commandResultDecoder, final SingleResultCallback callback) {
        executeProtocolAsync(new CommandProtocol(database, command, fieldNameValidator, commandResultDecoder)
                             .slaveOk(getSlaveOk(slaveOk)),
                             callback);
    }

    @Override
    public  QueryResult query(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
                                    final int numberToReturn, final int skip,
                                    final boolean slaveOk, final boolean tailableCursor,
                                    final boolean awaitData, final boolean noCursorTimeout,
                                    final boolean partial, final boolean oplogReplay,
                                    final Decoder resultDecoder) {
        return executeProtocol(new QueryProtocol(namespace, skip, numberToReturn, queryDocument, fields, resultDecoder)
                               .tailableCursor(tailableCursor)
                               .slaveOk(getSlaveOk(slaveOk))
                               .oplogReplay(oplogReplay)
                               .noCursorTimeout(noCursorTimeout)
                               .awaitData(awaitData)
                               .partial(partial));
    }

    @Override
    public  QueryResult query(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
                                    final int skip, final int limit, final int batchSize,
                                    final boolean slaveOk, final boolean tailableCursor,
                                    final boolean awaitData, final boolean noCursorTimeout,
                                    final boolean partial, final boolean oplogReplay,
                                    final Decoder resultDecoder) {
        return executeProtocol(new QueryProtocol(namespace, skip, limit, batchSize, queryDocument, fields, resultDecoder)
                               .tailableCursor(tailableCursor)
                               .slaveOk(getSlaveOk(slaveOk))
                               .oplogReplay(oplogReplay)
                               .noCursorTimeout(noCursorTimeout)
                               .awaitData(awaitData)
                               .partial(partial));
    }

    @Override
    public  void queryAsync(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
                               final int numberToReturn, final int skip,
                               final boolean slaveOk, final boolean tailableCursor, final boolean awaitData, final boolean noCursorTimeout,
                               final boolean partial,
                               final boolean oplogReplay, final Decoder resultDecoder,
                               final SingleResultCallback> callback) {
        executeProtocolAsync(new QueryProtocol(namespace, skip, numberToReturn, queryDocument, fields, resultDecoder)
                             .tailableCursor(tailableCursor)
                             .slaveOk(getSlaveOk(slaveOk))
                             .oplogReplay(oplogReplay)
                             .noCursorTimeout(noCursorTimeout)
                             .awaitData(awaitData)
                             .partial(partial), callback);
    }

    @Override
    public  void queryAsync(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields, final int skip,
                               final int limit, final int batchSize, final boolean slaveOk, final boolean tailableCursor,
                               final boolean awaitData, final boolean noCursorTimeout, final boolean partial, final boolean oplogReplay,
                               final Decoder resultDecoder, final SingleResultCallback> callback) {
        executeProtocolAsync(new QueryProtocol(namespace, skip, limit, batchSize, queryDocument, fields, resultDecoder)
                             .tailableCursor(tailableCursor)
                             .slaveOk(getSlaveOk(slaveOk))
                             .oplogReplay(oplogReplay)
                             .noCursorTimeout(noCursorTimeout)
                             .awaitData(awaitData)
                             .partial(partial), callback);
    }

    @Override
    public  QueryResult getMore(final MongoNamespace namespace, final long cursorId, final int numberToReturn,
                                      final Decoder resultDecoder) {
        return executeProtocol(new GetMoreProtocol(namespace, cursorId, numberToReturn, resultDecoder));
    }

    @Override
    public  void getMoreAsync(final MongoNamespace namespace, final long cursorId, final int numberToReturn,
                                 final Decoder resultDecoder, final SingleResultCallback> callback) {
        executeProtocolAsync(new GetMoreProtocol(namespace, cursorId, numberToReturn, resultDecoder), callback);
    }

    @Override
    public void killCursor(final List cursors) {
        killCursor(null, cursors);
    }

    @Override
    public void killCursor(final MongoNamespace namespace, final List cursors) {
        executeProtocol(new KillCursorProtocol(namespace, cursors));
    }

    @Override
    public void killCursorAsync(final List cursors, final SingleResultCallback callback) {
        killCursorAsync(null, cursors, callback);
    }

    @Override
    public void killCursorAsync(final MongoNamespace namespace, final List cursors, final SingleResultCallback callback) {
        executeProtocolAsync(new KillCursorProtocol(namespace, cursors), callback);
    }

    private boolean getSlaveOk(final boolean slaveOk) {
        return slaveOk
               || (clusterConnectionMode == ClusterConnectionMode.SINGLE && wrapped.getDescription().getServerType() != SHARD_ROUTER);
    }

    private  T executeProtocol(final Protocol protocol) {
        return protocolExecutor.execute(protocol, this.wrapped);
    }

    private  void executeProtocolAsync(final Protocol protocol, final SingleResultCallback callback) {
        SingleResultCallback wrappedCallback = errorHandlingCallback(callback);
        try {
            protocolExecutor.executeAsync(protocol, this.wrapped, wrappedCallback);
        } catch (Throwable t) {
            wrappedCallback.onResult(null, t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy