public static void SetUpEnvWithTxnAndLocking(string envHome,
		    out DatabaseEnvironment env, out Transaction txn,
		    uint maxLock, uint maxLocker, uint maxObject, uint partition)
        {
            // Configure env and locking subsystem.
            LockingConfig lkConfig = new LockingConfig();

            /*
             * If the maximum number of locks/lockers/objects
             * is given, then the LockingConfig is set. Unless,
             * it is not set to any value.
             */
            if (maxLock != 0)
                lkConfig.MaxLocks = maxLock;
            if (maxLocker != 0)
                lkConfig.MaxLockers = maxLocker;
            if (maxObject != 0)
                lkConfig.MaxObjects = maxObject;
            if (partition != 0)
                lkConfig.Partitions = partition;

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.LockSystemCfg = lkConfig;
            envConfig.UseLocking = true;
            envConfig.NoLocking = false;
            env = DatabaseEnvironment.Open(envHome, envConfig);
            txn = env.BeginTransaction();
        }
        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();
        }
Exemple #3
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();
        }
Exemple #4
0
        public void WriteRecordsInTxn(BTreeDatabase db, 
		    DatabaseEnvironment env)
        {
            Transaction txn = env.BeginTransaction();
            /*
             * Write ten records into the database. The records
             * from 1st to 5th and 8th to 10th are unique in the
             * database. The data in the 6th and 7th records
             * are the same.
             */
            for (int i = 0; i < 10; i++)
            {
                if (i == 5 || i == 6)
                    db.Put(new DatabaseEntry(
                        BitConverter.GetBytes(i)),
                        new DatabaseEntry(
                        BitConverter.GetBytes((int)10)), txn);
                else
                    db.Put(new DatabaseEntry(
                        BitConverter.GetBytes(i)),
                        new DatabaseEntry(BitConverter.GetBytes(i)), txn);
            }

            txn.Commit();
        }
Exemple #5
0
        public void MoveWithRMW(string home, string name)
        {
            paramEnv = null;
            paramDB = null;

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

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

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

            txn.Commit();

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

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

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

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

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

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

            t1.Join();
            t2.Join();
            paramDB.Close();
            paramEnv.Close();
        }
Exemple #6
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);
        }
Exemple #7
0
        public void TestSnapshotIsolation()
        {
            BTreeDatabaseConfig dbConfig;
            DatabaseEntry key, data;
            DatabaseEnvironmentConfig envConfig;
            Thread readThread, updateThread;
            Transaction txn;

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

            Configuration.ClearDir(testHome);

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

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

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

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

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

            paramDB.Close();
            paramEnv.Close();
        }
        public void PutRecordWithTxn(out DatabaseEnvironment env,
		    string home, string dbName, out Transaction txn)
        {
            BTreeDatabase db;

            // Open a new environment and begin a transaction.
            SetUpTransactionalEnv(home, out env);
            TransactionConfig txnConfig = new TransactionConfig();
            txnConfig.Name = "Transaction";
            txn = env.BeginTransaction(txnConfig);
            Assert.AreEqual("Transaction", txn.Name);

            // Open a new database within the transaction.
            OpenBtreeDBInEnv(dbName + ".db", env, out db, true, txn);

            // Write to the database within the transaction.
            WriteOneIntoBtreeDBWithTxn(db, txn);

            // Close the database.
            db.Close();
        }
Exemple #9
0
        /* Initialize environment and database (s) */
        public void InitDbs()
        {
            /* Open transactional environment */
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseMPool = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseTxns = true;
            envConfig.LockSystemCfg = new LockingConfig();
            envConfig.LockSystemCfg.MaxLocks =
                (this.dups == 0) ?
                (uint)this.num :
                (uint)(this.num * this.dups);
            envConfig.LockSystemCfg.MaxObjects =
                envConfig.LockSystemCfg.MaxLocks;
            if (this.cachesize != 0) {
                envConfig.MPoolSystemCfg = new MPoolConfig();
                envConfig.MPoolSystemCfg.CacheSize =
                    new CacheInfo(0, this.cachesize, 1);
            }

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

            Transaction txn = env.BeginTransaction();

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

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

                txn.Commit();
            } catch (DatabaseException e1) {
                txn.Abort();
                throw e1;
            } catch (FileNotFoundException e2) {
                txn.Abort();
                throw e2;
            }
        }
        public void SetUpEnvAndTxn(string home,
		    out DatabaseEnvironment env, out Transaction txn)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            env = DatabaseEnvironment.Open(home, envConfig);
            txn = env.BeginTransaction();
        }
