use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandProtocol method sendSucceededEvent.
private void sendSucceededEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final CommandMessage commandMessage, final BsonDocument response) {
if (commandListener != null) {
BsonDocument responseDocumentForEvent = (SECURITY_SENSITIVE_COMMANDS.contains(getCommandName())) ? new BsonDocument() : response;
sendCommandSucceededEvent(commandMessage, getCommandName(), responseDocumentForEvent, connectionDescription, startTimeNanos, commandListener);
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandProtocol method sendFailedEvent.
private void sendFailedEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final CommandMessage commandMessage, final Throwable t) {
if (commandListener != null) {
Throwable commandEventException = t;
if (t instanceof MongoCommandException && (SECURITY_SENSITIVE_COMMANDS.contains(getCommandName()))) {
commandEventException = new MongoCommandException(new BsonDocument(), connectionDescription.getServerAddress());
}
sendCommandFailedEvent(commandMessage, getCommandName(), connectionDescription, startTimeNanos, commandEventException, commandListener);
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class GetMoreProtocol method asGetMoreCommandResponseDocument.
private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
responseBuffers.getBodyByteBuffer().position(0);
rawResultDocuments = ByteBufBsonDocument.create(responseBuffers);
}
BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class GetMoreProtocol method execute.
@Override
public QueryResult<T> execute(final InternalConnection connection) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Getting more documents from namespace %s with cursor %d on connection [%s] to server %s", namespace, cursorId, connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
}
long startTimeNanos = System.nanoTime();
GetMoreMessage message = new GetMoreMessage(namespace.getFullName(), cursorId, numberToReturn);
QueryResult<T> queryResult = null;
try {
sendMessage(message, connection);
ResponseBuffers responseBuffers = connection.receiveMessage(message.getId());
try {
if (responseBuffers.getReplyHeader().isCursorNotFound()) {
throw new MongoCursorNotFoundException(message.getCursorId(), connection.getDescription().getServerAddress());
}
if (responseBuffers.getReplyHeader().isQueryFailure()) {
BsonDocument errorDocument = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), message.getId()).getDocuments().get(0);
throw getQueryFailureException(errorDocument, connection.getDescription().getServerAddress());
}
queryResult = new QueryResult<T>(namespace, new ReplyMessage<T>(responseBuffers, resultDecoder, message.getId()), connection.getDescription().getServerAddress());
if (commandListener != null) {
sendCommandSucceededEvent(message, COMMAND_NAME, asGetMoreCommandResponseDocument(queryResult, responseBuffers), connection.getDescription(), startTimeNanos, commandListener);
}
} finally {
responseBuffers.close();
}
LOGGER.debug("Get-more completed");
return queryResult;
} catch (RuntimeException e) {
if (commandListener != null) {
sendCommandFailedEvent(message, COMMAND_NAME, connection.getDescription(), startTimeNanos, e, commandListener);
}
throw e;
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class InsertCommandMessage method writeTheWrites.
protected InsertCommandMessage writeTheWrites(final BsonOutput bsonOutput, final int commandStartPosition, final BsonBinaryWriter writer) {
InsertCommandMessage nextMessage = null;
writer.writeStartArray("documents");
writer.pushMaxDocumentSize(getSettings().getMaxDocumentSize());
for (int i = 0; i < insertRequestList.size(); i++) {
writer.mark();
BsonDocument document = insertRequestList.get(i).getDocument();
getCodec(document).encode(writer, document, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
if (exceedsLimits(bsonOutput.getPosition() - commandStartPosition, i + 1)) {
writer.reset();
nextMessage = new InsertCommandMessage(getWriteNamespace(), isOrdered(), getWriteConcern(), getBypassDocumentValidation(), getSettings(), insertRequestList.subList(i, insertRequestList.size()));
break;
}
}
writer.popMaxDocumentSize();
writer.writeEndArray();
return nextMessage;
}
Aggregations