/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <typeparam name="TCommandResult">The type of the returned command result.</typeparam> /// <param name="command">The command object.</param> /// <param name="readPreference">The read preference.</param> /// <returns>A TCommandResult</returns> public TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, ReadPreference readPreference) where TCommandResult : CommandResult { return(UsingImplicitSession(session => RunCommandAs <TCommandResult>(session, command, readPreference))); }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <param name="commandResultType">The command result type.</param> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual CommandResult RunCommandAs(Type commandResultType, IMongoCommand command) { var methodDefinition = GetType().GetTypeInfo().GetMethod("RunCommandAs", new Type[] { typeof(IMongoCommand) }); var methodInfo = methodDefinition.MakeGenericMethod(commandResultType); return((CommandResult)methodInfo.Invoke(this, new object[] { command })); }
internal TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, IBsonSerializer <TCommandResult> resultSerializer) where TCommandResult : CommandResult { return(RunCommandAs <TCommandResult>(command, resultSerializer, _settings.ReadPreference)); }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command ) where TCommandResult : CommandResult, new() { var response = CommandCollection.FindOne(command); if (response == null) { var commandName = command.ToBsonDocument().GetElement(0).Name; var message = string.Format("Command '{0}' failed: no response returned", commandName); throw new MongoCommandException(message); } var commandResult = new TCommandResult(); // generic type constructor can't have arguments commandResult.Initialize(command, response); // so two phase construction required if (!commandResult.Ok) { if (commandResult.ErrorMessage == "not master") { server.Disconnect(); } throw new MongoCommandException(commandResult); } return(commandResult); }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <param name="commandResultType">The command result type.</param> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual CommandResult RunCommandAs( Type commandResultType, IMongoCommand command ) { var response = CommandCollection.FindOne(command); if (response == null) { var commandName = command.ToBsonDocument().GetElement(0).Name; var message = string.Format("Command '{0}' failed. No response returned.", commandName); throw new MongoCommandException(message); } var commandResult = (CommandResult)Activator.CreateInstance(commandResultType); // constructor can't have arguments commandResult.Initialize(command, response); // so two phase construction required if (!commandResult.Ok) { if (commandResult.ErrorMessage == "not master") { // TODO: figure out which instance gave the error and set its state to Unknown server.Disconnect(); } throw new MongoCommandException(commandResult); } return(commandResult); }
/// <summary> /// Initializes a new instance of the CommandResult class. /// </summary> /// <param name="command">The command.</param> /// <param name="response">The response.</param> public CommandResult( IMongoCommand command, BsonDocument response ) { this.command = command; this.response = response; }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <typeparam name="TCommandResult">The type of the returned command result.</typeparam> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual TCommandResult RunCommandAs <TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult { var resultSerializer = BsonSerializer.LookupSerializer(typeof(TCommandResult)); return(RunCommandAs <TCommandResult>(command, resultSerializer, null)); }
/// <summary> /// Runs a command on the admin database. /// </summary> /// <param name="commandResultType">The type to use for the command result.</param> /// <param name="command">The command to run.</param> /// <returns>The result of the command.</returns> public virtual object RunAdminCommandAs( Type commandResultType, IMongoCommand command ) { return(AdminDatabase.RunCommandAs(commandResultType, command)); }
internal TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, ReadPreference readPreference) where TCommandResult : CommandResult { var resultSerializer = BsonSerializer.LookupSerializer <TCommandResult>(); return(RunCommandAs <TCommandResult>(command, resultSerializer, readPreference)); }
private TCommandResult RunCommandAs <TCommandResult>( IClientSessionHandle session, IMongoCommand command, ReadPreference readPreference) where TCommandResult : CommandResult { var resultSerializer = BsonSerializer.LookupSerializer <TCommandResult>(); return(RunCommandAs <TCommandResult>(session, command, resultSerializer, readPreference)); }
// public methods /// <summary> /// Initializes an existing instance of the CommandResult class. /// </summary> /// <param name="command">The command.</param> /// <param name="response">The response.</param> // used after a constructor with no arguments (when creating a CommandResult from a generic type parameter) public void Initialize(IMongoCommand command, BsonDocument response) { if (_command != null || _response != null) { var message = string.Format("{0} has already been initialized.", this.GetType().Name); throw new InvalidOperationException(message); } _command = command; _response = response; }
private CommandOperation <CommandResult> CreateWriteCommandOperation(IMongoCommand command) { return(new CommandOperation <CommandResult>( _args.DatabaseName, _args.ReaderSettings, _args.WriterSettings, command, QueryFlags.None, null, // options ReadPreference.Primary, BsonSerializer.LookupSerializer <CommandResult>())); // resultSerializer }
/// <summary> /// Executes the command. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="command">The command.</param> /// <returns>Result of command execution</returns> public TResult ExecuteCommand <TResult>(IMongoCommand command) { int retryCount = 0; // Inject command dependencies command.ConnectionInfo = this.connectionInfo; command.RepositoryFactory = this.factories.RepositoryFactory; command.ErrorHandlingFactory = this.factories.ErrorHandlingFactory; command.IdGeneratorFactory = this.factories.IdGeneratorFactory; command.HashGeneratorFactory = this.factories.HashGeneratorFactory; command.CommandTimeout = TIMEOUT_IN_SEC; // Validate the command before we execute it command.Validate(); // Create the retry event handler EventHandler <RetryingEventArgs> retryHandler = (s, e) => { retryCount++; }; bool unsubscribeRetryEventHandler = false; TResult result; try { this.retryPolicy.Retrying += retryHandler; unsubscribeRetryEventHandler = true; result = this.retryPolicy.ExecuteAction(() => command.Execute <TResult>()); } catch (System.Exception) { if (retryCount == MAX_ATTEMPTS) { // TODO You might to do something special when max attempts exceed } throw; } finally { // Unsubscribe otherwise we will leak memory big time if (unsubscribeRetryEventHandler) { this.retryPolicy.Retrying -= retryHandler; } } return(result); }
#pragma warning restore private TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, IBsonSerializer <TCommandResult> resultSerializer) where TCommandResult : CommandResult { var readerSettings = new BsonBinaryReaderSettings { Encoding = _settings.ReadEncoding ?? MongoDefaults.ReadEncoding, GuidRepresentation = _settings.GuidRepresentation }; var writerSettings = new BsonBinaryWriterSettings { Encoding = _settings.WriteEncoding ?? MongoDefaults.WriteEncoding, GuidRepresentation = _settings.GuidRepresentation }; var readPreference = _settings.ReadPreference; if (readPreference != ReadPreference.Primary) { if (_server.ProxyType == MongoServerProxyType.Unknown) { _server.Connect(); } if (_server.ProxyType == MongoServerProxyType.ReplicaSet && !CanCommandBeSentToSecondary.Delegate(command.ToBsonDocument())) { readPreference = ReadPreference.Primary; } } var flags = (readPreference == ReadPreference.Primary) ? QueryFlags.None : QueryFlags.SlaveOk; var commandOperation = new CommandOperation <TCommandResult>( _name, readerSettings, writerSettings, command, flags, null, // options readPreference, resultSerializer); var connection = _server.AcquireConnection(readPreference); try { return(commandOperation.Execute(connection)); } finally { _server.ReleaseConnection(connection); } }
internal TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, IBsonSerializer <TCommandResult> resultSerializer, ReadPreference readPreference) where TCommandResult : CommandResult { var isReadCommand = CanCommandBeSentToSecondary.Delegate(command.ToBsonDocument()); if (readPreference != ReadPreference.Primary) { var slidingTimeout = new SlidingTimeout(_server.Settings.ConnectTimeout); var cluster = _server.Cluster; var clusterType = cluster.Description.Type; while (clusterType == ClusterType.Unknown) { // TODO: find a way to block until the cluster description changes slidingTimeout.ThrowIfExpired(); Thread.Sleep(TimeSpan.FromMilliseconds(20)); clusterType = cluster.Description.Type; } if (clusterType == ClusterType.ReplicaSet && !isReadCommand) { readPreference = ReadPreference.Primary; } } TCommandResult commandResult; var wrappedCommand = new BsonDocumentWrapper(command); MongoServerInstance serverInstance; if (isReadCommand) { commandResult = RunReadCommandAs <TCommandResult>(wrappedCommand, resultSerializer, readPreference, out serverInstance); } else { commandResult = RunWriteCommandAs <TCommandResult>(wrappedCommand, resultSerializer, out serverInstance); } commandResult.Command = command.ToBsonDocument(); commandResult.ServerInstance = serverInstance; return(commandResult); }
public CommandOperation( string databaseName, BsonBinaryReaderSettings readerSettings, BsonBinaryWriterSettings writerSettings, IMongoCommand command, QueryFlags flags, BsonDocument options, ReadPreference readPreference, IBsonSerializer <TCommandResult> serializer) : base(databaseName, "$cmd", readerSettings, writerSettings) { _command = command; _flags = flags; _options = options; _readPreference = readPreference; _serializer = serializer; }
internal TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, IBsonSerializer <TCommandResult> resultSerializer, ReadPreference readPreference) where TCommandResult : CommandResult { var commandDocument = command.ToBsonDocument(); var isReadCommand = CanCommandBeSentToSecondary.Delegate(commandDocument); if (readPreference != ReadPreference.Primary) { var timeoutAt = DateTime.UtcNow + _server.Settings.ConnectTimeout; var cluster = _server.Cluster; var clusterType = cluster.Description.Type; while (clusterType == ClusterType.Unknown) { // TODO: find a way to block until the cluster description changes if (DateTime.UtcNow >= timeoutAt) { throw new TimeoutException(); } Thread.Sleep(TimeSpan.FromMilliseconds(20)); clusterType = cluster.Description.Type; } if (clusterType == ClusterType.ReplicaSet && !isReadCommand) { readPreference = ReadPreference.Primary; } } var messageEncoderSettings = GetMessageEncoderSettings(); if (isReadCommand) { var operation = new ReadCommandOperation <TCommandResult>(_namespace, commandDocument, resultSerializer, messageEncoderSettings); return(ExecuteReadOperation(operation, readPreference)); } else { var operation = new WriteCommandOperation <TCommandResult>(_namespace, commandDocument, resultSerializer, messageEncoderSettings); return(ExecuteWriteOperation(operation)); } }
// private methods private CommandResult RunCommand(MongoConnection connection, string databaseName, IMongoCommand command) { var readerSettings = new BsonBinaryReaderSettings(); var writerSettings = new BsonBinaryWriterSettings(); var resultSerializer = BsonSerializer.LookupSerializer<CommandResult>(); var commandOperation = new CommandOperation<CommandResult>( databaseName, readerSettings, writerSettings, command, QueryFlags.None, null, // options null, // readPreference resultSerializer); return commandOperation.Execute(connection); }
internal TCommandResult RunCommandAs <TCommandResult>( IMongoCommand command, IBsonSerializer <TCommandResult> resultSerializer, ReadPreference readPreference) where TCommandResult : CommandResult { var commandDocument = command.ToBsonDocument(); var messageEncoderSettings = GetMessageEncoderSettings(); if (readPreference == ReadPreference.Primary) { var operation = new WriteCommandOperation <TCommandResult>(_namespace, commandDocument, resultSerializer, messageEncoderSettings); return(ExecuteWriteOperation(operation)); } else { var operation = new ReadCommandOperation <TCommandResult>(_namespace, commandDocument, resultSerializer, messageEncoderSettings); return(ExecuteReadOperation(operation, readPreference)); } }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <typeparam name="TCommandResult">The type of the returned command result.</typeparam> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual TCommandResult RunCommandAs <TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult { return(RunCommandAs <TCommandResult>(command, ReadPreference.Primary)); }
public TCommandResult RunCommandAs <TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult, new() { return(_mongoDatabase.RunCommandAs <TCommandResult>(command)); }
/// <summary> /// Runs a command on the admin database. /// </summary> /// <typeparam name="TCommandResult">The type to use for the command result.</typeparam> /// <param name="command">The command to run.</param> /// <returns>The result of the command (as a <typeparamref name="TCommandResult"/>).</returns> public virtual TCommandResult RunAdminCommandAs <TCommandResult>( IMongoCommand command ) where TCommandResult : CommandResult, new() { return(AdminDatabase.RunCommandAs <TCommandResult>(command)); }
// TODO: mongo shell has ResetError at the database level /// <summary> /// Runs a command on this database. /// </summary> /// <param name="command">The command object.</param> /// <returns>A CommandResult</returns> public virtual CommandResult RunCommand(IMongoCommand command) { return(RunCommandAs <CommandResult>(command)); }
private CommandOperation<CommandResult> CreateWriteCommandOperation(IMongoCommand command) { return new CommandOperation<CommandResult>( _args.DatabaseName, _args.ReaderSettings, _args.WriterSettings, command, QueryFlags.None, null, // options ReadPreference.Primary, null, // serializationOptions BsonSerializer.LookupSerializer(typeof(CommandResult))); // resultSerializer }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <typeparam name="TCommandResult">The type of the returned command result.</typeparam> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual TCommandResult RunCommandAs <TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult, new() { return((TCommandResult)RunCommandAs(typeof(TCommandResult), command)); }
// private methods private CommandResult RunCommand(MongoConnection connection, string databaseName, IMongoCommand command) { var readerSettings = new BsonBinaryReaderSettings(); var writerSettings = new BsonBinaryWriterSettings(); var resultSerializer = BsonSerializer.LookupSerializer(typeof(CommandResult)); var commandOperation = new CommandOperation <CommandResult>( databaseName, readerSettings, writerSettings, command, QueryFlags.SlaveOk, null, // options null, // readPreference null, // serializationOptions resultSerializer); return(commandOperation.Execute(connection)); }
public CommandResult RunCommand(IMongoCommand command) { return(_mongoDatabase.RunCommand(command)); }
/// <summary> /// Initializes a new instance of the CommandResult class. /// </summary> /// <param name="command">The command.</param> /// <param name="response">The response.</param> public CommandResult(IMongoCommand command, BsonDocument response) { _command = command; _response = response; }
public CommandResult RunCommandAs(Type commandResultType, IMongoCommand command) { return(_mongoDatabase.RunCommandAs(commandResultType, command)); }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <typeparam name="TCommandResult">The type of the returned command result.</typeparam> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual TCommandResult RunCommandAs <TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult { return(RunCommandAs <TCommandResult>(command, _settings.ReadPreference)); }
/// <summary> /// Runs a command on this database and returns the result as a TCommandResult. /// </summary> /// <param name="commandResultType">The command result type.</param> /// <param name="command">The command object.</param> /// <returns>A TCommandResult</returns> public virtual CommandResult RunCommandAs(Type commandResultType, IMongoCommand command) { return(CommandCollection.RunCommandAs(commandResultType, command)); }
public CommandResult RunCommand(IMongoCommand command) { return(_collection.Database.RunCommand(command)); }