예제 #1
0
        /// <summary>
        /// Create a new <see cref="FigaroEnv"/> instance using the specified <see cref="FigaroEnvElement"/> object.
        /// </summary>
        /// <param name="element">The <see cref="FigaroEnvElement"/> to use.</param>
        /// <returns>A new <see cref="FigaroEnv"/> instance.</returns>
        public static FigaroEnv Create(FigaroEnvElement element)
        {
            var  env   = new FigaroEnv();
            bool panic = false;

            #region Tracing
            if (element.Tracing != null)
            {
                if (!string.IsNullOrEmpty(element.Tracing.MessageFile))
                {
                    var msgPath = Path.GetDirectoryName(PathUtility.ResolvePath(element.Tracing.MessageFile));
                    var msgFile = Path.GetFileName(element.Tracing.MessageFile);
                    if (!Directory.Exists(msgPath))
                    {
                        Directory.CreateDirectory(msgPath);
                    }
                    env.SetMessageFile(Path.Combine(msgPath, msgFile));
                }
                if (!string.IsNullOrEmpty(element.Tracing.Category))
                {
                    LogConfiguration.SetCategory(
                        (LogConfigurationCategory)
                        Enum.Parse(typeof(LogConfigurationCategory), element.Tracing.Category.Replace(" ", ",")), true);
                }
                if (!string.IsNullOrEmpty(element.Tracing.Level))
                {
                    LogConfiguration.SetLogLevel((LogConfigurationLevel)Enum.Parse(typeof(LogConfigurationLevel), element.Tracing.Level.Replace(" ", ",")), true);
                }

                if (!string.IsNullOrEmpty(element.Tracing.ErrorPrefix))
                {
                    env.ErrorPrefix = element.Tracing.ErrorPrefix;
                }
                if (!string.IsNullOrEmpty(element.Tracing.ErrorFile))
                {
                    var errPath = Path.GetDirectoryName(PathUtility.ResolvePath(element.Tracing.ErrorFile));
                    var errFile = Path.GetFileName(element.Tracing.ErrorFile);

                    if (!Directory.Exists(errPath))
                    {
                        Directory.CreateDirectory(errPath);
                    }
                    env.SetErrorFile(Path.Combine(errPath, errFile));
                }
            }
            #endregion

            #region DataDirectories
            if (element.DataDirectories != null)
            {
                if (element.DataDirectories.Count > 0)
                {
                    foreach (DataDirectoryElement directoryElement in element.DataDirectories)
                    {
                        if (directoryElement.Create && !Directory.Exists(PathUtility.ResolvePath(directoryElement.Path)))
                        {
                            Directory.CreateDirectory(PathUtility.ResolvePath(directoryElement.Path));
                        }
                        env.AddDataDirectory(PathUtility.ResolvePath(directoryElement.Path));
                    }
                }
            }
            #endregion

            #region Encryption
            if (element.Encryption != null)
            {
                env.SetEncryption(element.Encryption.Password, element.Encryption.Enabled);
            }
            #endregion

            #region EnvConfig
            if (element.EnvConfig != null)
            {
                if (element.EnvConfig.Count > 0)
                {
                    foreach (EnvConfigElement config in element.EnvConfig)
                    {
                        if (config.Setting.ToLower().Contains("panicenvironment"))
                        {
                            panic = true;
                            continue;
                        }
                        if (string.IsNullOrEmpty(config.Setting))
                        {
                            continue;
                        }
                        env.SetEnvironmentOption((EnvConfig)Enum.Parse(typeof(EnvConfig), config.Setting.Replace(" ", ","), true),
                                                 config.Enabled);
                    }
                }
            }

            #endregion

            #region Cache & CacheMax
            if (element.Cache != null)
            {
                env.SetCacheSize(new EnvCacheSize(element.Cache.GBytes, element.Cache.Bytes), element.Cache.Regions);
            }
            if (element.CacheMax != null)
            {
                env.SetCacheMax(new EnvCacheSize(element.CacheMax.GBytes, element.CacheMax.Bytes));
            }
            #endregion

            #region Locking
            if (element.Locking != null)
            {
                if (!string.IsNullOrEmpty(element.Locking.AutoDetect))
                {
                    env.DeadlockDetectPolicy = (DeadlockDetectType)Enum.Parse(typeof(DeadlockDetectType), element.Locking.AutoDetect.Replace(" ", ","));
                }
                if (element.Locking.LockPartitions > 0)
                {
                    env.SetLockPartitions(element.Locking.LockPartitions);
                }
                if (element.Locking.MaxLockers > 0)
                {
                    env.SetMaxLockers(element.Locking.MaxLockers);
                }
                if (element.Locking.MaxLockObjects > 0)
                {
                    env.SetMaxLockedObjects(element.Locking.MaxLockObjects);
                }
                if (element.Locking.MaxLocks > 0)
                {
                    env.SetMaxLocks(element.Locking.MaxLocks);
                }
                if (element.Locking.Timeout > 0)
                {
                    env.SetTimeout(element.Locking.Timeout, EnvironmentTimeoutType.Lock);
                }
            }
            #endregion

            #region Log
#if TDS || HA
            if (element.Log != null)
            {
                if (element.Log.Create && !Directory.Exists(element.Log.Directory))
                {
                    Directory.CreateDirectory(PathUtility.ResolvePath(element.Log.Directory));
                }
                if (element.Log.BufferSize > 0)
                {
                    env.SetLogBufferSize(element.Log.BufferSize);
                }
                if (!string.IsNullOrEmpty(element.Log.Directory))
                {
                    env.SetLogDirectory(PathUtility.ResolvePath(element.Log.Directory));
                }

                if (!string.IsNullOrEmpty(element.Log.LogOptions))
                {
                    var logOptions =
                        (EnvLogOptions)Enum.Parse(typeof(EnvLogOptions), element.Log.LogOptions.Replace(" ", ","), true);
                    env.SetLogOptions(logOptions, true);
                }

                if (element.Log.MaxFileSize > 0)
                {
                    env.MaxLogSize = element.Log.MaxFileSize;
                }
                if (element.Log.MaxRegionSize > 0)
                {
                    env.SetMaxLogRegion(element.Log.MaxRegionSize);
                }
            }
#endif
            #endregion

            #region MemoryFileMap
            if (element.MemoryFileMap != null)
            {
                if (element.MemoryFileMap.MaxFile > 0)
                {
                    env.SetMaxFileDescriptors(element.MemoryFileMap.MaxFile);
                }
                if (element.MemoryFileMap.MaxFileMapSize > 0)
                {
                    env.MaxFileMapSize = element.MemoryFileMap.MaxFileMapSize;
                }
                if (element.MemoryFileMap.MaxWrite > 0 && element.MemoryFileMap.WriteSleep > 0)
                {
                    env.SetMaxSequentialWriteOperations(element.MemoryFileMap.MaxWrite, element.MemoryFileMap.WriteSleep);
                }
            }
            #endregion

            #region Mutex
            if (element.Mutex != null)
            {
                if (element.Mutex.Align > 0)
                {
                    env.SetMutexAlign(element.Mutex.Align);
                }
                if (element.Mutex.Increment > 0)
                {
                    env.SetMutexIncrement(element.Mutex.Increment);
                }
                if (element.Mutex.TasSpinCount > 0)
                {
                    env.SetTestAndSetSpinCount(element.Mutex.TasSpinCount);
                }
            }
            #endregion

            #region TempDirectory
            if (element.TempDirectory != null)
            {
                if (!string.IsNullOrEmpty(element.TempDirectory.Path))
                {
                    if (!Directory.Exists(PathUtility.ResolvePath(element.TempDirectory.Path)) && element.TempDirectory.Create)
                    {
                        Directory.CreateDirectory(PathUtility.ResolvePath(element.TempDirectory.Path));
                    }

                    env.SetTempDirectory(PathUtility.ResolvePath(element.TempDirectory.Path));
                }
            }
            #endregion

            #region ThreadCount
            if (element.ThreadCount > 0)
            {
                env.SetThreadCount(element.ThreadCount);
            }
            #endregion

            #region Transactions
#if TDS || HA
            if (element.Transactions != null)
            {
                if (element.Transactions.MaxTransactions > 0)
                {
                    env.SetMaxTransactions(element.Transactions.MaxTransactions);
                }

                if (element.Transactions.Timeout > 0)
                {
                    env.SetTimeout(element.Transactions.Timeout, EnvironmentTimeoutType.Transaction);
                }
            }
                #endif
            #endregion

            #region Open

            if (element.Open != null)
            {
                EnvOpenOptions openOptions = EnvOpenOptions.None;
                if (!string.IsNullOrEmpty(element.Open.Options))
                {
                    openOptions = (EnvOpenOptions)Enum.Parse(typeof(EnvOpenOptions), element.Open.Options.Replace(" ", ","), true);
                }

                if (openOptions == EnvOpenOptions.None && string.IsNullOrEmpty(element.Open.Home))
                {
                    openOptions = EnvOpenOptions.UseEnvironmentRoot | EnvOpenOptions.Create;
                }

                if (element.Open.Create && !Directory.Exists(PathUtility.ResolvePath(element.Open.Home)))
                {
                    Directory.CreateDirectory(PathUtility.ResolvePath(element.Open.Home));
                }

                env.Open(PathUtility.ResolvePath(element.Open.Home), openOptions);
            }
            #endregion

            #region post-open
#if TDS || HA
            if (panic)
            {
                env.SetEnvironmentOption(EnvConfig.PanicEnvironment, true);
            }
#endif
            #endregion

            return(env);
        }
