Exemplo n.º 1
0
        public void GetSecCursor(BTreeDatabase db,
		    string secFileName, SecondaryKeyGenDelegate keyGen,
		    out SecondaryBTreeDatabase secDB,
		    out SecondaryCursor cursor, bool ifCfg,
		    DatabaseEntry data)
        {
            // Open secondary database.
            SecondaryBTreeDatabaseConfig secCfg =
                new SecondaryBTreeDatabaseConfig(db, keyGen);
            secCfg.Creation = CreatePolicy.IF_NEEDED;
            secCfg.Duplicates = DuplicatesPolicy.SORTED;
            secDB = SecondaryBTreeDatabase.Open(secFileName, secCfg);

            int[] intArray = new int[4];
            intArray[0] = 0;
            intArray[1] = 1;
            intArray[2] = 2049;
            intArray[3] = 65537;
            for (int i = 0; i < 4; i++)
            {
                DatabaseEntry record = new DatabaseEntry(
                    BitConverter.GetBytes(intArray[i]));
                db.Put(record, record);
            }

            // Get secondary cursor on the secondary database.
            if (ifCfg == false)
                cursor = secDB.SecondaryCursor();
            else
                cursor = secDB.SecondaryCursor(new CursorConfig());

            // Position the cursor.
            if (data != null)
                Assert.IsTrue(cursor.Move(data, true));
        }
Exemplo n.º 2
0
        private void OpenBase()
        {
            var pathStr = Environment.GetEnvironmentVariable("PATH");

            var pwd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            pwd = Path.Combine(pwd, IntPtr.Size == 4 ? "x86" : "x64");
            if (pathStr != null && !pathStr.Contains(pwd))
            {
                pwd += ";" + Environment.GetEnvironmentVariable("PATH");
                Environment.SetEnvironmentVariable("PATH", pwd);
            }


            _btreeConfig = new BTreeDatabaseConfig
            {
                Duplicates = DuplicatesPolicy.NONE,
                ErrorPrefix = "QH_" + Path.GetFileName(_dbPAth),
                Creation = CreatePolicy.IF_NEEDED,
                FreeThreaded = true
            };

            if (_env != null)
            {
                _btreeConfig.Env = _env;
            }

            _btreeDb = BTreeDatabase.Open(_dbPAth, _name, _btreeConfig);
        }
