A class representing configuration parameters for Cursor
Example #1
0
 public void GetCursur(string dbFileName, bool ifConfig)
 {
     HeapDatabaseConfig dbConfig = new HeapDatabaseConfig();
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     HeapDatabase db = HeapDatabase.Open(dbFileName, dbConfig);
     HeapRecordId rid = db.Append(
         new DatabaseEntry(BitConverter.GetBytes((int)1)));
     Cursor cursor;
     CursorConfig cursorConfig = new CursorConfig();
     cursorConfig.Priority = CachePriority.HIGH;
     if (ifConfig == false)
         cursor = db.Cursor();
     else
         cursor = db.Cursor(cursorConfig);
     cursor.Add(new KeyValuePair<DatabaseEntry, DatabaseEntry>(
         new DatabaseEntry(rid.toArray()),
         new DatabaseEntry(BitConverter.GetBytes((int)2))));
     Cursor dupCursor = cursor.Duplicate(false);
     Assert.IsNull(dupCursor.Current.Key);
     if (ifConfig)
         Assert.AreEqual(CachePriority.HIGH, dupCursor.Priority);
     dupCursor.Close();
     cursor.Close();
     db.Close();
 }
Example #2
0
        // Get a cursor in CDS.
        public static void GetCursorInBtreeDBInCDS(
            string home, string name,
            CursorConfig cursorConfig,
            out DatabaseEnvironment env, out BTreeDatabase db,
            out BTreeCursor cursor)
        {
            string dbFileName = name + ".db";

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

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

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

            db = BTreeDatabase.Open(dbFileName, dbConfig);

            // Get a cursor in the transaction.
            cursor = db.Cursor(cursorConfig);
        }
Example #3
0
 /// <summary>
 /// Create a transactionally protected database cursor with the given
 /// configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new BTreeCursor Cursor(CursorConfig cfg, Transaction txn)
 {
     if (cfg.Priority == CachePriority.DEFAULT)
     {
         return(new BTreeCursor(db.cursor(
                                    Transaction.getDB_TXN(txn), cfg.flags), Pagesize));
     }
     else
     {
         return(new BTreeCursor(db.cursor(Transaction.getDB_TXN(txn),
                                          cfg.flags), Pagesize, cfg.Priority));
     }
 }
        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 TestConfig()
 {
     testName = "TestConfig";
     SetUpTest(false);
     /*
      * Configure the fields/properties and see if
      * they are updated successfully.
      */
     CursorConfig cursorConfig = new CursorConfig();
     XmlElement xmlElem = Configuration.TestSetUp(
         testFixtureName, testName);
     Config(xmlElem, ref cursorConfig, true);
     Confirm(xmlElem, cursorConfig, true);
 }
        public static void Config(XmlElement xmlElement, 
		    ref CursorConfig cursorConfig, bool compulsory)
        {
            Configuration.ConfigIsolation(xmlElement,
                "IsolationDegree", ref cursorConfig.IsolationDegree,
                compulsory);
            Configuration.ConfigCachePriority(xmlElement,
                "Priority", ref cursorConfig.Priority, compulsory);
            Configuration.ConfigBool(xmlElement,
                "SnapshotIsolation", ref cursorConfig.SnapshotIsolation,
                compulsory);
            Configuration.ConfigBool(xmlElement,
                "WriteCursor", ref cursorConfig.WriteCursor,
                compulsory);
        }
Example #7
0
 /// <summary>
 /// Create a secondary database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public SecondaryCursor SecondaryCursor(CursorConfig cfg)
 {
     return(SecondaryCursor(cfg, null));
 }
Example #8
0
        public void ReadTxn()
        {
            // Get a new transaction for reading the db.
            TransactionConfig txnConfig =
                new TransactionConfig();
            txnConfig.Snapshot = true;
            readTxn = paramEnv.BeginTransaction(
                txnConfig);

            // Get a new cursor for putting record into db.
            CursorConfig cursorConfig = new CursorConfig();
            cursorConfig.WriteCursor = false;
            BTreeCursor cursor = paramDB.Cursor(
                cursorConfig, readTxn);

            // Continually reading record from db.
            try
            {
                Assert.IsTrue(cursor.MoveFirst());
                int i = 0;
                do
                {
                    Assert.AreEqual(
                        BitConverter.ToInt32(
                        cursor.Current.Key.Data, 0),
                        BitConverter.ToInt32(
                        cursor.Current.Value.Data, 0));
                } while (i <= 1000 && cursor.MoveNext());
            }
            catch (DeadlockException)
            {
            }
            finally
            {
                cursor.Close();
            }
        }
