Exemple #1
0
        /// <summary>
        /// Sends the command core.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="factory">The factory.</param>
        /// <param name="database">The database.</param>
        /// <param name="rootType">Type of serialization root.</param>
        /// <param name="command">The spec.</param>
        /// <returns></returns>
        private T SendCommandCore <T>(ISerializationFactory factory, string database, Type rootType, object command)
            where T : class
        {
            var writerSettings = factory.GetBsonWriterSettings(rootType);

            var query = new QueryMessage(writerSettings)
            {
                FullCollectionName = database + ".$cmd",
                NumberToReturn     = -1,
                Query = command
            };

            var readerSettings = factory.GetBsonReaderSettings(typeof(T));

            try
            {
                var reply = SendTwoWayMessageCore <T>(query, readerSettings);

                if (reply.CursorId > 0)
                {
                    SendMessage(new KillCursorsMessage(reply.CursorId), database);
                }

                return(reply.Documents.FirstOrDefault());
            }
            catch (IOException exception)
            {
                throw new MongoConnectionException("Could not read data, communication failure", this, exception);
            }
        }
        /// <summary>
        /// Retrieves the data.
        /// </summary>
        /// <typeparam name="TReply">The type of the reply.</typeparam>
        /// <returns></returns>
        private ReplyMessage <TReply> RetrieveData <TReply>() where TReply : class
        {
            IsModifiable = false;

            IRequestMessage message;

            if (Id <= 0)
            {
                var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T));

                message = new QueryMessage(writerSettings)
                {
                    FullCollectionName = FullCollectionName,
                    Query               = BuildSpec(),
                    NumberToReturn      = _limit,
                    NumberToSkip        = _skip,
                    Options             = _options,
                    ReturnFieldSelector = ConvertFieldSelectorToDocument(_fields)
                };
            }
            else
            {
                message = new GetMoreMessage(FullCollectionName, Id, _limit);
            }

            var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

            try
            {
                var reply = _connection.SendTwoWayMessage <TReply>(message, readerSettings, _databaseName);

                Id = reply.CursorId;

                if ((reply.ResponseFlag & ResponseFlags.QueryFailure) != 0)
                {
                    var error = "Review server log to get the error.";

                    if (reply.Documents.Length > 0)
                    {
                        var document = reply.Documents[0] as Document;
                        if (document != null)
                        {
                            error = Convert.ToString(document["$err"]);
                        }
                    }

                    throw new MongoException("The query failed on server. " + error);
                }

                return(reply);
            }
            catch (IOException exception)
            {
                throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
            }
        }
Exemple #3
0
        /// <summary>
        /// Retrieves the data.
        /// </summary>
        /// <typeparam name="TReply">The type of the reply.</typeparam>
        /// <returns></returns>
        private ReplyMessage <TReply> RetrieveData <TReply>() where TReply : class
        {
            IsModifiable = false;

            IRequestMessage message;

            if (Id <= 0)
            {
                var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T));

                message = new QueryMessage(writerSettings)
                {
                    FullCollectionName = FullCollectionName,
                    Query               = BuildSpec(),
                    NumberToReturn      = _limit,
                    NumberToSkip        = _skip,
                    Options             = _options,
                    ReturnFieldSelector = ConvertFieldSelectorToDocument(_fields)
                };
            }
            else
            {
                message = new GetMoreMessage(FullCollectionName, Id, _limit);
            }

            var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

            try
            {
                var reply = _connection.SendTwoWayMessage <TReply>(message, readerSettings, _databaseName);

                Id = reply.CursorId;

                return(reply);
            }
            catch (IOException exception)
            {
                throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
            }
        }