Example #1
0
        static void Main(string[] args)
        {
            BTreeDatabase       btreeDB;
            BTreeDatabaseConfig btreeConfig;
            Cursor        dbc;
            DatabaseEntry key;
            DatabaseEntry data;
            string        buff, dbFileName, keyString;

            /*
             * excs_access 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;
            }

            try {
                dbFileName = "access.db";
                if (args.Length > 0)
                {
                    dbFileName = args[0];
                }
            } catch {
                usage();
                return;
            }

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

                if (buff == "y")
                {
                    try {
                        File.Delete(dbFileName);
                    } catch {
                        Console.WriteLine("Unable to delete {0}.", dbFileName);
                        return;
                    }
                }
            }

            /* Configure the database. */
            btreeConfig             = new BTreeDatabaseConfig();
            btreeConfig.Duplicates  = DuplicatesPolicy.SORTED;
            btreeConfig.ErrorPrefix = "excs_access";
            btreeConfig.Creation    = CreatePolicy.IF_NEEDED;
            btreeConfig.CacheSize   = new CacheInfo(0, 64 * 1024, 1);
            btreeConfig.PageSize    = 8 * 1024;

            /* Create and open a new database in the file. */
            try {
                btreeDB = BTreeDatabase.Open(dbFileName, btreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening {0}.", dbFileName);
                Console.WriteLine(e.Message);
                return;
            }

            /* Input key/data pairs and insert them into the database.*/
            key  = new DatabaseEntry();
            data = new DatabaseEntry();

            while (true)
            {
                Console.Write("key [blank line to quit] > ");
                keyString = Console.ReadLine();
                if (keyString == "")
                {
                    break;
                }

                dbtFromString(key, keyString);
                dbtFromString(data, reverse(keyString));

                try {
                    btreeDB.Put(key, data);
                } catch {
                    return;
                }
            }

            /* Acquire a cursor for the database. */
            using (dbc = btreeDB.Cursor()) {
                /* Walk through the database and print out key/data pairs. */
                Console.WriteLine("All key : data pairs:");
                foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> p in dbc)
                {
                    Console.WriteLine("{0}::{1}",
                                      strFromDBT(p.Key), strFromDBT(p.Value));
                }
            }

            Console.Write("Press any key to exit >");
            Console.ReadKey(true);

            /* Close the cursor and database. */
            btreeDB.Close();
        }
        public void TestDuplicateToDifferentPos()
        {
            BTreeDatabase             db;
            BTreeDatabaseConfig       dbConfig;
            BTreeCursor               cursor, dupCursor;
            DatabaseEnvironment       env;
            DatabaseEnvironmentConfig envConfig;
            DatabaseEntry             key, data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            Transaction txn;

            testName = "TestDuplicateToDifferentPos";
            SetUpTest(true);

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

            txn               = env.BeginTransaction();
            dbConfig          = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            db   = BTreeDatabase.Open(testName + ".db", dbConfig, txn);
            key  = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"));
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(key, data);
            db.Put(key, data, txn);
            txn.Commit();

            txn    = env.BeginTransaction();
            cursor = db.Cursor(txn);
            cursor.Move(key, true);

            //Duplicate a new cursor to the same position.
            dupCursor = cursor.Duplicate(false);

            /*
             * The duplicate cursor points to nothing so overwriting the
             * record is not allowed.
             */
            try
            {
                dupCursor.Overwrite(new DatabaseEntry(
                                        ASCIIEncoding.ASCII.GetBytes("newdata")));
            }
            catch (DatabaseException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                dupCursor.Close();
                cursor.Close();
                txn.Commit();
                db.Close();
                env.Close();
            }
        }
        public void TestMoveMultipleKey()
        {
            testName = "TestMoveMultipleKey";
            SetUpTest(true);
            string btreeDBFileName = testHome + "/" +
                                     testName + ".db";
            BTreeDatabase            db;
            BTreeCursor              cursor;
            DatabaseEntry            key;
            MultipleKeyDatabaseEntry mulPair;;
            int cnt;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.ALWAYS;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            dbConfig.PageSize   = 1024;
            GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor);

            /*
             * Bulk retrieve key/value pair from the pair whose key
             * is exact 99.
             */
            cnt = 0;
            key = new DatabaseEntry(BitConverter.GetBytes((int)99));
            cursor.MoveMultipleKey(key, true);
            mulPair = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     pair in mulPair)
            {
                Assert.GreaterOrEqual(3,
                                      BitConverter.ToInt32(pair.Key.Data, 0) - 98);
                cnt++;
            }
            Assert.AreEqual(3, cnt);

            /*
             * Bulk retrieve key/value pair from the pair whose key
             * is the smallest one larger than 100.
             */
            cnt = 0;
            key = new DatabaseEntry(BitConverter.GetBytes((int)100));
            cursor.MoveMultipleKey(key, false);
            mulPair = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     pair in mulPair)
            {
                Assert.AreEqual(101,
                                BitConverter.ToInt32(pair.Key.Data, 0));
                cnt++;
            }
            Assert.LessOrEqual(1, cnt);

            cnt = 0;
            key = new DatabaseEntry(BitConverter.GetBytes((int)100));
            cursor.MoveMultipleKey(key, false, 1024);
            mulPair = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     pair in mulPair)
            {
                Assert.AreEqual(101,
                                BitConverter.ToInt32(pair.Key.Data, 0));
                Assert.AreEqual(101,
                                BitConverter.ToInt32(pair.Value.Data, 0));
                cnt++;
            }
            Assert.LessOrEqual(1, cnt);

            cursor.Close();
            db.Close();
        }
Example #4
0
        public void TestLoggingSystemStats()
        {
            testName = "TestLoggingSystemStats";
            testHome = testFixtureHome + "/" + testName;
            string logDir = "./";

            Configuration.ClearDir(testHome);
            Directory.CreateDirectory(testHome + "/" + logDir);

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create                   = true;
            cfg.UseTxns                  = true;
            cfg.AutoCommit               = true;
            cfg.UseLocking               = true;
            cfg.UseMPool                 = true;
            cfg.UseLogging               = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1);

            cfg.LogSystemCfg              = new LogConfig();
            cfg.LogSystemCfg.AutoRemove   = false;
            cfg.LogSystemCfg.BufferSize   = 10240;
            cfg.LogSystemCfg.Dir          = logDir;
            cfg.LogSystemCfg.FileMode     = 755;
            cfg.LogSystemCfg.ForceSync    = true;
            cfg.LogSystemCfg.InMemory     = false;
            cfg.LogSystemCfg.MaxFileSize  = 1048576;
            cfg.LogSystemCfg.NoBuffer     = false;
            cfg.LogSystemCfg.RegionSize   = 204800;
            cfg.LogSystemCfg.ZeroOnCreate = true;

            DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);

            LogStats stats = env.LoggingSystemStats();

            env.PrintLoggingSystemStats();
            Assert.AreEqual(10240, stats.BufferSize);
            Assert.AreEqual(1, stats.CurrentFile);
            Assert.AreNotEqual(0, stats.CurrentOffset);
            Assert.AreEqual(1048576, stats.FileSize);
            Assert.AreNotEqual(0, stats.MagicNumber);
            Assert.AreNotEqual(0, stats.PermissionsMode);
            Assert.AreEqual(1, stats.Records);
            Assert.AreNotEqual(0, stats.RegionLockNoWait);
            Assert.LessOrEqual(204800, stats.RegionSize);
            Assert.AreNotEqual(0, stats.Version);

            Transaction         openTxn  = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);

            openTxn.Commit();

            Transaction writeTxn = env.BeginTransaction();

            byte[] byteArr = new byte[1024];
            for (int i = 0; i < 1000; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(byteArr), writeTxn);
            }
            writeTxn.Commit();

            stats = env.LoggingSystemStats();
            Assert.AreNotEqual(0, stats.Bytes);
            Assert.AreNotEqual(0, stats.BytesSinceCheckpoint);
            Assert.AreNotEqual(0, stats.DiskFileNumber);
            Assert.AreNotEqual(0, stats.DiskOffset);
            Assert.AreNotEqual(0, stats.MaxCommitsPerFlush);
            Assert.AreNotEqual(0, stats.MBytes);
            Assert.AreNotEqual(0, stats.MBytesSinceCheckpoint);
            Assert.AreNotEqual(0, stats.MinCommitsPerFlush);
            Assert.AreNotEqual(0, stats.OverflowWrites);
            Assert.AreNotEqual(0, stats.Syncs);
            Assert.AreNotEqual(0, stats.Writes);
            Assert.AreEqual(0, stats.Reads);
            Assert.AreEqual(0, stats.RegionLockWait);

            stats = env.LoggingSystemStats(true);
            stats = env.LoggingSystemStats();
            Assert.AreEqual(0, stats.Bytes);
            Assert.AreEqual(0, stats.BytesSinceCheckpoint);
            Assert.AreEqual(0, stats.MaxCommitsPerFlush);
            Assert.AreEqual(0, stats.MBytes);
            Assert.AreEqual(0, stats.MBytesSinceCheckpoint);
            Assert.AreEqual(0, stats.MinCommitsPerFlush);
            Assert.AreEqual(0, stats.OverflowWrites);
            Assert.AreEqual(0, stats.Syncs);
            Assert.AreEqual(0, stats.Writes);
            Assert.AreEqual(0, stats.Reads);

            env.PrintLoggingSystemStats(true, true);

            db.Close();
            env.Close();
        }