Example #9
0
        public void TestWriteCursor()
        {
            BTreeCursor cursor;
            BTreeDatabase db;
            CursorConfig cursorConfig;
            DatabaseEnvironment env;

            testName = "TestWriteCursor";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            cursorConfig = new CursorConfig();
            cursorConfig.WriteCursor = true;

            GetCursorInBtreeDBInCDS(testHome, testName,
                cursorConfig, out env, out db, out cursor);

            /*
             * Add a record by cursor to the database. If the
             * WriteCursor doesn't work, exception will be
             * throwed in the environment which is configured
             * with DB_INIT_CDB.
             */
            try
            {
                AddOneByCursor(db, cursor);
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                cursor.Close();
                db.Close();
                env.Close();
            }
        }
Example #10
0
        /*
         * This simply counts the number of records contained in the
         * database and returns the result. You can use this method
         * in three ways:
         *
         * First call it with an active txn handle.
         * Secondly, configure the cursor for dirty reads
         * Third, call countRecords AFTER the writer has committed
         * its transaction.
         *
         * If you do none of these things, the writer thread will
         * self-deadlock.
         *
         * Note that this method exists only for illustrative purposes.
         * A more straight-forward way to count the number of records in
         * a database is to use the Database.getStats() method.
         */
        private int CountRecords(Transaction txn)
        {
            int count = 0;
            Cursor cursor = null;

            try {
                // Get the cursor.
                CursorConfig cc = new CursorConfig();

                /*
                 * Isolation degree one is ignored if the
                 * database was not opened for uncommitted
                 * read support. TxnGuide opens its database
                 * in this way and TxnGuideInMemory does not.
                 */
                cc.IsolationDegree = Isolation.DEGREE_ONE;
                cursor = db.Cursor(cc, txn);
                while (cursor.MoveNext())
                    count++;
            } finally {
                if (cursor != null)
                    cursor.Close();
            }

            return count;
        }
Example #11
0
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new RecnoCursor Cursor(CursorConfig cfg)
 {
     return Cursor(cfg, null);
 }
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public Cursor Cursor(CursorConfig cfg)
 {
     return Cursor(cfg, null);
 }
Example #13
0
 /// <summary>
 /// Create a transactionally protected database cursor with the given
 /// configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new HashCursor Cursor(CursorConfig cfg, Transaction txn)
 {
     return new HashCursor(
         db.cursor(Transaction.getDB_TXN(txn), cfg.flags), Pagesize);
 }
Example #14
0
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new HashCursor Cursor(CursorConfig cfg)
 {
     return Cursor(cfg, null);
 }
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new HashCursor Cursor(CursorConfig cfg)
 {
     return(Cursor(cfg, null));
 }
Example #16
0
 /// <summary>
 /// Create a transactionally protected secondary database cursor with
 /// the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public SecondaryCursor SecondaryCursor(
     CursorConfig cfg, Transaction txn)
 {
     return(new SecondaryCursor(
                db.cursor(Transaction.getDB_TXN(txn), cfg.flags)));
 }
Example #17
0
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new RecnoCursor Cursor(CursorConfig cfg)
 {
     return(Cursor(cfg, null));
 }
Example #18
0
        public static void GetCursorInBtreeDBInTDS(
		    string home, string name,
		    CursorConfig cursorConfig,
		    out DatabaseEnvironment env, out BTreeDatabase db,
		    out BTreeCursor cursor, out Transaction txn)
        {
            string dbFileName = name + ".db";

            Configuration.ClearDir(home);

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

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

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

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

            // Get a cursor in the transaction.
            if (cursorConfig != null)
                cursor = db.Cursor(cursorConfig, txn);
            else
                cursor = db.Cursor(txn);
        }
Example #19
0
 /// <summary>
 /// Create a transactionally protected database cursor with the given
 /// configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new RecnoCursor Cursor(CursorConfig cfg, Transaction txn)
 {
     return(new RecnoCursor(
                db.cursor(Transaction.getDB_TXN(txn), cfg.flags), Pagesize));
 }
