コード例 #1
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;
        }