Example #5
0
        public void TestMoveJoinCursor()
        {
            testName = "TestMoveJoinCursor";
            SetUpTest(true);
            string dbFileName   = testHome + "/" + testName + ".db";
            string secFileName1 = testHome + "/" + "sec_" + testName + "1.db";
            string secFileName2 = testHome + "/" + "sec_" + testName + "2.db";

            // Open a primary database.
            BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig();

            dbCfg.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbCfg);

            /*
             * Open a secondary database on high byte of
             * its data and another secondary database on
             * little byte of its data.
             */
            SecondaryBTreeDatabase secDB1, secDB2;

            SecondaryCursor[] cursors   = new SecondaryCursor[2];
            byte[]            byteValue = new byte[1];

            byteValue[0] = 0;
            GetSecCursor(db, secFileName1,
                         new SecondaryKeyGenDelegate(KeyGenOnBigByte),
                         out secDB1, out cursors[0], false,
                         new DatabaseEntry(byteValue));

            byteValue[0] = 1;
            GetSecCursor(db, secFileName2,
                         new SecondaryKeyGenDelegate(KeyGenOnLittleByte),
                         out secDB2, out cursors[1], true,
                         new DatabaseEntry(byteValue));

            // Get join cursor.
            JoinCursor joinCursor = db.Join(cursors, true);

            /*
             * MoveNextJoinItem do not use data value found
             * in all of the cursor so the data in Current won't be
             * changed.
             */
            Assert.IsTrue(joinCursor.MoveNextItem());
            Assert.AreEqual(0, joinCursor.Current.Key.Data[
                                joinCursor.Current.Key.Data.Length - 1]);
            Assert.AreEqual(1, joinCursor.Current.Key.Data[0]);
            Assert.IsNull(joinCursor.Current.Value.Data);

            // Iterate on join cursor.
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> pair in joinCursor)
            {
                /*
                 * Confirm that the key got by join cursor has 0 at
                 * its highest byte and 1 at its lowest byte.
                 */
                Assert.AreEqual(0, pair.Key.Data[pair.Key.Data.Length - 1]);
                Assert.AreEqual(1, pair.Key.Data[0]);
            }

            Assert.IsFalse(joinCursor.MoveNext());

            // Close all.
            joinCursor.Close();
            cursors[0].Close();
            cursors[1].Close();
            secDB1.Close();
            secDB2.Close();
            db.Close();
        }
Example #6
0
        public void TestForeignKeyDelete(DatabaseType dbtype, ForeignKeyDeleteAction action)
        {
            SetUpTest(true);
            string dbFileName  = testHome + "/" + testName + ".db";
            string fdbFileName = testHome + "/" + testName + "foreign.db";
            string sdbFileName = testHome + "/" + testName + "sec.db";

            Database          primaryDB, fdb;
            SecondaryDatabase secDB;

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

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

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

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

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

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

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

            // Close foreign database
            fdb.Close();
        }
Example #7
0
        public void TestDuplicate()
        {
            testName = "TestDuplicate";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName    = testHome + "/" + testName + ".db";
            string dbSecFileName = testHome + "/" + testName +
                                   "_sec.db";

            Configuration.ClearDir(testHome);

            // 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();
        }
Example #8
0
        /* Initialize environment and database (s) */
        public void InitDbs()
        {
            /* Open transactional environment */
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                 = true;
            envConfig.UseMPool               = true;
            envConfig.UseLocking             = true;
            envConfig.UseLogging             = true;
            envConfig.UseTxns                = true;
            envConfig.LockSystemCfg          = new LockingConfig();
            envConfig.LockSystemCfg.MaxLocks =
                (this.dups == 0) ?
                (uint)this.num :
                (uint)(this.num * this.dups);
            envConfig.LockSystemCfg.MaxObjects =
                envConfig.LockSystemCfg.MaxLocks;
            if (this.cachesize != 0)
            {
                envConfig.MPoolSystemCfg           = new MPoolConfig();
                envConfig.MPoolSystemCfg.CacheSize =
                    new CacheInfo(0, this.cachesize, 1);
            }

            try {
                env = DatabaseEnvironment.Open(home, envConfig);
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                System.Environment.Exit(1);
            }

            Transaction txn = env.BeginTransaction();

            try {
                /* Open primary db in transaction */
                BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                dbConfig.Env      = env;
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                if (this.pagesize != 0)
                {
                    dbConfig.PageSize = this.pagesize;
                }
                if (this.dups != 0)
                {
                    dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
                }
                pdb = BTreeDatabase.Open(dbFileName, pDbName, dbConfig, txn);

                /* Open secondary db in transaction */
                if (this.secondary)
                {
                    SecondaryBTreeDatabaseConfig sdbConfig =
                        new SecondaryBTreeDatabaseConfig(
                            pdb, new SecondaryKeyGenDelegate(SecondaryKeyGen));
                    sdbConfig.Creation = CreatePolicy.IF_NEEDED;
                    if (this.pagesize != 0)
                    {
                        sdbConfig.PageSize = this.pagesize;
                    }
                    sdbConfig.Duplicates = DuplicatesPolicy.SORTED;
                    sdbConfig.Env        = env;
                    sdb = SecondaryBTreeDatabase.Open(
                        dbFileName, sDbName, sdbConfig, txn);
                }

                txn.Commit();
            } catch (DatabaseException e1) {
                txn.Abort();
                throw e1;
            } catch (FileNotFoundException e2) {
                txn.Abort();
                throw e2;
            }
        }
        public int doloop()
        {
            BTreeDatabase db = null;

            for (;;)
            {
                if (db == null)
                {
                    BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                    dbConfig.Env = dbenv.env;

                    if (dbenv.IsMaster)
                    {
                        /*
                         * Open database allowing create only if this is a master
                         * database.  A client database uses polling to attempt
                         * to open the database, without creating it, until the
                         * open succeeds.
                         *
                         * This polling logic for allowing create can be
                         * simplified under some circumstances.  For example, if
                         * the application can be sure a database is already
                         * there, it would never need to open it allowing create.
                         */
                        dbConfig.Creation = CreatePolicy.IF_NEEDED;
                    }

                    dbConfig.AutoCommit = true;

                    try
                    {
                        db = BTreeDatabase.Open(RepConfig.progname, dbConfig);
                    } catch (DatabaseException)
                    {
                        Console.WriteLine("no stock database available yet.");
                        if (db != null)
                        {
                            db.Close(true);
                            db = null;
                        }

                        Thread.Sleep(RepConfig.SLEEPTIME);
                        continue;
                    }
                }

                /* Listen for input, and add it to the database. */
                Console.Write("QUOTESERVER");
                if (dbenv.IsMaster == false)
                {
                    Console.Write("(read-only)");
                }
                Console.Write("> ");
                string nextLine = null;
                try
                {
                    nextLine = Console.ReadLine();
                } catch (System.IO.IOException)
                {
                    Console.WriteLine("Unable to get data");
                    break;
                }

                /* A blank line causes the DB to be dumped. */
                string[] words = nextLine.Split(' ');
                if (words.Length == 0 ||
                    words.Length == 1 && words[0].Length == 0)
                {
                    try
                    {
                        if (dbenv.InClientSync)
                        {
                            Console.WriteLine("Cannot read data during " +
                                              "client initialization - please try again.");
                        }
                        else
                        {
                            printStocks(db);
                        }
                    } catch (DeadlockException)
                    {
                        continue;
                    } catch (DatabaseException e)
                    {
                        /*
                         * This could be DB_REP_HANDLE_DEAD, which
                         * should close the database and continue.
                         */
                        Console.WriteLine("Got db exception reading replication"
                                          + "DB: " + e);
                        Console.WriteLine("Expected if it was due to a dead " +
                                          "replication handle, otherwise an unexpected error.");
                        db.Close(false);                        /* Close no sync. */
                        db = null;
                        continue;
                    }
                    continue;
                }

                if (words.Length == 1 &&
                    (words[0].ToLower().Equals("quit") ||
                     words[0].ToLower().Equals("exit")))
                {
                    dbenv.AppFinished = true;
                    break;
                }
                else if (words.Length != 2)
                {
                    Console.WriteLine("Format: TICKER VALUE");
                    continue;
                }

                if (!dbenv.IsMaster)
                {
                    Console.WriteLine("Can't update client");
                    continue;
                }

                DatabaseEntry key = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(words[0]));
                DatabaseEntry data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(words[1]));

                db.Put(key, data);
            }

            if (db != null)
            {
                db.Close(true);
            }

            return(0);
        }
