Beispiel #1
0
        public void GetCursorInBtreeDBUsingRecno(string home,
                                                 string name, out BTreeDatabase db,
                                                 out BTreeCursor cursor)
        {
            string dbFileName            = home + "/" + name + ".db";
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.UseRecordNumbers = true;
            dbConfig.Creation         = CreatePolicy.IF_NEEDED;
            db     = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
        public void TestCompare()
        {
            testName = "TestCompare";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

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

            btreeDBConfig.Creation = CreatePolicy.ALWAYS;
            BTreeDatabase btreeDB = BTreeDatabase.Open(
                dbFileName, btreeDBConfig);

            // Open a secondary btree database.
            SecondaryBTreeDatabaseConfig secBtreeDBConfig =
                new SecondaryBTreeDatabaseConfig(null, null);

            secBtreeDBConfig.Primary = btreeDB;
            secBtreeDBConfig.Compare =
                new EntryComparisonDelegate(
                    SecondaryEntryComparison);
            secBtreeDBConfig.KeyGen =
                new SecondaryKeyGenDelegate(SecondaryKeyGen);
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(
                    dbFileName, secBtreeDBConfig);

            /*
             * Get the compare function set in the configuration
             * and run it in a comparison to see if it is alright.
             */
            EntryComparisonDelegate cmp =
                secDB.Compare;
            DatabaseEntry dbt1, dbt2;

            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((int)257));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((int)255));
            Assert.Less(0, cmp(dbt1, dbt2));


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

            secDB.Close();
            btreeDB.Close();
        }
Beispiel #3
0
        public void TestDupCompare()
        {
            testName = "TestDupCompare";
            SetUpTest(true);
            string dbFileName = testHome + "/" + testName + ".db";

            BTreeDatabaseConfig cfg =
                new BTreeDatabaseConfig();

            cfg.Creation = CreatePolicy.ALWAYS;
            BTreeDatabase pDb = BTreeDatabase.Open(
                dbFileName, "p", cfg);

            SecondaryBTreeDatabaseConfig sCfg =
                new SecondaryBTreeDatabaseConfig(pDb,
                                                 new SecondaryKeyGenDelegate(SecondaryKeyGen));

            sCfg.Creation = CreatePolicy.IF_NEEDED;

            DatabaseEntry dbt1, dbt2;

            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((Int32)1));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((Int64)4294967297));

            // Do not set the duplicate comparison delegate.
            using (SecondaryBTreeDatabase sDb =
                       SecondaryBTreeDatabase.Open(dbFileName, "s1", sCfg)) {
                try {
                    int ret = sDb.DupCompare(dbt1, dbt2);
                    throw new TestException();
                } catch (NullReferenceException) {
                }
            }

            sCfg.DuplicateCompare = new EntryComparisonDelegate(
                SecondaryEntryComparison);
            using (SecondaryBTreeDatabase sDb =
                       SecondaryBTreeDatabase.Open(dbFileName, "s2", sCfg)) {
                /*
                 * Get the dup compare function set in the
                 * configuration and run it in a comparison to
                 * see if it is alright.
                 */
                int ret = 1 - BitConverter.ToInt32(
                    BitConverter.GetBytes((Int64)4294967297), 0);

                Assert.AreEqual(ret, sDb.DupCompare(dbt1, dbt2));
            }
            pDb.Close();
        }