Example #20
0
        public void TestPriority()
        {
            CachePriority[] priorities;
            CursorConfig cursorConfig;

            testName = "TestPriority";
            SetUpTest(true);
            cursorConfig = new CursorConfig();
            priorities = new CachePriority[6];
            priorities[0] = CachePriority.DEFAULT;
            priorities[1] = CachePriority.HIGH;
            priorities[2] = CachePriority.LOW;
            priorities[3] = CachePriority.VERY_HIGH;
            priorities[4] = CachePriority.VERY_LOW;
            priorities[5] = null;

            for (int i = 0; i < 6; i++) {
                if (priorities[i] != null) {
                    cursorConfig.Priority = priorities[i];
                    Assert.AreEqual(priorities[i], cursorConfig.Priority);
                }
                GetCursorWithConfig(testHome + "/" + testName + ".db",
                    DatabaseType.BTREE.ToString(), cursorConfig, DatabaseType.BTREE);
                GetCursorWithConfig(testHome + "/" + testName + ".db",
                    DatabaseType.HASH.ToString(), cursorConfig, DatabaseType.HASH);
                GetCursorWithConfig(testHome + "/" + testName + ".db",
                    DatabaseType.QUEUE.ToString(), cursorConfig, DatabaseType.QUEUE);
                GetCursorWithConfig(testHome + "/" + testName + ".db",
                    DatabaseType.RECNO.ToString(), cursorConfig, DatabaseType.RECNO);
            }
        }
Example #21
0
 /// <summary>
 /// Create a transactionally protected database cursor with the given
 /// configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new RecnoCursor Cursor(CursorConfig cfg, Transaction txn)
 {
     if (cfg.Priority == CachePriority.DEFAULT)
         return new RecnoCursor(
             db.cursor(Transaction.getDB_TXN(txn), cfg.flags), Pagesize);
     else
         return new RecnoCursor(db.cursor(Transaction.getDB_TXN(txn),
             cfg.flags), Pagesize, cfg.Priority);
 }