Example #10
0
        public MyDbs(string databaseHome)
        {
            vDbName     = "vendordb.db";
            iDbName     = "inventorydb.db";
            itemSDbName = "itemname.sdb";

            if (databaseHome != null)
            {
                vDbName     = databaseHome + "\\" + vDbName;
                iDbName     = databaseHome + "\\" + iDbName;
                itemSDbName = databaseHome + "\\" + itemSDbName;
            }

            btreeConfig             = new BTreeDatabaseConfig();
            btreeConfig.Creation    = CreatePolicy.IF_NEEDED;
            btreeConfig.CacheSize   = new CacheInfo(0, 64 * 1024, 1);
            btreeConfig.ErrorPrefix = "excs_getting_started";
            btreeConfig.PageSize    = 8 * 1024;

            /* Optionally remove existing database files. */
            try {
                RemoveDbFile(vDbName);
                RemoveDbFile(iDbName);
                RemoveDbFile(itemSDbName);
            } catch (Exception e) {
                Console.WriteLine("Error deleting db.");
                Console.WriteLine(e.Message);
                throw e;
            }

            /* Create and open the Inventory and Vendor database files. */
            try {
                vbtreeDB = BTreeDatabase.Open(vDbName, btreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening {0}.", vDbName);
                Console.WriteLine(e.Message);
                throw e;
            }

            try {
                ibtreeDB = BTreeDatabase.Open(iDbName, btreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening {0}.", iDbName);
                Console.WriteLine(e.Message);
                throw e;
            }

            /*
             * Open a secondary btree database associated with the
             * Inventory database.
             */
            try {
                itemSecbtreeConfig = new SecondaryBTreeDatabaseConfig(
                    ibtreeDB, new SecondaryKeyGenDelegate(
                        CreateSecondaryKey));

                itemSecbtreeConfig.Creation   = CreatePolicy.IF_NEEDED;
                itemSecbtreeConfig.Duplicates = DuplicatesPolicy.UNSORTED;
                itemSecbtreeDB = SecondaryBTreeDatabase.Open(
                    itemSDbName, itemSecbtreeConfig);
            } catch (Exception e) {
                Console.WriteLine("Error opening secondary {0}", itemSDbName);
                Console.WriteLine(e.Message);
                throw e;
            }
        }
Example #11
0
        public void TestLockStats()
        {
            testName = "TestLockStats";
            SetUpTest(true);

            // Configure locking subsystem.
            LockingConfig lkConfig = new LockingConfig();

            lkConfig.MaxLockers         = 60;
            lkConfig.MaxLocks           = 50;
            lkConfig.MaxObjects         = 70;
            lkConfig.Partitions         = 20;
            lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Configure and open environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                   = true;
            envConfig.FreeThreaded             = true;
            envConfig.LockSystemCfg            = lkConfig;
            envConfig.LockTimeout              = 1000;
            envConfig.MPoolSystemCfg           = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1);
            envConfig.NoLocking                = false;
            envConfig.TxnTimeout               = 2000;
            envConfig.UseLocking               = true;
            envConfig.UseMPool                 = true;
            envConfig.UseTxns                  = true;
            DatabaseEnvironment env =
                DatabaseEnvironment.Open(testHome, envConfig);

            // Get and confirm locking subsystem statistics.
            LockStats stats = env.LockingSystemStats();

            env.PrintLockingSystemStats(true, true);
            Assert.AreEqual(0, stats.AllocatedLockers);
            Assert.AreNotEqual(0, stats.AllocatedLocks);
            Assert.AreNotEqual(0, stats.AllocatedObjects);
            Assert.AreEqual(0, stats.InitLockers);
            Assert.AreNotEqual(0, stats.InitLocks);
            Assert.AreNotEqual(0, stats.InitObjects);
            Assert.AreEqual(0, stats.LastAllocatedLockerID);
            Assert.AreEqual(0, stats.LockConflictsNoWait);
            Assert.AreEqual(0, stats.LockConflictsWait);
            Assert.AreEqual(0, stats.LockDeadlocks);
            Assert.AreEqual(0, stats.LockDowngrades);
            Assert.AreEqual(0, stats.LockerNoWait);
            Assert.AreEqual(0, stats.Lockers);
            Assert.AreEqual(0, stats.LockerWait);
            Assert.AreEqual(9, stats.LockModes);
            Assert.AreEqual(0, stats.LockPuts);
            Assert.AreEqual(0, stats.LockRequests);
            Assert.AreEqual(0, stats.Locks);
            Assert.AreEqual(0, stats.LockSteals);
            Assert.AreEqual(1000, stats.LockTimeoutLength);
            Assert.AreEqual(0, stats.LockTimeouts);
            Assert.AreEqual(0, stats.LockUpgrades);
            Assert.AreEqual(0, stats.MaxBucketLength);
            Assert.AreEqual(0, stats.MaxLockers);
            Assert.AreEqual(60, stats.MaxLockersInTable);
            Assert.AreEqual(0, stats.MaxLocks);
            Assert.AreEqual(0, stats.MaxLocksInBucket);
            Assert.AreEqual(50, stats.MaxLocksInTable);
            Assert.AreEqual(0, stats.MaxLockSteals);
            Assert.AreEqual(0, stats.MaxObjects);
            Assert.AreEqual(0, stats.MaxObjectsInBucket);
            Assert.AreEqual(70, stats.MaxObjectsInTable);
            Assert.AreEqual(0, stats.MaxObjectSteals);
            Assert.AreEqual(0, stats.MaxPartitionLockNoWait);
            Assert.AreEqual(0, stats.MaxPartitionLockWait);
            Assert.AreNotEqual(0, stats.MaxUnusedID);
            Assert.AreEqual(20, stats.nPartitions);
            Assert.AreEqual(0, stats.ObjectNoWait);
            Assert.AreEqual(0, stats.Objects);
            Assert.AreEqual(0, stats.ObjectSteals);
            Assert.AreEqual(0, stats.ObjectWait);
            Assert.LessOrEqual(0, stats.PartitionLockNoWait);
            Assert.AreEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.AreNotEqual(0, stats.RegionSize);
            Assert.AreEqual(0, stats.RegionWait);
            Assert.AreNotEqual(0, stats.TableSize);
            Assert.AreEqual(2000, stats.TxnTimeoutLength);
            Assert.AreEqual(0, stats.TxnTimeouts);

            env.PrintLockingSystemStats();

            Transaction         txn      = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation     = CreatePolicy.IF_NEEDED;
            dbConfig.Env          = env;
            dbConfig.FreeThreaded = true;
            BTreeDatabase db = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);

            txn.Commit();

            testLockStatsEnv = env;
            testLockStatsDb  = db;

            Thread thread1 = new Thread(new ThreadStart(Write));
            Thread thread2 = new Thread(new ThreadStart(Read));

            thread1.Start();
            Thread.Sleep(10);
            thread2.Start();
            thread1.Join();
            thread2.Join();

            env.PrintLockingSystemStats();
            stats = env.LockingSystemStats();
            Assert.Less(0, stats.LastAllocatedLockerID);
            Assert.Less(0, stats.LockConflictsNoWait);
            Assert.LessOrEqual(0, stats.LockConflictsWait);
            Assert.LessOrEqual(0, stats.LockDeadlocks);
            Assert.Less(0, stats.LockDowngrades);
            Assert.LessOrEqual(0, stats.LockerNoWait);
            Assert.Less(0, stats.Lockers);
            Assert.LessOrEqual(0, stats.LockerWait);
            Assert.Less(0, stats.LockPuts);
            Assert.Less(0, stats.LockRequests);
            Assert.Less(0, stats.Locks);
            Assert.LessOrEqual(0, stats.LockSteals);
            Assert.LessOrEqual(0, stats.LockTimeouts);
            Assert.LessOrEqual(0, stats.LockUpgrades);
            Assert.Less(0, stats.MaxBucketLength);
            Assert.Less(0, stats.MaxLockers);
            Assert.Less(0, stats.MaxLocks);
            Assert.Less(0, stats.MaxLocksInBucket);
            Assert.LessOrEqual(0, stats.MaxLockSteals);
            Assert.Less(0, stats.MaxObjects);
            Assert.Less(0, stats.MaxObjectsInBucket);
            Assert.LessOrEqual(0, stats.MaxObjectSteals);
            Assert.LessOrEqual(0, stats.MaxPartitionLockNoWait);
            Assert.LessOrEqual(0, stats.MaxPartitionLockWait);
            Assert.Less(0, stats.MaxUnusedID);
            Assert.Less(0, stats.ObjectNoWait);
            Assert.Less(0, stats.Objects);
            Assert.LessOrEqual(0, stats.ObjectSteals);
            Assert.LessOrEqual(0, stats.ObjectWait);
            Assert.Less(0, stats.PartitionLockNoWait);
            Assert.LessOrEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.LessOrEqual(0, stats.RegionWait);
            Assert.Less(0, stats.TxnTimeouts);

            db.Close();
            env.Close();
        }