Exemplo n.º 3
0
        // Get a cursor in CDS.
        public static void GetCursorInBtreeDBInCDS(
            string home, string name,
            CursorConfig cursorConfig,
            out DatabaseEnvironment env, out BTreeDatabase db,
            out BTreeCursor cursor)
        {
            string dbFileName = name + ".db";

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseCDB = true;
            envConfig.UseMPool = true;
            env = DatabaseEnvironment.Open(home, envConfig);

            /*
             * Open an btree database. The underlying database
             * should be opened with ReadUncommitted if the
             * cursor's isolation degree will be set to be 1.
             */
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;

            if (cursorConfig.IsolationDegree == Isolation.DEGREE_ONE)
                dbConfig.ReadUncommitted = true;

            db = BTreeDatabase.Open(dbFileName, dbConfig);

            // Get a cursor in the transaction.
            cursor = db.Cursor(cursorConfig);
        }
        public void GetCursorInBtreeDBUsingRecno(string home,
		    string name, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            string dbFileName = home + "/" + name + ".db";
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.UseRecordNumbers = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
Exemplo n.º 5
0
        private static int doDupCompare(
            IntPtr dbp, IntPtr dbt1p, IntPtr dbt2p)
        {
            DB  db   = new DB(dbp, false);
            DBT dbt1 = new DBT(dbt1p, false);
            DBT dbt2 = new DBT(dbt2p, false);

            BTreeDatabase btdb = (BTreeDatabase)(db.api_internal);

            return(btdb.DupCompare(
                       DatabaseEntry.fromDBT(dbt1), DatabaseEntry.fromDBT(dbt2)));
        }
        public override void Init(int flowCount, long flowRecordCount)
        {
            BTreeDatabaseConfig config = new BTreeDatabaseConfig();

            config.Creation = CreatePolicy.IF_NEEDED;
            config.CacheSize = new CacheInfo(5, 0, 2);
            config.PageSize = 8 * 1024;
            config.BTreeCompare = new EntryComparisonDelegate(CompareFunctionKeyByteArray);
            config.Duplicates = DuplicatesPolicy.NONE;

            string fileName = Path.Combine(DataDirectory, string.Format("{0}.oracle", CollectionName));
            database = BTreeDatabase.Open(fileName, config);
        }
Exemplo n.º 7
0
        private static int doCompare(IntPtr dbp,
                                     IntPtr dbtp1, IntPtr dbtp2, IntPtr locp)
        {
            DB  db   = new DB(dbp, false);
            DBT dbt1 = new DBT(dbtp1, false);
            DBT dbt2 = new DBT(dbtp2, false);

            if (locp != IntPtr.Zero)
            {
                locp = IntPtr.Zero;
            }
            BTreeDatabase btdb = (BTreeDatabase)(db.api_internal);

            return(btdb.Compare(
                       DatabaseEntry.fromDBT(dbt1), DatabaseEntry.fromDBT(dbt2)));
        }
Exemplo n.º 8
0
        public void OpenBtreeDBInEnv(string dbName,
		    DatabaseEnvironment env, out BTreeDatabase db,
		    bool create, Transaction txn)
        {
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Env = env;
            if (create == true)
                btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            else
                btreeDBConfig.Creation = CreatePolicy.NEVER;
            if (txn == null)
                db = BTreeDatabase.Open(dbName,
                    btreeDBConfig);
            else
                db = BTreeDatabase.Open(dbName,
                    btreeDBConfig, txn);
        }
        public void OpenPrimaryAndSecondaryDB(string dbFileName,
		    out BTreeDatabase primaryDB,
		    out SecondaryBTreeDatabase secDB)
        {
            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

            // Open secondary database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(primaryDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secDB = SecondaryBTreeDatabase.Open(dbFileName,
                secDBConfig);
        }
Exemplo n.º 10
0
        public static void Confirm(XmlElement xmlElem,
		    BTreeDatabase btreeDB, bool compulsory)
        {
            DatabaseTest.Confirm(xmlElem, btreeDB, compulsory);
            Configuration.ConfirmDuplicatesPolicy(xmlElem,
                "Duplicates", btreeDB.Duplicates, compulsory);
            Configuration.ConfirmUint(xmlElem, "MinKeysPerPage",
                btreeDB.MinKeysPerPage, compulsory);
            /*
             * BTreeDatabase.RecordNumbers is the value of
             * BTreeDatabaseConfig.UseRecordNumbers.
             */
            Configuration.ConfirmBool(xmlElem, "UseRecordNumbers",
                btreeDB.RecordNumbers, compulsory);
            /*
             * BTreeDatabase.ReverseSplit is the value of
             * BTreeDatabaseConfig.NoReverseSplitting.
             */
            Configuration.ConfirmBool(xmlElem, "NoReverseSplitting",
                btreeDB.ReverseSplit, compulsory);
            Assert.AreEqual(DatabaseType.BTREE, btreeDB.Type);
            string type = btreeDB.ToString();
            Assert.IsNotNull(type);
        }
Exemplo n.º 11
0
        public void OpenNewSequenceInEnv(string home, string dbname,
		    out DatabaseEnvironment env, out BTreeDatabase db,
		    out Sequence seq)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            env = DatabaseEnvironment.Open(home, envConfig);

            Transaction openTxn = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            db = BTreeDatabase.Open(dbname + ".db", dbConfig,
                openTxn);
            openTxn.Commit();

            Transaction seqTxn = env.BeginTransaction();
            SequenceConfig seqConfig = new SequenceConfig();
            seqConfig.BackingDatabase = db;
            seqConfig.Creation = CreatePolicy.ALWAYS;
            seqConfig.Decrement = false;
            seqConfig.FreeThreaded = true;
            seqConfig.Increment = true;
            seqConfig.InitialValue = 0;
            seqConfig.key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            seq = new Sequence(seqConfig);
            seqTxn.Commit();
        }
