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

            XmlElement xmlElem = Configuration.TestSetUp(
                testFixtureName, testName);
            // Open a primary btree database.
            HashDatabaseConfig hashDBConfig =
                new HashDatabaseConfig();

            hashDBConfig.Creation = CreatePolicy.IF_NEEDED;
            HashDatabase hashDB = HashDatabase.Open(
                dbFileName, hashDBConfig);

            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(hashDB, null);

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

            // Close the primary btree database.
            hashDB.Close();
        }
        public void TestHashComparison()
        {
            testName = "TestHashComparison";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation       = CreatePolicy.IF_NEEDED;
            dbConfig.HashComparison = new EntryComparisonDelegate(EntryComparison);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);
            int          ret;

            /*
             * Comparison gets the value that lowest byte of the
             * former dbt minus that of the latter one.
             */
            ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(2)),
                             new DatabaseEntry(BitConverter.GetBytes(2)));
            Assert.AreEqual(0, ret);

            ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(256)),
                             new DatabaseEntry(BitConverter.GetBytes(1)));
            Assert.Greater(0, ret);

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

            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            hashConfig.Creation   = CreatePolicy.ALWAYS;
            hashConfig.Duplicates = DuplicatesPolicy.SORTED;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            // Put the same record into db twice.
            DatabaseEntry key, data;

            key  = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            try
            {
                hashDB.PutNoDuplicate(key, data);
                hashDB.PutNoDuplicate(key, data);
            }
            catch (KeyExistException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                hashDB.Close();
            }
        }
        public void TestPutNoDuplicateWithUnsortedDuplicate()
        {
            testName = "TestPutNoDuplicateWithUnsortedDuplicate";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            HashDatabaseConfig hashConfig = new HashDatabaseConfig();

            hashConfig.Creation    = CreatePolicy.ALWAYS;
            hashConfig.Duplicates  = DuplicatesPolicy.UNSORTED;
            hashConfig.ErrorPrefix = testName;

            HashDatabase  hashDB = HashDatabase.Open(dbFileName, hashConfig);
            DatabaseEntry key, data;

            key  = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1"));

            try
            {
                hashDB.PutNoDuplicate(key, data);
            }
            catch (DatabaseException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                hashDB.Close();
            }
        }
示例#5
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            //
            // close the index database

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

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            ConfigCase1(dbConfig);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);

            HashStats stats     = db.Stats();
            HashStats fastStats = db.FastStats();

            ConfirmStatsPart1Case1(stats);
            ConfirmStatsPart1Case1(fastStats);

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

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

            // Delete some data to get some free pages.
            byte[] bigArray = new byte[262144];
            db.Delete(new DatabaseEntry(bigArray));
            stats = db.Stats();
            ConfirmStatsPart3Case1(stats);

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

            HashDatabaseConfig hashConfig =
                new HashDatabaseConfig();

            hashConfig.Creation   = CreatePolicy.ALWAYS;
            hashConfig.Duplicates = DuplicatesPolicy.SORTED;
            hashConfig.TableSize  = 20;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            DatabaseEntry key, data;

            for (int i = 1; i <= 10; i++)
            {
                key  = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(BitConverter.GetBytes(i));
                hashDB.PutNoDuplicate(key, data);
            }

            Assert.IsTrue(hashDB.Exists(
                              new DatabaseEntry(BitConverter.GetBytes((int)5))));

            hashDB.Close();
        }
示例#8
0
        public void TestDuplicates()
        {
            testName = "TestDuplicates";
            SetUpTest(true);
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName
                                   + "_sec.db";

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig);

            // Open a secondary hash database.
            SecondaryHashDatabaseConfig secConfig =
                new SecondaryHashDatabaseConfig(null, null);

            secConfig.Primary    = db;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secConfig.Creation   = CreatePolicy.IF_NEEDED;
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(
                    dbSecFileName, secConfig);

            // Confirm the duplicate in opened secondary database.
            Assert.AreEqual(DuplicatesPolicy.SORTED,
                            secDB.Duplicates);

            secDB.Close();
            db.Close();
        }
 private void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     db?.Close(true);
     db?.Dispose();
 }
        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.
            HashDatabaseConfig DBConfig =
                new HashDatabaseConfig();

            DBConfig.Env      = env;
            DBConfig.Creation = CreatePolicy.IF_NEEDED;

            string       DBFileName = testName + ".db";
            HashDatabase db         = HashDatabase.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();
        }
        public void TestOpenNewHashDB()
        {
            testName = "TestOpenNewHashDB";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

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

            HashDatabaseConfigTest.Config(xmlElem, ref hashConfig, true);
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            Confirm(xmlElem, hashDB, true);
            hashDB.Close();
        }