Example #12
0
        public void TestRefreshMultipleAndMultipleKey()
        {
            testName = "TestRefreshMultipleAndMultipleKey";
            SetUpTest(true);
            string btreeDBFileName = testHome + "/" +
                                     testName + ".db";
            BTreeDatabase db;
            BTreeCursor   cursor;
            DatabaseEntry key, data;
            KeyValuePair <DatabaseEntry, DatabaseEntry>         pair;
            KeyValuePair <DatabaseEntry, MultipleDatabaseEntry> pairs;
            MultipleKeyDatabaseEntry multiPair;
            int cnt;

            int[] size = new int[2];
            size[0] = 0;
            size[1] = 1024;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.ALWAYS;
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            dbConfig.PageSize   = 1024;
            GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor);

            key  = new DatabaseEntry(BitConverter.GetBytes(99));
            data = new DatabaseEntry(BitConverter.GetBytes(99));
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(key, data);

            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 2; i++)
                {
                    cnt = 0;
                    cursor.Move(pair, true);
                    if (j == 0)
                    {
                        if (size[i] == 0)
                        {
                            Assert.IsTrue(cursor.RefreshMultiple());
                        }
                        else
                        {
                            Assert.IsTrue(cursor.RefreshMultiple(size[i]));
                        }
                        pairs = cursor.CurrentMultiple;
                        foreach (DatabaseEntry dbt in pairs.Value)
                        {
                            cnt++;
                        }
                        Assert.AreEqual(2, cnt);
                    }
                    else
                    {
                        if (size[i] == 0)
                        {
                            Assert.IsTrue(cursor.RefreshMultipleKey());
                        }
                        else
                        {
                            Assert.IsTrue(cursor.RefreshMultipleKey(size[i]));
                        }
                        multiPair = cursor.CurrentMultipleKey;
                        foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> p in multiPair)
                        {
                            cnt++;
                        }
                        Assert.AreEqual(3, cnt);
                    }
                }
            }

            cursor.Close();
            db.Close();
        }
Example #13
0
        public void TestMoveNextMultipleKey()
        {
            testName = "TestMoveNextMultipleKey";
            SetUpTest(true);

            BTreeDatabase       db;
            BTreeCursor         cursor;
            DatabaseEnvironment env;
            Transaction         txn;
            int cnt = 0;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.ALWAYS;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;

            GetMultipleDB(testHome, testName + ".db", dbConfig,
                          out env, out txn, out db, out cursor);

            /*
             * Position the cursor to the first record, and bulk get
             * all other records.
             */
            Assert.True(cursor.MoveFirst());
            Assert.True(cursor.MoveNextMultipleKey());
            MultipleKeyDatabaseEntry pairs =
                cursor.CurrentMultipleKey;

            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> p
                     in pairs)
            {
                cnt++;
            }
            Assert.AreEqual(cnt, 100);

            // Bulk read uncommitted records.
            cnt = 0;
            LockingInfo lockingInfo = new LockingInfo();

            Assert.True(cursor.MoveFirst(lockingInfo));
            lockingInfo.IsolationDegree = Isolation.DEGREE_ONE;
            Assert.True(cursor.MoveNextMultipleKey(lockingInfo));
            pairs = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry> p
                     in pairs)
            {
                cnt++;
            }
            Assert.AreEqual(cnt, 100);

            cursor.Dispose();
            txn.Commit();

            /*
             * Insert duplicate records of the key in the last
             * record.
             */
            txn    = env.BeginTransaction();
            cursor = db.Cursor(txn);
            Assert.IsTrue(cursor.MoveLast());
            DatabaseEntry key = cursor.Current.Key;

            for (int i = 200; i < 800; i++)
            {
                cursor.Insert(new DatabaseEntry(
                                  BitConverter.GetBytes(i)),
                              Cursor.InsertLocation.AFTER);
            }

            /*
             * Move to the first duplicate records, and bulk read
             * all duplicate ones.
             */
            cnt = 0;
            Assert.True(cursor.Move(key, true));
            while (cursor.MoveNextDuplicateMultipleKey(
                       (int)db.Pagesize))
            {
                pairs = cursor.CurrentMultipleKey;
                foreach (KeyValuePair <
                             DatabaseEntry, DatabaseEntry> p in pairs)
                {
                    Assert.AreEqual(key.Data, p.Key.Data);
                    cnt++;
                }
            }
            Assert.AreEqual(cnt, 600);

            cursor.Close();
            db.Close();
            txn.Commit();
            env.Close();
        }
        public static int excs_btrec(string dbFileName,
                                     string dbName, string wordFile)
        {
            BTreeCursor         dbc;
            BTreeDatabase       btreeDB;
            BTreeDatabaseConfig btreeDBconfig;
            DatabaseEntry       key, data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            StreamReader wordList;
            string       buff;

            char[] rbuff;
            int    i, j;
            uint   cnt;

            string progName = "excs_btrec";

            /* Check if word list exists. */
            if (!File.Exists(wordFile))
            {
                Console.WriteLine("Cannot find {0}/{1}.",
                                  Environment.CurrentDirectory, wordFile);
                return(1);
            }

            /* Optionally remove the previous database. */
            if (File.Exists(dbFileName))
            {
                while (true)
                {
                    Console.Write
                        ("Database 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",
                                      dbName);
                }
            }

            /* Create and initialize database object, open the database. */
            btreeDBconfig                  = new BTreeDatabaseConfig();
            btreeDBconfig.Creation         = CreatePolicy.IF_NEEDED;
            btreeDBconfig.ErrorPrefix      = progName;
            btreeDBconfig.UseRecordNumbers = true;
            try {
                btreeDB = BTreeDatabase.Open(dbFileName, dbName,
                                             btreeDBconfig);
            } catch {
                Console.WriteLine("Database can not be opened");
                return(1);
            }

            /*
             * Insert records into the database, where the key is the word
             * preceded by its record number, and the data is the same,
             * but in reverse order.
             */
            key      = new DatabaseEntry();
            data     = new DatabaseEntry();
            wordList = new StreamReader(wordFile);
            for (cnt = 1; cnt <= 1000; cnt++)
            {
                /* Read one word from word list. */
                try {
                    buff = wordList.ReadLine();
                } catch {
                    Console.WriteLine("Error when reading word list");
                    btreeDB.Close();
                    wordList.Close();
                    return(1);
                }

                /*
                 * Prefix the word with record number and store
                 * them in key.
                 */
                try {
                    buff = cnt.ToString("0000") + buff;
                } catch {
                    Console.WriteLine(
                        "Wrong format for record number.");
                    btreeDB.Close();
                    wordList.Close();
                    return(1);
                }
                dbtFromString(key, buff);

                /* Reverse buff to rbuff and store them in data. */
                rbuff = new char[buff.Length];
                for (i = 0, j = buff.Length - 1; i < buff.Length; i++, j--)
                {
                    rbuff[i] = buff[j];
                }
                dbtFromString(data, new string(rbuff));

                /* Put the key/data pair into database. */
                try {
                    btreeDB.PutNoOverwrite(key, data);
                } catch {
                    Console.WriteLine("Put error with key::{0} and"
                                      + "data::{1}", buff, new string(rbuff));
                    btreeDB.Close();
                    wordList.Close();
                    return(1);
                }
            }

            /* Close the word list. */
            wordList.Close();

            /* Acquire a cursor for the database. */
            dbc = btreeDB.Cursor();

            /*
             * Input an available record number and get its
             * corresponding key/data pair in the database.
             */
            while (true)
            {
                Console.WriteLine("Please input record number"
                                  + ", input 0 to stop");

                while (true)
                {
                    Console.WriteLine("recno #:\t");
                    buff = Console.ReadLine();

                    try {
                        cnt = uint.Parse(buff);
                        break;
                    } catch {
                        Console.WriteLine("Invalid Record Number. "
                                          + "Try Again.");
                    }
                }

                if (cnt == 0)
                {
                    break;
                }

                /*
                 * Position the cursor at a key/data pair in the database
                 * where the record number points to.
                 */
                if (!dbc.Move(cnt))
                {
                    Console.WriteLine("No record is found");
                    break;
                }

                /* Get the current key/data pair and display them.*/
                pair = dbc.Current;
                Console.WriteLine("key::{0, 18:G}    data::{1, 18:G}",
                                  strFromDBT(pair.Key), strFromDBT(pair.Value));

                /* Check if it's the right record number. */
                if (dbc.Recno() != cnt)
                {
                    Console.WriteLine("The current record is not"
                                      + "the right one of the given record number.");
                    btreeDB.Close();
                    dbc.Close();
                    return(1);
                }

                /* Get the next record. */
                if (!dbc.MoveNext())
                {
                    Console.WriteLine(
                        "Next record is not found");
                    break;
                }

                /* Get the current key/data pair and display them.*/
                pair = dbc.Current;
                Console.WriteLine("key::{0, 18:G}    data::{1, 18:G}\n",
                                  strFromDBT(pair.Key), strFromDBT(pair.Value));
            }

            /* Close cursor and database. */
            dbc.Close();
            btreeDB.Close();

            return(0);
        }
