public void OpenSecQueueDB(string className, 
		    string funName, string dbFileName, string dbSecFileName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

            // Open a primary queue database.
            QueueDatabaseConfig primaryDBConfig =
                new QueueDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            QueueDatabase primaryDB;

            /*
             * If secondary database name is given, the primary
             * database is also opened with database name.
             */
            primaryDB = QueueDatabase.Open(dbFileName,
                primaryDBConfig);

            try
            {
                // Open a new secondary database.
                SecondaryQueueDatabaseConfig secQueueDBConfig =
                    new SecondaryQueueDatabaseConfig(
                    primaryDB, null);
                SecondaryQueueDatabaseConfigTest.Config(
                    xmlElem, ref secQueueDBConfig, false);
                secQueueDBConfig.Creation =
                    CreatePolicy.IF_NEEDED;

                SecondaryQueueDatabase secQueueDB;
                secQueueDB = SecondaryQueueDatabase.Open(
                    dbSecFileName, secQueueDBConfig);

                // Close the secondary database.
                secQueueDB.Close();

                // Open the existing secondary database.
                SecondaryDatabaseConfig secDBConfig =
                    new SecondaryQueueDatabaseConfig(
                    primaryDB, null);

                SecondaryDatabase secDB;
                secDB = SecondaryQueueDatabase.Open(
                    dbSecFileName, secDBConfig);

                // Close secondary database.
                secDB.Close();
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                // Close primary database.
                primaryDB.Close();
            }
        }
 public void ConfigCase1(QueueDatabaseConfig dbConfig)
 {
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     dbConfig.PageSize = 4096;
     dbConfig.ExtentSize = 1024;
     dbConfig.Length = 4000;
     dbConfig.PadByte = 32;
 }
Beispiel #3
0
        /// <summary>
        /// Instantiate a new QueueDatabase object and open the database
        /// represented by <paramref name="Filename"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// If <paramref name="Filename"/> is 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.
        /// </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="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 QueueDatabase Open(
            string Filename, QueueDatabaseConfig cfg, Transaction txn)
        {
            QueueDatabase ret = new QueueDatabase(cfg.Env, 0);

            ret.Config(cfg);
            ret.db.open(Transaction.getDB_TXN(txn),
                        Filename, null, DBTYPE.DB_QUEUE, cfg.openFlags, 0);
            ret.isOpen = true;
            return(ret);
        }
