com.mongodb.connection.InsertCommandProtocol Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
The newest version!
/*
* Copyright (c) 2008-2014 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.async.SingleResultCallback;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.bulk.InsertRequest;
import com.mongodb.bulk.WriteRequest;
import com.mongodb.diagnostics.logging.Logger;
import com.mongodb.diagnostics.logging.Loggers;
import java.util.List;
import static java.lang.String.format;
/**
* An implementation of the insert command.
*
* @mongodb.driver.manual reference/command/insert/#dbcmd.insert Insert Command
*/
class InsertCommandProtocol extends WriteCommandProtocol {
private static final Logger LOGGER = Loggers.getLogger("protocol.insert");
private final List insertRequests;
/**
* Construct an instance.
*
* @param namespace the namespace
* @param ordered whether the inserts are ordered
* @param writeConcern the write concern
* @param insertRequests the list of inserts
*/
public InsertCommandProtocol(final MongoNamespace namespace, final boolean ordered, final WriteConcern writeConcern,
final List insertRequests) {
super(namespace, ordered, writeConcern);
this.insertRequests = insertRequests;
}
@Override
public BulkWriteResult execute(final InternalConnection connection) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Inserting %d documents into namespace %s on connection [%s] to server %s",
insertRequests.size(),
getNamespace(),
connection.getDescription().getConnectionId(),
connection.getDescription().getServerAddress()));
}
BulkWriteResult writeResult = super.execute(connection);
LOGGER.debug("Insert completed");
return writeResult;
}
@Override
public void executeAsync(final InternalConnection connection, final SingleResultCallback callback) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Asynchronously inserting %d documents into namespace %s on connection [%s] to server %s",
insertRequests.size(), getNamespace(), connection.getDescription().getConnectionId(),
connection.getDescription().getServerAddress()));
}
super.executeAsync(connection, new SingleResultCallback() {
@Override
public void onResult(final BulkWriteResult result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
LOGGER.debug("Asynchronous insert completed");
callback.onResult(result, null);
}
}
});
} catch (Throwable t) {
callback.onResult(null, t);
}
}
@Override
protected WriteRequest.Type getType() {
return WriteRequest.Type.INSERT;
}
@Override
protected InsertCommandMessage createRequestMessage(final MessageSettings messageSettings) {
return new InsertCommandMessage(getNamespace(), isOrdered(), getWriteConcern(), insertRequests, messageSettings);
}
@Override
protected com.mongodb.diagnostics.logging.Logger getLogger() {
return LOGGER;
}
}