Exemple #1
0
        private void Config(BTreeDatabaseConfig cfg) {
            base.Config(cfg);
            /* 
             * Database.Config calls set_flags, but that does not get the BTree
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);

            if (cfg.BlobDir != null && cfg.Env == null)
                db.set_blob_dir(cfg.BlobDir);

            if (cfg.blobThresholdIsSet)
                db.set_blob_threshold(cfg.BlobThreshold, 0);

            if (cfg.BTreeCompare != null)
                Compare = cfg.BTreeCompare;

            if (cfg.BTreePrefixCompare != null)
                PrefixCompare = cfg.BTreePrefixCompare;

            // The duplicate comparison function cannot change.
            if (cfg.DuplicateCompare != null)
                DupCompare = cfg.DuplicateCompare;

            if (cfg.minkeysIsSet)
                db.set_bt_minkey(cfg.MinKeysPerPage);
                
            if (cfg.compressionIsSet) {
                Compress = cfg.Compress;
                Decompress = cfg.Decompress;
                if (Compress == null)
                    doCompressRef = null;
                else
                    doCompressRef = new BDB_CompressDelegate(doCompress);
                if (Decompress == null)
                    doDecompressRef = null;
                else
                    doDecompressRef = new BDB_DecompressDelegate(doDecompress);
                db.set_bt_compress(doCompressRef, doDecompressRef);
            }

            if (cfg.partitionIsSet) {
                nparts = cfg.NParts;
                Partition = cfg.Partition;
                if (Partition == null)
                    doPartitionRef = null;
                else
                    doPartitionRef = new BDB_PartitionDelegate(doPartition);
                partitionKeys = cfg.PartitionKeys;
                IntPtr[] ptrs = null;
                if (partitionKeys != null) {
                    int size = (int)nparts - 1;
                    ptrs = new IntPtr[size];
                    for (int i = 0; i < size; i++)
                        ptrs[i] = DBT.getCPtr(
                            DatabaseEntry.getDBT(partitionKeys[i])).Handle;
                }
                db.set_partition(nparts, ptrs, doPartitionRef);
            }
        }
 public void ConfigCase1(BTreeDatabaseConfig dbConfig)
 {
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
     dbConfig.PageSize = 4096;
     dbConfig.MinKeysPerPage = 10;
 }
Exemple #3
0
        private void OpenBase()
        {
            var pathStr = Environment.GetEnvironmentVariable("PATH");

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


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

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

            _btreeDb = BTreeDatabase.Open(_dbPAth, _name, _btreeConfig);
        }
Exemple #4
0
        private void Config(BTreeDatabaseConfig cfg) {
            base.Config(cfg);
            /* 
             * Database.Config calls set_flags, but that doesn't get the BTree
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);
            
            if (cfg.BTreeCompare != null)
                Compare = cfg.BTreeCompare;

            if (cfg.BTreePrefixCompare != null)
                PrefixCompare = cfg.BTreePrefixCompare;

            // The duplicate comparison function cannot change.
            if (cfg.DuplicateCompare != null)
                DupCompare = cfg.DuplicateCompare;

            if (cfg.minkeysIsSet)
                db.set_bt_minkey(cfg.MinKeysPerPage);
                
            if (cfg.compressionIsSet) {
                Compress = cfg.Compress;
                Decompress = cfg.Decompress;
                if (Compress == null)
                    doCompressRef = null;
                else
                    doCompressRef = new BDB_CompressDelegate(doCompress);
                if (Decompress == null)
                    doDecompressRef = null;
                else
                    doDecompressRef = new BDB_DecompressDelegate(doDecompress);
                db.set_bt_compress(doCompressRef, doDecompressRef);
            }
        }
        public void GetCursorInBtreeDBUsingRecno(string home,
		    string name, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            string dbFileName = home + "/" + name + ".db";
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.UseRecordNumbers = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
        public override void Init(int flowCount, long flowRecordCount)
        {
            BTreeDatabaseConfig config = new BTreeDatabaseConfig();

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

            string fileName = Path.Combine(DataDirectory, string.Format("{0}.oracle", CollectionName));
            database = BTreeDatabase.Open(fileName, config);
        }
        public void GetSecondaryCursurWithTxn(string home,
		    string name, bool ifCfg)
        {
            string dbFileName = name + ".db";
            SecondaryCursor cursor;

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

            // Open primary/secondary database.
            Transaction txn = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db = BTreeDatabase.Open(dbFileName,
                dbConfig, txn);

            SecondaryBTreeDatabaseConfig secDBConfig = new
                SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secDBConfig.Env = env;
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(dbFileName,
                secDBConfig, txn);

            for (int i = 0; i < 10; i++)
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                    new DatabaseEntry(BitConverter.GetBytes((int)i)), txn);

            // Create secondary cursor.
            if (ifCfg == false)
                secDB.SecondaryCursor(txn);
            else if (ifCfg == true)
            {
                CursorConfig cursorConfig = new CursorConfig();
                cursorConfig.WriteCursor = false;
                cursor = secDB.SecondaryCursor(cursorConfig, txn);
                cursor.Close();
            }

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

            // Open a primary btree database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Creation = CreatePolicy.ALWAYS;
            BTreeDatabase btreeDB = BTreeDatabase.Open(
                dbFileName, btreeDBConfig);

            // Open a secondary btree database.
            SecondaryBTreeDatabaseConfig secBtreeDBConfig =
                new SecondaryBTreeDatabaseConfig(null, null);
            secBtreeDBConfig.Primary = btreeDB;
            secBtreeDBConfig.Compare =
                new EntryComparisonDelegate(
                SecondaryEntryComparison);
            secBtreeDBConfig.KeyGen =
                new SecondaryKeyGenDelegate(SecondaryKeyGen);
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(
                dbFileName, secBtreeDBConfig);

            /*
             * Get the compare function set in the configuration
             * and run it in a comparison to see if it is alright.
             */
            EntryComparisonDelegate cmp =
                secDB.Compare;
            DatabaseEntry dbt1, dbt2;
            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((int)257));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((int)255));
            Assert.Less(0, cmp(dbt1, dbt2));

            for (int i = 0; i < 1000; i++)
                btreeDB.Put(new DatabaseEntry(
                    BitConverter.GetBytes(i)), new DatabaseEntry(
                    BitConverter.GetBytes(i)));

            secDB.Close();
            btreeDB.Close();
        }
        public void OpenBtreeDBInEnv(string dbName,
		    DatabaseEnvironment env, out BTreeDatabase db,
		    bool create, Transaction txn)
        {
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Env = env;
            if (create == true)
                btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            else
                btreeDBConfig.Creation = CreatePolicy.NEVER;
            if (txn == null)
                db = BTreeDatabase.Open(dbName,
                    btreeDBConfig);
            else
                db = BTreeDatabase.Open(dbName,
                    btreeDBConfig, txn);
        }
        public BTreeDatabase Open(DatabaseEnvironment env, bool isMaster)
        {
            string dbName = "rep.db";

            // Set up the database.
            BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig();
            dbCfg.Env = env;
            if (isMaster)
                dbCfg.Creation = CreatePolicy.IF_NEEDED;

            dbCfg.AutoCommit = true;
            dbCfg.FreeThreaded = true;

            /*
             * Open the database. Do not provide a txn handle. This
             * Open is autocommitted because BTreeDatabaseConfig.AutoCommit
             * is true.
             */
            return BTreeDatabase.Open(dbName, dbCfg);
        }
        public static void Confirm(XmlElement
		    xmlElement, BTreeDatabaseConfig btreeDBConfig,
		    bool compulsory)
        {
            DatabaseConfig dbConfig = btreeDBConfig;
            Confirm(xmlElement, dbConfig, compulsory);

            // Confirm Btree database specific configuration
            Configuration.ConfirmDuplicatesPolicy(xmlElement,
                "Duplicates", btreeDBConfig.Duplicates, compulsory);
            Configuration.ConfirmBool(xmlElement,
                "NoReverseSplitting",
                btreeDBConfig.NoReverseSplitting, compulsory);
            Configuration.ConfirmBool(xmlElement,
                "UseRecordNumbers", btreeDBConfig.UseRecordNumbers,
                compulsory);
            Configuration.ConfirmCreatePolicy(xmlElement,
                "Creation", btreeDBConfig.Creation, compulsory);
            Configuration.ConfirmUint(xmlElement, "MinKeysPerPage",
                btreeDBConfig.MinKeysPerPage, compulsory);
        }
        public virtual void TestConfig()
        {
            testName = "TestConfig";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName;

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

            // Open a primary btree database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase btreeDB = BTreeDatabase.Open(
                dbFileName, btreeDBConfig);

            SecondaryDatabaseConfig secDBConfig =
                new SecondaryDatabaseConfig(btreeDB, null);
            Config(xmlElem, ref secDBConfig, true);
            Confirm(xmlElem, secDBConfig, true);
            btreeDB.Close();
        }
        public static void Config(XmlElement xmlElement,
		    ref BTreeDatabaseConfig btreeDBConfig, bool compulsory)
        {
            uint minKeysPerPage = new uint();
            DatabaseConfig dbConfig = btreeDBConfig;
            Config(xmlElement, ref dbConfig, compulsory);

            // Configure specific fields/properties of Btree db
            Configuration.ConfigDuplicatesPolicy(xmlElement,
                "Duplicates", ref btreeDBConfig.Duplicates,
                compulsory);
            Configuration.ConfigBool(xmlElement,
                "NoReverseSplitting",
                ref btreeDBConfig.NoReverseSplitting, compulsory);
            Configuration.ConfigBool(xmlElement,
                "UseRecordNumbers",
                ref btreeDBConfig.UseRecordNumbers, compulsory);
            Configuration.ConfigCreatePolicy(xmlElement,
                "Creation", ref btreeDBConfig.Creation, compulsory);
            if (Configuration.ConfigUint(xmlElement,
                "MinKeysPerPage", ref minKeysPerPage, compulsory))
                btreeDBConfig.MinKeysPerPage = minKeysPerPage;
        }
        public void OpenNewSequenceInEnv(string home, string dbname,
		    out DatabaseEnvironment env, out BTreeDatabase db,
		    out Sequence seq)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            env = DatabaseEnvironment.Open(home, envConfig);

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

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

            // Configure and initialize sequence.
            SequenceConfig seqConfig = new SequenceConfig();
            seqConfig.BackingDatabase = db;
            seqConfig.CacheSize = 1000;
            seqConfig.Creation = CreatePolicy.ALWAYS;
            seqConfig.Decrement = false;
            seqConfig.FreeThreaded = true;
            seqConfig.Increment = true;
            seqConfig.InitialValue = 100;
            seqConfig.key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            seq = new Sequence(seqConfig);
        }
        public void TestKeyGen()
        {
            testName = "TestKeyGen";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

            // Open secondary database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(primaryDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(dbFileName,
                secDBConfig);

            primaryDB.Put(new DatabaseEntry(
                BitConverter.GetBytes((int)1)),
                new DatabaseEntry(BitConverter.GetBytes((int)11)));

            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
            pair = secDB.Get(new DatabaseEntry(
                BitConverter.GetBytes((int)11)));
            Assert.IsNotNull(pair.Value);

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

            // Close primary database.
            primaryDB.Close();
        }