Example #15
0
        public void Master()
        {
            string home   = testHome + "/Master";
            string dbName = "rep.db";

            Configuration.ClearDir(home);

            /*
             * Configure and open environment with replication
             * application.
             */
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority        = 100;
            cfg.RepSystemCfg.BulkTransfer    = true;
            cfg.RepSystemCfg.AckTimeout      = 2000;
            cfg.RepSystemCfg.BulkTransfer    = true;
            cfg.RepSystemCfg.CheckpointDelay = 1500;
            cfg.RepSystemCfg.Clockskew(102, 100);
            cfg.RepSystemCfg.ConnectionRetry     = 10;
            cfg.RepSystemCfg.DelayClientSync     = false;
            cfg.RepSystemCfg.ElectionRetry       = 5;
            cfg.RepSystemCfg.ElectionTimeout     = 3000;
            cfg.RepSystemCfg.FullElectionTimeout = 5000;
            cfg.RepSystemCfg.HeartbeatMonitor    = 100;
            cfg.RepSystemCfg.HeartbeatSend       = 10;
            cfg.RepSystemCfg.LeaseTimeout        = 1300;
            cfg.RepSystemCfg.AutoInit            = true;
            cfg.RepSystemCfg.NoBlocking          = false;
            cfg.RepSystemCfg.RepMgrAckPolicy     =
                AckPolicy.ALL_PEERS;
            cfg.RepSystemCfg.RetransmissionRequest(10, 100);
            cfg.RepSystemCfg.Strict2Site     = true;
            cfg.RepSystemCfg.UseMasterLeases = false;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            // Get initial replication stats.
            ReplicationStats repStats = env.ReplicationSystemStats();

            env.PrintReplicationSystemStats();
            Assert.AreEqual(100, repStats.EnvPriority);
            Assert.AreEqual(1,
                            repStats.CurrentElectionGenerationNumber);
            Assert.AreEqual(0, repStats.CurrentGenerationNumber);
            Assert.AreEqual(0, repStats.AppliedTransactions);
            Assert.AreEqual(0, repStats.ElectionDataGeneration);

            // Start a master site with replication manager.
            env.RepMgrStartMaster(3);

            // Open a btree database and write some data.
            Transaction         txn      = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            dbConfig.PageSize = 512;
            BTreeDatabase db = BTreeDatabase.Open(dbName,
                                                  dbConfig, txn);

            txn.Commit();
            txn = env.BeginTransaction();
            for (int i = 0; i < 5; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(BitConverter.GetBytes(i)), txn);
            }
            txn.Commit();

            Console.WriteLine(
                "Master: Finished initialization and data#1.");

            // Client site could enter now.
            clientStartSignal.Set();
            Console.WriteLine(
                "Master: Wait for Client to join and get #1.");

            Console.WriteLine("...");

            // Put some new data into master site.
            txn = env.BeginTransaction();
            for (int i = 10; i < 15; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(BitConverter.GetBytes(i)),
                       txn);
            }
            txn.Commit();
            Console.WriteLine(
                "Master: Write something new, data #2.");
            Console.WriteLine("Master: Wait for client to read #2...");

            // Get the stats.
            repStats = env.ReplicationSystemStats(true);
            env.PrintReplicationSystemStats();
            Assert.LessOrEqual(0, repStats.AppliedTransactions);
            Assert.LessOrEqual(0, repStats.AwaitedLSN.LogFileNumber);
            Assert.LessOrEqual(0, repStats.AwaitedLSN.Offset);
            Assert.LessOrEqual(0, repStats.AwaitedPage);
            Assert.LessOrEqual(0, repStats.BadGenerationMessages);
            Assert.LessOrEqual(0, repStats.BulkBufferFills);
            Assert.LessOrEqual(0, repStats.BulkBufferOverflows);
            Assert.LessOrEqual(0, repStats.BulkBufferTransfers);
            Assert.LessOrEqual(0, repStats.BulkRecordsStored);
            Assert.LessOrEqual(0, repStats.ClientServiceRequests);
            Assert.LessOrEqual(0, repStats.ClientServiceRequestsMissing);
            Assert.IsInstanceOf(typeof(bool), repStats.ClientStartupComplete);
            Assert.AreEqual(2, repStats.CurrentElectionGenerationNumber);
            Assert.AreEqual(1, repStats.CurrentGenerationNumber);
            Assert.LessOrEqual(0, repStats.CurrentQueuedLogRecords);
            Assert.LessOrEqual(0, repStats.CurrentWinner);
            Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.LogFileNumber);
            Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.Offset);
            Assert.LessOrEqual(0, repStats.DuplicateLogRecords);
            Assert.LessOrEqual(0, repStats.DuplicatePages);
            Assert.LessOrEqual(0, repStats.DupMasters);
            Assert.LessOrEqual(0, repStats.ElectionGenerationNumber);
            Assert.LessOrEqual(0, repStats.ElectionPriority);
            Assert.LessOrEqual(0, repStats.Elections);
            Assert.LessOrEqual(0, repStats.ElectionStatus);
            Assert.LessOrEqual(0, repStats.ElectionsWon);
            Assert.LessOrEqual(0, repStats.ElectionTiebreaker);
            Assert.LessOrEqual(0, repStats.ElectionTimeSec);
            Assert.LessOrEqual(0, repStats.ElectionTimeUSec);
            Assert.AreEqual(repStats.EnvID, repStats.MasterEnvID);
            Assert.LessOrEqual(0, repStats.EnvPriority);
            Assert.LessOrEqual(0, repStats.FailedMessageSends);
            Assert.LessOrEqual(0, repStats.ForcedRerequests);
            Assert.LessOrEqual(0, repStats.IgnoredMessages);
            Assert.LessOrEqual(0, repStats.MasterChanges);
            Assert.LessOrEqual(0, repStats.MasterEnvID);
            Assert.LessOrEqual(0, repStats.MaxLeaseSec);
            Assert.LessOrEqual(0, repStats.MaxLeaseUSec);
            Assert.LessOrEqual(0, repStats.MaxPermanentLSN.Offset);
            Assert.LessOrEqual(0, repStats.MaxQueuedLogRecords);
            Assert.LessOrEqual(0, repStats.MessagesSent);
            Assert.LessOrEqual(0, repStats.MissedLogRecords);
            Assert.LessOrEqual(0, repStats.MissedPages);
            Assert.LessOrEqual(0, repStats.NewSiteMessages);
            Assert.LessOrEqual(repStats.MaxPermanentLSN.LogFileNumber,
                               repStats.NextLSN.LogFileNumber);
            if (repStats.MaxPermanentLSN.LogFileNumber ==
                repStats.NextLSN.LogFileNumber)
            {
                Assert.Less(repStats.MaxPermanentLSN.Offset,
                            repStats.NextLSN.Offset);
            }
            Assert.LessOrEqual(0, repStats.NextPage);
            Assert.LessOrEqual(0, repStats.Outdated);
            Assert.LessOrEqual(0, repStats.QueuedLogRecords);
            Assert.LessOrEqual(0, repStats.ReceivedLogRecords);
            Assert.LessOrEqual(0, repStats.ReceivedMessages);
            Assert.LessOrEqual(0, repStats.ReceivedPages);
            Assert.LessOrEqual(0, repStats.RegisteredSites);
            Assert.LessOrEqual(0, repStats.RegisteredSitesNeeded);
            Assert.LessOrEqual(0, repStats.Sites);
            Assert.LessOrEqual(0, repStats.StartSyncMessagesDelayed);
            Assert.AreEqual(2, repStats.Status);
            Assert.LessOrEqual(0, repStats.Throttled);
            Assert.LessOrEqual(0, repStats.Votes);

            // Get replication manager statistics.
            RepMgrStats repMgrStats = env.RepMgrSystemStats(true);

            Assert.LessOrEqual(0, repMgrStats.DroppedConnections);
            Assert.LessOrEqual(0, repMgrStats.DroppedMessages);
            Assert.LessOrEqual(0, repMgrStats.FailedConnections);
            Assert.LessOrEqual(0, repMgrStats.FailedMessages);
            Assert.LessOrEqual(0, repMgrStats.QueuedMessages);

            // Print them out.
            env.PrintRepMgrSystemStats();

            // Wait until client has finished reading.
            masterCloseSignal.WaitOne();
            Console.WriteLine("Master: Leave as well.");

            // Close all.
            db.Close(false);
            env.LogFlush();
            env.Close();
        }
        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));
            }
        }
