<commit_before>namespace EventStore.Persistence.SqlPersistence
{
	using System;
	using System.Configuration;
	using System.Data;
	using System.Data.Common;
	using System.Data.SqlClient;

	public class ConfigurationConnectionFactory : IConnectionFactory
	{
		private const int DefaultShards = 16;
		private const string DefaultConnectionName = "EventStore";
		private const string DefaultProvider = "System.Data.SqlClient";
		private readonly string masterConnectionName;
		private readonly string slaveConnectionName;
		private readonly int shards;

		public ConfigurationConnectionFactory()
			: this(null, null, DefaultShards)
		{
		}
		public ConfigurationConnectionFactory(string connectionName)
			: this(connectionName, connectionName, DefaultShards)
		{
		}
		public ConfigurationConnectionFactory(string masterConnectionName, string slaveConnectionName, int shards)
		{
			this.masterConnectionName = masterConnectionName ?? DefaultConnectionName;
			this.slaveConnectionName = slaveConnectionName ?? this.masterConnectionName;
			this.shards = shards >= 0 ? shards : DefaultShards;
		}

		public virtual IDbConnection OpenMaster(Guid streamId)
		{
			return this.Open(streamId, this.masterConnectionName);
		}
		public virtual IDbConnection OpenSlave(Guid streamId)
		{
			return this.Open(streamId, this.slaveConnectionName);
		}
		protected virtual IDbConnection Open(Guid streamId, string connectionName)
		{
			var setting = ConfigurationManager.ConnectionStrings[connectionName];
			var factory = DbProviderFactories.GetFactory(setting.ProviderName ?? DefaultProvider);
			var connection = factory.CreateConnection() ?? new SqlConnection();
			connection.ConnectionString = this.BuildConnectionString(streamId, setting);
			connection.Open();
			return connection;
		}
		protected virtual string BuildConnectionString(Guid streamId, ConnectionStringSettings setting)
		{
			if (this.shards == 0)
				return setting.ConnectionString;

			return setting.ConnectionString.FormatWith(this.ComputeHashKey(streamId));
		}
		protected virtual string ComputeHashKey(Guid streamId)
		{
			// simple sharding scheme which could easily be improved through such techniques
			// as consistent hashing (Amazon Dynamo) or other kinds of sharding.
			return (this.shards == 0 ? 0 : streamId.ToByteArray()[0] % this.shards).ToString();
		}
	}
}<commit_msg>Added a meaningful Exception message in case ConnectionString could not be found<commit_after>namespace EventStore.Persistence.SqlPersistence
{
	using System;
	using System.Configuration;
	using System.Data;
	using System.Data.Common;
	using System.Data.SqlClient;
	using System.Linq;

    public class ConfigurationConnectionFactory : IConnectionFactory
	{
		private const int DefaultShards = 16;
		private const string DefaultConnectionName = "EventStore";
		private const string DefaultProvider = "System.Data.SqlClient";
		private readonly string masterConnectionName;
		private readonly string slaveConnectionName;
		private readonly int shards;

		public ConfigurationConnectionFactory()
			: this(null, null, DefaultShards)
		{
		}
		public ConfigurationConnectionFactory(string connectionName)
			: this(connectionName, connectionName, DefaultShards)
		{
		}
		public ConfigurationConnectionFactory(string masterConnectionName, string slaveConnectionName, int shards)
		{
			this.masterConnectionName = masterConnectionName ?? DefaultConnectionName;
			this.slaveConnectionName = slaveConnectionName ?? this.masterConnectionName;
			this.shards = shards >= 0 ? shards : DefaultShards;
		}

		public virtual IDbConnection OpenMaster(Guid streamId)
		{
			return this.Open(streamId, this.masterConnectionName);
		}
		public virtual IDbConnection OpenSlave(Guid streamId)
		{
			return this.Open(streamId, this.slaveConnectionName);
		}
		protected virtual IDbConnection Open(Guid streamId, string connectionName)
		{
			var setting = GetConnectionStringSettings(connectionName);
			var factory = DbProviderFactories.GetFactory(setting.ProviderName ?? DefaultProvider);
			var connection = factory.CreateConnection() ?? new SqlConnection();
			connection.ConnectionString = this.BuildConnectionString(streamId, setting);
			connection.Open();
			return connection;
		}

	    private ConnectionStringSettings GetConnectionStringSettings(string connectionName)
	    {
            var keyExists = ConfigurationManager.ConnectionStrings
                .Cast<ConnectionStringSettings>()
	            .Any(p => p.Name == connectionName);

            if (!keyExists)
                throw new StorageException("Could not find connectionstring '{0}' in your configuration");
	        return ConfigurationManager.ConnectionStrings[connectionName];
	    }

	    protected virtual string BuildConnectionString(Guid streamId, ConnectionStringSettings setting)
		{
			if (this.shards == 0)
				return setting.ConnectionString;

			return setting.ConnectionString.FormatWith(this.ComputeHashKey(streamId));
		}
		protected virtual string ComputeHashKey(Guid streamId)
		{
			// simple sharding scheme which could easily be improved through such techniques
			// as consistent hashing (Amazon Dynamo) or other kinds of sharding.
			return (this.shards == 0 ? 0 : streamId.ToByteArray()[0] % this.shards).ToString();
		}
	}
}
{'fraction_non_alphanumeric': 0.07760910815939279, 'fraction_numerical': 0.0028462998102466793, 'mean_word_length': 4.8566666666666665, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7395101', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1317, 'n_words': 356}