Exemplo n.º 1
0
        public void TestOpenExistingRecnoDB()
        {
            testName = "TestOpenExistingRecnoDB";
            SetUpTest(true);
            string recnoDBFileName = testHome + "/" +
                                     testName + ".db";

            RecnoDatabaseConfig recConfig =
                new RecnoDatabaseConfig();

            recConfig.Creation = CreatePolicy.ALWAYS;
            RecnoDatabase recDB = RecnoDatabase.Open(
                recnoDBFileName, recConfig);

            recDB.Close();

            RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig();
            string backingFile           = testHome + "/backingFile";

            File.Copy(recnoDBFileName, backingFile);
            dbConfig.BackingFile = backingFile;
            RecnoDatabase db = RecnoDatabase.Open(recnoDBFileName, dbConfig);

            Assert.AreEqual(db.Type, DatabaseType.RECNO);
            db.Close();
        }
        protected Repository(string databaseName, uint tableSize, ILoggerFactory loggerService)
        {
            logger = loggerService.CreateLogger(databaseName);
            path   = Environment.GetEnvironmentVariable("DATA_DIR");
            var cfg = new RecnoDatabaseConfig
            {
                Creation      = CreatePolicy.IF_NEEDED,
                CacheSize     = new CacheInfo(1, 0, 1),
                ErrorFeedback = (prefix, message) =>
                {
                    logger.LogCritical($"{prefix}: {message}");
                },
                ErrorPrefix = databaseName,

                // Not a required Recno setting like it is for Queue
                //Length =500,

                BackingFile = Path.Combine(path, databaseName + ".txt"),

                //Duplicates = DuplicatesPolicy.UNSORTED,
                //TableSize = tableSize
            };

            if (!File.Exists(Path.Combine(path, databaseName + ".txt")))
            {
                using (var f = File.Create(Path.Combine(path, databaseName + ".txt")))
                {
                    f.Close();
                }
            }

            //null file name loads the databse in memory and uses the backing file for data
            db = RecnoDatabase.Open(null, cfg);
        }
Exemplo n.º 3
0
        public void TestTruncateUnusedPages()
        {
            testName = "TestTruncateUnusedPages";
            SetUpTest(true);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

            RecnoDatabaseConfig dbConfig =
                new RecnoDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            dbConfig.PageSize = 512;
            RecnoDatabase db = RecnoDatabase.Open(
                testName + ".db", dbConfig);

            ModifyRecordsInDB(db, null);
            Assert.Less(0, db.TruncateUnusedPages());

            db.Close();
            env.Close();
        }
Exemplo n.º 4
0
        public void TestStats()
        {
            testName = "TestStats";
            SetUpTest(true);
            string dbFileName = testHome + "/" +
                                testName + ".db";

            RecnoDatabaseConfig dbConfig =
                new RecnoDatabaseConfig();

            ConfigCase1(dbConfig);
            RecnoDatabase db = RecnoDatabase.Open(dbFileName,
                                                  dbConfig);
            RecnoStats stats = db.Stats();

            ConfirmStatsPart1Case1(stats);

            // Put 1000 records into the database.
            PutRecordCase1(db, null);
            stats = db.Stats();
            ConfirmStatsPart2Case1(stats);

            // Delete 500 records.
            for (int i = 250; i <= 750; i++)
            {
                db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)));
            }
            stats = db.Stats();
            ConfirmStatsPart3Case1(stats);

            db.Close();
        }
Exemplo n.º 5
0
 public void ConfigCase1(RecnoDatabaseConfig dbConfig)
 {
     dbConfig.Creation = CreatePolicy.IF_NEEDED;
     dbConfig.PageSize = 4096;
     dbConfig.Length   = 4000;
     dbConfig.PadByte  = 256;
 }
