Example #1
0
        public void TestAddKeyLast()
        {
            BTreeDatabase db;
            BTreeCursor   cursor;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

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

            Configuration.ClearDir(testHome);

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

            // Add new record("key","data1") as the last of the data item of "key".
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1")));
            cursor.Add(pair, Cursor.InsertLocation.LAST);

            // Confirm the record is added as the first of the data item of "key".
            cursor.Move(new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), true);
            Assert.AreNotEqual(ASCIIEncoding.ASCII.GetBytes("data1"),
                               cursor.Current.Value.Data);

            cursor.Close();
            db.Close();
        }
Example #2
0
        public void TestInsertAfter()
        {
            BTreeDatabase db;
            BTreeCursor   cursor;
            DatabaseEntry data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            testName = "TestInsertAfter";
            SetUpTest(true);

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

            // Insert the new record("key","data1") after the record("key", "data").
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"));
            cursor.Insert(data, Cursor.InsertLocation.AFTER);

            /*
             * Move the cursor to the record("key", "data") and confirm that
             * the next record is the one just inserted.
             */
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
            Assert.IsTrue(cursor.Move(pair, true));
            Assert.IsTrue(cursor.MoveNext());
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), cursor.Current.Key.Data);
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), cursor.Current.Value.Data);

            cursor.Close();
            db.Close();
        }
Example #3
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();
            }
        }
Example #4
0
        public void TestInsertBefore()
        {
            BTreeDatabase db;
            BTreeCursor   cursor;
            DatabaseEntry data;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

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

            Configuration.ClearDir(testHome);

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

            // Insert the new record("key","data1") before the record("key", "data").
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"));
            cursor.Insert(data, Cursor.InsertLocation.BEFORE);

            /*
             * Move the cursor to the record("key", "data") and confirm
             * that the previous record is the one just inserted.
             */
            pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
            Assert.IsTrue(cursor.Move(pair, true));
            Assert.IsTrue(cursor.MovePrev());
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), cursor.Current.Key.Data);
            Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), cursor.Current.Value.Data);

            cursor.Close();
            db.Close();
        }