Example #22
0
        private void GetCursorWithConfig(string dbFile, string dbName, 
		    CursorConfig cfg, DatabaseType type)
        {
            Database db;
            Cursor cursor;
            Configuration.ClearDir(testHome);
            if (type == DatabaseType.BTREE) {
                BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = BTreeDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((BTreeDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.HASH) {
                HashDatabaseConfig dbConfig = new HashDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = (HashDatabase)HashDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((HashDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.QUEUE) {
                QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                dbConfig.Length = 100;
                db = QueueDatabase.Open(dbFile, dbConfig);
                cursor = ((QueueDatabase)db).Cursor(cfg);
            } else if (type == DatabaseType.RECNO) {
                RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = RecnoDatabase.Open(dbFile, dbName, dbConfig);
                cursor = ((RecnoDatabase)db).Cursor(cfg);
            } else
                throw new TestException();

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

            Cursor dupCursor = cursor.Duplicate(false);
            Assert.AreEqual(cursor.Priority, dupCursor.Priority);
            cursor.Close();
            db.Close();
        }
Example #23
0
        public void TestPriority()
        {
            BTreeCursor cursor;
            BTreeDatabase db;
            CachePriority[] priorities;
            CursorConfig cursorConfig;
            DatabaseEnvironment env;

            cursorConfig = new CursorConfig();

            priorities = new CachePriority[5];
            priorities[0] = CachePriority.DEFAULT;
            priorities[1] = CachePriority.HIGH;
            priorities[2] = CachePriority.LOW;
            priorities[3] = CachePriority.VERY_HIGH;
            priorities[4] = CachePriority.VERY_LOW;

            testName = "TestPriority";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            for (int i = 0; i < 5; i++)
            {
                // Configure the cursor priority.
                cursorConfig.Priority = priorities[i];

                // Open a database to test a specified priority.
                GetCursorInBtreeDBInCDS(testHome, testName,
                    cursorConfig, out env, out db, out cursor);
                Assert.AreEqual(priorities[i], cursorConfig.Priority);
                cursor.Close();
                db.Close();
                env.Close();
            }
        }
Example #24
0
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new BTreeCursor Cursor(CursorConfig cfg)
 {
     return Cursor(cfg, null);
 }
Example #25
0
        void TestCloseResmgr_int(bool havetxn)
        {
            BTreeDatabase db;
            BTreeCursor cursor, csr;
            CursorConfig cursorConfig;
            DatabaseEnvironment env;
            Transaction txn;

            DatabaseEntry key, data;
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;

            txn = null;
            testName = "TestCloseResmgr";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);
            cursorConfig = new CursorConfig();
            // Open an environment, a database and a cursor.
            if (havetxn)
                GetCursorInBtreeDBInTDS(testHome, testName,
                    cursorConfig, out env, out db,
                    out cursor, out txn);
            else
                GetCursorInBtreeDB(testHome, testName,
                    cursorConfig, out env, out db,
                    out cursor);

            // Add a record to db via cursor.
            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            pair = new KeyValuePair<DatabaseEntry,DatabaseEntry>
                (key, data);
            byte []kbytes;
            byte []dbytes;

            for (int i = 0; i < 100; i++) {
                kbytes = ASCIIEncoding.ASCII.
                    GetBytes("key" + i);
                dbytes = ASCIIEncoding.ASCII.
                    GetBytes("data" + i);
                key.Data = kbytes;

                data.Data = dbytes;

                cursor.Add(pair);
            }
            // Do not close cursor.

            csr = db.Cursor(cursorConfig, txn);

            while (csr.MoveNext()) {
                // Do nothing for now.
            }
            // Do not close csr.
            if (havetxn && txn != null)
                txn.Commit();
            env.CloseForceSync();
        }
Example #26
0
 /// <summary>
 /// Create a secondary database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public SecondaryCursor SecondaryCursor(CursorConfig cfg) {
     return SecondaryCursor(cfg, null);
 }
Example #27
0
        public void TestIsolationDegree()
        {
            BTreeDatabase db;
            BTreeCursor cursor;
            CursorConfig cursorConfig;
            DatabaseEnvironment env;
            Transaction txn;

            testName = "TestIsolationDegree";
            testHome = testFixtureHome + "/" + testName;

            Isolation[] isolationDegrees = new Isolation[3];
            isolationDegrees[0] = Isolation.DEGREE_ONE;
            isolationDegrees[1] = Isolation.DEGREE_TWO;
            isolationDegrees[2] = Isolation.DEGREE_THREE;

            IsolationDelegate[] delegates = {
                    new IsolationDelegate(CursorReadUncommited),
                    new IsolationDelegate(CursorReadCommited),
                    new IsolationDelegate(CursorRead)};

            cursorConfig = new CursorConfig();
            for (int i = 0; i < 3; i++)
            {
                cursorConfig.IsolationDegree = isolationDegrees[i];
                GetCursorInBtreeDBInTDS(testHome + "/" + i.ToString(),
                    testName, cursorConfig, out env, out db,
                    out cursor, out txn);
                cursor.Close();
                db.Close();
                txn.Commit();
                env.Close();
            }
        }
        private void GetMultipleDB(string dbFileName, 
		    BTreeDatabaseConfig dbConfig, Transaction txn, 
		    out BTreeDatabase db, out BTreeCursor cursor)
        {
            if (txn == null) {
                db = BTreeDatabase.Open(dbFileName, dbConfig);
                cursor = db.Cursor();
            } else {
                db = BTreeDatabase.Open(
                    dbFileName, dbConfig, txn);
                CursorConfig cursorConfig = new CursorConfig();
                cursor = db.Cursor(cursorConfig, txn);
            }

            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
            DatabaseEntry key, data;
            for (int i = 1; i < 100; i++) {
                key = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(BitConverter.GetBytes(i));
                pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(key, data);
                cursor.Add(pair);
            }

            if (dbConfig.UseRecordNumbers == true) {
                byte[] bytes = new byte[512];
                for (int i = 0; i < 512; i++)
                    bytes[i] = (byte)i;
                key = new DatabaseEntry(BitConverter.GetBytes(100));
                data = new DatabaseEntry(bytes);
                pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(key, data);
                cursor.Add(pair);
            } else {
                if (dbConfig.Duplicates == DuplicatesPolicy.UNSORTED ||
                    dbConfig.Duplicates == DuplicatesPolicy.SORTED) {
                    key = new DatabaseEntry(BitConverter.GetBytes(99));
                    data = new DatabaseEntry(BitConverter.GetBytes(100));
                    pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(key, data);
                    cursor.Add(pair);
                }

                key = new DatabaseEntry(BitConverter.GetBytes(101));
                data = new DatabaseEntry(BitConverter.GetBytes(101));
                pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(key, data);
                cursor.Add(pair);
            }
        }
        public void TestSecondaryCursorWithConfig()
        {
            testName = "TestSecondaryCursorWithConfig";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            BTreeDatabase db;
            SecondaryBTreeDatabase secDB;
            OpenPrimaryAndSecondaryDB(dbFileName, out db, out secDB);

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

            CursorConfig cursorConfig = new CursorConfig();
            cursorConfig.WriteCursor = false;
            SecondaryCursor cursor =
                secDB.SecondaryCursor(cursorConfig);

            cursor.Move(new DatabaseEntry(
                BitConverter.GetBytes((int)5)), true);

            Assert.AreEqual(1, cursor.Count());

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

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

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

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

            Configuration.ClearDir(testHome);
            HashDatabaseConfig cfg = new HashDatabaseConfig();
            cfg.Creation = CreatePolicy.ALWAYS;
            string blrootdir = "__db_bl";

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

            // Open the database and verify the blob config.
            if (db_threshold > 0)
            cfg.BlobThreshold = db_threshold;
            if (db_blobdir != null)
            {
            cfg.BlobDir = db_blobdir;
            /*
             * The blob directory setting in the database
             * is effective only when it is opened without
             * an environment.
             */
            if (cfg.Env == null)
                blrootdir = db_blobdir;
            }

            HashDatabase db = HashDatabase.Open(hashDBName, cfg);
            Assert.AreEqual(
            db_threshold > 0 ? db_threshold : env_threshold,
            db.BlobThreshold);
            if (db_blobdir == null && cfg.Env == null)
            Assert.IsNull(db.BlobDir);
            else
            Assert.AreEqual(0,
                db.BlobDir.CompareTo(blrootdir));

            // Insert and verify some blob data by database methods.
            string[] records = {"a", "b", "c", "d", "e", "f", "g", "h",
            "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z"};
            DatabaseEntry kdbt = new DatabaseEntry();
            DatabaseEntry ddbt = new DatabaseEntry();
            byte[] kdata, ddata;
            string str;
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
            ddbt.Blob = blobdbt;
            Assert.AreEqual(blobdbt, ddbt.Blob);
            for (int i = 0; i < records.Length; i++)
            {
            kdata = BitConverter.GetBytes(i);
            str = records[i];
            if (!blobdbt)
            {
                for (int j = 0; j < db_threshold; j++)
                    str = str + records[i];
            }
            ddata = Encoding.ASCII.GetBytes(str);
            kdbt.Data = kdata;
            ddbt.Data = ddata;
            db.Put(kdbt, ddbt);
            try
            {
                pair = db.Get(kdbt);
            }
            catch (DatabaseException)
            {
                db.Close();
                if (cfg.Env != null)
                    cfg.Env.Close();
                throw new TestException();
            }
            Assert.AreEqual(ddata, pair.Value.Data);
            }

            /*
             * Insert some blob data by cursor, update it and verify
             * the update by database stream.
             */
            kdata = BitConverter.GetBytes(records.Length);
            ddata = Encoding.ASCII.GetBytes("abc");
            kdbt.Data = kdata;
            ddbt.Data = ddata;
            ddbt.Blob = true;
            Assert.IsTrue(ddbt.Blob);
            pair =
            new KeyValuePair<DatabaseEntry, DatabaseEntry>(kdbt, ddbt);
            CursorConfig dbcConfig = new CursorConfig();
            Transaction txn = null;
            if (cfg.Env != null)
            txn = cfg.Env.BeginTransaction();
            HashCursor cursor = db.Cursor(dbcConfig, txn);
            cursor.Add(pair);
            DatabaseStreamConfig dbsc = new DatabaseStreamConfig();
            dbsc.SyncPerWrite = true;
            DatabaseStream dbs = cursor.DbStream(dbsc);
            Assert.AreNotEqual(null, dbs);
            Assert.IsFalse(dbs.GetConfig.ReadOnly);
            Assert.IsTrue(dbs.GetConfig.SyncPerWrite);
            Assert.AreEqual(3, dbs.Size());
            DatabaseEntry sdbt = dbs.Read(0, 3);
            Assert.IsNotNull(sdbt);
            Assert.AreEqual(ddata, sdbt.Data);
            sdbt = new DatabaseEntry(Encoding.ASCII.GetBytes("defg"));
            Assert.IsTrue(dbs.Write(sdbt, 3));
            Assert.AreEqual(7, dbs.Size());
            sdbt = dbs.Read(0, 7);
            Assert.IsNotNull(sdbt);
            Assert.AreEqual(Encoding.ASCII.GetBytes("abcdefg"), sdbt.Data);
            dbs.Close();

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

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

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

            // Verify the stats.
            HashStats st = db.Stats();
            Assert.AreEqual(records.Length + 1, st.nBlobRecords);

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

            /*
             * Remove the default blob directory
             * when it is not under the test home.
             */
            if (db_blobdir == null && cfg.Env == null)
            Directory.Delete("__db_bl", true);
        }
Example #31
0
 /// <summary>
 /// Create a transactionally protected secondary database cursor with
 /// the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <param name="txn">
 /// The transaction context in which the cursor may be used.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public SecondaryCursor SecondaryCursor(
     CursorConfig cfg, Transaction txn) {
     return new SecondaryCursor(
         db.cursor(Transaction.getDB_TXN(txn), cfg.flags));
 }
Example #32
0
 /// <summary>
 /// Create a database cursor with the given configuration.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the cursor.
 /// </param>
 /// <returns>A newly created cursor</returns>
 public new BTreeCursor Cursor(CursorConfig cfg)
 {
     return(Cursor(cfg, null));
 }