Exemplo n.º 6
0
        public void GetCursorWithImplicitTxn(string home,
                                             string dbFile, bool ifConfig)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create   = true;
            envConfig.UseCDB   = true;
            envConfig.UseMPool = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            RecnoDatabaseConfig dbConfig =
                new RecnoDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            RecnoDatabase db = RecnoDatabase.Open(dbFile,
                                                  dbConfig);

            RecnoCursor cursor;

            if (ifConfig == false)
            {
                cursor = db.Cursor();
            }
            else
            {
                cursor = db.Cursor(new CursorConfig());
            }

            cursor.Close();
            db.Close();
            env.Close();
        }
        public static void Config(XmlElement xmlElement,
                                  ref RecnoDatabaseConfig recnoDBConfig, bool compulsory)
        {
            int            intValue  = new int();
            uint           uintValue = new uint();
            DatabaseConfig dbConfig  = recnoDBConfig;

            Config(xmlElement, ref dbConfig, compulsory);

            // Configure specific fields/properties of Recno database
            Configuration.ConfigCreatePolicy(xmlElement, "Creation",
                                             ref recnoDBConfig.Creation, compulsory);
            if (Configuration.ConfigInt(xmlElement, "Delimiter",
                                        ref intValue, compulsory))
            {
                recnoDBConfig.Delimiter = intValue;
            }
            if (Configuration.ConfigUint(xmlElement, "Length",
                                         ref uintValue, compulsory))
            {
                recnoDBConfig.Length = uintValue;
            }
            if (Configuration.ConfigInt(xmlElement, "PadByte",
                                        ref intValue, compulsory))
            {
                recnoDBConfig.PadByte = intValue;
            }
            Configuration.ConfigBool(xmlElement, "Renumber",
                                     ref recnoDBConfig.Renumber, compulsory);
            Configuration.ConfigBool(xmlElement, "Snapshot",
                                     ref recnoDBConfig.Snapshot, compulsory);
        }
        public void TestAppend()
        {
            uint recno;

            testName = "TestAppend";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            RecnoDatabaseConfig recnoConfig =
                new RecnoDatabaseConfig();

            recnoConfig.Creation = CreatePolicy.IF_NEEDED;
            recnoConfig.Append   = new AppendRecordDelegate(
                AppendRecord);
            RecnoDatabase recnoDB = RecnoDatabase.Open(
                dbFileName, recnoConfig);

            recno = recnoDB.Append(new DatabaseEntry(
                                       ASCIIEncoding.ASCII.GetBytes("data")));

            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            pair = recnoDB.Get(
                new DatabaseEntry(BitConverter.GetBytes(recno)));
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data"),
                            pair.Value.Data);

            recnoDB.Close();
        }
Exemplo n.º 9
0
        public void TestConfig()
        {
            testName = "TestConfig";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

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

            // Open a primary btree database.
            RecnoDatabaseConfig recDBConfig =
                new RecnoDatabaseConfig();

            recDBConfig.Creation = CreatePolicy.IF_NEEDED;
            RecnoDatabase recDB = RecnoDatabase.Open(
                dbFileName, recDBConfig);

            SecondaryRecnoDatabaseConfig secDBConfig =
                new SecondaryRecnoDatabaseConfig(recDB, null);

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

            // Close the primary btree database.
            recDB.Close();
        }
Exemplo n.º 10
0
        public void TestAppendWithoutTxn()
        {
            testName = "TestAppendWithoutTxn";
            SetUpTest(true);
            string recnoDBFileName = testHome + "/" +
                                     testName + ".db";

            RecnoDatabaseConfig recnoConfig =
                new RecnoDatabaseConfig();

            recnoConfig.Creation = CreatePolicy.ALWAYS;
            RecnoDatabase recnoDB = RecnoDatabase.Open(
                recnoDBFileName, recnoConfig);

            DatabaseEntry data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            uint          num = recnoDB.Append(data);
            DatabaseEntry key = new DatabaseEntry(
                BitConverter.GetBytes(num));

            Assert.IsTrue(recnoDB.Exists(key));
            KeyValuePair <DatabaseEntry, DatabaseEntry> record =
                recnoDB.Get(key);

            Assert.IsTrue(data.Data.Length ==
                          record.Value.Data.Length);
            for (int i = 0; i < data.Data.Length; i++)
            {
                Assert.IsTrue(data.Data[i] ==
                              record.Value.Data[i]);
            }
            recnoDB.Close();
        }