Exemple #17
0
 /// <summary>
 /// Instantiate a new BTreeDatabase object and open the database
 /// represented by <paramref name="Filename"/> and
 /// <paramref name="DatabaseName"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If both <paramref name="Filename"/> and
 /// <paramref name="DatabaseName"/> are null, the database is strictly
 /// temporary and cannot be opened by any other thread of control, thus
 /// the database can only be accessed by sharing the single database
 /// object that created it, in circumstances where doing so is safe. If
 /// <paramref name="Filename"/> is null and
 /// <paramref name="DatabaseName"/> is non-null, the database can be
 /// opened by other threads of control and be replicated to client
 /// sites in any replication group.
 /// </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 that 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 BTreeDatabase Open(
     string Filename, string DatabaseName, BTreeDatabaseConfig cfg)
 {
     return(Open(Filename, DatabaseName, cfg, null));
 }
        public void TestBadSecondaryException()
        {
            testName = "TestBadSecondaryException";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";
            string secDBFileName = testHome + "/" +
                testName + "_sec.db";

            // Open primary database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase btreeDB =
                BTreeDatabase.Open(dbFileName, btreeDBConfig);

            // Open secondary database.
            SecondaryBTreeDatabaseConfig secBtDbConfig =
                new SecondaryBTreeDatabaseConfig(btreeDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secBtDbConfig.Creation = CreatePolicy.IF_NEEDED;
            SecondaryBTreeDatabase secBtDb =
                SecondaryBTreeDatabase.Open(secDBFileName,
                secBtDbConfig);

            // Put some data into primary database.
            for (int i = 0; i < 10; i++)
                btreeDB.Put(new DatabaseEntry(
                    BitConverter.GetBytes(i)),
                    new DatabaseEntry(BitConverter.GetBytes(i)));

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

            // Delete record(5, 5) in primary database.
            btreeDB.Delete(new DatabaseEntry(
                BitConverter.GetBytes((int)5)));

            // Reopen the secondary database.
            SecondaryDatabase secDB = SecondaryDatabase.Open(
                secDBFileName,
                new SecondaryDatabaseConfig(btreeDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen)));

            /*
             * Getting record(5, 5) by secondary database should
             * throw BadSecondaryException since it has been
             * deleted in the primary database.
             */
            try
            {
                secDB.Exists(new DatabaseEntry(
                    BitConverter.GetBytes((int)5)));
            }
            catch (BadSecondaryException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                secDB.Close();
                btreeDB.Close();
            }
        }
        public void OpenSecQueueDB(string dbFileName, 
		    string dbSecFileName, bool ifDBName)
        {
            // Open a primary btree database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase primaryDB;

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

            try
            {
                // Open a new secondary database.
                SecondaryBTreeDatabaseConfig secBTDBConfig =
                    new SecondaryBTreeDatabaseConfig(
                    primaryDB, null);
                secBTDBConfig.Creation =
                    CreatePolicy.IF_NEEDED;

                SecondaryBTreeDatabase secBTDB;
                if (ifDBName == false)
                    secBTDB = SecondaryBTreeDatabase.Open(
                        dbSecFileName, secBTDBConfig);
                else
                    secBTDB = SecondaryBTreeDatabase.Open(
                        dbSecFileName, "secondary",
                        secBTDBConfig);

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

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

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

                // Close secondary database.
                secDB.Close();
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                // Close primary database.
                primaryDB.Close();
            }
        }
        public void OpenSecDBWithinTxn(string home,
		    string dbFileName, string dbSecFileName, bool ifDbName)
        {
            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            envConfig.UseLocking = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            // Open a primary btree database.
            Transaction openDBTxn = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db = BTreeDatabase.Open(
                dbFileName, dbConfig, openDBTxn);

            // Open a secondary btree database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secDBConfig.Env = env;
            secDBConfig.Creation = CreatePolicy.IF_NEEDED;

            SecondaryBTreeDatabase secDB;
            if (ifDbName == false)
                secDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, secDBConfig, openDBTxn);
            else
                secDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, "secondary", secDBConfig,
                    openDBTxn);
            openDBTxn.Commit();
            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 = SecondaryBTreeDatabase.Open(
                    dbSecFileName, secConfig, secTxn);
            else
                secExDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, "secondary", secConfig,
                    secTxn);
            secExDB.Close();
            secTxn.Commit();

            db.Close();
            env.Close();
        }