예제 #2
0
        /// <summary>
        /// Initialize the Figaro data objects via Figaro.Configuration
        /// </summary>
        public FigaroDataContext(string rootPath)
        {
            Initialized = false;
            //The Figaro.Configuration will create the FigaroEnv object for the XmlManager it is
            // assigned to, so we can simply retrieve the reference to it from the manager and
            // avoid creating multiple instances and adding additional, unnecessary reference
            // instances. Otherwise, we'd simply create it first and assign to the manager.

            Environment = new FigaroEnv();
            Environment.SetThreadCount(20);
            Environment.SetCacheSize(new EnvCacheSize(1, 0), 1);
            Environment.SetCacheMax(new EnvCacheSize(2, 0));

            //http://help.bdbxml.net/html/M_Figaro_FigaroEnv_SetMaxSequentialWriteOperations.htm
            //Environment.SetMaxSequentialWriteOperations(10, 1000000); // set to 1 second (1000000 nanoseconds)

            // Configuring the Locking Subsystem: http://help.bdbxml.net/html/6c964163-f0d1-4b9e-97dc-38b1ab02a895.htm
            //http://help.bdbxml.net/html/M_Figaro_FigaroEnv_SetLockPartitions.htm
            Environment.SetLockPartitions(20);
            // Configuring Deadlock Detection: http://help.bdbxml.net/html/99788b9d-b930-4191-96f3-311f0b8ffebf.htm
            // DeadlockDetectType Enumeration: http://help.bdbxml.net/html/T_Figaro_DeadlockDetectType.htm
            Environment.DeadlockDetectPolicy = DeadlockDetectType.Oldest;
            Environment.SetMaxLockers(5000);
            Environment.SetMaxLocks(50000);
            Environment.SetMaxLockedObjects(50000);

            var path = Path.Combine(rootPath, "data");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //Console.WriteLine("data directory is " + path);

            // to log transactions in memory:
            // see http://help.bdbxml.net/html/b166adda-4545-403d-a034-66a1d2774004.htm for details.
            //Environment.SetLogOptions(EnvLogOptions.InMemory, true);
            //Environment.SetLogOptions(EnvLogOptions.AutoRemove, true);
            Environment.SetMaxFileDescriptors(100);
            Environment.SetLogBufferSize(1024 * 1024 * 750);
            Environment.MaxLogSize = 1024 * 1024 * 100;

            Environment.SetMaxTransactions(500);
            Environment.SetLogOptions(EnvLogOptions.Direct, true);
            Environment.SetEnvironmentOption(EnvConfig.MultiVersion, true);
            Environment.SetEnvironmentOption(EnvConfig.DirectDB, true);

            //Environment.SetTimeout(10000,EnvironmentTimeoutType.Lock);
            Environment.SetTimeout(10000, EnvironmentTimeoutType.Transaction);
            Environment.SetTimeout(1000, EnvironmentTimeoutType.Lock);

            /* Enable message events for tracing purposes */
            //Environment.OnProcess += Environment_OnProcess;
            //Environment.OnMessage += Environment_OnMessage;
            Environment.OnErr += Environment_OnErr;

            Environment.ErrEventEnabled = true;
            //Environment.MessageEventEnabled = true;
            //Environment.ProcessEventEnabled = true;

            Environment.Open(path, EnvOpenOptions.SystemSharedMem | EnvOpenOptions.Recover | EnvOpenOptions.TransactionDefaults | EnvOpenOptions.Create | EnvOpenOptions.Thread);

            Manager = new XmlManager(Environment, ManagerInitOptions.AllOptions);

            // for resolving XQuery constructs - for more info:
            //http://help.bdbxml.net/html/e1571f63-0de0-4119-8dd3-68dc8693f732.htm
            resolver = new NancyXQueryResolver(new Uri("http://modules.bdbxml.net/nancy/"), rootPath);
            Manager.RegisterResolver(resolver);

            /*
             * open the container
             */
            using (var tx = Manager.CreateTransaction(TransactionType.SyncTransaction))
            {
                try
                {
                    //more info on ContainerConfig: http://help.bdbxml.net/html/b54e4294-4814-404f-a15f-32162b672260.htm
                    BeerDb = Manager.OpenContainer(tx, "beer.dbxml",
                                                   new ContainerConfig
                    {
                        MultiVersion  = true,
                        AllowCreate   = true,
                        Threaded      = true,
                        IndexNodes    = ConfigurationState.Off,
                        Transactional = true,
                        NoMMap        = false,
                        Statistics    = ConfigurationState.On
                    });
                    tx.Commit();
                    BeerDb.AddAlias("beer");
                    ConfigureContainerIndex();
                }
                catch (Exception)
                {
                    tx.Abort();
                    throw;
                }
            }

            Initialized = true;
        }