Exemplo n.º 11
0
        public void TestMessageFile()
        {
            testName = "TestMessageFile";
            SetUpTest(true);

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

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

            // Configure and open a database.
            RecnoDatabaseConfig DBConfig =
                new RecnoDatabaseConfig();

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

            string        DBFileName = testName + ".db";
            RecnoDatabase db         = RecnoDatabase.Open(
                DBFileName, DBConfig);

            // Confirm message file does not exist.
            string messageFile = testHome + "/" + "msgfile";

            Assert.AreEqual(false, File.Exists(messageFile));

            // Call set_msgfile() of db.
            db.Msgfile = messageFile;

            // Print db statistic to message file.
            db.PrintStats(true);

            // Confirm message file exists now.
            Assert.AreEqual(true, File.Exists(messageFile));

            db.Msgfile = "";
            string line = null;

            // Read the third line of message file.
            System.IO.StreamReader file = new System.IO.StreamReader(@"" + messageFile);
            line = file.ReadLine();
            line = file.ReadLine();
            line = file.ReadLine();

            // Confirm the message file is not empty.
            Assert.AreEqual(line, "DB handle information:");
            file.Close();

            // Close database and environment.
            db.Close();
            env.Close();
        }
Exemplo n.º 12
0
        public void TestInsertToLoc()
        {
            RecnoDatabase       db;
            RecnoDatabaseConfig dbConfig;
            RecnoCursor         cursor;
            DatabaseEntry       data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            string dbFileName;

            testName   = "TestInsertToLoc";
            testHome   = testFixtureHome + "/" + testName;
            dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            // Open database and cursor.
            dbConfig          = new RecnoDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Renumber = true;
            db     = RecnoDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();

            // Add record(1,1) into database.

            /*
             * Add a record(1, 1) by cursor and move
             * the cursor to the current record.
             */
            AddOneByCursor(cursor);
            cursor.Refresh();

            /*
             * Insert the new record(1,10) after the
             * record(1,1).
             */
            data = new DatabaseEntry(
                BitConverter.GetBytes((int)10));
            cursor.Insert(data, Cursor.InsertLocation.AFTER);

            /*
             * Move the cursor to the record(1,1) and
             * confirm that the next record is the one just inserted.
             */
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(
                    BitConverter.GetBytes((int)1)),
                new DatabaseEntry(
                    BitConverter.GetBytes((int)1)));
            Assert.IsTrue(cursor.Move(pair, true));
            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(BitConverter.GetBytes((int)10),
                            cursor.Current.Value.Data);

            cursor.Close();
            db.Close();
        }
Exemplo n.º 13
0
        override public void TestConfigWithoutEnv()
        {
            testName = "TestConfigWithoutEnv";
            XmlElement xmlElem = Configuration.TestSetUp(
                testFixtureName, testName);

            RecnoDatabaseConfig recnoDBConfig =
                new RecnoDatabaseConfig();

            Config(xmlElem, ref recnoDBConfig, true);
            Confirm(xmlElem, recnoDBConfig, true);
        }
Exemplo n.º 14
0
        public void TestDuplicate()
        {
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            RecnoDatabase       db;
            RecnoDatabaseConfig dbConfig;
            RecnoCursor         cursor, dupCursor;
            string dbFileName;

            testName   = "TestDuplicate";
            testHome   = testFixtureHome + "/" + testName;
            dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            dbConfig          = new RecnoDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            db     = RecnoDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();

            /*
             * Add a record(1, 1) by cursor and move
             * the cursor to the current record.
             */
            AddOneByCursor(cursor);
            cursor.Refresh();

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

            // Overwrite the record.
            dupCursor.Overwrite(new DatabaseEntry(
                                    ASCIIEncoding.ASCII.GetBytes("newdata")));

            // Confirm that the original data doesn't exist.
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(
                    BitConverter.GetBytes((int)1)),
                new DatabaseEntry(
                    BitConverter.GetBytes((int)1)));
            Assert.IsFalse(dupCursor.Move(pair, true));

            dupCursor.Close();
            cursor.Close();
            db.Close();
        }