Beispiel #4
0
        private void CreateDB(DatabaseConfig config)
        {
            string dbFile = Path.Combine(home, dbName + dbFileEx);

            db_File = dbFile = dbName + dbFileEx;
            dbFile  = Path.Combine(Environment.CurrentDirectory, db_File);
            switch (DBType)
            {
            case DBType.BTree:
            case DBType.Sequence:
            {
                BTreeDatabaseConfig dbcfg = config as BTreeDatabaseConfig;

                if (DBType == DBType.Sequence)
                {
                    dbcfg.Duplicates = DuplicatesPolicy.NONE;
                }
                db = BTreeDatabase.Open(db_File, dbName, dbcfg);
                if (dbcfg.Duplicates != DuplicatesPolicy.SORTED)
                {
                    /* Configure and initialize sequence. */
                    seqConfig = new SequenceConfig();
                    seqConfig.BackingDatabase = db;
                    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);
                }
            }
            break;

            case DBType.Hash:
                db = HashDatabase.Open(dbFile, config as HashDatabaseConfig);
                break;

            case DBType.Recno:
                db = RecnoDatabase.Open(dbFile, config as RecnoDatabaseConfig);
                break;

            case DBType.Queue:
                db = QueueDatabase.Open(dbFile, config as QueueDatabaseConfig);
                break;

            default:
                db = BTreeDatabase.Open(dbFile, config as BTreeDatabaseConfig);
                break;
            }
        }
        public void TestPrefixCompare()
        {
            testName = "TestPrefixCompare";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            // 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.Compare =
                new EntryComparisonDelegate(
                    SecondaryEntryComparison);
            secBtreeDBConfig.PrefixCompare =
                new EntryComparisonDelegate(
                    SecondaryEntryComparison);
            secBtreeDBConfig.KeyGen =
                new SecondaryKeyGenDelegate(
                    SecondaryKeyGen);
            SecondaryBTreeDatabase secDB =
                SecondaryBTreeDatabase.Open(
                    dbFileName, secBtreeDBConfig);

            /*
             * Get the prefix compare function set in the
             * configuration and run it in a comparison to
             * see if it is alright.
             */
            EntryComparisonDelegate cmp =
                secDB.PrefixCompare;
            DatabaseEntry dbt1, dbt2;

            dbt1 = new DatabaseEntry(
                BitConverter.GetBytes((int)1));
            dbt2 = new DatabaseEntry(
                BitConverter.GetBytes((int)129));
            Assert.Greater(0, cmp(dbt1, dbt2));

            secDB.Close();
            btreeDB.Close();
        }
Beispiel #6
0
        public void TestDuplicateWithSamePos()
        {
            BTreeDatabase             db;
            BTreeDatabaseConfig       dbConfig;
            BTreeCursor               cursor, dupCursor;
            DatabaseEnvironment       env;
            DatabaseEnvironmentConfig envConfig;
            DatabaseEntry             key, data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
            Transaction txn;

            testName = "TestDuplicateWithSamePos";
            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(true);

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

            // Confirm that the original data doesn't exist.
            Assert.IsFalse(dupCursor.Move(pair, true));

            dupCursor.Close();
            cursor.Close();
            txn.Commit();
            db.Close();
            env.Close();
        }
Beispiel #7
0
        public BDBHelper(string dbPath)
        {
            BTreeDatabaseConfig bTreeDatabaseConfig = new BTreeDatabaseConfig();

            //文件不存在则创建
            bTreeDatabaseConfig.Creation = CreatePolicy.IF_NEEDED;
            //页大小
            bTreeDatabaseConfig.PageSize = 512;
            //缓存大小
            bTreeDatabaseConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1);

            db = BTreeDatabase.Open(dbPath, bTreeDatabaseConfig);
        }
Beispiel #8
0
        public void TestLockAndUnlockMutex()
        {
            testName = "TestLockAndUnlockMutex";
            testHome = testFixtureHome + "/" + testName;
            Configuration.ClearDir(testHome);

            /*
             * Open an environment without locking and
             * deadlock detection.
             */
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

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

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

            // Get a mutex which will be used in two threads.
            TestMutex = env.GetMutex(true, false);

            // Begin two threads to write records into database.
            Thread mutexThread1 = new Thread(
                new ThreadStart(MutexThread1));
            Thread mutexThread2 = new Thread(
                new ThreadStart(MutexThread2));

            mutexThread1.Start();
            mutexThread2.Start();
            mutexThread1.Join();
            mutexThread2.Join();

            // Free the mutex.
            TestMutex.Dispose();

            // Close all.
            TestDB.Close();
            env.Close();
        }
        public override void Init(int flowCount, long flowRecordCount)
        {
            BTreeDatabaseConfig config = new BTreeDatabaseConfig();

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

            string fileName = Path.Combine(DataDirectory, string.Format("{0}.oracle", CollectionName));

            database = BTreeDatabase.Open(fileName, config);
        }
Beispiel #10
0
        public static void GetCursorInBtreeDBWithoutEnv(
            string home, string name, out BTreeDatabase db,
            out BTreeCursor cursor)
        {
            string dbFileName = home + "/" + name + ".db";

            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.IF_NEEDED;
            dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
            db     = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();
        }