Exemplo n.º 12
0
        public void OpenNewSequence(string dbFileName,
		    out BTreeDatabase db, out Sequence seq)
        {
            // Open a database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            db = BTreeDatabase.Open(dbFileName, btreeDBConfig);

            // Configure and initialize sequence.
            SequenceConfig seqConfig = new SequenceConfig();
            seqConfig.BackingDatabase = db;
            seqConfig.CacheSize = 1000;
            seqConfig.Creation = CreatePolicy.ALWAYS;
            seqConfig.Decrement = false;
            seqConfig.FreeThreaded = true;
            seqConfig.Increment = true;
            seqConfig.InitialValue = 100;
            seqConfig.key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            seq = new Sequence(seqConfig);
        }
Exemplo n.º 13
0
        /*
         * Configure a transactional cursor to have degree 1
         * isolation. The cursor's read operations could return
         * modified but not yet commited data.
         */
        public void CursorReadUncommited(
		    DatabaseEnvironment env, BTreeDatabase db,
		    Cursor cursor, Transaction txn)
        {
            Console.WriteLine("CursorReadUncommited");
        }
Exemplo n.º 14
0
        public static void GetCursorInBtreeDBWithoutEnv(
		    string home, string name, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            string dbFileName = home + "/" + name + ".db";

            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
Exemplo n.º 15
0
        public void TestSnapshotIsolation()
        {
            BTreeDatabaseConfig dbConfig;
            DatabaseEntry key, data;
            DatabaseEnvironmentConfig envConfig;
            Thread readThread, updateThread;
            Transaction txn;

            updateTxn = null;
            readTxn = null;
            paramDB = null;
            paramEnv = null;
            testName = "TestSnapshotIsolation";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            /*
             * Open environment with DB_MULTIVERSION
             * which is required by DB_TXN_SNAPSHOT.
             */
            envConfig = new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseMVCC = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLocking = true;
            envConfig.TxnTimeout = 1000;
            paramEnv = DatabaseEnvironment.Open(
                testHome, envConfig);
            paramEnv.DetectDeadlocks(DeadlockPolicy.YOUNGEST);

            /*
             * Open a transactional database and put 1000 records
             * into it within transaction.
             */
            txn = paramEnv.BeginTransaction();
            dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.UseMVCC = true;
            dbConfig.Env = paramEnv;
            paramDB = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);
            for (int i = 0; i < 256; i++)
            {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                paramDB.Put(key, data, txn);
            }
            txn.Commit();

            /*
             * Begin two threads, read and update thread.
             * The update thread runs a update transaction
             * using full read/write locking. The read thread
             * set DB_TXN_SNAPSHOT on read-only cursor.
             */
            readThread = new Thread(new ThreadStart(ReadTxn));
            updateThread = new Thread(new ThreadStart(UpdateTxn));
            updateThread.Start();
            Thread.Sleep(1000);
            readThread.Start();
            readThread.Join();
            updateThread.Join();

            // Commit transacion in both two threads.
            if (updateTxn != null)
                updateTxn.Commit();
            if (readTxn != null)
                readTxn.Commit();

            /*
             * Confirm that the overwrite operation works.
             */
            ConfirmOverwrite();

            paramDB.Close();
            paramEnv.Close();
        }