Beispiel #4
0
        private void Config(QueueDatabaseConfig cfg) {
            base.Config(cfg);
            /* 
             * Database.Config calls set_flags, but that doesn't get the Queue
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);
            
            db.set_re_len(cfg.Length);

            if (cfg.padIsSet)
                db.set_re_pad(cfg.PadByte);
            if (cfg.extentIsSet)
                db.set_q_extentsize(cfg.ExtentSize);
        }
        public static void Confirm(XmlElement xmlElement,
		    QueueDatabaseConfig queueDBConfig, bool compulsory)
        {
            DatabaseConfig dbConfig = queueDBConfig;
            Confirm(xmlElement, dbConfig, compulsory);

            // Confirm Queue database specific configuration
            Configuration.ConfirmBool(xmlElement, "ConsumeInOrder",
                queueDBConfig.ConsumeInOrder, compulsory);
            Configuration.ConfirmCreatePolicy(xmlElement, "Creation",
                queueDBConfig.Creation, compulsory);
            Configuration.ConfirmUint(xmlElement, "Length",
                queueDBConfig.Length, compulsory);
            Configuration.ConfirmInt(xmlElement, "PadByte",
                queueDBConfig.PadByte, compulsory);
            Configuration.ConfirmUint(xmlElement, "ExtentSize",
                queueDBConfig.ExtentSize, compulsory);
        }
Beispiel #6
0
        private void Config(QueueDatabaseConfig cfg)
        {
            base.Config(cfg);

            /*
             * Database.Config calls set_flags, but that doesn't get the Queue
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);

            db.set_re_len(cfg.Length);

            if (cfg.padIsSet)
            {
                db.set_re_pad(cfg.PadByte);
            }
            if (cfg.extentIsSet)
            {
                db.set_q_extentsize(cfg.ExtentSize);
            }
        }
 public void GetCursur(string dbFileName, bool ifConfig)
 {
     QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     dbConfig.Length = 100;
     QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig);
     Cursor cursor;
     CursorConfig cursorConfig = new CursorConfig();
     cursorConfig.Priority = CachePriority.HIGH;
     if (ifConfig == false)
         cursor = db.Cursor();
     else
         cursor = db.Cursor(cursorConfig);
     cursor.Add(new KeyValuePair<DatabaseEntry,DatabaseEntry>(
         new DatabaseEntry(BitConverter.GetBytes((int)1)),
         new DatabaseEntry(BitConverter.GetBytes((int)1))));
     Cursor dupCursor = cursor.Duplicate(false);
     Assert.IsNull(dupCursor.Current.Key);
     Assert.AreEqual(CachePriority.HIGH, dupCursor.Priority);
     dupCursor.Close();
     cursor.Close();
     db.Close();
 }
        public static void Config(XmlElement xmlElement,
		    ref QueueDatabaseConfig queueDBConfig, bool compulsory)
        {
            uint uintValue = new uint();
            int intValue = new int();
            DatabaseConfig dbConfig = queueDBConfig;
            Config(xmlElement, ref dbConfig, compulsory);

            // Configure specific fields/properties of Queue database
            Configuration.ConfigBool(xmlElement, "ConsumeInOrder",
                ref queueDBConfig.ConsumeInOrder, compulsory);
            Configuration.ConfigCreatePolicy(xmlElement, "Creation",
                ref queueDBConfig.Creation, compulsory);
            if (Configuration.ConfigUint(xmlElement, "Length",
                ref uintValue, compulsory))
                queueDBConfig.Length = uintValue;
            if (Configuration.ConfigInt(xmlElement, "PadByte",
                ref intValue, compulsory))
                queueDBConfig.PadByte = intValue;
            if (Configuration.ConfigUint(xmlElement, "ExtentSize",
                ref uintValue, compulsory))
                queueDBConfig.ExtentSize = uintValue;
        }
        public void TestOpenExistingQueueDB()
        {
            testName = "TestOpenExistingQueueDB";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";

            QueueDatabaseConfig queueConfig = new QueueDatabaseConfig();
            queueConfig.Creation = CreatePolicy.ALWAYS;
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBFileName, queueConfig);
            queueDB.Close();

            DatabaseConfig dbConfig = new DatabaseConfig();
            Database db = Database.Open(queueDBFileName, dbConfig);
            Assert.AreEqual(db.Type, DatabaseType.QUEUE);
            db.Close();
        }
        public void TestMessageFile()
        {
            testName = "TestMessageFile";
            SetUpTest(true);

            // Configure and open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseMPool = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            // Configure and open a database.
            QueueDatabaseConfig DBConfig =
                new QueueDatabaseConfig();
            DBConfig.Env = env;
            DBConfig.Creation = CreatePolicy.IF_NEEDED;

            string DBFileName = testName + ".db";
            QueueDatabase db = QueueDatabase.Open(
                DBFileName, DBConfig);

            // Confirm message file does not exist.
            string messageFile = testHome + "/" + "msgfile";
            Assert.AreEqual(false, File.Exists(messageFile));

            // Call set_msgfile() of db.
            db.Msgfile = messageFile;

            // Print db statistic to message file.
            db.PrintStats(true);

            // Confirm message file exists now.
            Assert.AreEqual(true, File.Exists(messageFile));

            db.Msgfile = "";
            string line = null;

            // Read the third line of message file.
            System.IO.StreamReader file = new System.IO.StreamReader(@"" + messageFile);
            line = file.ReadLine();
            line = file.ReadLine();
            line = file.ReadLine();

            // Confirm the message file is not empty.
            Assert.AreEqual(line, "DB handle information:");
            file.Close();

            // Close database and environment.
            db.Close();
            env.Close();
        }
Beispiel #11
0
 public void GetCursur(string dbFileName, bool ifConfig)
 {
     QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     dbConfig.Length = 100;
     QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig);
     Cursor cursor;
     if (ifConfig == false)
         cursor = db.Cursor();
     else
         cursor = db.Cursor(new CursorConfig());
     cursor.Close();
     db.Close();
 }
Beispiel #12
0
        private void GetCursorWithConfig(string dbFile, string dbName, 
		    CursorConfig cfg, DatabaseType type)
        {
            Database db;
            Cursor cursor;
            Configuration.ClearDir(testHome);
            if (type == DatabaseType.BTREE) {
                BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = BTreeDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((BTreeDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.HASH) {
                HashDatabaseConfig dbConfig = new HashDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = (HashDatabase)HashDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((HashDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.QUEUE) {
                QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                dbConfig.Length = 100;
                db = QueueDatabase.Open(dbFile, dbConfig);
                cursor = ((QueueDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.RECNO) {
                RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = RecnoDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((RecnoDatabase)db).Cursor(cfg);
            } else
                throw new TestException();

            if (cfg.Priority != null)
                Assert.AreEqual(cursor.Priority, cfg.Priority);
            else
                Assert.AreEqual(CachePriority.DEFAULT, cursor.Priority);

            Cursor dupCursor = cursor.Duplicate(false);
            Assert.AreEqual(cursor.Priority, dupCursor.Priority);
            cursor.Close();
            db.Close();
        }
        public void TestConsumeWithoutTxn()
        {
            testName = "TestConsumeWithoutTxn";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" +
                testName + ".db";

            QueueDatabaseConfig queueConfig =
                new QueueDatabaseConfig();
            queueConfig.Creation = CreatePolicy.ALWAYS;
            queueConfig.ErrorPrefix = testName;
            queueConfig.Length = 1000;

            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBFileName, queueConfig);
            DatabaseEntry data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            queueDB.Append(data);

            DatabaseEntry consumeData = new DatabaseEntry();
            KeyValuePair<uint, DatabaseEntry> pair = queueDB.Consume(false);
            try
            {
                DatabaseEntry key =
                    new DatabaseEntry(BitConverter.GetBytes(pair.Key));
                queueDB.Get(key);
            }
            catch (NotFoundException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                queueDB.Close();
            }
        }
        public void TestPutToQueue()
        {
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;

            testName = "TestPutQueue";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" +
                testName + ".db";

            QueueDatabaseConfig queueConfig =
                new QueueDatabaseConfig();
            queueConfig.Length = 512;
            queueConfig.Creation = CreatePolicy.ALWAYS;
            using (QueueDatabase queueDB = QueueDatabase.Open(
                queueDBFileName, queueConfig))
            {
                DatabaseEntry key = new DatabaseEntry();
                key.Data = BitConverter.GetBytes((int)100);
                DatabaseEntry data = new DatabaseEntry(
                    BitConverter.GetBytes((int)1));
                queueDB.Put(key, data);
                pair = queueDB.GetBoth(key, data);
            }
        }
        public void TestAppendWithoutTxn()
        {
            testName = "TestAppendWithoutTxn";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";

            QueueDatabaseConfig queueConfig = new QueueDatabaseConfig();
            queueConfig.Creation = CreatePolicy.ALWAYS;
            queueConfig.Length = 1000;
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBFileName, queueConfig);

            byte[] byteArr = new byte[4];
            byteArr = BitConverter.GetBytes((int)1);
            DatabaseEntry data = new DatabaseEntry(byteArr);
            uint recno = queueDB.Append(data);

            // Confirm that the recno is larger than 0.
            Assert.AreNotEqual(0, recno);

            // Confirm that the record exists in the database.
            byteArr = BitConverter.GetBytes(recno);
            DatabaseEntry key = new DatabaseEntry();
            key.Data = byteArr;
            Assert.IsTrue(queueDB.Exists(key));
            queueDB.Close();
        }
        public void TestAppendWithTxn()
        {
            testName = "TestAppendWithTxn";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";
            string queueDBName =
                Path.GetFileNameWithoutExtension(queueDBFileName);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;

            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);
            Transaction txn = env.BeginTransaction();

            QueueDatabaseConfig queueConfig = new QueueDatabaseConfig();
            queueConfig.Creation = CreatePolicy.ALWAYS;
            queueConfig.Env = env;
            queueConfig.Length = 1000;

            /* If environmnet home is set, the file name in
             * Open() is the relative path.
             */
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBName, queueConfig, txn);
            DatabaseEntry data;
            int i = 1000;
            try
            {
                while (i > 0)
                {
                    data = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                    queueDB.Append(data, txn);
                    i--;
                }
                txn.Commit();
            }
            catch
            {
                txn.Abort();
            }
            finally
            {
                queueDB.Close();
                env.Close();
            }
        }
        public void StatsInTxn(string home, string name, bool ifIsolation)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            EnvConfigCase1(envConfig);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            Transaction openTxn = env.BeginTransaction();
            QueueDatabaseConfig dbConfig =
                new QueueDatabaseConfig();
            ConfigCase1(dbConfig);
            dbConfig.Env = env;
            QueueDatabase db = QueueDatabase.Open(name + ".db",
                dbConfig, openTxn);
            openTxn.Commit();

            Transaction statsTxn = env.BeginTransaction();
            QueueStats stats;
            if (ifIsolation == false)
                stats = db.Stats(statsTxn);
            else
                stats = db.Stats(statsTxn, Isolation.DEGREE_ONE);

            ConfirmStatsPart1Case1(stats);
            db.Msgfile = home + "/" + name+ ".log";
            db.PrintStats(true);

            // Put 500 records into the database.
            PutRecordCase1(db, statsTxn);

            if (ifIsolation == false)
                stats = db.Stats(statsTxn);
            else
                stats = db.Stats(statsTxn, Isolation.DEGREE_TWO);
            ConfirmStatsPart2Case1(stats);
            db.PrintStats();

            statsTxn.Commit();
            db.Close();
            env.Close();
        }
 public new void TestConfigWithoutEnv()
 {
     testName = "TestConfigWithoutEnv";
     SetUpTest(false);
     XmlElement xmlElem = Configuration.TestSetUp(
         testFixtureName, testName);
     QueueDatabaseConfig queueDBConfig =
         new QueueDatabaseConfig();
     Config(xmlElem, ref queueDBConfig, true);
     Confirm(xmlElem, queueDBConfig, true);
 }
 /// <summary>
 /// Instantiate a new QueueDatabase object and open the database
 /// represented by <paramref name="Filename"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If <paramref name="Filename"/> is 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.
 /// </para>
 /// <para>
 /// If <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.
 /// </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="cfg">The database's configuration</param>
 /// <returns>A new, open database object</returns>
 public static QueueDatabase Open(
     string Filename, QueueDatabaseConfig cfg)
 {
     return Open(Filename, cfg, null);
 }
        public void TestOpenNewQueueDB()
        {
            testName = "TestOpenNewQueueDB";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";

            // Configure all fields/properties in queue database.
            XmlElement xmlElem = Configuration.TestSetUp(
                testFixtureName, testName);
            QueueDatabaseConfig queueConfig = new QueueDatabaseConfig();
            QueueDatabaseConfigTest.Config(xmlElem, ref queueConfig, true);
            queueConfig.Feedback = new DatabaseFeedbackDelegate(DbFeedback);

            // Open the queue database with above configuration.
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBFileName, queueConfig);

            // Check the fields/properties in opened queue database.
            Confirm(xmlElem, queueDB, true);

            queueDB.Close();
        }
        public void TestConsumeWithTxn()
        {
            testName = "TestConsumeWithTxn";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";
            string queueDBName = Path.GetFileName(queueDBFileName);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;

            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);
            Transaction txn = env.BeginTransaction();

            QueueDatabaseConfig queueConfig =
                new QueueDatabaseConfig();
            queueConfig.Creation = CreatePolicy.ALWAYS;
            queueConfig.Env = env;
            queueConfig.Length = 1000;
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBName, queueConfig, txn);

            int i = 1;
            DatabaseEntry data;
            DatabaseEntry getData = new DatabaseEntry();
            while (i <= 10)
            {
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(i.ToString()));
                queueDB.Append(data, txn);
                if (i == 5)
                {
                    getData = data;
                }
                i++;
            }

            KeyValuePair<uint, DatabaseEntry> pair = queueDB.Consume(false, txn);

            queueDB.Close();
            txn.Commit();
            env.Close();

            Database db = Database.Open(queueDBFileName,
                new QueueDatabaseConfig());
            try
            {
                DatabaseEntry key =
                    new DatabaseEntry(BitConverter.GetBytes(pair.Key));
                db.Get(key);
            }
            catch (NotFoundException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                db.Close();
            }
        }
        public void TestPutMultiple()
        {
            testName = "TestPutMultiple";
            SetUpTest(true);

            QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.ExtentSize = 1024;
            dbConfig.Length = 520;
            dbConfig.PadByte = 0;
            QueueDatabase db = QueueDatabase.Open(testHome + "/" +
                testName + ".db", dbConfig);

            List<uint> kList = new List<uint>();
            List<DatabaseEntry> vList = new List<DatabaseEntry>();
            DatabaseEntry key, data;
            for (uint i = 1; i <= 9;i++) {
                key = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" + i +
                    Configuration.RandomString(512)));
                kList.Add(i);
                vList.Add(data);
            }

            // Create bulk buffer for recno based keys.
            MultipleDatabaseEntry kBuff =
                new MultipleDatabaseEntry(kList);
            Assert.IsTrue(kBuff.Recno);
            int val = 0;
            foreach (DatabaseEntry dbt in kBuff) {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Create bulk buffer for data.
            MultipleDatabaseEntry vBuff =
                new MultipleDatabaseEntry(vList, false);

            /*
             * Create recno bulk buffer from another recno bulk
             * buffer.
             */
            MultipleDatabaseEntry kBuff1 =
                new MultipleDatabaseEntry(kBuff.Data, kBuff.Recno);
            val = 0;
            foreach (DatabaseEntry dbt in kBuff1) {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Bulk insert to database with key and value buffers.
            db.Put(kBuff, vBuff);
            Cursor cursor = db.Cursor();
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
            val = 0;
            while (cursor.MoveNext()) {
                pair = cursor.Current;
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    pair.Key.Data);
                for (int i = 0; i < 520; i++) {
                    if (i < vList[val].Data.Length)
                        Assert.AreEqual(vList[val].Data[i],
                            pair.Value.Data[i]);
                    else
                        // The pad byte is 0.
                        Assert.AreEqual(0, pair.Value.Data[i]);
                }
                Assert.IsFalse(cursor.MoveNextDuplicate());
                val++;
            }
            Assert.AreEqual(9, val);

            cursor.Close();
            db.Close();
        }