Beispiel #11
0
        public DataWriter(Settings settings, int pointCount)
        {
            m_settings = settings;
            if (m_settings.WriteToOpenHistorian) // Initialize OH instance
            {
                string historianName = "DestinationHistorian";
                HistorianServerDatabaseConfig archiveInfo = new HistorianServerDatabaseConfig(historianName, settings.HistorianArchive, true)
                {
                    TargetFileSize     = (long)(1 * SI.Giga), // Just because
                    DirectoryMethod    = ArchiveDirectoryMethod.TopDirectoryOnly,
                    StagingCount       = 3,
                    DiskFlushInterval  = 1000, // Smallest available time interval
                    CacheFlushInterval = 1000  // Largest available value
                };

                m_historianServer  = new HistorianServer(archiveInfo, m_settings.DestinationHistorianDataPort);
                m_historianArchive = m_historianServer[historianName];
                m_historianKey     = new HistorianKey();
                m_historianValue   = new HistorianValue();
            }
            else if (m_settings.WriteToBerkeleyDB) // Initialize BDB instance
            {
                m_berkeleyDbCfg = new BTreeDatabaseConfig()
                {
                    BTreeCompare = BerkeleyDBKeyComparison,
                    CacheSize    = new CacheInfo(10, 0, 1),
                    PageSize     = 65536,
                    NoMMap       = true,
                    Creation     = CreatePolicy.IF_NEEDED
                };

                string databasePath = null;
                if (!m_settings.InMemoryBerkeleyDB)
                {
                    databasePath = Path.Combine(settings.HistorianArchive, settings.HistorianName);
                }

                m_berkeleyDb = BTreeDatabase.Open(databasePath, m_berkeleyDbCfg);

                m_berkeleyDbKey   = new DatabaseEntry();
                m_berkeleyDbValue = new DatabaseEntry();

                m_berkeleyDbPointList = new KeyValuePair <DatabaseEntry, DatabaseEntry> [pointCount];
                Parallel.For(0, m_berkeleyDbPointList.Length, (i) => m_berkeleyDbPointList[i] = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                                 new DatabaseEntry(),
                                 new DatabaseEntry()));
            }
        }
Beispiel #12
0
        public void TestRemoveWithNoSync()
        {
            testName = "TestRemoveWithNoSync";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testName + ".db";

            Configuration.ClearDir(testHome);

            // Open a database and an increase sequence.
            DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig();

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

            /* Configure and open sequence's database. */
            BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig();

            btreeDBConfig.AutoCommit = true;
            btreeDBConfig.Creation   = CreatePolicy.IF_NEEDED;
            btreeDBConfig.Env        = env;
            BTreeDatabase btreeDB = BTreeDatabase.Open(dbFileName, btreeDBConfig);

            /* Configure and initialize sequence. */
            SequenceConfig 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;
            seqConfig.key      = new DatabaseEntry();
            seqConfig.key.Data = ASCIIEncoding.ASCII.GetBytes("ex_csharp_sequence");
            Sequence seq = new Sequence(seqConfig);

            /*
             * Remove the sequence. The sequence handle can not
             * be accessed again.
             */
            seq.Remove(true);
            btreeDB.Close();
            env.Close();
        }
Beispiel #13
0
        public void TestJoin()
        {
            testName = "TestJoin";
            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);

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

            /*
             * Open two databases, their secondary databases and
             * secondary cursors.
             */
            SecondaryBTreeDatabase secDB1, secDB2;

            SecondaryCursor[] cursors = new SecondaryCursor[2];
            GetSecCursor(db, secFileName1,
                         new SecondaryKeyGenDelegate(KeyGenOnBigByte),
                         out secDB1, out cursors[0], false, null);
            GetSecCursor(db, secFileName2,
                         new SecondaryKeyGenDelegate(KeyGenOnLittleByte),
                         out secDB2, out cursors[1], true, null);

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

            Assert.IsNotNull(joinCursor.GetEnumerator().Current);

            // Close all.
            joinCursor.Dispose();
            cursors[0].Close();
            cursors[1].Close();
            secDB1.Close();
            secDB2.Close();
            db.Close();
        }
Beispiel #14
0
        private void GetMultipleDB(string dbFileName, BTreeDatabaseConfig dbConfig,
                                   out BTreeDatabase db, out BTreeCursor cursor)
        {
            db     = BTreeDatabase.Open(dbFileName, dbConfig);
            cursor = db.Cursor();

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

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

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

                key  = new DatabaseEntry(BitConverter.GetBytes(101));
                data = new DatabaseEntry(BitConverter.GetBytes(101));
                pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(key, data);
                cursor.Add(pair);
            }
        }