Exemplo n.º 16
0
        public MyDbs(string databaseHome)
        {
            vDbName = "vendordb.db";
            iDbName = "inventorydb.db";
            itemSDbName = "itemname.sdb";

            if (databaseHome != null) {
                vDbName = databaseHome + "\\" + vDbName;
                iDbName = databaseHome + "\\" + iDbName;
                itemSDbName = databaseHome + "\\" + itemSDbName;
            }

            btreeConfig = new BTreeDatabaseConfig();
            btreeConfig.Creation = CreatePolicy.IF_NEEDED;
            btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1);
            btreeConfig.ErrorPrefix = "excs_getting_started";
            btreeConfig.PageSize = 8 * 1024;

            /* Optionally remove existing database files. */
            try {
                RemoveDbFile(vDbName);
                RemoveDbFile(iDbName);
                RemoveDbFile(itemSDbName);
            } catch (Exception e) {
                Console.WriteLine("Error deleting db.");
                Console.WriteLine(e.Message);
                throw e;
            }

            /* Create and open the Inventory and Vendor database files. */
            try {
                vbtreeDB = BTreeDatabase.Open(vDbName, btreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening {0}.", vDbName);
                Console.WriteLine(e.Message);
                throw e;
            }

            try {
                ibtreeDB = BTreeDatabase.Open(iDbName, btreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening {0}.", iDbName);
                Console.WriteLine(e.Message);
                throw e;
            }

            /*
             * Open a secondary btree database associated with the
             * Inventory database.
             */
            try {
                itemSecbtreeConfig = new SecondaryBTreeDatabaseConfig(
                    ibtreeDB, new SecondaryKeyGenDelegate(
                    CreateSecondaryKey));

                itemSecbtreeConfig.Creation = CreatePolicy.IF_NEEDED;
                itemSecbtreeConfig.Duplicates = DuplicatesPolicy.UNSORTED;
                itemSecbtreeDB = SecondaryBTreeDatabase.Open(
                    itemSDbName, itemSecbtreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening secondary {0}", itemSDbName);
                Console.WriteLine(e.Message);
                throw e;
            }
        }
Exemplo n.º 17
0
        public void TestLockAndUnlockMutex()
        {
            testName = "TestLockAndUnlockMutex";
            SetUpTest(true);

            /*
             * Open an environment without locking and
             * deadlock detection.
             */
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.FreeThreaded = true;
            envConfig.UseLogging = true;
            envConfig.Create = true;
            envConfig.UseMPool = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            // Open a database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            TestDB = BTreeDatabase.Open(
                testName + ".db", dbConfig);

            // Get a mutex which will be used in two threads.
            TestMutex = env.GetMutex(true, false);

            // Begin two threads to write records into database.
            Thread mutexThread1 = new Thread(
                new ThreadStart(MutexThread1));
            Thread mutexThread2 = new Thread(
                new ThreadStart(MutexThread2));
            mutexThread1.Start();
            mutexThread2.Start();
            mutexThread1.Join();
            mutexThread2.Join();

            // Free the mutex.
            TestMutex.Dispose();

            // Close all.
            TestDB.Close();
            env.Close();
        }
        public void TestBeginTransaction()
        {
            testName = "TestBeginTransaction";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.NoLocking = false;
            cfg.FreeThreaded = true;
            testBeginTransactionEnv = DatabaseEnvironment.Open(testHome, cfg);
            testBeginTransactionEnv.DeadlockResolution = DeadlockPolicy.OLDEST;

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testBeginTransactionEnv;
            dbConfig.Duplicates = DuplicatesPolicy.NONE;
            dbConfig.FreeThreaded = true;
            testBeginTransactionDB = BTreeDatabase.Open(
                testName + ".db", dbConfig);

            testBeginTransactionDB.Put(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));

            // Begin two threads to run dead lock detection.
            Thread thread1 = new Thread(new ThreadStart(
                DeadLockThreadWithLockTimeOut));
            Thread thread2 = new Thread(new ThreadStart(
                DeadLockThreadWithTxnTimeout));
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            thread1.Start();
            thread2.Start();
            Thread.Sleep(1000);
            signal.Set();
            thread1.Join();
            thread2.Join();

            // Close all.
            testBeginTransactionDB.Close();
            testBeginTransactionEnv.Close();
        }
Exemplo n.º 19
0
        public void WriteOneIntoBtreeDBWithTxn(BTreeDatabase db,
		    Transaction txn)
        {
            DatabaseEntry key, data;

            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            db.Put(key, data, txn);
        }
