Пример #1
0
        /// <summary>
        /// Instantiate a new SecondaryHashDatabase object, open the
        /// database represented by <paramref name="Filename"/> and associate
        /// the database with the
        /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
        /// </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
        /// 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 SecondaryHashDatabase Open(
            string Filename, string DatabaseName,
            SecondaryHashDatabaseConfig cfg, Transaction txn)
        {
            SecondaryHashDatabase ret = new SecondaryHashDatabase(cfg.Env, 0);

            ret.Config(cfg);
            ret.db.open(Transaction.getDB_TXN(txn), Filename,
                        DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0);
            ret.isOpen = true;

            ret.doAssocRef =
                new BDB_AssociateDelegate(SecondaryDatabase.doAssociate);
            cfg.Primary.db.associate(Transaction.getDB_TXN(txn),
                                     ret.db, ret.doAssocRef, cfg.assocFlags);

            if (cfg.ForeignKeyDatabase != null)
            {
                if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY)
                {
                    ret.doNullifyRef =
                        new BDB_AssociateForeignDelegate(doNullify);
                }
                else
                {
                    ret.doNullifyRef = null;
                }
                cfg.ForeignKeyDatabase.db.associate_foreign(
                    ret.db, ret.doNullifyRef, cfg.foreignFlags);
            }
            return(ret);
        }
Пример #2
0
        private void Config(SecondaryHashDatabaseConfig cfg)
        {
            base.Config(cfg);
            db.set_flags(cfg.flags);

            if (cfg.HashFunction != null)
            {
                HashFunction = cfg.HashFunction;
            }
            if (cfg.DuplicateCompare != null)
            {
                DupCompare = cfg.DuplicateCompare;
            }
            if (cfg.fillFactorIsSet)
            {
                db.set_h_ffactor(cfg.FillFactor);
            }
            if (cfg.nelemIsSet)
            {
                db.set_h_nelem(cfg.TableSize);
            }
            if (cfg.Compare != null)
            {
                Compare = cfg.Compare;
            }
        }
Пример #3
0
 private void Config(SecondaryHashDatabaseConfig cfg) {
     base.Config(cfg);
     db.set_flags(cfg.flags);
     
     if (cfg.HashFunction != null)
         HashFunction = cfg.HashFunction;
     if (cfg.DuplicateCompare != null)
         DupCompare = cfg.DuplicateCompare;
     if (cfg.fillFactorIsSet)
         db.set_h_ffactor(cfg.FillFactor);
     if (cfg.nelemIsSet)
         db.set_h_nelem(cfg.TableSize);
     if (cfg.Compare != null)
         Compare = cfg.Compare;
 }
        public static void Confirm(XmlElement xmlElement,
		    SecondaryHashDatabaseConfig secHashDBConfig,
		    bool compulsory)
        {
            SecondaryDatabaseConfig secDBConfig =
                secHashDBConfig;
            SecondaryDatabaseConfigTest.Confirm(xmlElement,
                secDBConfig, compulsory);

            // Confirm secondary hash database specific configuration.
            Configuration.ConfirmCreatePolicy(xmlElement,
                "Creation", secHashDBConfig.Creation, compulsory);
            Configuration.ConfirmDuplicatesPolicy(xmlElement,
                "Duplicates", secHashDBConfig.Duplicates, compulsory);
            Configuration.ConfirmUint(xmlElement, "FillFactor",
                secHashDBConfig.FillFactor, compulsory);
            Configuration.ConfirmUint(xmlElement,
                "NumElements",
                secHashDBConfig.TableSize, compulsory);
        }
        public static void Config(XmlElement xmlElement,
		    ref SecondaryHashDatabaseConfig secHashDBConfig,
		    bool compulsory)
        {
            uint fillFactor = new uint();
            uint numElements = new uint();

            SecondaryDatabaseConfig secDBConfig = secHashDBConfig;
            SecondaryDatabaseConfigTest.Config(xmlElement,
                ref secDBConfig, compulsory);

            // Configure specific fields/properties of hash db
            Configuration.ConfigCreatePolicy(xmlElement,
                "Creation", ref secHashDBConfig.Creation, compulsory);
            Configuration.ConfigDuplicatesPolicy(xmlElement,
                "Duplicates", ref secHashDBConfig.Duplicates, compulsory);
            if (Configuration.ConfigUint(xmlElement, "FillFactor",
                 ref fillFactor, compulsory))
                secHashDBConfig.FillFactor = fillFactor;
            if (Configuration.ConfigUint(xmlElement, "NumElements",
                 ref numElements, compulsory))
                secHashDBConfig.TableSize = numElements;
        }
Пример #6
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();
        }