Beispiel #23
0
 /// <summary>
 /// Instantiate a new QueueDatabase object and open the database
 /// represented by <paramref name="Filename"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If <paramref name="Filename"/> is 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.
 /// </para>
 /// <para>
 /// If <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.
 /// </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="cfg">The database's configuration</param>
 /// <returns>A new, open database object</returns>
 public static QueueDatabase Open(
     string Filename, QueueDatabaseConfig cfg)
 {
     return(Open(Filename, cfg, null));
 }
        public void TestStats()
        {
            testName = "TestStats";
            SetUpTest(true);
            string dbFileName = testHome + "/" +
                testName + ".db";

            QueueDatabaseConfig dbConfig =
                new QueueDatabaseConfig();
            ConfigCase1(dbConfig);
            QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig);

            QueueStats stats = db.Stats();
            ConfirmStatsPart1Case1(stats);
            db.Msgfile = testHome + "/" + testName+ ".log";
            db.PrintFastStats(true);

            // Put 500 records into the database.
            PutRecordCase1(db, null);

            stats = db.Stats();
            ConfirmStatsPart2Case1(stats);
            db.PrintFastStats();

            db.Close();
        }
Beispiel #25
0
        public void TestStats()
        {
            testName = "TestStats";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testHome + "/" +
                testName + ".db";
            Configuration.ClearDir(testHome);

            QueueDatabaseConfig dbConfig =
                new QueueDatabaseConfig();
            ConfigCase1(dbConfig);
            QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig);

            QueueStats stats = db.Stats();
            ConfirmStatsPart1Case1(stats);
            db.PrintFastStats(true);

            // Put 500 records into the database.
            PutRecordCase1(db, null);

            stats = db.Stats();
            ConfirmStatsPart2Case1(stats);
            db.PrintFastStats();

            db.Close();
        }
        public void TestConfig()
        {
            testName = "TestConfig";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            XmlElement xmlElem = Configuration.TestSetUp(
                testFixtureName, testName);

            // Open a primary btree database.
            QueueDatabaseConfig queueDBConfig =
                new QueueDatabaseConfig();
            queueDBConfig.Creation = CreatePolicy.IF_NEEDED;
            QueueDatabase queueDB = QueueDatabase.Open(
                dbFileName, queueDBConfig);

            SecondaryQueueDatabaseConfig secDBConfig =
                new SecondaryQueueDatabaseConfig(queueDB, null);

            Config(xmlElem, ref secDBConfig, true);
            Confirm(xmlElem, secDBConfig, true);

            // Close the primary btree database.
            queueDB.Close();
        }
        public void OpenSecQueueDBWithinTxn(string className, 
		    string funName, string home, string dbFileName, 
		    string dbSecFileName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

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

            // Open a primary queue database.
            Transaction openDBTxn = env.BeginTransaction();
            QueueDatabaseConfig dbConfig =
                new QueueDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            QueueDatabase db = QueueDatabase.Open(
                dbFileName, dbConfig, openDBTxn);
            openDBTxn.Commit();

            // Open a secondary queue database.
            Transaction openSecTxn = env.BeginTransaction();
            SecondaryQueueDatabaseConfig secDBConfig =
                new SecondaryQueueDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            SecondaryQueueDatabaseConfigTest.Config(xmlElem,
                ref secDBConfig, true);
            secDBConfig.Env = env;
            SecondaryQueueDatabase secDB;
            secDB = SecondaryQueueDatabase.Open(
                dbSecFileName, secDBConfig, openSecTxn);

            openSecTxn.Commit();

            // Confirm its flags configured in secDBConfig.
            Confirm(xmlElem, secDB, true);
            secDB.Close();

            // Open the existing secondary database.
            Transaction secTxn = env.BeginTransaction();
            SecondaryDatabaseConfig secConfig =
                new SecondaryDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secConfig.Env = env;

            SecondaryDatabase secExDB;
            secExDB = SecondaryQueueDatabase.Open(
                dbSecFileName, secConfig, secTxn);

            secExDB.Close();
            secTxn.Commit();

            db.Close();
            env.Close();
        }