Exemplo n.º 20
0
        public void OpenSecDBInTxn(string home, string dbFileName, 
		    string dbSecFileName, out DatabaseEnvironment env,
		    out BTreeDatabase db, out SecondaryBTreeDatabase secDB)
        {
            // Open environment.
            DatabaseEnvironmentConfig envCfg =
                new DatabaseEnvironmentConfig();
            envCfg.Create = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseMPool = true;
            envCfg.UseTxns = true;
            env = DatabaseEnvironment.Open(
                home, envCfg);

            // Open primary and secondary database in a transaction.
            Transaction openTxn = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            dbConfig.PageSize = 4096;
            dbConfig.Duplicates = DuplicatesPolicy.NONE;
            dbConfig.ReadUncommitted = true;
            db = BTreeDatabase.Open(dbFileName, dbConfig,
                openTxn);
            openTxn.Commit();

            openTxn = env.BeginTransaction();
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secConfig.Env = env;
            secConfig.ReadUncommitted = true;
            secDB = SecondaryBTreeDatabase.Open(dbSecFileName,
                secConfig, openTxn);
            openTxn.Commit();
        }
        public void TestCheckpoint()
        {
            testName = "TestCheckpoint";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.NoLocking = false;
            cfg.FreeThreaded = true;
            testCheckpointEnv = DatabaseEnvironment.Open(testHome, cfg);

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testCheckpointEnv;
            dbConfig.FreeThreaded = true;
            testCheckpointDB = BTreeDatabase.Open(testName + ".db", dbConfig);

            // Run a thread to put records into database.
            Thread thread1 = new Thread(new ThreadStart(PutRecordsThread));

            /*
             * Run a thread to do checkpoint periodically and
             * finally do a checkpoint to flush all in memory pool
             * to log files.
             */
            Thread thread2 = new Thread(new ThreadStart(CheckpointThread));

            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            // Close all.
            testCheckpointDB.Close();
            testCheckpointEnv.Close();
        }
Exemplo n.º 22
0
        public void WriteRecordsInTxn(BTreeDatabase db, 
		    DatabaseEnvironment env)
        {
            Transaction txn = env.BeginTransaction();
            /*
             * Write ten records into the database. The records
             * from 1st to 5th and 8th to 10th are unique in the
             * database. The data in the 6th and 7th records
             * are the same.
             */
            for (int i = 0; i < 10; i++)
            {
                if (i == 5 || i == 6)
                    db.Put(new DatabaseEntry(
                        BitConverter.GetBytes(i)),
                        new DatabaseEntry(
                        BitConverter.GetBytes((int)10)), txn);
                else
                    db.Put(new DatabaseEntry(
                        BitConverter.GetBytes(i)),
                        new DatabaseEntry(BitConverter.GetBytes(i)), txn);
            }

            txn.Commit();
        }