Exemple #21
0
        public void OpenSecDB(string dbFileName, 
		    string dbSecFileName, out BTreeDatabase db,
		    out SecondaryBTreeDatabase secDB)
        {
            // Open a primary database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);

            // Open a secondary database.
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secDB = SecondaryBTreeDatabase.Open(dbSecFileName,
                secConfig);
        }
        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();
        }
Exemple #23
0
        public static void GetCursorInBtreeDB(
		    string home, string name,
		    CursorConfig cursorConfig,
		    out DatabaseEnvironment env, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            string dbFileName = name + ".db";

            Configuration.ClearDir(home);

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

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

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

            // Get a cursor in the transaction.
            cursor = db.Cursor(cursorConfig, null);
        }
        static void Main(string[] args) {
            BTreeDatabase btreeDB;
            BTreeDatabaseConfig btreeDBConfig;
            DatabaseEnvironment env;
            DatabaseEnvironmentConfig envConfig;
            Sequence seq;
            SequenceConfig seqConfig;
            string buff;
            string dbFileName;
            string home;
            string progName;

            /*
             * excs_sequence is meant to be run from build_windows\AnyCPU, in
             * either the Debug or Release directory. The required core
             * libraries, however, are in either build_windows\Win32 or
             * build_windows\x64, depending upon the platform.  That location
             * needs to be added to the PATH environment variable for the
             * P/Invoke calls to work.
             */
            try {
                String pwd = Environment.CurrentDirectory;
                pwd = Path.Combine(pwd, "..");
                pwd = Path.Combine(pwd, "..");
                if (IntPtr.Size == 4)
                    pwd = Path.Combine(pwd, "Win32");
                else
                    pwd = Path.Combine(pwd, "x64");
#if DEBUG
                pwd = Path.Combine(pwd, "Debug");
#else
                pwd = Path.Combine(pwd, "Release");
#endif
                pwd += ";" + Environment.GetEnvironmentVariable("PATH");
                Environment.SetEnvironmentVariable("PATH", pwd);
            } catch (Exception e) {
                Console.WriteLine(
                    "Unable to set the PATH environment variable.");
                Console.WriteLine(e.Message);
                return;
            }
            
            progName = "excs_sequence";
            try {
                home = args[0];
                dbFileName = args[1];
            } catch {
                Usage();
                return;
            };

            /* Optiionally remove the existing database file. */
            if (File.Exists(dbFileName)) {
                while (true) {
                    Console.Write
                        ("File already exists, delete or not (y/n)?");
                    buff = Console.ReadLine();
                    if (buff == "y" || buff == "n")
                        break;
                }

                if (buff == "y") {
                    File.Delete(dbFileName);
                    Console.WriteLine("The existing {0} is deleted",
                        dbFileName);
                }
            }

            /* Configure and open environment. */
            envConfig = new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            try {
                env = DatabaseEnvironment.Open(home, envConfig);
            } catch (Exception e) {
                Console.WriteLine("{0}:{1}\n{2}",
                    e.Source, e.Message, e.StackTrace);
                return;
            }

            /* Configure and open sequence's database. */
            btreeDBConfig = new BTreeDatabaseConfig();
            btreeDBConfig.AutoCommit = true;
            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            btreeDBConfig.ErrorPrefix = progName;
            btreeDBConfig.Env = env;
            try {
                btreeDB = BTreeDatabase.Open(dbFileName,
                    btreeDBConfig);
            } catch (Exception e) {
                Console.WriteLine("{0}:{1}\n{2}",
                    e.Source, e.Message, e.StackTrace);
                return;
            }

            /* Configure and initialize sequence. */
            seqConfig = new SequenceConfig();
            seqConfig.BackingDatabase = btreeDB;
            seqConfig.Creation = CreatePolicy.IF_NEEDED;
            seqConfig.Increment = true;
            seqConfig.InitialValue = Int64.MaxValue;
            seqConfig.key = new DatabaseEntry();
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            DbtFromString(seqConfig.key, "excs_sequence");
            seq = new Sequence(seqConfig);

            /* Get from sequence. */
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("{0}", seq.Get(2));
                Console.ReadLine();
            }

            Console.WriteLine("Sequence Name: {0}",
                seq.BackingDatabase.FileName);
            Console.WriteLine("Sequence Key: {0}",
                StrFromDBT(seq.Key));
            Console.WriteLine("{0}->{1}", seq.Min, seq.Max);
            Console.WriteLine(seq.Wrap ? "wrap" : "not wrap");
            Console.WriteLine(seq.Increment ? "increase" :
                "decrease");

            /* Close sequence, database and environment. */
            seq.Close();
            btreeDB.Close();
            env.Close();
        }
