public IEnumerable<WriteConcernResult> Execute(MongoConnection connection) { List<WriteConcernResult> results = (WriteConcern.Enabled) ? new List<WriteConcernResult>() : null; using (var bsonBuffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true)) { var readerSettings = GetNodeAdjustedReaderSettings(connection.ServerInstance); var writerSettings = GetNodeAdjustedWriterSettings(connection.ServerInstance); var message = new MongoInsertMessage(writerSettings, CollectionFullName, _checkElementNames, _flags); message.WriteToBuffer(bsonBuffer); // must be called before AddDocument foreach (var document in _documents) { if (document == null) { throw new ArgumentException("Batch contains one or more null documents."); } if (_assignIdOnInsert) { var serializer = BsonSerializer.LookupSerializer(document.GetType()); var idProvider = serializer as IBsonIdProvider; if (idProvider != null) { object id; Type idNominalType; IIdGenerator idGenerator; if (idProvider.GetDocumentId(document, out id, out idNominalType, out idGenerator)) { if (idGenerator != null && idGenerator.IsEmpty(id)) { id = idGenerator.GenerateId(this, document); idProvider.SetDocumentId(document, id); } } } } message.AddDocument(bsonBuffer, _documentType, document); if (message.MessageLength > connection.ServerInstance.MaxMessageLength) { byte[] lastDocument = message.RemoveLastDocument(bsonBuffer); if (WriteConcern.Enabled || (_flags & InsertFlags.ContinueOnError) != 0) { var intermediateResult = SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern); if (WriteConcern.Enabled) { results.Add(intermediateResult); } } else { // if WriteConcern is disabled and ContinueOnError is false we have to check for errors and stop if sub-batch has error try { SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern.Acknowledged); } catch (WriteConcernException) { return null; } } message.ResetBatch(bsonBuffer, lastDocument); } } var finalResult = SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern); if (WriteConcern.Enabled) { results.Add(finalResult); } return results; } }
public IEnumerable <WriteConcernResult> Execute(MongoConnection connection) { WriteConcernException finalException = null; List <WriteConcernResult> results = (WriteConcern.Enabled) ? new List <WriteConcernResult>() : null; using (var bsonBuffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true)) { var readerSettings = GetNodeAdjustedReaderSettings(connection.ServerInstance); var writerSettings = GetNodeAdjustedWriterSettings(connection.ServerInstance); var message = new MongoInsertMessage(writerSettings, CollectionFullName, _checkElementNames, _flags); message.WriteToBuffer(bsonBuffer); // must be called before AddDocument var writeConcernEnabled = WriteConcern.Enabled; var continueOnError = (_flags & InsertFlags.ContinueOnError) != 0; foreach (var document in _documents) { if (document == null) { throw new ArgumentException("Batch contains one or more null documents."); } if (_assignIdOnInsert) { var serializer = BsonSerializer.LookupSerializer(document.GetType()); var idProvider = serializer as IBsonIdProvider; if (idProvider != null) { object id; Type idNominalType; IIdGenerator idGenerator; if (idProvider.GetDocumentId(document, out id, out idNominalType, out idGenerator)) { if (idGenerator != null && idGenerator.IsEmpty(id)) { id = idGenerator.GenerateId(_idGeneratorContainer, document); idProvider.SetDocumentId(document, id); } } } } message.AddDocument(bsonBuffer, _documentType, document); if (message.MessageLength > connection.ServerInstance.MaxMessageLength) { byte[] lastDocument = message.RemoveLastDocument(bsonBuffer); if (writeConcernEnabled && !continueOnError) { try { var result = SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern); results.Add(result); } catch (WriteConcernException ex) { results.Add((WriteConcernResult)ex.CommandResult); ex.Data["results"] = results; throw ex; } } else if (writeConcernEnabled && continueOnError) { try { var result = SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern); results.Add(result); } catch (WriteConcernException ex) { finalException = ex; results.Add((WriteConcernResult)ex.CommandResult); } } else if (!writeConcernEnabled && !continueOnError) { try { SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern.Acknowledged); } catch (WriteConcernException) { return(null); } } else if (!writeConcernEnabled && continueOnError) { SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern.Unacknowledged); } message.ResetBatch(bsonBuffer, lastDocument); } } if (writeConcernEnabled) { try { var result = SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern); results.Add(result); } catch (WriteConcernException ex) { finalException = ex; results.Add((WriteConcernResult)ex.CommandResult); } if (finalException != null) { finalException.Data["results"] = results; throw finalException; } return(results); } else { SendMessageWithWriteConcern(connection, bsonBuffer, message.RequestId, readerSettings, writerSettings, WriteConcern.Unacknowledged); return(null); } } }