Exemplo n.º 23
0
        public void MoveWithRMW(string home, string name)
        {
            paramEnv = null;
            paramDB = null;

            // Open the environment.
            DatabaseEnvironmentConfig envCfg =
                new DatabaseEnvironmentConfig();
            envCfg.Create = true;
            envCfg.FreeThreaded = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseMPool = true;
            envCfg.UseTxns = true;
            paramEnv = DatabaseEnvironment.Open(home, envCfg);

            // Open database in transaction.
            Transaction openTxn = paramEnv.BeginTransaction();
            BTreeDatabaseConfig cfg = new BTreeDatabaseConfig();
            cfg.Creation = CreatePolicy.ALWAYS;
            cfg.Env = paramEnv;
            cfg.FreeThreaded = true;
            cfg.PageSize = 4096;
            cfg.Duplicates = DuplicatesPolicy.UNSORTED;
            paramDB = BTreeDatabase.Open(name + ".db", cfg, openTxn);
            openTxn.Commit();

            /*
             * Put 10 different, 2 duplicate and another different
             * records into database.
             */
            Transaction txn = paramEnv.BeginTransaction();
            for (int i = 0; i < 13; i++)
            {
                DatabaseEntry key, data;
                if (i == 10 || i == 11)
                {
                    key = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes("key"));
                    data = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes("data"));
                }
                else
                {
                    key = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                    data = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                }
                paramDB.Put(key, data, txn);
            }

            txn.Commit();

            // Get a event wait handle.
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);

            /*
             * Start RdMfWt() in two threads. RdMfWt() reads
             * and writes data into database.
             */
            Thread t1 = new Thread(new ThreadStart(RdMfWt));
            Thread t2 = new Thread(new ThreadStart(RdMfWt));
            t1.Start();
            t2.Start();

            /*
             * Give both threads time to read before signalling
             * them to write.
             */
            Thread.Sleep(1000);

            // Invoke the write operation in both threads.
            signal.Set();

            // Return the number of deadlocks.
            while (t1.IsAlive || t2.IsAlive)
            {
                /*
                 * Give both threads time to write before
                 * counting the number of deadlocks.
                 */
                Thread.Sleep(1000);
                uint deadlocks = paramEnv.DetectDeadlocks(
                   DeadlockPolicy.DEFAULT);

                // Confirm that there won't be any deadlock.
                Assert.AreEqual(0, deadlocks);
            }

            t1.Join();
            t2.Join();
            paramDB.Close();
            paramEnv.Close();
        }
Exemplo n.º 24
0
 public void PutRecordCase1(BTreeDatabase db, Transaction txn)
 {
     byte[] bigArray = new byte[10240];
     for (int i = 0; i < 100; i++)
     {
         if (txn == null)
             db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                 new DatabaseEntry(BitConverter.GetBytes(i)));
         else
             db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                 new DatabaseEntry(BitConverter.GetBytes(i)), txn);
     }
     for (int i = 100; i < 500; i++)
     {
         if (txn == null)
             db.Put(new DatabaseEntry(bigArray),
                 new DatabaseEntry(bigArray));
         else
             db.Put(new DatabaseEntry(bigArray),
                 new DatabaseEntry(bigArray), txn);
     }
 }
Exemplo n.º 25
0
        public void OpenBtreeDB(DatabaseEnvironment env,
		    Transaction txn, string dbFileName,
		    out BTreeDatabase db)
        {
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            if (env != null)
            {
                dbConfig.Env = env;
                dbConfig.NoMMap = false;
                db = BTreeDatabase.Open(dbFileName, dbConfig, txn);
            }
            else
            {
                db = BTreeDatabase.Open(dbFileName, dbConfig);
            }
        }
        public void TestDetectDeadlocks()
        {
            testName = "TestDetectDeadlocks";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.FreeThreaded = true;
            testDetectDeadlocksEnv = DatabaseEnvironment.Open(
                testHome, cfg);

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testDetectDeadlocksEnv;
            dbConfig.Duplicates = DuplicatesPolicy.NONE;
            dbConfig.FreeThreaded = true;
            testDetectDeadlocksDB = BTreeDatabase.Open(
                testName + ".db", dbConfig);

            // Put one record("key", "data") into database.
            testDetectDeadlocksDB.Put(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));

            // Begin two threads to read and write record.
            Thread thread1 = new Thread(new ThreadStart(ReadAndPutRecordThread));
            Thread thread2 = new Thread(new ThreadStart(ReadAndPutRecordThread));
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            thread1.Start();
            thread2.Start();

            // Give enough time for threads to read record.
            Thread.Sleep(1000);

            /*
             * Let the two threads apply for write lock
             * synchronously.
             */
            signal.Set();

            // Confirm that there is deadlock in the environment.
            Thread.Sleep(1000);
            uint deadlockNum = testDetectDeadlocksEnv.DetectDeadlocks(
                DeadlockPolicy.DEFAULT);
            Assert.Less(0, deadlockNum);

            thread1.Join();
            thread2.Join();

            // Close all.
            testDetectDeadlocksDB.Close(false);
            testDetectDeadlocksEnv.Close();
        }