示例#12
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            //
            // write the current value for the auto-incrementing index integer to file

            using (var sw = new BinaryWriter(File.Open(m_fileNextValue, FileMode.Create, FileAccess.Write, FileShare.None))) {
                sw.Write(m_next);
            }

            //
            // close the BerkeleyDB databases

            m_dbStr2Long.Close(true);
            m_dbLong2Str.Close(true);
        }
示例#13
0
        public void TestPutNoDuplicateWithTxn()
        {
            testName = "TestPutNoDuplicateWithTxn";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

            // Open a hash database within a transaction.
            Transaction        txn      = env.BeginTransaction();
            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.IF_NEEDED;
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            dbConfig.Env        = env;
            HashDatabase db = HashDatabase.Open(testName + ".db", dbConfig, txn);

            DatabaseEntry dbt = new DatabaseEntry(BitConverter.GetBytes((int)100));

            db.PutNoDuplicate(dbt, dbt, txn);
            try
            {
                db.PutNoDuplicate(dbt, dbt, txn);
            }
            catch (KeyExistException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                // Close all.
                db.Close();
                txn.Commit();
                env.Close();
            }
        }
        public void TestHashFunction()
        {
            testName = "TestHashFunction";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" +
                                   testName + "_sec.db";

            Configuration.ClearDir(testHome);

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            HashDatabase hashDB = HashDatabase.Open(
                dbFileName, dbConfig);

            /*
             * Define hash function and open a secondary
             * hash database.
             */
            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(hashDB, null);

            secDBConfig.HashFunction =
                new HashFunctionDelegate(HashFunction);
            secDBConfig.Creation = CreatePolicy.IF_NEEDED;
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(dbSecFileName,
                                           secDBConfig);

            /*
             * Confirm the hash function defined in the configuration.
             * Call the hash function and the one from secondary
             * database. If they return the same value, then the hash
             * function is configured successfully.
             */
            uint data = secDB.HashFunction(BitConverter.GetBytes(1));

            Assert.AreEqual(0, data);

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

            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            dbConfig.Creation     = CreatePolicy.IF_NEEDED;
            dbConfig.HashFunction = new HashFunctionDelegate(HashFunction);
            HashDatabase db = HashDatabase.Open(dbFileName, dbConfig);

            // Hash function will change the lowest byte to 0;
            uint data = db.HashFunction(BitConverter.GetBytes(1));

            Assert.AreEqual(0, data);
            db.Close();
        }
        public void TestCompare()
        {
            testName = "TestCompare";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName +
                                   "_sec.db";

            Configuration.ClearDir(testHome);

            // Open a primary hash database.
            HashDatabaseConfig dbConfig =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig);

            // Open a secondary hash database.
            SecondaryHashDatabaseConfig secConfig =
                new SecondaryHashDatabaseConfig(null, null);

            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Primary  = db;
            secConfig.Compare  =
                new EntryComparisonDelegate(SecondaryEntryComparison);
            SecondaryHashDatabase secDB =
                SecondaryHashDatabase.Open(dbSecFileName, secConfig);

            /*
             * Get the compare function set in the configuration
             * and run it in a comparison to see if it is alright.
             */
            DatabaseEntry dbt1, dbt2;

            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((int)257));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((int)255));
            Assert.Less(0, secDB.Compare(dbt1, dbt2));

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

            HashDatabaseConfig hashConfig =
                new HashDatabaseConfig();

            hashConfig.Creation = CreatePolicy.ALWAYS;
            HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig);

            hashDB.Close();

            DatabaseConfig dbConfig = new DatabaseConfig();
            Database       db       = Database.Open(dbFileName, dbConfig);

            Assert.AreEqual(db.Type, DatabaseType.HASH);
            db.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();
            HashDatabaseConfig dbConfig = new HashDatabaseConfig();

            ConfigCase1(dbConfig);
            dbConfig.Env = env;
            HashDatabase db = HashDatabase.Open(name + ".db", dbConfig, openTxn);

            openTxn.Commit();

            Transaction statsTxn = env.BeginTransaction();
            HashStats   stats;
            HashStats   fastStats;

            if (ifIsolation == false)
            {
                stats     = db.Stats(statsTxn);
                fastStats = db.Stats(statsTxn);
            }
            else
            {
                stats     = db.Stats(statsTxn, Isolation.DEGREE_ONE);
                fastStats = db.Stats(statsTxn, Isolation.DEGREE_ONE);
            }

            ConfirmStatsPart1Case1(stats);

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

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

            // Delete some data to get some free pages.
            byte[] bigArray = new byte[262144];
            db.Delete(new DatabaseEntry(bigArray), statsTxn);
            if (ifIsolation == false)
            {
                stats = db.Stats(statsTxn);
            }
            else
            {
                stats = db.Stats(statsTxn, Isolation.DEGREE_THREE);
            }
            ConfirmStatsPart3Case1(stats);

            statsTxn.Commit();
            db.Close();
            env.Close();
        }