Example #17
0
        public void Client()
        {
            string home = testHome + "/Client";

            Configuration.ClearDir(home);

            clientStartSignal.WaitOne();
            Console.WriteLine("Client: Join the replication");

            // Open a environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[1];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            // Start a client site with replication manager.
            env.RepMgrStartClient(3, false);

            // Leave enough time to sync.
            Thread.Sleep(20000);

            // Open database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.NEVER;
            dbConfig.AutoCommit = true;
            dbConfig.Env        = env;
            dbConfig.PageSize   = 512;
            BTreeDatabase db = BTreeDatabase.Open("rep.db",
                                                  dbConfig);

            // Write data into database.
            Console.WriteLine("Client: Start reading data #1.");
            for (int i = 0; i < 5; i++)
            {
                db.GetBoth(new DatabaseEntry(
                               BitConverter.GetBytes(i)), new DatabaseEntry(
                               BitConverter.GetBytes(i)));
            }

            // Leave sometime for client to read new data from master.
            Thread.Sleep(20000);

            /*
             * Read the data. All data exists in master site should
             * appear in the client site.
             */
            Console.WriteLine("Client: Start reading data #2.");
            for (int i = 10; i < 15; i++)
            {
                db.GetBoth(new DatabaseEntry(
                               BitConverter.GetBytes(i)), new DatabaseEntry(
                               BitConverter.GetBytes(i)));
            }

            // Get the latest replication subsystem statistics.
            ReplicationStats repStats = env.ReplicationSystemStats();

            Assert.IsTrue(repStats.ClientStartupComplete);
            Assert.LessOrEqual(0, repStats.DuplicateLogRecords);
            Assert.LessOrEqual(0, repStats.EnvID);
            Assert.LessOrEqual(0, repStats.NextPage);
            Assert.LessOrEqual(0, repStats.ReceivedPages);
            Assert.AreEqual(1, repStats.Status);

            // Close all.
            db.Close(false);
            env.LogFlush();
            env.Close();
            Console.WriteLine(
                "Client: All data is read. Leaving the replication");

            // The master is closed after client's close.
            masterCloseSignal.Set();
        }
        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();
            }
        }
Example #19
0
        public void TestLockStats()
        {
            testName = "TestLockStats";
            SetUpTest(true);

            // Configure locking subsystem.
            LockingConfig lkConfig = new LockingConfig();

            lkConfig.MaxLockers         = 60;
            lkConfig.MaxLocks           = 50;
            lkConfig.MaxObjects         = 70;
            lkConfig.Partitions         = 20;
            lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Configure and open environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                   = true;
            envConfig.FreeThreaded             = true;
            envConfig.LockSystemCfg            = lkConfig;
            envConfig.LockTimeout              = 1000;
            envConfig.MPoolSystemCfg           = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1);
            envConfig.NoLocking                = false;
            envConfig.TxnTimeout               = 2000;
            envConfig.UseLocking               = true;
            envConfig.UseMPool                 = true;
            envConfig.UseTxns                  = true;
            DatabaseEnvironment env =
                DatabaseEnvironment.Open(testHome, envConfig);

            // Get and confirm locking subsystem statistics.
            LockStats stats = env.LockingSystemStats();

            env.PrintLockingSystemStats(true, true);
            Assert.AreEqual(0, stats.AllocatedLockers);
            Assert.AreNotEqual(0, stats.AllocatedLocks);
            Assert.AreNotEqual(0, stats.AllocatedObjects);
            Assert.AreEqual(0, stats.InitLockers);
            Assert.AreNotEqual(0, stats.InitLocks);
            Assert.AreNotEqual(0, stats.InitObjects);
            Assert.AreEqual(0, stats.LastAllocatedLockerID);
            Assert.AreEqual(0, stats.LockConflictsNoWait);
            Assert.AreEqual(0, stats.LockConflictsWait);
            Assert.AreEqual(0, stats.LockDeadlocks);
            Assert.AreEqual(0, stats.LockDowngrades);
            Assert.AreEqual(0, stats.LockerNoWait);
            Assert.AreEqual(0, stats.Lockers);
            Assert.AreEqual(0, stats.LockerWait);
            Assert.AreEqual(9, stats.LockModes);
            Assert.AreEqual(0, stats.LockPuts);
            Assert.AreEqual(0, stats.LockRequests);
            Assert.AreEqual(0, stats.Locks);
            Assert.AreEqual(0, stats.LockSteals);
            Assert.AreEqual(1000, stats.LockTimeoutLength);
            Assert.AreEqual(0, stats.LockTimeouts);
            Assert.AreEqual(0, stats.LockUpgrades);
            Assert.AreEqual(0, stats.MaxBucketLength);
            Assert.AreEqual(0, stats.MaxLockers);
            Assert.AreEqual(60, stats.MaxLockersInTable);
            Assert.AreEqual(0, stats.MaxLocks);
            Assert.AreEqual(0, stats.MaxLocksInBucket);
            Assert.AreEqual(50, stats.MaxLocksInTable);
            Assert.AreEqual(0, stats.MaxLockSteals);
            Assert.AreEqual(0, stats.MaxObjects);
            Assert.AreEqual(0, stats.MaxObjectsInBucket);
            Assert.AreEqual(70, stats.MaxObjectsInTable);
            Assert.AreEqual(0, stats.MaxObjectSteals);
            Assert.AreEqual(0, stats.MaxPartitionLockNoWait);
            Assert.AreEqual(0, stats.MaxPartitionLockWait);
            Assert.AreNotEqual(0, stats.MaxUnusedID);
            Assert.AreEqual(20, stats.nPartitions);
            Assert.AreEqual(0, stats.ObjectNoWait);
            Assert.AreEqual(0, stats.Objects);
            Assert.AreEqual(0, stats.ObjectSteals);
            Assert.AreEqual(0, stats.ObjectWait);
            Assert.LessOrEqual(0, stats.PartitionLockNoWait);
            Assert.AreEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.AreNotEqual(0, stats.RegionSize);
            Assert.AreEqual(0, stats.RegionWait);
            Assert.AreNotEqual(0, stats.TableSize);
            Assert.AreEqual(2000, stats.TxnTimeoutLength);
            Assert.AreEqual(0, stats.TxnTimeouts);

            env.PrintLockingSystemStats();

            Transaction         txn      = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation     = CreatePolicy.IF_NEEDED;
            dbConfig.Env          = env;
            dbConfig.FreeThreaded = true;
            BTreeDatabase db = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);

            txn.Commit();

            testLockStatsEnv = env;
            testLockStatsDb  = db;

            // Use some locks, to ensure  the stats work when populated.
            txn = testLockStatsEnv.BeginTransaction();
            for (int i = 0; i < 500; i++)
            {
                testLockStatsDb.Put(
                    new DatabaseEntry(BitConverter.GetBytes(i)),
                    new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes(
                                          Configuration.RandomString(i))), txn);
                testLockStatsDb.Sync();
            }
            txn.Commit();

            env.PrintLockingSystemStats();
            stats = env.LockingSystemStats();
            Assert.Less(0, stats.LastAllocatedLockerID);
            Assert.Less(0, stats.LockDowngrades);
            Assert.LessOrEqual(0, stats.LockerNoWait);
            Assert.Less(0, stats.Lockers);
            Assert.LessOrEqual(0, stats.LockerWait);
            Assert.Less(0, stats.LockPuts);
            Assert.Less(0, stats.LockRequests);
            Assert.Less(0, stats.Locks);
            Assert.LessOrEqual(0, stats.LockSteals);
            Assert.LessOrEqual(0, stats.LockTimeouts);
            Assert.LessOrEqual(0, stats.LockUpgrades);
            Assert.Less(0, stats.MaxBucketLength);
            Assert.Less(0, stats.MaxLockers);
            Assert.Less(0, stats.MaxLocks);
            Assert.Less(0, stats.MaxLocksInBucket);
            Assert.LessOrEqual(0, stats.MaxLockSteals);
            Assert.Less(0, stats.MaxObjects);
            Assert.Less(0, stats.MaxObjectsInBucket);
            Assert.LessOrEqual(0, stats.MaxObjectSteals);
            Assert.LessOrEqual(0, stats.MaxPartitionLockNoWait);
            Assert.LessOrEqual(0, stats.MaxPartitionLockWait);
            Assert.Less(0, stats.MaxUnusedID);
            Assert.LessOrEqual(0, stats.ObjectNoWait);
            Assert.Less(0, stats.Objects);
            Assert.LessOrEqual(0, stats.ObjectSteals);
            Assert.LessOrEqual(0, stats.ObjectWait);
            Assert.Less(0, stats.PartitionLockNoWait);
            Assert.LessOrEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.LessOrEqual(0, stats.RegionWait);
            Assert.LessOrEqual(0, stats.TxnTimeouts);

            // Force a deadlock to ensure the stats work.
            txn = testLockStatsEnv.BeginTransaction();
            testLockStatsDb.Put(new DatabaseEntry(BitConverter.GetBytes(10)),
                                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes(
                                                      Configuration.RandomString(200))), txn);

            Thread thread1 = new Thread(GenerateDeadlock);

            thread1.Start();
            while (DeadlockDidPut == 0)
            {
                Thread.Sleep(10);
            }

            try
            {
                testLockStatsDb.Get(new DatabaseEntry(
                                        BitConverter.GetBytes(100)), txn);
            }
            catch (DeadlockException) { }
            // Abort unconditionally - we don't care about the transaction
            txn.Abort();
            thread1.Join();

            stats = env.LockingSystemStats();
            Assert.Less(0, stats.LockConflictsNoWait);
            Assert.LessOrEqual(0, stats.LockConflictsWait);

            db.Close();
            env.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();
        }