Beispiel #15
0
        public void TestSecondaryCursor()
        {
            testName = "TestSecondaryCursor";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();

            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

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

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


            SecondaryCursor cursor = secDB.SecondaryCursor();

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

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

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

            // Close primary database.
            primaryDB.Close();
        }
Beispiel #16
0
        public void TestInvalidToken()
        {
            DatabaseEntry       key, data;
            DatabaseEnvironment master = SetUpEnv("./master", 100, 8000, true);
            BTreeDatabase       db     = Open(master, true);
            Transaction         txn    = master.BeginTransaction();

            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Data"));
            db.Put(key, data, txn);
            txn.Commit();
            byte[] token = txn.CommitToken;
            token[19] += 123;
            Assert.Throws <DatabaseException>(delegate { master.IsTransactionApplied(token, 1000); });
            db.Close();
            master.Close();
        }
Beispiel #17
0
        public static ByteOrder getMachineByteOrder()
        {
            string fixtureHome = "./TestOut/DatabaseTest";
            string dbName      = fixtureHome + "/" + "getMachineByteOrder" + ".db";

            Configuration.ClearDir(fixtureHome);

            ByteOrder byteOrder;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.ByteOrder = ByteOrder.MACHINE;
            dbConfig.Creation  = CreatePolicy.ALWAYS;
            using (BTreeDatabase db = BTreeDatabase.Open(dbName, dbConfig))
            {
                byteOrder = db.Endianness;
            }
            return(byteOrder);
        }
Beispiel #18
0
        public static void GetCursorInBtreeDBInTDS(
            string home, string name,
            CursorConfig cursorConfig,
            out DatabaseEnvironment env, out BTreeDatabase db,
            out BTreeCursor cursor, out Transaction txn)
        {
            string dbFileName = name + ".db";

            Configuration.ClearDir(home);

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

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

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

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

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            if (cursorConfig.IsolationDegree == Isolation.DEGREE_ONE)
            {
                dbConfig.ReadUncommitted = true;
            }

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

            // Get a cursor in the transaction.
            cursor = db.Cursor(cursorConfig, txn);
        }
Beispiel #19
0
        public static uint getDefaultCacheSizeBytes()
        {
            uint defaultCacheSizeBytes;

            string fixtureHome = "./TestOut/DatabaseTest";
            string dbName      = fixtureHome + "/" + "getDefaultCacheSizeBytes" + ".db";

            Configuration.ClearDir(fixtureHome);

            BTreeDatabaseConfig cfg = new BTreeDatabaseConfig();

            cfg.Creation = CreatePolicy.ALWAYS;
            using (BTreeDatabase db = BTreeDatabase.Open(dbName, cfg))
            {
                defaultCacheSizeBytes = db.CacheSize.Bytes;
            }

            return(defaultCacheSizeBytes);
        }
Beispiel #20
0
        public void OpenSecDBInTxn(string home, string dbFileName,
                                   string dbSecFileName, out DatabaseEnvironment env,
                                   out BTreeDatabase db, out SecondaryBTreeDatabase secDB)
        {
            // Open environment.
            DatabaseEnvironmentConfig envCfg =
                new DatabaseEnvironmentConfig();

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

            // Open primary and secondary database in a transaction.
            Transaction         openTxn  = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation        = CreatePolicy.IF_NEEDED;
            dbConfig.Env             = env;
            dbConfig.PageSize        = 4096;
            dbConfig.Duplicates      = DuplicatesPolicy.NONE;
            dbConfig.ReadUncommitted = true;
            db = BTreeDatabase.Open(dbFileName, dbConfig,
                                    openTxn);
            openTxn.Commit();

            openTxn = env.BeginTransaction();
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                                                 new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secConfig.Creation        = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates      = DuplicatesPolicy.SORTED;
            secConfig.Env             = env;
            secConfig.ReadUncommitted = true;
            secDB = SecondaryBTreeDatabase.Open(dbSecFileName,
                                                secConfig, openTxn);
            openTxn.Commit();
        }