Exemple #11
0
        public void TestLockStats()
        {
            testName = "TestLockStats";
            SetUpTest(true);

            // Configure locking subsystem.
            LockingConfig lkConfig = new LockingConfig();
            lkConfig.MaxLockers = 60;
            lkConfig.MaxLocks = 50;
            lkConfig.MaxObjects = 70;
            lkConfig.Partitions = 20;
            lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT;

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

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

            env.PrintLockingSystemStats();

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

            testLockStatsEnv = env;
            testLockStatsDb = db;

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

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

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

            Thread thread1 = new Thread(GenerateDeadlock);
            thread1.Start();
            while (DeadlockDidPut == 0)
                Thread.Sleep(10);

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

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

            db.Close();
            env.Close();
        }
        private void GetMultipleDB(string home, string dbFileName, 
		    BTreeDatabaseConfig dbConfig, out DatabaseEnvironment env, 
		    out Transaction txn, out BTreeDatabase db,
		    out BTreeCursor cursor)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseTxns = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool = true;
            env = DatabaseEnvironment.Open(home, envConfig);
            txn = env.BeginTransaction();
            if (dbConfig == null)
                dbConfig = new BTreeDatabaseConfig();
            dbConfig.Env = env;
            GetMultipleDB(dbFileName, dbConfig, txn, out db,
                out cursor);
        }
        private void PutMultipleKey(DatabaseEnvironment env)
        {
            List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                new List<KeyValuePair<
                DatabaseEntry, DatabaseEntry>>();
            BTreeDatabase db;
            DatabaseEntry key, value;
            Transaction txn;
            string dbFileName = (env == null) ? testHome +
                "/" + testName + ".db" : testName + ".db";
            int i;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            if (env != null) {
                dbConfig.Env = env;
                txn = env.BeginTransaction();
                db = BTreeDatabase.Open(
                    dbFileName, dbConfig, txn);
                txn.Commit();
            } else
                db = BTreeDatabase.Open(dbFileName, dbConfig);

            for (i = 0; i < 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                value = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" +
                    i + Configuration.RandomString(512)));
                pList.Add(new KeyValuePair<
                    DatabaseEntry, DatabaseEntry>(key, value));
            }

            // Create btree bulk buffer for key/value pairs.
            MultipleKeyDatabaseEntry pairBuff =
                new MultipleKeyDatabaseEntry(pList, false);
            i = 0;
            foreach (KeyValuePair<DatabaseEntry, DatabaseEntry>
                pair in pairBuff) {
                Assert.AreEqual(pList[i].Key.Data,
                    pair.Key.Data);
                Assert.AreEqual(pList[i].Value.Data,
                    pair.Value.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            /*
             * Create bulk buffer from another key/value pairs
             * bulk buffer.
             */
            MultipleKeyDatabaseEntry pairBuff1 =
                new MultipleKeyDatabaseEntry(
                pairBuff.Data, false);
            Assert.AreEqual(false, pairBuff1.Recno);
            i = 0;
            foreach (KeyValuePair<DatabaseEntry, DatabaseEntry>
                pair in pairBuff1) {
                Assert.AreEqual(pList[i].Key.Data,
                    pair.Key.Data);
                Assert.AreEqual(pList[i].Value.Data,
                    pair.Value.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            if (env == null) {
                // Bulk insert with key/value pair bulk buffer.
                db.Put(pairBuff);
                Cursor cursor = db.Cursor();
                Assert.IsTrue(cursor.MoveFirst());
                i = 0;
                Assert.AreEqual(pList[i].Key.Data,
                     cursor.Current.Key.Data);
                Assert.AreEqual(pList[i].Value.Data,
                     cursor.Current.Value.Data);
                while (cursor.MoveNext()) {
                    i++;
                    Assert.AreEqual(pList[i].Key.Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(pList[i].Value.Data,
                        cursor.Current.Value.Data);
                }
                Assert.AreEqual(99, i);
                cursor.Close();
                /*
                 * Dump all records from the database. The
                 * number of records should be 100.
                 */
                Assert.AreEqual(100, db.Truncate());

                // Bulk insert with copied key/value pair buffer.
                db.Put(pairBuff1);
                cursor = db.Cursor();
                Assert.IsTrue(cursor.MoveFirst());
                i = 0;
                Assert.AreEqual(pList[i].Key.Data,
                    cursor.Current.Key.Data);
                Assert.AreEqual(pList[i].Value.Data,
                    cursor.Current.Value.Data);
                while (cursor.MoveNext()) {
                    i++;
                    Assert.AreEqual(pList[i].Key.Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(pList[i].Value.Data,
                        cursor.Current.Value.Data);
                }
                Assert.AreEqual(99, i);
                cursor.Close();
                Assert.AreEqual(100, db.Truncate());
            } else {
                txn = env.BeginTransaction();
                db.Put(pairBuff, txn);
                Assert.AreEqual(100, db.Truncate(txn));
                txn.Commit();
            }

            db.Close();
        }
        private void PutMultiple(DatabaseEnvironment env)
        {
            List<DatabaseEntry> kList = new List<DatabaseEntry>();
            List<DatabaseEntry> vList = new List<DatabaseEntry>();
            BTreeDatabase db;
            DatabaseEntry key, value;
            Transaction txn;
            string dbFileName = (env == null) ? testHome +
                "/" + testName + ".db" : testName + ".db";
            int i;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            if (env != null) {
                dbConfig.Env = env;
                txn = env.BeginTransaction();
                db = BTreeDatabase.Open(
                    dbFileName, dbConfig, txn);
                txn.Commit();
            } else
                db = BTreeDatabase.Open(dbFileName, dbConfig);

            for (i = 0; i < 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                value = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" + i +
                    Configuration.RandomString(512)));
                kList.Add(key);
                vList.Add(value);
            }

            // Create bulk buffer for non-recno based keys.
            MultipleDatabaseEntry keyBuff =
                new MultipleDatabaseEntry(kList, false);
            Assert.IsFalse(keyBuff.Recno);

            // Create bulk buffer for values.
            MultipleDatabaseEntry valBuff =
                new MultipleDatabaseEntry(vList, false);
            i = 0;
            foreach (DatabaseEntry dbt in valBuff) {
                Assert.AreEqual(vList[i].Data, dbt.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            // Create bulk buffer from another key buffer.
            MultipleDatabaseEntry keyBuff1 =
                new MultipleDatabaseEntry(
                keyBuff.Data, keyBuff.Recno);
            i = 0;
            foreach (DatabaseEntry dbt in keyBuff1) {
                Assert.AreEqual(kList[i].Data, dbt.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            if (env != null) {
                txn = env.BeginTransaction();
                db.Put(keyBuff, valBuff, txn);
                Assert.AreEqual(100, db.Truncate(txn));
                txn.Commit();
            } else {
                /*
                 * Bulk insert to database with key and value
                 * buffers.
                 */
                db.Put(keyBuff, valBuff);

                // Verify all records exist as expected.
                Cursor cursor = db.Cursor();
                i = 99;
                Assert.IsTrue(cursor.MoveLast());
                Assert.AreEqual(kList[i].Data,
                    cursor.Current.Key.Data);
                Assert.AreEqual(vList[i].Data,
                    cursor.Current.Value.Data);
                while (cursor.MovePrev()) {
                    i--;
                    Assert.AreEqual(kList[i].Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(vList[i].Data,
                        cursor.Current.Value.Data);
                }
                Assert.AreEqual(0, i);
                cursor.Close();
                /*
                 * Dumped all records. The number of records
                 * should be 100.
                 */
                Assert.AreEqual(100, db.Truncate());

                /*
                 * Bulk insert to database with a copied key
                 * buffer and a value buffer.
                 */
                db.Put(keyBuff1, valBuff);
                cursor = db.Cursor();
                Assert.IsTrue(cursor.MoveLast());
                i = 99;
                Assert.AreEqual(kList[i].Data,
                    cursor.Current.Key.Data);
                Assert.AreEqual(vList[i].Data,
                    cursor.Current.Value.Data);
                while (cursor.MovePrev()) {
                    i--;
                    Assert.AreEqual(kList[i].Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(vList[i].Data,
                        cursor.Current.Value.Data);
                }
                cursor.Close();
                Assert.AreEqual(0, i);
                /*
                 * Dumped all records. The number of records
                 * should be 100.
                 */
                Assert.AreEqual(100, db.Truncate());
            }

            db.Close();
        }
Exemple #15
0
        private void PopulateDb(DatabaseEnvironment env,
		    out BTreeDatabase db)
        {
            DatabaseEntry key, data;
            Transaction txn = null;
            string dbName;

            if (env != null) {
                txn = env.BeginTransaction();
                dbName = testName + ".db";
            } else
                dbName = testHome + "/" + testName + ".db";

            OpenBtreeDB(env, txn, dbName, out db);

            for (int i = 0; i < 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("hello"));
                db.Put(key, data, txn);
            }

            if (txn != null)
                txn.Commit();
        }