Exemple #25
0
        public void MoveWithRMW(string home, string name)
        {
            paramEnv = null;
            paramDB = null;

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

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

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

            txn.Commit();

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

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

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

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

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

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

            t1.Join();
            t2.Join();
            paramDB.Close();
            paramEnv.Close();
        }
Exemple #26
0
        public static void GetCursorInBtreeDBWithoutEnv(
		    string home, string name, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            string dbFileName = home + "/" + name + ".db";

            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            db = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
Exemple #27
0
 /// <summary>
 /// Instantiate a new BTreeDatabase 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
 /// 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 BTreeDatabase Open(
     string Filename, BTreeDatabaseConfig cfg, Transaction txn)
 {
     return(Open(Filename, null, cfg, txn));
 }
        public void TestMultiKeyGen()
        {
            testName = "TestPrefixCompare";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            // Open a primary btree database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();
            btreeDBConfig.Creation = CreatePolicy.ALWAYS;
            BTreeDatabase btreeDB = BTreeDatabase.Open(
                dbFileName, btreeDBConfig);

            // Open a secondary btree database.
            SecondaryBTreeDatabaseConfig secBtreeDBConfig =
                new SecondaryBTreeDatabaseConfig(btreeDB, null);
            secBtreeDBConfig.Primary = btreeDB;
            secBtreeDBConfig.KeyGen =
                new SecondaryKeyGenDelegate(
                MultipleKeyGen);
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(
                dbFileName, secBtreeDBConfig);

            /* Check that multiple secondary keys work */
            DatabaseEntry key, data;
            String keyStr = "key";
            key = new DatabaseEntry();
            Configuration.dbtFromString(key, keyStr);

            data = new DatabaseEntry();
            String[] dataStrs = { "abc", "def", "ghi", "jkl" };
            String dataStr = String.Join(",", dataStrs);
            Configuration.dbtFromString(data, dataStr);
            btreeDB.Put(key, data);

            foreach (String skeyStr in dataStrs) {
                DatabaseEntry skey = new DatabaseEntry();
                Configuration.dbtFromString(skey, skeyStr);
                Assert.IsTrue(secDB.Exists(skey));
            }

            /*
             * Check that a single secondary key, returned in a
             * MultipleDatabaseEntry works.
             */
            keyStr = "key2";
            key = new DatabaseEntry();
            Configuration.dbtFromString(key, keyStr);

            data = new DatabaseEntry();
            dataStrs = new string[1]{ "abcdefghijkl" };
            dataStr = String.Join(",", dataStrs);
            Configuration.dbtFromString(data, dataStr);
            btreeDB.Put(key, data);

            // Check that secondary keys work
            foreach (String skeyStr in dataStrs) {
                DatabaseEntry skey = new DatabaseEntry();
                Configuration.dbtFromString(skey, skeyStr);
                Assert.IsTrue(secDB.Exists(skey));
            }
        }