示例#19
0
        public void OpenSecHashDBWithinTxn(string className,
                                           string funName, string home, string dbFileName,
                                           string dbSecFileName, bool ifDbName)
        {
            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 hash database.
            Transaction        openDBTxn = env.BeginTransaction();
            HashDatabaseConfig dbConfig  =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig, openDBTxn);

            openDBTxn.Commit();

            // Open a secondary hash database.
            Transaction openSecTxn = env.BeginTransaction();
            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(db,
                                                new SecondaryKeyGenDelegate(SecondaryKeyGen));

            SecondaryHashDatabaseConfigTest.Config(xmlElem,
                                                   ref secDBConfig, false);
            secDBConfig.HashFunction = null;
            secDBConfig.Env          = env;
            SecondaryHashDatabase secDB;

            if (ifDbName == false)
            {
                secDB = SecondaryHashDatabase.Open(
                    dbSecFileName, secDBConfig, openSecTxn);
            }
            else
            {
                secDB = SecondaryHashDatabase.Open(
                    dbSecFileName, "secondary", 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;

            if (ifDbName == false)
            {
                secExDB = SecondaryHashDatabase.Open(
                    dbSecFileName, secConfig, secTxn);
            }
            else
            {
                secExDB = SecondaryHashDatabase.Open(
                    dbSecFileName, "secondary", secConfig,
                    secTxn);
            }
            secExDB.Close();
            secTxn.Commit();

            db.Close();
            env.Close();
        }
        /*
         * Test the external file database with or without environment.
         * 1. Config and open the environment;
         * 2. Verify the environment external file configs;
         * 3. Config and open the database;
         * 4. Verify the database external file configs;
         * 5. Insert and verify some external file data by database methods;
         * 6. Insert some external file data by cursor, update it and verify
         * the update by database stream and cursor;
         * 7. Verify the stats;
         * 8. Close all handles.
         * If "blobdbt" is true, set the data DatabaseEntry.ExternalFile as
         * true, otherwise make the data DatabaseEntry reach the external file
         * threshold in size.
         */
        void TestBlobHashDatabase(uint env_threshold, string env_blobdir,
                                  uint db_threshold, string db_blobdir, bool blobdbt)
        {
            if (env_threshold == 0 && db_threshold == 0)
            {
                return;
            }

            string hashDBName =
                testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);
            HashDatabaseConfig cfg = new HashDatabaseConfig();

            cfg.Creation = CreatePolicy.ALWAYS;
            string blrootdir = "__db_bl";

            // Open the environment and verify the external file config.
            if (env_threshold > 0)
            {
                DatabaseEnvironmentConfig envConfig =
                    new DatabaseEnvironmentConfig();
                envConfig.AutoCommit            = true;
                envConfig.Create                = true;
                envConfig.UseMPool              = true;
                envConfig.UseLogging            = true;
                envConfig.UseTxns               = true;
                envConfig.UseLocking            = true;
                envConfig.ExternalFileThreshold = env_threshold;
                if (env_blobdir != null)
                {
                    envConfig.ExternalFileDir = env_blobdir;
                    blrootdir = env_blobdir;
                }
                DatabaseEnvironment env = DatabaseEnvironment.Open(
                    testHome, envConfig);
                if (env_blobdir == null)
                {
                    Assert.IsNull(env.ExternalFileDir);
                }
                else
                {
                    Assert.AreEqual(0,
                                    env.ExternalFileDir.CompareTo(env_blobdir));
                }
                Assert.AreEqual(env_threshold,
                                env.ExternalFileThreshold);
                cfg.Env    = env;
                hashDBName = testName + ".db";
            }

            // Open the database and verify the external file config.
            if (db_threshold > 0)
            {
                cfg.ExternalFileThreshold = db_threshold;
            }
            if (db_blobdir != null)
            {
                cfg.ExternalFileDir = db_blobdir;

                /*
                 * The external file directory setting in the database
                 * is effective only when it is opened without
                 * an environment.
                 */
                if (cfg.Env == null)
                {
                    blrootdir = db_blobdir;
                }
            }

            HashDatabase db = HashDatabase.Open(hashDBName, cfg);

            Assert.AreEqual(
                db_threshold > 0 ? db_threshold : env_threshold,
                db.ExternalFileThreshold);
            if (db_blobdir == null && cfg.Env == null)
            {
                Assert.IsNull(db.ExternalFileDir);
            }
            else
            {
                Assert.AreEqual(0,
                                db.ExternalFileDir.CompareTo(blrootdir));
            }

            // Insert and verify some external file data by database
            // methods.
            string[]      records = { "a", "b", "c", "d", "e", "f", "g", "h",
                                      "i",      "j", "k", "l", "m", "n", "o", "p","q","r", "s",
                                      "t",      "u", "v", "w", "x", "y", "z" };
            DatabaseEntry kdbt = new DatabaseEntry();
            DatabaseEntry ddbt = new DatabaseEntry();

            byte[] kdata, ddata;
            string str;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            ddbt.ExternalFile = blobdbt;
            Assert.AreEqual(blobdbt, ddbt.ExternalFile);
            for (int i = 0; i < records.Length; i++)
            {
                kdata = BitConverter.GetBytes(i);
                str   = records[i];
                if (!blobdbt)
                {
                    for (int j = 0; j < db_threshold; j++)
                    {
                        str = str + records[i];
                    }
                }
                ddata     = Encoding.ASCII.GetBytes(str);
                kdbt.Data = kdata;
                ddbt.Data = ddata;
                db.Put(kdbt, ddbt);
                try
                {
                    pair = db.Get(kdbt);
                }
                catch (DatabaseException)
                {
                    db.Close();
                    if (cfg.Env != null)
                    {
                        cfg.Env.Close();
                    }
                    throw new TestException();
                }
                Assert.AreEqual(ddata, pair.Value.Data);
            }

            /*
             * Insert some external file data by cursor, update it and
             * verify the update by database stream.
             */
            kdata             = BitConverter.GetBytes(records.Length);
            ddata             = Encoding.ASCII.GetBytes("abc");
            kdbt.Data         = kdata;
            ddbt.Data         = ddata;
            ddbt.ExternalFile = true;
            Assert.IsTrue(ddbt.ExternalFile);
            pair =
                new KeyValuePair <DatabaseEntry, DatabaseEntry>(kdbt, ddbt);
            CursorConfig dbcConfig = new CursorConfig();
            Transaction  txn       = null;

            if (cfg.Env != null)
            {
                txn = cfg.Env.BeginTransaction();
            }
            HashCursor cursor = db.Cursor(dbcConfig, txn);

            cursor.Add(pair);
            DatabaseStreamConfig dbsc = new DatabaseStreamConfig();

            dbsc.SyncPerWrite = true;
            DatabaseStream dbs = cursor.DbStream(dbsc);

            Assert.AreNotEqual(null, dbs);
            Assert.IsFalse(dbs.GetConfig.ReadOnly);
            Assert.IsTrue(dbs.GetConfig.SyncPerWrite);
            Assert.AreEqual(3, dbs.Size());
            DatabaseEntry sdbt = dbs.Read(0, 3);

            Assert.IsNotNull(sdbt);
            Assert.AreEqual(ddata, sdbt.Data);
            sdbt = new DatabaseEntry(Encoding.ASCII.GetBytes("defg"));
            Assert.IsTrue(dbs.Write(sdbt, 3));
            Assert.AreEqual(7, dbs.Size());
            sdbt = dbs.Read(0, 7);
            Assert.IsNotNull(sdbt);
            Assert.AreEqual(Encoding.ASCII.GetBytes("abcdefg"), sdbt.Data);
            dbs.Close();

            /*
             * Verify the database stream can not write when it is
             * configured to be read-only.
             */
            dbsc.ReadOnly = true;
            dbs           = cursor.DbStream(dbsc);
            Assert.IsTrue(dbs.GetConfig.ReadOnly);
            try
            {
                Assert.IsFalse(dbs.Write(sdbt, 7));
                throw new TestException();
            }
            catch (DatabaseException)
            {
            }
            dbs.Close();

            // Verify the update by cursor.
            Assert.IsTrue(cursor.Move(kdbt, true));
            pair = cursor.Current;
            Assert.AreEqual(Encoding.ASCII.GetBytes("abcdefg"),
                            pair.Value.Data);
            cursor.Close();
            if (cfg.Env != null)
            {
                txn.Commit();
            }

            /*
             * Verify the external files are created in the expected
             * location.
             * This part of test is disabled since BTreeDatabase.BlobSubDir
             * is not exposed to users.
             */
            //if (cfg.Env != null)
            //	blrootdir = testHome + "/" + blrootdir;
            //string blobdir = blrootdir + "/" + db.BlobSubDir;
            //Assert.AreEqual(records.Length + 1,
            //    Directory.GetFiles(blobdir, "__db.bl*").Length);
            //Assert.AreEqual(1,
            //    Directory.GetFiles(blobdir, "__db_blob_meta.db").Length);

            // Verify the stats.
            HashStats st = db.Stats();

            Assert.AreEqual(records.Length + 1, st.nExternalFiles);

            // Close all handles.
            db.Close();
            if (cfg.Env != null)
            {
                cfg.Env.Close();
            }

            /*
             * Remove the default external file directory
             * when it is not under the test home.
             */
            if (db_blobdir == null && cfg.Env == null)
            {
                Directory.Delete("__db_bl", true);
            }
        }