Exemplo n.º 15
0
        public void TestOpenNewRecnoDB()
        {
            RecnoDatabase       recnoDB;
            RecnoDatabaseConfig recnoConfig;

            testName = "TestOpenNewRecnoDB";
            SetUpTest(true);
            string recnoDBFileName = testHome + "/" +
                                     testName + ".db";

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

            recnoConfig = new RecnoDatabaseConfig();
            RecnoDatabaseConfigTest.Config(xmlElem,
                                           ref recnoConfig, true);
            recnoDB = RecnoDatabase.Open(recnoDBFileName,
                                         recnoConfig);
            Confirm(xmlElem, recnoDB, true);
            recnoDB.Close();
        }
Exemplo n.º 16
0
        public void GetCursorWithExplicitTxn(string home,
                                             string dbFile, bool ifConfig)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

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

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            RecnoDatabase db = RecnoDatabase.Open(dbFile,
                                                  dbConfig, openTxn);

            openTxn.Commit();

            Transaction cursorTxn = env.BeginTransaction();
            RecnoCursor cursor;

            if (ifConfig == false)
            {
                cursor = db.Cursor(cursorTxn);
            }
            else
            {
                cursor = db.Cursor(new CursorConfig(), cursorTxn);
            }
            cursor.Close();
            cursorTxn.Commit();
            db.Close();
            env.Close();
        }
Exemplo n.º 17
0
        /*
         * Open environment, database and write data into database.
         * Generated log files are put under testHome.
         */
        public void Logging(string home, string dbName,
                            out DatabaseEnvironment env, out RecnoDatabase recnoDB)
        {
            string dbFileName = dbName + ".db";

            Configuration.ClearDir(home);

            // Open environment with logging subsystem.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                    = true;
            envConfig.UseLogging                = true;
            envConfig.LogSystemCfg              = new LogConfig();
            envConfig.LogSystemCfg.FileMode     = 755;
            envConfig.LogSystemCfg.ZeroOnCreate = true;
            envConfig.UseMPool                  = true;
            env = DatabaseEnvironment.Open(home, envConfig);

            /*
             * Open recno database, write 100000 records into
             * the database and close it.
             */
            RecnoDatabaseConfig recnoConfig =
                new RecnoDatabaseConfig();

            recnoConfig.Creation = CreatePolicy.IF_NEEDED;
            recnoConfig.Env      = env;
            // The db needs mpool to open.
            recnoConfig.NoMMap = false;
            recnoDB            = RecnoDatabase.Open(dbFileName,
                                                    recnoConfig);
            for (int i = 0; i < 1000; i++)
            {
                recnoDB.Append(new DatabaseEntry(
                                   ASCIIEncoding.ASCII.GetBytes("key")));
            }
        }
Exemplo n.º 18
0
        public void TestTruncateUnusedPagesInTxn()
        {
            testName = "TestTruncateUnusedPagesInTxn";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

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

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

            openTxn.Commit();

            Transaction modifyTxn = env.BeginTransaction();

            ModifyRecordsInDB(db, modifyTxn);
            Assert.Less(0, db.TruncateUnusedPages(modifyTxn));
            modifyTxn.Commit();

            db.Close();
            env.Close();
        }
Exemplo n.º 19
0
        public static void Confirm(XmlElement xmlElement,
                                   RecnoDatabaseConfig recnoDBConfig, bool compulsory)
        {
            DatabaseConfig dbConfig = recnoDBConfig;

            Confirm(xmlElement, dbConfig, compulsory);

            // Confirm Recno database specific configuration
            Configuration.ConfirmString(xmlElement, "BackingFile",
                                        recnoDBConfig.BackingFile, compulsory);
            Configuration.ConfirmCreatePolicy(xmlElement,
                                              "Creation", recnoDBConfig.Creation, compulsory);
            Configuration.ConfirmInt(xmlElement, "Delimiter",
                                     recnoDBConfig.Delimiter, compulsory);
            Configuration.ConfirmUint(xmlElement, "Length",
                                      recnoDBConfig.Length, compulsory);
            Configuration.ConfirmInt(xmlElement, "PadByte",
                                     recnoDBConfig.PadByte, compulsory);
            Configuration.ConfirmBool(xmlElement, "Renumber",
                                      recnoDBConfig.Renumber, compulsory);
            Configuration.ConfirmBool(xmlElement, "Snapshot",
                                      recnoDBConfig.Snapshot, compulsory);
        }