Beispiel #21
0
        public void GetSecCursor(BTreeDatabase db,
                                 string secFileName, SecondaryKeyGenDelegate keyGen,
                                 out SecondaryBTreeDatabase secDB,
                                 out SecondaryCursor cursor, bool ifCfg,
                                 DatabaseEntry data)
        {
            // Open secondary database.
            SecondaryBTreeDatabaseConfig secCfg =
                new SecondaryBTreeDatabaseConfig(db, keyGen);

            secCfg.Creation   = CreatePolicy.IF_NEEDED;
            secCfg.Duplicates = DuplicatesPolicy.SORTED;
            secDB             = SecondaryBTreeDatabase.Open(secFileName, secCfg);

            int[] intArray = new int[4];
            intArray[0] = 0;
            intArray[1] = 1;
            intArray[2] = 2049;
            intArray[3] = 65537;
            for (int i = 0; i < 4; i++)
            {
                DatabaseEntry record = new DatabaseEntry(
                    BitConverter.GetBytes(intArray[i]));
                db.Put(record, record);
            }

            // Get secondary cursor on the secondary database.
            if (ifCfg == false)
            {
                cursor = secDB.SecondaryCursor();
            }
            else
            {
                cursor = secDB.SecondaryCursor(new CursorConfig());
            }

            // Position the cursor.
            if (data != null)
            {
                Assert.IsTrue(cursor.Move(data, true));
            }
        }
Beispiel #22
0
        public void OpenPrimaryAndSecondaryDB(string dbFileName,
                                              out BTreeDatabase primaryDB,
                                              out SecondaryBTreeDatabase secDB)
        {
            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();

            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

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

            secDB = SecondaryBTreeDatabase.Open(dbFileName,
                                                secDBConfig);
        }
        protected Repository(string databaseName, ILoggerFactory loggerService)
        {
            logger = loggerService.CreateLogger(databaseName);
            path   = Environment.GetEnvironmentVariable("DATA_DIR");
            var cfg = new BTreeDatabaseConfig
            {
                Creation      = CreatePolicy.IF_NEEDED,
                CacheSize     = new CacheInfo(1, 0, 1),
                ErrorFeedback = (prefix, message) =>
                {
                    logger.LogError($"{prefix}: {message}");
                },
                ErrorPrefix = databaseName,
                Duplicates  = DuplicatesPolicy.SORTED,
            };

            db = BTreeDatabase.Open(Path.Combine(path, databaseName + ".db"), cfg);

            cursor = db.Cursor();
        }
Beispiel #24
0
        public void TestAddUnique()
        {
            BTreeDatabase db;
            BTreeCursor   cursor;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

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

            Configuration.ClearDir(testHome);

            // Open a database and cursor.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;

            // To put no duplicate data, the database should be set to be sorted.
            dbConfig.Duplicates = DuplicatesPolicy.SORTED;
            db     = BTreeDatabase.Open(testHome + "/" + testName + ".db", dbConfig);
            cursor = db.Cursor();

            // Add record("key", "data") into database.
            CursorTest.AddOneByCursor(db, cursor);

            // Fail to add duplicate record("key","data").
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
            try
            {
                cursor.AddUnique(pair);
            }
            catch (KeyExistException)
            {
            }
            finally
            {
                cursor.Close();
                db.Close();
            }
        }
Beispiel #25
0
        public void TestJoin()
        {
            testName = "TestJoin";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName   = testHome + "/" + testName + ".db";
            string secFileName1 = testHome + "/" + "sec_" + testName + "1.db";
            string secFileName2 = testHome + "/" + "sec_" + testName + "2.db";

            Configuration.ClearDir(testHome);

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

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

            /*
             * Open two databases, their secondary databases and
             * secondary cursors.
             */
            SecondaryBTreeDatabase secDB1, secDB2;

            SecondaryCursor[] cursors = new SecondaryCursor[2];
            GetSecCursor(db, secFileName1,
                         new SecondaryKeyGenDelegate(KeyGenOnBigByte),
                         out secDB1, out cursors[0], false, null);
            GetSecCursor(db, secFileName2,
                         new SecondaryKeyGenDelegate(KeyGenOnLittleByte),
                         out secDB2, out cursors[1], true, null);

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

            // Close all.
            joinCursor.Close();
            cursors[0].Close();
            cursors[1].Close();
            secDB1.Close();
            secDB2.Close();
            db.Close();
        }