Beispiel #28
0
        public void TestForeignKeyDelete(DatabaseType dbtype, ForeignKeyDeleteAction action)
        {
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";
            string fdbFileName = testHome + "/" + testName + "foreign.db";
            string sdbFileName = testHome + "/" + testName + "sec.db";

            Database primaryDB, fdb;
            SecondaryDatabase secDB;

            // Open primary database.
            if (dbtype == DatabaseType.BTREE) {
                BTreeDatabaseConfig btConfig = new BTreeDatabaseConfig();
                btConfig.Creation = CreatePolicy.ALWAYS;
                primaryDB = BTreeDatabase.Open(dbFileName, btConfig);
                fdb = BTreeDatabase.Open(fdbFileName, btConfig);
            } else if (dbtype == DatabaseType.HASH) {
                HashDatabaseConfig hConfig = new HashDatabaseConfig();
                hConfig.Creation = CreatePolicy.ALWAYS;
                primaryDB = HashDatabase.Open(dbFileName, hConfig);
                fdb = HashDatabase.Open(fdbFileName, hConfig);
            } else if (dbtype == DatabaseType.QUEUE) {
                QueueDatabaseConfig qConfig = new QueueDatabaseConfig();
                qConfig.Creation = CreatePolicy.ALWAYS;
                qConfig.Length = 4;
                primaryDB = QueueDatabase.Open(dbFileName, qConfig);
                fdb = QueueDatabase.Open(fdbFileName, qConfig);
            } else if (dbtype == DatabaseType.RECNO) {
                RecnoDatabaseConfig rConfig = new RecnoDatabaseConfig();
                rConfig.Creation = CreatePolicy.ALWAYS;
                primaryDB = RecnoDatabase.Open(dbFileName, rConfig);
                fdb = RecnoDatabase.Open(fdbFileName, rConfig);
            } else {
                throw new ArgumentException("Invalid DatabaseType");
            }

            // Open secondary database.
            if (dbtype == DatabaseType.BTREE) {
                SecondaryBTreeDatabaseConfig secbtConfig =
                    new SecondaryBTreeDatabaseConfig(primaryDB,
                    new SecondaryKeyGenDelegate(SecondaryKeyGen));
                secbtConfig.Creation = CreatePolicy.ALWAYS;
                secbtConfig.Duplicates = DuplicatesPolicy.SORTED;
                if (action == ForeignKeyDeleteAction.NULLIFY)
                    secbtConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify));
                else
                    secbtConfig.SetForeignKeyConstraint(fdb, action);
                secDB = SecondaryBTreeDatabase.Open(sdbFileName, secbtConfig);
            } else if (dbtype == DatabaseType.HASH) {
                SecondaryHashDatabaseConfig sechConfig =
                    new SecondaryHashDatabaseConfig(primaryDB,
                    new SecondaryKeyGenDelegate(SecondaryKeyGen));
                sechConfig.Creation = CreatePolicy.ALWAYS;
                sechConfig.Duplicates = DuplicatesPolicy.SORTED;
                if (action == ForeignKeyDeleteAction.NULLIFY)
                    sechConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify));
                else
                    sechConfig.SetForeignKeyConstraint(fdb, action);
                secDB = SecondaryHashDatabase.Open(sdbFileName, sechConfig);
            } else if (dbtype == DatabaseType.QUEUE) {
                SecondaryQueueDatabaseConfig secqConfig =
                    new SecondaryQueueDatabaseConfig(primaryDB,
                    new SecondaryKeyGenDelegate(SecondaryKeyGen));
                secqConfig.Creation = CreatePolicy.ALWAYS;
                secqConfig.Length = 4;
                if (action == ForeignKeyDeleteAction.NULLIFY)
                    secqConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify));
                else
                    secqConfig.SetForeignKeyConstraint(fdb, action);
                secDB = SecondaryQueueDatabase.Open(sdbFileName, secqConfig);
            } else if (dbtype == DatabaseType.RECNO) {
                SecondaryRecnoDatabaseConfig secrConfig =
                    new SecondaryRecnoDatabaseConfig(primaryDB,
                    new SecondaryKeyGenDelegate(SecondaryKeyGen));
                secrConfig.Creation = CreatePolicy.ALWAYS;
                if (action == ForeignKeyDeleteAction.NULLIFY)
                    secrConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify));
                else
                    secrConfig.SetForeignKeyConstraint(fdb, action);
                secDB = SecondaryRecnoDatabase.Open(sdbFileName, secrConfig);
            } else {
                throw new ArgumentException("Invalid DatabaseType");
            }

            /* Use integer keys for Queue/Recno support. */
            fdb.Put(new DatabaseEntry(BitConverter.GetBytes(100)),
                new DatabaseEntry(BitConverter.GetBytes(1001)));
            fdb.Put(new DatabaseEntry(BitConverter.GetBytes(200)),
                new DatabaseEntry(BitConverter.GetBytes(2002)));
            fdb.Put(new DatabaseEntry(BitConverter.GetBytes(300)),
                new DatabaseEntry(BitConverter.GetBytes(3003)));

            primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(1)),
                new DatabaseEntry(BitConverter.GetBytes(100)));
            primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(2)),
                new DatabaseEntry(BitConverter.GetBytes(200)));
            if (dbtype == DatabaseType.BTREE || dbtype == DatabaseType.HASH)
                primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(3)),
                    new DatabaseEntry(BitConverter.GetBytes(100)));

            try {
                fdb.Delete(new DatabaseEntry(BitConverter.GetBytes(100)));
            } catch (ForeignConflictException) {
                Assert.AreEqual(action, ForeignKeyDeleteAction.ABORT);
            }
            if (action == ForeignKeyDeleteAction.ABORT) {
                Assert.IsTrue(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
                Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1))));
                Assert.IsTrue(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
            } else if (action == ForeignKeyDeleteAction.CASCADE) {
                try {
                    Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
                } catch (KeyEmptyException) {
                    Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO);
                }
                try {
                    Assert.IsFalse(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1))));
                } catch (KeyEmptyException) {
                    Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO);
                }
                try {
                    Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
                } catch (KeyEmptyException) {
                    Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO);
                }
            } else if (action == ForeignKeyDeleteAction.NULLIFY) {
                try {
                    Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
                } catch (KeyEmptyException) {
                    Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO);
                }
                Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1))));
                try {
                    Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100))));
                } catch (KeyEmptyException) {
                    Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO);
                }
            }

            // Close secondary database.
            secDB.Close();

            // Close primary database.
            primaryDB.Close();

            // Close foreign database
            fdb.Close();
        }
        public void TestKeyEmptyException()
        {
            testName = "TestKeyEmptyException";
            SetUpTest(true);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            QueueDatabase db;
            try
            {
                Transaction openTxn = env.BeginTransaction();
                try
                {
                    QueueDatabaseConfig queueConfig =
                        new QueueDatabaseConfig();
                    queueConfig.Creation = CreatePolicy.IF_NEEDED;
                    queueConfig.Length = 10;
                    queueConfig.Env = env;
                    db = QueueDatabase.Open(testName + ".db",
                        queueConfig, openTxn);
                    openTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    openTxn.Abort();
                    throw e;
                }

                Transaction cursorTxn = env.BeginTransaction();
                Cursor cursor;
                try
                {
                    /*
                     * Put a record into queue database with
                     * cursor and abort the operation.
                     */
                    cursor = db.Cursor(cursorTxn);
                    KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
                    pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(
                        new DatabaseEntry(BitConverter.GetBytes((int)10)),
                        new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
                    cursor.Add(pair);
                    cursor.Close();
                    cursorTxn.Abort();
                }
                catch (DatabaseException e)
                {
                    cursorTxn.Abort();
                    db.Close();
                    throw e;
                }

                Transaction delTxn = env.BeginTransaction();
                try
                {
                    /*
                     * The put operation is aborted in the queue
                     * database so querying if the record still exists
                     * throws KeyEmptyException.
                     */
                    db.Exists(new DatabaseEntry(
                        BitConverter.GetBytes((int)10)), delTxn);
                    delTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    delTxn.Abort();
                    throw e;
                }
                finally
                {
                    db.Close();
                }
            }
            catch (KeyEmptyException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                env.Close();
            }
        }
        public void TestDeleteMultiple()
        {
            testName = "TestDeleteMultiple";
            SetUpTest(true);

            QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.ExtentSize = 1024;
            dbConfig.Length = 270;
            QueueDatabase db = QueueDatabase.Open(testHome + "/" +
                testName + ".db", dbConfig);

            List<uint> rList = new List<uint>();
            List<DatabaseEntry> kList = new List<DatabaseEntry>();
            List<DatabaseEntry> vList = new List<DatabaseEntry>();
            DatabaseEntry key, data;

            for (uint i = 1; i <= 100; i++) {
                key = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(
                    "data" + i.ToString() +
                    Configuration.RandomString(256)));
                rList.Add(i);
                kList.Add(key);
                vList.Add(data);
                db.Put(key, data);
            }

            // Bulk delete all records with recno in rList.
            db.Delete(new MultipleDatabaseEntry(rList));
            Cursor cursor = db.Cursor();
            Assert.IsFalse(cursor.MoveFirst());

            /*
             * Bulk insert records whose key bulk buffer is
             * constructed by recno lists, then delete all.
             */
            db.Put(new MultipleDatabaseEntry(rList),
                new MultipleDatabaseEntry(vList, false));
            Assert.IsTrue(cursor.MoveFirst());
            db.Delete(new MultipleDatabaseEntry(kList, true));
            Assert.IsFalse(cursor.MoveFirst());

            /*
             * Bulk insert records whose key bulk buffer is
             * constructed by DatabaseEntry lists, then delete all.
             */
            db.Put(new MultipleDatabaseEntry(kList, true),
                new MultipleDatabaseEntry(vList, false));
            Assert.IsTrue(cursor.MoveFirst());
            db.Delete(new MultipleDatabaseEntry(kList, true));
            Assert.IsFalse(cursor.MoveFirst());

            cursor.Close();
            db.Close();
        }
 /// <summary>
 /// Instantiate a new QueueDatabase object and open the database
 /// represented by <paramref name="Filename"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If <paramref name="Filename"/> is 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.
 /// </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="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 QueueDatabase Open(
     string Filename, QueueDatabaseConfig cfg, Transaction txn)
 {
     QueueDatabase ret = new QueueDatabase(cfg.Env, 0);
     ret.Config(cfg);
     ret.db.open(Transaction.getDB_TXN(txn),
         Filename, null, DBTYPE.DB_QUEUE, cfg.openFlags, 0);
     ret.isOpen = true;
     return ret;
 }
        private void DeleteMultipleAndMultipleKey(string dbFileName,
		    string dbName, DatabaseType type, bool mulKey)
        {
            List<DatabaseEntry> kList = new List<DatabaseEntry>();
            List<uint> rList = new List<uint>();
            List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                new List<KeyValuePair<DatabaseEntry, DatabaseEntry>>();
            DatabaseEntry key;
            Database db;
            SecondaryDatabase secDb;

            Configuration.ClearDir(testHome);

            if (type == DatabaseType.BTREE) {
                BTreeDatabaseConfig dbConfig =
                    new BTreeDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = BTreeDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryBTreeDatabaseConfig secDbConfig =
                    new SecondaryBTreeDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryBTreeDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else if (type == DatabaseType.HASH) {
                HashDatabaseConfig dbConfig =
                    new HashDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = HashDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryHashDatabaseConfig secDbConfig =
                    new SecondaryHashDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryHashDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else if (type == DatabaseType.QUEUE) {
                QueueDatabaseConfig dbConfig =
                    new QueueDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                dbConfig.Length = 4;
                db = QueueDatabase.Open(dbFileName, dbConfig);
                SecondaryQueueDatabaseConfig secDbConfig =
                    new SecondaryQueueDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Length = 4;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryQueueDatabase.Open(
                    dbFileName + "_sec", secDbConfig);
            } else if (type == DatabaseType.RECNO) {
                RecnoDatabaseConfig dbConfig =
                    new RecnoDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = RecnoDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryRecnoDatabaseConfig secDbConfig =
                    new SecondaryRecnoDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryRecnoDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else
                throw new TestException();

            for (uint i = 1; i <= 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                if (i >= 50 && i < 60)
                    kList.Add(key);
                else if (i > 80)
                    pList.Add(new KeyValuePair<
                        DatabaseEntry, DatabaseEntry>(
                        key, key));
                else if (type == DatabaseType.QUEUE
                    || type == DatabaseType.RECNO)
                    rList.Add(i);

                db.Put(key, key);
            }

            if (mulKey) {
                // Create bulk buffer for key/value pairs.
                MultipleKeyDatabaseEntry pBuff;
                if (type == DatabaseType.BTREE)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                else if (type == DatabaseType.HASH)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                else if (type == DatabaseType.QUEUE)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);
                else
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);

                // Bulk delete with the key/value pair bulk buffer.
                secDb.Delete(pBuff);
                foreach (KeyValuePair<DatabaseEntry,
                    DatabaseEntry>pair in pList) {
                    try {
                        db.GetBoth(pair.Key, pair.Value);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE)
                            throw e1;
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH ||
                            type == DatabaseType.RECNO)
                            throw e2;
                    }
                }

                /*
                 * Dump the database to verify that 80 records
                 * remain after bulk delete.
                 */
                Assert.AreEqual(80, db.Truncate());
            } else {
                // Create bulk buffer for key.
                MultipleDatabaseEntry kBuff;
                if (type == DatabaseType.BTREE)
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                else if (type == DatabaseType.HASH)
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                else if (type == DatabaseType.QUEUE)
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);
                else
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);

                /*
                 * Bulk delete in secondary database with key
                 * buffer. Primary records that the deleted
                 * records in secondar database should be
                 * deleted as well.
                 */
                secDb.Delete(kBuff);
                foreach (DatabaseEntry dbt in kList) {
                    try {
                        db.Get(dbt);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE ||
                            type == DatabaseType.RECNO)
                            throw e1;
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH)
                            throw e2;
                    }
                }

                /*
                 * Bulk delete in secondary database with recno
                 * based key buffer.
                 */
                if (type == DatabaseType.QUEUE ||
                    type == DatabaseType.RECNO) {
                    MultipleDatabaseEntry rBuff =
                        new MultipleDatabaseEntry(rList);
                    secDb.Delete(rBuff);
                    Assert.AreEqual(20, db.Truncate());
                }
            }

            secDb.Close();
            db.Close();
        }
        public void TestDeleteMultipleKey()
        {
            testName = "TestDeleteMultipleKey";
            SetUpTest(true);

            QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.ExtentSize = 1024;
            dbConfig.Length = 520;
            QueueDatabase db = QueueDatabase.Open(testHome + "/" +
                testName + ".db", dbConfig);

            List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                new List<KeyValuePair<DatabaseEntry, DatabaseEntry>>();
            DatabaseEntry key, data;

            for (uint i = 1; i <= 100; i++)
            {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(
                    "data" + i.ToString() +
                    Configuration.RandomString(512)));
                pList.Add(new KeyValuePair<
                    DatabaseEntry, DatabaseEntry>(key, data));
                db.Put(key, data);
            }

            // Create key/value pair bulk buffer and delete all.
            db.Delete(new MultipleKeyDatabaseEntry(pList, true));
            // Verify that the database is empty.
            Assert.AreEqual(0, db.Truncate());
            db.Close();
        }