Exemplo n.º 20
0
        /// <summary>
        /// 初始化
        /// </summary>
        protected bool Init()
        {
            //
            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(false);
            }
            //

            /* Set up environment. */
            if (SetUpEnv(home, data_dir) == EXIT_FAILURE)
            {
                Console.WriteLine("Fail to set up the environment.");
                return(false);
            }
            Console.WriteLine("Set up the environment.");
            //if(!Directory.Exists(db_blobdir))
            //{
            //    Directory.CreateDirectory(db_blobdir);
            //}

            /* Configure the database. */
            switch (DBType)
            {
            case DBType.BTree:
            case DBType.Sequence:
            {
                BTreeDatabaseConfig config = new BTreeDatabaseConfig
                {
                    Duplicates      = DuplicatesPolicy.SORTED,
                    Creation        = CreatePolicy.IF_NEEDED,
                    ExternalFileDir = db_blobdir
                };

                databaseConfig = config;
            }
            break;

            case DBType.Hash:
            {
                HashDatabaseConfig config = new HashDatabaseConfig();
                config.Creation   = CreatePolicy.IF_NEEDED;
                config.Duplicates = DuplicatesPolicy.SORTED;
                config.CacheSize  = new CacheInfo(0, 64 * 1024, 1);
                config.BlobDir    = db_blobdir;
                databaseConfig    = config;
            }
            break;

            case DBType.Recno:
            {
                RecnoDatabaseConfig config = new RecnoDatabaseConfig();
                config.Creation = CreatePolicy.IF_NEEDED;
                config.Length   = 1000;
                databaseConfig  = config;
            }
            break;

            case DBType.Queue:
            {
                QueueDatabaseConfig config = new QueueDatabaseConfig();
                config.Creation = CreatePolicy.IF_NEEDED;
                databaseConfig  = config;
            }
            break;

            default:
                databaseConfig = new BTreeDatabaseConfig();
                break;
            }

            databaseConfig.ErrorPrefix = "excs_access";
            databaseConfig.PageSize    = 8 * 1024;
            databaseConfig.Env         = env;


            try
            {
                /* Create and open a new database in the file. */
                CreateDB(databaseConfig);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error opening {0}.", db_File);
                Console.WriteLine(e.Message);
                return(false);
            }
        }
Exemplo n.º 21
0
        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();
        }
