public object Create(object parent, object configContext, XmlNode section)
        {
            XmlSerializer ser = new XmlSerializer(typeof(BerkeleyDbSectionConfig));

            try
            {
                object configurationObject = ser.Deserialize(new XmlNodeReader(section));
                BerkeleyDbSectionConfig            typedConfig = configurationObject as BerkeleyDbSectionConfig;
                System.Configuration.Configuration confFile    = ConfigurationManager.OpenExeConfiguration(string.Empty);
                string basePath = confFile.FilePath;
                if (typedConfig != null)
                {
                    foreach (XmlNode node in section.ChildNodes)
                    {
                        switch (node.Name)
                        {
                        case "BerkeleyDbConfig":
                            BerkeleyDbConfig berkeleyDbConfig = GetSourcedObject <BerkeleyDbConfig>(basePath, node);
                            typedConfig.BerkeleyDbConfig = berkeleyDbConfig;
                            break;
                        }
                    }
                    configurationObject = typedConfig;
                }

                return(configurationObject);
            }
            catch (Exception ex)
            {
                if (Log.IsErrorEnabled)
                {
                    Log.Error(null, ex);
                }
                return(null);
            }
        }
 /// <summary>
 /// Reloads the storage component.
 /// </summary>
 /// <param name="berkeleyDbConfig">The berkeley db config.</param>
 /// <param name="relayComponent">The relay component.</param>
 private static void ReloadStorageComponent(BerkeleyDbConfig berkeleyDbConfig, IRelayComponent relayComponent)
 {
     if (relayComponent is BerkeleyDbComponent)
     {
         (relayComponent as BerkeleyDbComponent).ReloadConfig(berkeleyDbConfig);
     }
     else
     {
         throw new Exception("Reload of CacheIndexStoreV3 Failed. Expected underlying storage to be a BDB component.");
     }
 }
 /// <summary>
 /// Initializes the storage component.
 /// </summary>
 /// <param name="berkeleyDbConfig">The berkeley db config.</param>
 /// <returns>Storage Component</returns>
 private static IRelayComponent InitializeStorageComponent(BerkeleyDbConfig berkeleyDbConfig)
 {
     // create and init bdb component
     BerkeleyDbComponent bdbComponent = new BerkeleyDbComponent();
     try
     {
         bdbComponent.Initialize(berkeleyDbConfig, COMPONENT_NAME, null);
     }
     catch (Exception ex)
     {
         LoggingUtil.Log.ErrorFormat("Failed to Initialize BerkeleyDbComponent : {0}", ex);
         throw ex;
     }
     return bdbComponent;
 }
		public void ReloadConfig(RelayNodeConfig config)
		{
			bdbConfig = GetConfig(config);
			ReloadConfig(bdbConfig);
		}
		public void ReloadConfig(BerkeleyDbConfig newConfig)
		{
			if (newConfig != null && storage != null)
			{
				storage.ReloadConfig(newConfig);
			}
			else
			{
				if (Log.IsErrorEnabled)
				{
					Log.ErrorFormat("Either BerkeleyDbConfig or BerkeleyDbStorage is null");
				}
			}
		}
		public void Initialize(BerkeleyDbConfig config, string InstanceName, ComponentRunState runState)
		{
			try
			{
			   
				instanceName = InstanceName;
				#region Perf Counter init
				BerkeleyDbCounters.Instance.Initialize(InstanceName);
				storage = new BerkeleyDbStorage
						  {
							  TrickledPagesCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.PagesTrickled),
							  DeletedObjectsCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.DeletedObjects),
							  StoredObjectsCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.ObjectsStored),
							  PooledBufferSizeCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 PooledBufferSize),
							  AllocatedBuffersCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 AllocatedBuffers),
							  BuffersInUseCounter =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.BuffersInUse),
							  LockStatCurrentMaxLockerId =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatCurrentMaxLockerId),
							  LockStatLastLockerId =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLastLockerId),
							  LockStatLockersNoWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockersNoWait),
							  LockStatLockersWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockersWait),
							  LockStatLockNoWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockNoWait),
							  LockStatLocksWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLocksWait),
							  LockStatLockTimeout =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockTimeout),
							  LockStatLockWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockWait),
							  LockStatMaxLockersPossible =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatMaxLockersPossible),
							  LockStatMaxLockObjectsPossible =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatMaxLockObjectsPossible),
							  LockStatMaxLocksPossible =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatMaxLocksPossible),
							  LockStatMaxNumberLockersAtOneTime =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatMaxNumberLockersAtOneTime),
							  LockStatMaxNumberLocksAtOneTime =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatMaxNumberLocksAtOneTime),
							  LockStatNumberCurrentLockers =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberCurrentLockers),
							  LockStatNumberCurrentLockObjects =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberCurrentLockObjects),
							  LockStatNumberCurrentLockObjectsAtOneTime =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberCurrentLockObjectsAtOneTime),
							  LockStatNumberCurrentLocks =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberCurrentLocks),
							  LockStatNumberDeadLocks =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberDeadLocks),
							  LockStatNumberLockModes =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLockModes),
							  LockStatNumberLocksDownGraded =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLocksDownGraded),
							  LockStatNumberLocksReleased =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLocksReleased),
							  LockStatNumberLocksRequested =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLocksRequested),
							  LockStatNumberLocksUpgraded =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLocksUpgraded),
							  LockStatNumberLockTimeouts =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberLockTimeouts),
							  LockStatNumberTxnTimeouts =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatNumberTxnTimeouts),
							  LockStatObjectsNoWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatObjectsNoWait),
							  LockStatObjectsWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatObjectsWait),
							  LockStatTxnTimeout =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatTxnTimeout),
							  LockStatLockHashLen =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockHashLen),
							  LockStatLockRegionSize =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLockRegionSize),
							  LockStatLocksNoWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatLocksNoWait),
							  LockStatRegionNoWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatRegionNoWait),
							  LockStatRegionWait =
								  BerkeleyDbCounters.Instance.GetCounter(GetInstanceName(),
																		 BerkeleyDbCounters.PerformanceCounterIndexes.
																			 LockStatRegionWait)
						  };


				
					if (storage.LockStatRegionWait == null)
					{
						Log.WarnFormat("Lock Statistics not initialized properly from BerkeleyDbComponent.");
					}
					else
					{
						Log.DebugFormat("Lock Statistics initialized properly from BerkeleyDbComponent.");
					}
				
				#endregion
				bdbConfig = config;
				storage.Initialize(InstanceName, bdbConfig);

				if (relayNodeConfig != null)
				{
					// tell database objects if they are to check for race conditions - Dale Earnhardt Jr. # 8 yes!
					DatabaseConfig dbConfig;
					foreach (TypeSetting typeSetting in relayNodeConfig.TypeSettings.TypeSettingCollection)
					{
						try
						{
							dbConfig = bdbConfig.EnvironmentConfig.DatabaseConfigs.GetConfigFor(typeSetting.TypeId);
						}
						catch (Exception e)
						{
							Log.ErrorFormat("Exception getting database config for type {0}: {1}", typeSetting.TypeId, e);
							throw;
						}
						if (dbConfig.Id == 0)
						{
							DatabaseConfig newDbConfig = dbConfig.Clone(typeSetting.TypeId);
							bdbConfig.EnvironmentConfig.DatabaseConfigs.Add(newDbConfig);
						}						
						RaceConditionLookup[typeSetting.TypeId] = typeSetting.CheckRaceCondition;
					}
				}

				ThrottleThreads throttleThreads = bdbConfig.ThrottleThreads;
				bool throttleThreadsEnabled = throttleThreads != null && throttleThreads.Enabled;
				if (Log.IsDebugEnabled)
				{
					Log.DebugFormat("Initialize() BerkeleyDbConfig: ThrottleThreads = {0}", throttleThreadsEnabled);
				}
				if (throttleThreadsEnabled)
				{
					threadCount = bdbConfig.ThrottleThreads.ThreadCount;
					if (Log.IsInfoEnabled)
					{
						Log.InfoFormat("Initialize() BerkeleyDbConfig: ThreadCount = {0}", threadCount);
					}
					//int federationSize = 10;
					int dbCount = bdbConfig.GetDatabaseCount();
					if (Log.IsInfoEnabled)
					{
						Log.InfoFormat("Initialize() Calculated number of databases = {0}", dbCount);
					}
					if (dbCount < threadCount)
					{
						threadCount = dbCount;
						if (Log.IsInfoEnabled)
						{
							Log.InfoFormat("Initialize() Thread count reduced to {0}", threadCount);
						}
					}
					const string threadPoolName = "BerkeleyDb Thread Pool";
					const string dispatcherQueueName = "Single Thread Queue";
					queues = new ThrottledQueue[threadCount];
					PostMessageDelegate postMessageDelegate = PostMessage;
					for (int i = 0; i < threadCount; i++)
					{
						queues[i] = new ThrottledQueue(threadPoolName, dispatcherQueueName + i,
							postMessageDelegate, bdbConfig.MaxPoolItemReuse);
					}
					queueCounterTimer = new Timer(CountThrottledQueues, null, 5000, 5000);
				}
			}
			catch (Exception exc)
			{
				if (Log.IsErrorEnabled)
				{
					Log.Error("Error initializing.", exc);
				}
				throw;
			}
		}
		private static BerkeleyDbConfig GetConfig(RelayNodeConfig config)
		{
			object configObject = config.RelayComponents.GetConfigFor(componentName);
			BerkeleyDbConfig getConfig = configObject as BerkeleyDbConfig;
			if (getConfig == null)
			{
				if (Log.IsInfoEnabled)
				{
					Log.Info("Initialize() No configuration found, using defaults.");
				}
				getConfig = new BerkeleyDbConfig();
			}

			return getConfig;
		}