Exemplo n.º 27
0
 public void WriteRecords(BTreeDatabase db)
 {
     /*
      * Write ten records into the database. The records
      * from 1st to 5th and 8th to 10th are unique in the
      * database. The data in the 6th and 7th records
      * are the same.
      */
     for (int i = 0; i < 10; i++)
     {
         if (i == 5 || i == 6)
             db.Put(new DatabaseEntry(
                 BitConverter.GetBytes(i)),
                 new DatabaseEntry(
                 BitConverter.GetBytes((int)10)));
         else
             db.Put(new DatabaseEntry(
                 BitConverter.GetBytes(i)),
                 new DatabaseEntry(BitConverter.GetBytes(i)));
     }
 }
Exemplo n.º 28
0
        public static void GetCursorInBtreeDBInTDS(
		    string home, string name,
		    CursorConfig cursorConfig,
		    out DatabaseEnvironment env, out BTreeDatabase db,
		    out BTreeCursor cursor, out Transaction txn)
        {
            string dbFileName = name + ".db";

            Configuration.ClearDir(home);

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            envConfig.NoMMap = false;
            envConfig.UseLocking = true;
            env = DatabaseEnvironment.Open(home, envConfig);

            // Begin a transaction.
            txn = env.BeginTransaction();

            /*
             * Open an btree database. The underlying database
             * should be opened with ReadUncommitted if the
             * cursor's isolation degree will be set to be 1.
             */
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            if (cursorConfig != null &&
                cursorConfig.IsolationDegree == Isolation.DEGREE_ONE)
                dbConfig.ReadUncommitted = true;

            db = BTreeDatabase.Open(dbFileName, dbConfig, txn);

            // Get a cursor in the transaction.
            if (cursorConfig != null)
                cursor = db.Cursor(cursorConfig, txn);
            else
                cursor = db.Cursor(txn);
        }
Exemplo n.º 29
0
        public void OpenSecDB(string dbFileName, 
		    string dbSecFileName, out BTreeDatabase db,
		    out SecondaryBTreeDatabase secDB)
        {
            // Open a primary database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);

            // Open a secondary database.
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secDB = SecondaryBTreeDatabase.Open(dbSecFileName,
                secConfig);
        }
Exemplo n.º 30
0
 private void PopulateDb(ref BTreeDatabase db)
 {
     if (db == null) {
         BTreeDatabaseConfig cfg =
             new BTreeDatabaseConfig();
         cfg.Creation = CreatePolicy.IF_NEEDED;
         db = BTreeDatabase.Open(
             testHome + "/" + testName, cfg);
     }
     for (int i = 0; i < 100; i++)
         db.Put(
             new DatabaseEntry(BitConverter.GetBytes(i)),
             new DatabaseEntry(BitConverter.GetBytes(i)));
 }