Example #21
0
        private void Open()
        {
            Console.WriteLine("Opening environment and database");

            // Set up the environment.
            DatabaseEnvironmentConfig envCfg = new DatabaseEnvironmentConfig();

            envCfg.Create     = true;
            envCfg.UseMPool   = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseTxns    = true;

            // Allow multiple threads visit to the environment handle.
            envCfg.FreeThreaded = true;

            if (inMem)
            {
                envCfg.Private = true;
            }
            else
            {
                envCfg.RunRecovery = true;
            }

            /*
             * Indicate that we want db to internally perform
             * deadlock detection, aborting the transaction that
             * has performed the least amount of WriteData activity
             * in the event of a deadlock.
             */
            envCfg.LockSystemCfg = new LockingConfig();
            envCfg.LockSystemCfg.DeadlockResolution =
                DeadlockPolicy.MIN_WRITE;

            if (inMem)
            {
                // Specify in-memory logging.
                envCfg.LogSystemCfg          = new LogConfig();
                envCfg.LogSystemCfg.InMemory = true;

                /*
                 * Specify the size of the in-memory log buffer
                 * Must be large enough to handle the log data
                 * created by the largest transaction.
                 */
                envCfg.LogSystemCfg.BufferSize = 10 * 1024 * 1024;

                /*
                 * Specify the size of the in-memory cache,
                 * large enough to avoid paging to disk.
                 */
                envCfg.MPoolSystemCfg           = new MPoolConfig();
                envCfg.MPoolSystemCfg.CacheSize =
                    new CacheInfo(0, 10 * 1024 * 1024, 1);
            }

            // Set up the database.
            BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig();

            dbCfg.AutoCommit      = true;
            dbCfg.Creation        = CreatePolicy.IF_NEEDED;
            dbCfg.Duplicates      = DuplicatesPolicy.SORTED;
            dbCfg.FreeThreaded    = true;
            dbCfg.ReadUncommitted = true;

            /*
             * Open the environment. Any errors will be caught
             * by the caller.
             */
            env = DatabaseEnvironment.Open(home, envCfg);

            /*
             * Open the database. Do not provide a txn handle. This
             * Open is autocommitted because BTreeDatabaseConfig.AutoCommit
             * is true.
             */
            dbCfg.Env = env;
            db        = BTreeDatabase.Open(dbName, dbCfg);
        }
        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();
        }
Example #23
0
        public void TestFullLogBufferException()
        {
            testName = "TestFullLogBufferException";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            // Open an environment and configured log subsystem.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create                    = true;
            cfg.TxnNoSync                 = true;
            cfg.UseTxns                   = true;
            cfg.UseLocking                = true;
            cfg.UseMPool                  = true;
            cfg.UseLogging                = true;
            cfg.LogSystemCfg              = new LogConfig();
            cfg.LogSystemCfg.AutoRemove   = false;
            cfg.LogSystemCfg.BufferSize   = 409600;
            cfg.LogSystemCfg.MaxFileSize  = 10480;
            cfg.LogSystemCfg.NoBuffer     = false;
            cfg.LogSystemCfg.ZeroOnCreate = true;
            cfg.LogSystemCfg.InMemory     = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);

            BTreeDatabase db;

            try
            {
                Transaction openTxn = env.BeginTransaction();
                try
                {
                    BTreeDatabaseConfig dbConfig =
                        new BTreeDatabaseConfig();
                    dbConfig.Creation = CreatePolicy.IF_NEEDED;
                    dbConfig.Env      = env;
                    db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
                    openTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    openTxn.Abort();
                    throw e;
                }

                Transaction writeTxn = env.BeginTransaction();
                try
                {
                    /*
                     * Writing 10 large records into in-memory logging
                     * database should throw FullLogBufferException since
                     * the amount of put data is larger than buffer size.
                     */
                    byte[] byteArr = new byte[204800];
                    for (int i = 0; i < 10; i++)
                    {
                        db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                               new DatabaseEntry(byteArr), writeTxn);
                    }
                    writeTxn.Commit();
                }
                catch (Exception e)
                {
                    writeTxn.Abort();
                    throw e;
                }
                finally
                {
                    db.Close(true);
                }
            }
            catch (FullLogBufferException e)
            {
                Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode);
                throw new ExpectedTestException();
            }
            finally
            {
                env.Close();
            }
        }
        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 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;
            // Use record number.
            cfg.UseRecordNumbers = true;
            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();
        }