Exemple #29
0
        public void OpenSecDBInTxn(string home, string dbFileName, 
		    string dbSecFileName, out DatabaseEnvironment env,
		    out BTreeDatabase db, out SecondaryBTreeDatabase secDB)
        {
            // Open environment.
            DatabaseEnvironmentConfig envCfg =
                new DatabaseEnvironmentConfig();
            envCfg.Create = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseMPool = true;
            envCfg.UseTxns = true;
            env = DatabaseEnvironment.Open(
                home, envCfg);

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

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

            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

            // Open secondary database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(primaryDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(dbFileName,
                secDBConfig);

            primaryDB.Put(new DatabaseEntry(
                BitConverter.GetBytes((int)1)),
                new DatabaseEntry(BitConverter.GetBytes((int)11)));

            SecondaryCursor cursor = secDB.SecondaryCursor();
            cursor.Move(new DatabaseEntry(
                BitConverter.GetBytes((int)11)), true);
            Assert.AreEqual(BitConverter.GetBytes((int)11),
                cursor.Current.Key.Data);

            // Close the cursor.
            cursor.Close();

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

            // Close primary database.
            primaryDB.Close();
        }
Exemple #31
0
        public void TestDuplicate()
        {
            testName = "TestDuplicate";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName +
                "_sec.db";

            // Open a primary database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase db = BTreeDatabase.Open(
                dbFileName, dbConfig);

            // Open a secondary database.
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secConfig.Creation = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(dbSecFileName,
                secConfig);

            // Put a pair of key and data into the database.
            DatabaseEntry key, data;
            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            db.Put(key, data);

            // Create a cursor.
            SecondaryCursor cursor = secDB.SecondaryCursor();
            cursor.Move(key, true);

            // Duplicate the cursor.
            SecondaryCursor dupCursor;
            dupCursor = cursor.Duplicate(true);

            /*
             * Confirm that the duplicate cursor has the same
             * position as the original one.
             */
            Assert.AreEqual(cursor.Current.Key,
                dupCursor.Current.Key);
            Assert.AreEqual(cursor.Current.Value,
                dupCursor.Current.Value);

            // Close the cursor and the duplicate cursor.
            dupCursor.Close();
            cursor.Close();

            // Close secondary and primary database.
            secDB.Close();
            db.Close();
        }
        public void OpenPrimaryAndSecondaryDB(string dbFileName,
		    out BTreeDatabase primaryDB,
		    out SecondaryBTreeDatabase secDB)
        {
            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();
            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

            // Open secondary database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(primaryDB,
                new SecondaryKeyGenDelegate(SecondaryKeyGen));
            secDB = SecondaryBTreeDatabase.Open(dbFileName,
                secDBConfig);
        }