Exemplo n.º 22
0
        public void OpenSecRecnoDBWithinTxn(string className,
                                            string funName, string home, string dbFileName,
                                            string dbSecFileName, bool ifDbName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

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

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

            // Open a primary recno database.
            Transaction         openDBTxn = env.BeginTransaction();
            RecnoDatabaseConfig dbConfig  =
                new RecnoDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;

            dbConfig.Env = env;
            RecnoDatabase db = RecnoDatabase.Open(
                dbFileName, dbConfig, openDBTxn);

            openDBTxn.Commit();

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

            SecondaryRecnoDatabaseConfigTest.Config(xmlElem,
                                                    ref secDBConfig, false);
            secDBConfig.Env = env;
            SecondaryRecnoDatabase secDB;

            if (ifDbName == false)
            {
                secDB = SecondaryRecnoDatabase.Open(
                    dbSecFileName, secDBConfig, openSecTxn);
            }
            else
            {
                secDB = SecondaryRecnoDatabase.Open(
                    dbSecFileName, "secondary", secDBConfig,
                    openSecTxn);
            }
            openSecTxn.Commit();

            // Confirm its flags configured in secDBConfig.
            Confirm(xmlElem, secDB, true);
            secDB.Close();

            // Open the existing secondary database.
            Transaction             secTxn    = env.BeginTransaction();
            SecondaryDatabaseConfig secConfig =
                new SecondaryDatabaseConfig(db,
                                            new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secConfig.Env = env;

            SecondaryDatabase secExDB;

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

            db.Close();
            env.Close();
        }
Exemplo n.º 23
0
        public void OpenSecRecnoDB(string className,
                                   string funName, string dbFileName, string dbSecFileName,
                                   bool ifDBName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

            // Open a primary recno database.
            RecnoDatabaseConfig primaryDBConfig =
                new RecnoDatabaseConfig();

            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            RecnoDatabase primaryDB;

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

            try
            {
                // Open a new secondary database.
                SecondaryRecnoDatabaseConfig secRecnoDBConfig =
                    new SecondaryRecnoDatabaseConfig(
                        primaryDB, null);
                SecondaryRecnoDatabaseConfigTest.Config(
                    xmlElem, ref secRecnoDBConfig, false);
                secRecnoDBConfig.Creation =
                    CreatePolicy.IF_NEEDED;

                SecondaryRecnoDatabase secRecnoDB;
                if (ifDBName == false)
                {
                    secRecnoDB = SecondaryRecnoDatabase.Open(
                        dbSecFileName, secRecnoDBConfig);
                }
                else
                {
                    secRecnoDB = SecondaryRecnoDatabase.Open(
                        dbSecFileName, "secondary",
                        secRecnoDBConfig);
                }

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

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

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

                // Close secondary database.
                secDB.Close();
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                // Close primary database.
                primaryDB.Close();
            }
        }
Exemplo n.º 24
0
        public void TestCompact()
        {
            testName = "TestCompact";
            SetUpTest(true);
            string recnoDBFileName = testHome + "/" +
                                     testName + ".db";

            RecnoDatabaseConfig recnoConfig =
                new RecnoDatabaseConfig();

            recnoConfig.Creation = CreatePolicy.ALWAYS;
            recnoConfig.Length   = 512;

            DatabaseEntry key, data;
            RecnoDatabase recnoDB;

            using (recnoDB = RecnoDatabase.Open(
                       recnoDBFileName, recnoConfig))
            {
                for (int i = 1; i <= 5000; i++)
                {
                    data = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                    recnoDB.Append(data);
                }

                for (int i = 1; i <= 5000; i++)
                {
                    if (i > 500 && (i % 5 != 0))
                    {
                        key = new DatabaseEntry(
                            BitConverter.GetBytes(i));
                        recnoDB.Delete(key);
                    }
                }

                int           startInt = 1;
                int           stopInt = 2500;
                DatabaseEntry start, stop;

                start = new DatabaseEntry(
                    BitConverter.GetBytes(startInt));
                stop = new DatabaseEntry(
                    BitConverter.GetBytes(stopInt));
                Assert.IsTrue(recnoDB.Exists(start));
                Assert.IsTrue(recnoDB.Exists(stop));

                CompactConfig cCfg = new CompactConfig();
                cCfg.start          = start;
                cCfg.stop           = stop;
                cCfg.FillPercentage = 30;
                cCfg.Pages          = 1;
                cCfg.returnEnd      = true;
                cCfg.Timeout        = 5000;
                cCfg.TruncatePages  = true;
                CompactData compactData = recnoDB.Compact(cCfg);

                Assert.IsNotNull(compactData.End);
                Assert.AreNotEqual(0, compactData.PagesExamined);
            }
        }
Exemplo n.º 25
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();
        }
Exemplo n.º 26
0
        public void StatsInTxn(string home, string name, bool ifIsolation)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            EnvConfigCase1(envConfig);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

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

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

            openTxn.Commit();

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

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

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

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

            // Delete 500 records.
            for (int i = 250; i <= 750; i++)
            {
                db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)),
                          statsTxn);
            }

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

            statsTxn.Commit();
            db.Close();
            env.Close();
        }