Example #26
0
        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();
        }
        public void TestMoveFirstMultipleAndMultipleKey()
        {
            testName = "TestMoveFirstMultipleAndMultipleKey";
            SetUpTest(true);
            string btreeDBFileName = testHome + "/" +
                                     testName + ".db";
            BTreeDatabase db;
            BTreeCursor   cursor;
            KeyValuePair <DatabaseEntry, MultipleDatabaseEntry> pair;
            MultipleKeyDatabaseEntry multiPair;
            int cnt;

            int[] size = new int[2];
            size[0] = 0;
            size[1] = 1024;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.ALWAYS;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            dbConfig.PageSize   = 1024;
            GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor);

            for (int i = 0; i < 2; i++)
            {
                cnt = 0;
                if (size[i] == 0)
                {
                    cursor.MoveFirstMultiple();
                }
                else
                {
                    cursor.MoveFirstMultiple(size[i]);
                }
                pair = cursor.CurrentMultiple;
                foreach (DatabaseEntry dbt in pair.Value)
                {
                    cnt++;
                }
                Assert.AreEqual(1, cnt);
            }

            for (int i = 0; i < 2; i++)
            {
                cnt = 0;
                if (size[i] == 0)
                {
                    cursor.MoveFirstMultipleKey();
                }
                else
                {
                    cursor.MoveFirstMultipleKey(size[i]);
                }
                multiPair = cursor.CurrentMultipleKey;
                foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                         dbt in multiPair)
                {
                    cnt++;
                }
                Assert.Less(1, cnt);
            }

            cursor.Close();
            db.Close();
        }
        static void Main()
        {
            if (String.IsNullOrEmpty(Environment.GetEnvironmentVariable("DATA_DIR")))
            {
                throw new ApplicationException("DATA_DIR must be set to the directory containing student data!");
            }

            var dataPath = Environment.GetEnvironmentVariable("DATA_DIR");

            var config = new BTreeDatabaseConfig
            {
                Creation      = CreatePolicy.IF_NEEDED,
                CacheSize     = new CacheInfo(1, 0, 1),
                ErrorFeedback = (prefix, message) =>
                {
                    var fg = ForegroundColor;
                    ForegroundColor = ConsoleColor.Red;
                    WriteLine($"{prefix}: {message}");
                    ForegroundColor = fg;
                },
                ErrorPrefix = "students",
            };

            var db = BTreeDatabase.Open(Path.Combine(dataPath, "students.db"), "students", config);

            var cfg = new SecondaryBTreeDatabaseConfig(db, (key, data) =>
            {
                var last = Encoding.Default.GetString(data.Data).Split('#').Last().Replace("\0", "");
                //Console.WriteLine($"adding {last} to index");
                return(new DatabaseEntry(Encoding.Default.GetBytes(last)));
            })
            {
                Creation      = CreatePolicy.IF_NEEDED, Duplicates = DuplicatesPolicy.SORTED,
                ErrorFeedback = (prefix, message) => WriteLine($"{prefix}: {message}")
            };

            var dbIdx = SecondaryBTreeDatabase.Open(Path.Combine(dataPath, "students.db"), "studentsIdx", cfg);

            if (db.Stats().nKeys < 1)
            {
                foreach (var line in File.ReadAllLines(Path.Combine(dataPath, "students.csv")))
                {
                    var split = line.Split("#");
                    var key   = new DatabaseEntry(Encoding.Default.GetBytes(split[0]));
                    var bytes = new List <byte>();

                    //Add first and last name delimited by the '#' still
                    bytes.AddRange(Encoding.Default.GetBytes(split[1]));
                    bytes.AddRange(BitConverter.GetBytes('#'));
                    bytes.AddRange(Encoding.Default.GetBytes(split[2]));
                    var dbval = new DatabaseEntry(bytes.ToArray());
                    db.Put(key, dbval);
                }

                db.Sync();
                dbIdx.Sync();
            }

            var dbKeys = db.Stats().nKeys;
            /* Data upload complete. Now let's find and delete some records. */

            var idxCursor = dbIdx.Cursor();

            idxCursor.MoveFirst();

            int results = 0;

            //var results = from c in idxCursor where c.Key == barrera select c;
            do
            {
                if (Encoding.Default.GetString(idxCursor.Current.Key.Data) == "Barrera")
                {
                    results++;
                    WriteLine(Encoding.Default.GetString(idxCursor.Current.Value.Data));
                }
            } while (idxCursor.MoveNext());

            WriteLine($"found {results} results containing Barrera.");
            WriteLine();

            // delete everyone named Barrera


            var dbCursor = db.Cursor();

            dbCursor.MoveFirst();
            do
            {
                var ckey = Encoding.Default.GetString(dbCursor.Current.Key.Data);
                if (ckey != "994BE3B4")
                {
                    continue;
                }
                WriteLine("bartlett found! deleting...");
                try
                {
                    var v = db.Get(dbCursor.Current.Key);
                    WriteLine($"{Encoding.Default.GetString(v.Key.Data)}");
                    db.Delete(dbCursor.Current.Key);
                }
                catch (Exception ex)
                {
                    WriteLine($"{ex.GetType()}: {ex.Message}");
                }

                break;
            } while (dbCursor.MoveNext());

            string lastName = string.Empty;

            idxCursor.MoveFirst();
            do
            {
                var key = Encoding.Default.GetString(idxCursor.Current.Key.Data).Trim();
                if (lastName != key)
                {
                    lastName = key;
                }
                if (key == "Barrera")
                {
                    try
                    {
                        WriteLine($"deleting '{key}'");
                        dbIdx.Delete(idxCursor.Current.Key);
                    }
                    catch (Exception ex)
                    {
                        WriteLine($"{ex.GetType()}: {ex.Message}");
                    }
                }
            } while (idxCursor.MoveNext());

            WriteLine($"{db.Stats().nKeys} left in the database ({dbKeys} original count).");
            idxCursor.Close();
            dbCursor.Close();

            dbIdx.Close(true);
            db.Close(true);

            ReadLine();
        }
        public void TestMoveMultiplePairs()
        {
            testName = "TestMoveMultiplePairs";
            SetUpTest(true);
            string btreeDBFileName = testHome + "/" +
                                     testName + ".db";
            BTreeDatabase db;
            BTreeCursor   cursor;
            DatabaseEntry key, data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            MultipleKeyDatabaseEntry multiKeyDBTs1, multiKeyDBTs2;
            int cnt;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.ALWAYS;
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            dbConfig.PageSize   = 1024;
            GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor);

            /*
             * Bulk retrieve pairs from the pair whose key/data
             * is exact 99/99.
             */
            cnt  = 0;
            key  = new DatabaseEntry(BitConverter.GetBytes((int)99));
            data = new DatabaseEntry(BitConverter.GetBytes((int)99));
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(key, data);
            cursor.MoveMultipleKey(pair, true);
            multiKeyDBTs1 = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     p in multiKeyDBTs1)
            {
                cnt++;
            }
            Assert.AreEqual(3, cnt);

            // Bulk retrieve pairs from the pair whose key is exact 99.
            cnt  = 0;
            key  = new DatabaseEntry(BitConverter.GetBytes((int)99));
            data = new DatabaseEntry(BitConverter.GetBytes((int)98));
            cursor.MoveMultipleKey(pair, true);
            multiKeyDBTs2 = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     dbts in multiKeyDBTs2)
            {
                cnt++;
            }
            Assert.AreEqual(3, cnt);

            /*
             * Bulk retrieve pairs from the pair whose key is
             * exact 99 in buffer size of 1024k.
             */
            cnt  = 0;
            key  = new DatabaseEntry(BitConverter.GetBytes((int)99));
            data = new DatabaseEntry(BitConverter.GetBytes((int)102));
            cursor.MoveMultipleKey(pair, true, 1024);
            multiKeyDBTs2 = cursor.CurrentMultipleKey;
            foreach (KeyValuePair <DatabaseEntry, DatabaseEntry>
                     dbts in multiKeyDBTs2)
            {
                cnt++;
            }
            Assert.AreEqual(3, cnt);

            cursor.Close();
            db.Close();
        }
Example #30
0
        public void TestSnapshotIsolation()
        {
            BTreeDatabaseConfig       dbConfig;
            DatabaseEntry             key, data;
            DatabaseEnvironmentConfig envConfig;
            Thread      readThread, updateThread;
            Transaction txn;

            updateTxn = null;
            readTxn   = null;
            paramDB   = null;
            paramEnv  = null;
            testName  = "TestSnapshotIsolation";
            testHome  = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            /*
             * Open environment with DB_MULTIVERSION
             * which is required by DB_TXN_SNAPSHOT.
             */
            envConfig            = new DatabaseEnvironmentConfig();
            envConfig.Create     = true;
            envConfig.UseMVCC    = true;
            envConfig.UseTxns    = true;
            envConfig.UseMPool   = true;
            envConfig.UseLocking = true;
            envConfig.TxnTimeout = 1000;
            paramEnv             = DatabaseEnvironment.Open(
                testHome, envConfig);
            paramEnv.DetectDeadlocks(DeadlockPolicy.YOUNGEST);

            /*
             * Open a transactional database and put 1000 records
             * into it within transaction.
             */
            txn               = paramEnv.BeginTransaction();
            dbConfig          = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.UseMVCC  = true;
            dbConfig.Env      = paramEnv;
            paramDB           = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);
            for (int i = 0; i < 256; i++)
            {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                paramDB.Put(key, data, txn);
            }
            txn.Commit();

            /*
             * Begin two threads, read and update thread.
             * The update thread runs a update transaction
             * using full read/write locking. The read thread
             * set DB_TXN_SNAPSHOT on read-only cursor.
             */
            readThread   = new Thread(new ThreadStart(ReadTxn));
            updateThread = new Thread(new ThreadStart(UpdateTxn));
            updateThread.Start();
            Thread.Sleep(1000);
            readThread.Start();
            readThread.Join();
            updateThread.Join();

            // Commit transacion in both two threads.
            if (updateTxn != null)
            {
                updateTxn.Commit();
            }
            if (readTxn != null)
            {
                readTxn.Commit();
            }

            /*
             * Confirm that the overwrite operation works.
             */
            ConfirmOverwrite();

            paramDB.Close();
            paramEnv.Close();
        }