Exemple #33
0
        private void Config(BTreeDatabaseConfig cfg)
        {
            base.Config(cfg);

            /*
             * Database.Config calls set_flags, but that does not get the BTree
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);

            if (cfg.BlobDir != null && cfg.Env == null)
            {
                db.set_ext_file_dir(cfg.BlobDir);
            }

            if (cfg.ExternalFileDir != null && cfg.Env == null)
            {
                db.set_ext_file_dir(cfg.ExternalFileDir);
            }

            if (cfg.blobThresholdIsSet)
            {
                db.set_ext_file_threshold(cfg.BlobThreshold, 0);
            }

            if (cfg.BTreeCompare != null)
            {
                Compare = cfg.BTreeCompare;
            }

            if (cfg.BTreePrefixCompare != null)
            {
                PrefixCompare = cfg.BTreePrefixCompare;
            }

            // The duplicate comparison function cannot change.
            if (cfg.DuplicateCompare != null)
            {
                DupCompare = cfg.DuplicateCompare;
            }

            if (cfg.minkeysIsSet)
            {
                db.set_bt_minkey(cfg.MinKeysPerPage);
            }

            if (cfg.compressionIsSet)
            {
                Compress   = cfg.Compress;
                Decompress = cfg.Decompress;
                if (Compress == null)
                {
                    doCompressRef = null;
                }
                else
                {
                    doCompressRef = new BDB_CompressDelegate(doCompress);
                }
                if (Decompress == null)
                {
                    doDecompressRef = null;
                }
                else
                {
                    doDecompressRef = new BDB_DecompressDelegate(doDecompress);
                }
                db.set_bt_compress(doCompressRef, doDecompressRef);
            }

            if (cfg.partitionIsSet)
            {
                nparts    = cfg.NParts;
                Partition = cfg.Partition;
                if (Partition == null)
                {
                    doPartitionRef = null;
                }
                else
                {
                    doPartitionRef = new BDB_PartitionDelegate(doPartition);
                }
                partitionKeys = cfg.PartitionKeys;
                IntPtr[] ptrs = null;
                if (partitionKeys != null)
                {
                    int size = (int)nparts - 1;
                    ptrs = new IntPtr[size];
                    for (int i = 0; i < size; i++)
                    {
                        ptrs[i] = DBT.getCPtr(
                            DatabaseEntry.getDBT(partitionKeys[i])).Handle;
                    }
                }
                db.set_partition(nparts, ptrs, doPartitionRef);
            }
        }