Exemplo n.º 31
0
 /// <summary>
 /// Instantiate a new Database object and open the database represented
 /// by <paramref name="Filename"/> and <paramref name="DatabaseName"/>. 
 /// The file specified by <paramref name="Filename"/> must exist.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If both <paramref name="Filename"/> and
 /// <paramref name="DatabaseName"/> are null, the database is strictly
 /// temporary and cannot be opened by any other thread of control, thus
 /// the database can only be accessed by sharing the single database 
 /// object that created it, in circumstances where doing so is safe. If
 /// <paramref name="Filename"/> is null and
 /// <paramref name="DatabaseName"/> is non-null, the database can be
 /// opened by other threads of control and be replicated to client
 /// sites in any replication group.
 /// </para>
 /// <para>
 /// If <paramref name="txn"/> is null, but
 /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation
 /// is implicitly transaction protected. Transactionally
 /// protected operations on a database object requires the object itself
 /// be transactionally protected during its open. The
 /// transaction must be committed before the object is closed.
 /// </para>
 /// </remarks>
 /// <param name="Filename">
 /// The name of an underlying file used to back the
 /// database. In-memory databases never intended to be preserved on disk
 /// may be created by setting this parameter to null.
 /// </param>
 /// <param name="DatabaseName">
 /// This parameter allows applications to have multiple databases in a
 /// single file. Although no DatabaseName needs to be specified, it is
 /// an error to attempt to open a second database in a file that was not
 /// initially created using a database name.
 /// </param>
 /// <param name="cfg">The database's configuration</param>
 /// <param name="txn">
 /// If the operation is part of an application-specified transaction,
 /// <paramref name="txn"/> is a Transaction object returned from
 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
 /// the operation is part of a Berkeley DB Concurrent Data Store group,
 /// <paramref name="txn"/> is a handle returned from
 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
 /// </param>
 /// <returns>A new, open database object</returns>
 public static new Database Open(string Filename,
     string DatabaseName, DatabaseConfig cfg, Transaction txn)
 {
     Database ret;
     BaseDatabase db = BaseDatabase.Open(
         Filename, DatabaseName, cfg, txn);
     switch (db.Type.getDBTYPE()) {
         case DBTYPE.DB_BTREE:
             ret = new BTreeDatabase(db);
             break;
         case DBTYPE.DB_HASH:
             ret = new HashDatabase(db);
             break;
         case DBTYPE.DB_HEAP:
             ret = new HeapDatabase(db);
             break;
         case DBTYPE.DB_QUEUE:
             ret = new QueueDatabase(db);
             break;
         case DBTYPE.DB_RECNO:
             ret = new RecnoDatabase(db);
             break;
         default:
             throw new DatabaseException(0);
     }
     db.Dispose();
     ret.isOpen = true;
     return ret;
 }
Exemplo n.º 32
0
 /// <summary>
 /// Instantiate a new BTreeDatabase object and open the database
 /// represented by <paramref name="Filename"/> and
 /// <paramref name="DatabaseName"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If both <paramref name="Filename"/> and
 /// <paramref name="DatabaseName"/> are null, the database is strictly
 /// temporary and cannot be opened by any other thread of control, thus
 /// the database can only be accessed by sharing the single database 
 /// object that created it, in circumstances where doing so is safe. If
 /// <paramref name="Filename"/> is null and
 /// <paramref name="DatabaseName"/> is non-null, the database can be
 /// opened by other threads of control and will be replicated to client
 /// sites in any replication group.
 /// </para>
 /// <para>
 /// If <paramref name="txn"/> is null, but
 /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will
 /// be implicitly transaction protected. Note that transactionally
 /// protected operations on a datbase object requires the object itself
 /// be transactionally protected during its open. Also note that the
 /// transaction must be committed before the object is closed.
 /// </para>
 /// </remarks>
 /// <param name="Filename">
 /// The name of an underlying file that will be used to back the
 /// database. In-memory databases never intended to be preserved on disk
 /// may be created by setting this parameter to null.
 /// </param>
 /// <param name="DatabaseName">
 /// This parameter allows applications to have multiple databases in a
 /// single file. Although no DatabaseName needs to be specified, it is
 /// an error to attempt to open a second database in a file that was not
 /// initially created using a database name.
 /// </param>
 /// <param name="cfg">The database's configuration</param>
 /// <param name="txn">
 /// If the operation is part of an application-specified transaction,
 /// <paramref name="txn"/> is a Transaction object returned from
 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
 /// the operation is part of a Berkeley DB Concurrent Data Store group,
 /// <paramref name="txn"/> is a handle returned from
 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
 /// </param>
 /// <returns>A new, open database object</returns>
 public static BTreeDatabase Open(string Filename,
     string DatabaseName, BTreeDatabaseConfig cfg, Transaction txn)
 {
     BTreeDatabase ret = new BTreeDatabase(cfg.Env, 0);
     ret.Config(cfg);
     ret.db.open(Transaction.getDB_TXN(txn),
         Filename, DatabaseName, DBTYPE.DB_BTREE, cfg.openFlags, 0);
     ret.isOpen = true;
     return ret;
 }