Пример #7
0
        /// <summary>
        /// Instantiate a new SecondaryHashDatabase object, open the
        /// database represented by <paramref name="Filename"/> and associate 
        /// the database with the
        /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
        /// </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 SecondaryHashDatabase Open(
            string Filename, string DatabaseName,
            SecondaryHashDatabaseConfig cfg, Transaction txn)
        {
            SecondaryHashDatabase ret = new SecondaryHashDatabase(cfg.Env, 0);
            ret.Config(cfg);
            ret.db.open(Transaction.getDB_TXN(txn), Filename,
                DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0);
            ret.isOpen = true;

            ret.doAssocRef =
                new BDB_AssociateDelegate(SecondaryDatabase.doAssociate);
            cfg.Primary.db.associate(Transaction.getDB_TXN(txn),
                ret.db, ret.doAssocRef, cfg.assocFlags);

            if (cfg.ForeignKeyDatabase != null) {
                if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY)
                    ret.doNullifyRef =
                        new BDB_AssociateForeignDelegate(doNullify);
                else
                    ret.doNullifyRef = null;
                cfg.ForeignKeyDatabase.db.associate_foreign(
                    ret.db, ret.doNullifyRef, cfg.foreignFlags);
            }
            return ret;
        }
Пример #8
0
 /// <summary>
 /// Instantiate a new SecondaryHashDatabase object, open the
 /// database represented by <paramref name="Filename"/> and associate 
 /// the database with the
 /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
 /// </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 SecondaryHashDatabase Open(string Filename,
     SecondaryHashDatabaseConfig cfg, Transaction txn)
 {
     return Open(Filename, null, cfg, txn);
 }
Пример #9
0
 /// <summary>
 /// Instantiate a new SecondaryHashDatabase object, open the
 /// database represented by <paramref name="Filename"/> and associate 
 /// the database with the
 /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
 /// </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 <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="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>
 /// <returns>A new, open database object</returns>
 public static SecondaryHashDatabase Open(string Filename,
     string DatabaseName, SecondaryHashDatabaseConfig cfg)
 {
     return Open(Filename, DatabaseName, cfg, null);
 }
        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();
        }
Пример #11
0
 /// <summary>
 /// Instantiate a new SecondaryHashDatabase object, open the
 /// database represented by <paramref name="Filename"/> and associate
 /// the database with the
 /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
 /// </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
 /// 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="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 SecondaryHashDatabase Open(string Filename,
                                          SecondaryHashDatabaseConfig cfg, Transaction txn)
 {
     return(Open(Filename, null, cfg, txn));
 }
        public void OpenSecHashDB(string className,
		    string funName, string dbFileName, string dbSecFileName,
		    bool ifDBName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

            // Open a primary recno database.
            HashDatabaseConfig primaryDBConfig =
                new HashDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            HashDatabase primaryDB;

            /*
             * If secondary database name is given, the primary
             * database is also opened with database name.
             */
            if (ifDBName == false)
                primaryDB = HashDatabase.Open(dbFileName,
                    primaryDBConfig);
            else
                primaryDB = HashDatabase.Open(dbFileName,
                    "primary", primaryDBConfig);

            try
            {
                // Open a new secondary database.
                SecondaryHashDatabaseConfig secHashDBConfig =
                    new SecondaryHashDatabaseConfig(
                    primaryDB, null);
                SecondaryHashDatabaseConfigTest.Config(
                    xmlElem, ref secHashDBConfig, false);
                secHashDBConfig.Creation =
                    CreatePolicy.IF_NEEDED;

                SecondaryHashDatabase secHashDB;
                if (ifDBName == false)
                    secHashDB = SecondaryHashDatabase.Open(
                        dbSecFileName, secHashDBConfig);
                else
                    secHashDB = SecondaryHashDatabase.Open(
                        dbSecFileName, "secondary",
                        secHashDBConfig);

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

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

                SecondaryDatabase secDB;
                if (ifDBName == false)
                    secDB = SecondaryHashDatabase.Open(
                        dbSecFileName, secDBConfig);
                else
                    secDB = SecondaryHashDatabase.Open(
                        dbSecFileName, "secondary", secDBConfig);

                // Close secondary database.
                secDB.Close();
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                // Close primary database.
                primaryDB.Close();
            }
        }
        public void TestHashFunction()
        {
            testName = "TestHashFunction";
            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.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 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();
        }
        public void TestCompare()
        {
            testName = "TestCompare";
            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.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 TestConfig()
        {
            testName = "TestConfig";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);
            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();
        }
        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();
        }
Пример #18
0
 /// <summary>
 /// Instantiate a new SecondaryHashDatabase object, open the
 /// database represented by <paramref name="Filename"/> and associate
 /// the database with the
 /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
 /// </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
 /// is implicitly transaction protected. Transactionally
 /// protected operations on a database object requires the object itself
 /// be transactionally protected during its open.
 /// </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="cfg">The database's configuration</param>
 /// <returns>A new, open database object</returns>
 public static SecondaryHashDatabase Open(
     string Filename, SecondaryHashDatabaseConfig cfg)
 {
     return(Open(Filename, null, cfg, null));
 }