Beispiel #26
0
        public void TestNestedTXN()
        {
            DatabaseEntry       key, data;
            DatabaseEnvironment master    = SetUpEnv("./master", 100, 8000, true);
            BTreeDatabase       db        = Open(master, true);
            TransactionConfig   txnconfig = new TransactionConfig();
            Transaction         txn       = master.BeginTransaction();
            Transaction         txn1      = master.BeginTransaction(txnconfig, txn);

            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Data"));
            db.Put(key, data, txn1);
            txn1.Commit();
            txn.Commit();

            byte[] token;
            Assert.Throws <ArgumentException>(delegate { token = txn1.CommitToken; });
            master.Close();
        }
Beispiel #27
0
        public void OpenSecDB(string dbFileName,
                              string dbSecFileName, out BTreeDatabase db,
                              out SecondaryBTreeDatabase secDB)
        {
            // Open a primary database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

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

            // Open a secondary database.
            SecondaryBTreeDatabaseConfig secConfig =
                new SecondaryBTreeDatabaseConfig(db,
                                                 new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secConfig.Creation   = CreatePolicy.IF_NEEDED;
            secConfig.Duplicates = DuplicatesPolicy.SORTED;
            secDB = SecondaryBTreeDatabase.Open(dbSecFileName,
                                                secConfig);
        }
Beispiel #28
0
        public void TestConfig()
        {
            testName = "TestConfig";
            testHome = testFixtureHome + "/" + testName;
            string     dbFileName = testHome + "/" + testName + ".db";
            XmlElement xmlElem    = Configuration.TestSetUp(
                testFixtureName, testName);

            Configuration.ClearDir(testHome);

            // Open a database.
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();

            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase btreeDB = BTreeDatabase.Open(
                dbFileName, btreeDBConfig);

            // Configure and initialize sequence.
            SequenceConfig seqConfig = new SequenceConfig();

            seqConfig.BackingDatabase = btreeDB;
            seqConfig.Creation        = CreatePolicy.IF_NEEDED;
            seqConfig.key             = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            SequenceConfigTest.Config(xmlElem, ref seqConfig, true);
            Sequence seq = new Sequence(seqConfig);


            /*
             * Confirm that the squence is opened with the
             * configuration that we set.
             */
            Confirm(xmlElem, seq, true);

            /* Close sequence, database and environment. */
            seq.Close();
            btreeDB.Close();
        }
Beispiel #29
0
        public void TestKeyGen()
        {
            testName = "TestKeyGen";
            testHome = testFixtureHome + "/" + testName;
            string dbFileName = testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);

            // Open primary database.
            BTreeDatabaseConfig primaryDBConfig =
                new BTreeDatabaseConfig();

            primaryDBConfig.Creation = CreatePolicy.IF_NEEDED;
            BTreeDatabase primaryDB =
                BTreeDatabase.Open(dbFileName, primaryDBConfig);

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

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

            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

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

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

            // Close primary database.
            primaryDB.Close();
        }
Beispiel #30
0
        public void OpenNewSequenceInEnv(string home, string dbname,
                                         out DatabaseEnvironment env, out BTreeDatabase db,
                                         out Sequence seq)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

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

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

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

            Transaction    seqTxn    = env.BeginTransaction();
            SequenceConfig seqConfig = new SequenceConfig();

            seqConfig.BackingDatabase = db;
            seqConfig.Creation        = CreatePolicy.ALWAYS;
            seqConfig.Decrement       = false;
            seqConfig.FreeThreaded    = true;
            seqConfig.Increment       = true;
            seqConfig.InitialValue    = 0;
            seqConfig.key             = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            seq            = new Sequence(seqConfig);
            seqTxn.Commit();
        }
Beispiel #31
0
        public void EnsureIndex()
        {
            Name = registeredIndexer.IndexName;
            config = environment.IndexDatabaseConfigForTypes.ContainsKey(registeredIndexer.YieldType)
                         ? environment.IndexDatabaseConfigForTypes[registeredIndexer.YieldType]
                         : environment.IndexDatabaseConfigForTypes[typeof(object)];

            //Before we open (with create if needed).
            var recompileRequired = RecompileIsRequired();

            index =
                BTreeDatabase.Open(
                    getIndexFilename(),
                    config);

            reverseIndex =
                HashDatabase.Open(
                    getReverseIndexFilename(),
                    environment.ReverseIndexDatabaseConfig);

            if (recompileRequired)
                BuildIndexFromGraphs();
            else
                indexCompiled.Set();
        }