Package com.sleepycat.je

Examples of com.sleepycat.je.Database$DbState


    }

    public void testOpenAndClose()
        throws DatabaseException {

        Database priDb = openDatabase(false, "testDB", false);

        /* Open two secondaries as regular databases and as secondaries. */
        Database secDbDetached = openDatabase(true, "testSecDB", false);
        SecondaryDatabase secDb = openSecondary(priDb, true, "testSecDB",
                                                false, false);
        Database secDb2Detached = openDatabase(true, "testSecDB2", false);
        SecondaryDatabase secDb2 = openSecondary(priDb, true, "testSecDB2",
                                                 false, false);
        assertEquals(priDb.getSecondaryDatabases(),
                     Arrays.asList(new SecondaryDatabase[] {secDb, secDb2}));

        Transaction txn = txnBegin();

        /* Check that primary writes to both secondaries. */
        checkSecondaryUpdate(txn, priDb, 1, secDbDetached, true,
                                            secDb2Detached, true);
       
        /* New txn before closing database. */
        txnCommit(txn);
        txn = txnBegin();

        /* Close 2nd secondary. */
        secDb2.close();
        assertEquals(priDb.getSecondaryDatabases(),
                     Arrays.asList(new SecondaryDatabase[] {secDb }));

        /* Check that primary writes to 1st secondary only. */
        checkSecondaryUpdate(txn, priDb, 2, secDbDetached, true,
                                             secDb2Detached, false);
       
        /* New txn before closing database. */
        txnCommit(txn);
        txn = txnBegin();

        /* Close 1st secondary. */
        secDb.close();
        assertEquals(0, priDb.getSecondaryDatabases().size());

        /* Check that primary writes to no secondaries. */
        checkSecondaryUpdate(txn, priDb, 3, secDbDetached, false,
                                            secDb2Detached, false);

        /* Open the two secondaries again. */
        secDb = openSecondary(priDb, true, "testSecDB", false, false);
        secDb2 = openSecondary(priDb, true, "testSecDB2", false, false);
        assertEquals(priDb.getSecondaryDatabases(),
                     Arrays.asList(new SecondaryDatabase[] {secDb, secDb2}));

        /* Check that primary writes to both secondaries. */
        checkSecondaryUpdate(txn, priDb, 4, secDbDetached, true,
                                            secDb2Detached, true);

        /* Close the primary first to disassociate secondaries. */
        txnCommit(txn);
        priDb.close();
        assertNull(secDb.getPrimaryDatabase());
        assertNull(secDb2.getPrimaryDatabase());
        secDb2.close();
        secDb.close();

        secDb2Detached.close();
        secDbDetached.close();
    }
View Full Code Here


    public void testReadOnly()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();
        OperationStatus status;
        Transaction txn = txnBegin();

        for (int i = 0; i < NUM_RECS; i += 1) {
            status = priDb.put(txn, entry(i), entry(i));
            assertSame(OperationStatus.SUCCESS, status);
        }

        /*
         * Secondaries can be opened without a key creator if the primary is
         * read only.  openSecondary will specify a null key creator if the
         * readOnly param is false.
         */
        Database readOnlyPriDb = openDatabase(false, "testDB", true);
        SecondaryDatabase readOnlySecDb = openSecondary(readOnlyPriDb,
                                                        true, "testSecDB",
                                                        false, true);
        assertNull(readOnlySecDb.getSecondaryConfig().getKeyCreator());
        verifyRecords(txn, readOnlySecDb, NUM_RECS, true);

        txnCommit(txn);
        readOnlySecDb.close();
        readOnlyPriDb.close();
        secDb.close();
        priDb.close();
    }
View Full Code Here

    }

    public void testPopulate()
        throws DatabaseException {

        Database priDb = openDatabase(false, "testDB", false);
        OperationStatus status;
        Transaction txn = txnBegin();

        /* Test population of newly created secondary database. */
       
        for (int i = 0; i < NUM_RECS; i += 1) {
            assertSame(OperationStatus.SUCCESS,
                       priDb.put(txn, entry(i), entry(i)));
        }
        txnCommit(txn);

        SecondaryDatabase secDb = openSecondary(priDb, true, "testSecDB",
                                                true, false);
        txn = txnBegin();
        verifyRecords(txn, secDb, NUM_RECS, true);
        txnCommit(txn);

        /*
         * Clear secondary and perform populate again, to test the case where
         * an existing database is opened, and therefore a write txn will only
         * be created in order to populate it
         */
        Database secDbDetached = openDatabase(true, "testSecDB", false);
        secDb.close();
        txn = txnBegin();
        for (int i = 0; i < NUM_RECS; i += 1) {
            assertSame(OperationStatus.SUCCESS,
                       secDbDetached.delete(txn, entry(i + KEY_OFFSET)));
        }
        verifyRecords(txn, secDbDetached, 0, true);
        txnCommit(txn);
        secDb = openSecondary(priDb, true, "testSecDB", true, false);
        txn = txnBegin();
        verifyRecords(txn, secDb, NUM_RECS, true);
        verifyRecords(txn, secDbDetached, NUM_RECS, true);

        txnCommit(txn);
        secDbDetached.close();
        secDb.close();
        priDb.close();
    }
View Full Code Here

    public void testTruncate()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();
        OperationStatus status;
        Transaction txn = txnBegin();
       
        for (int i = 0; i < NUM_RECS; i += 1) {
            priDb.put(txn, entry(i), entry(i));
        }
        verifyRecords(txn, priDb, NUM_RECS, false);
        verifyRecords(txn, secDb, NUM_RECS, true);
        txnCommit(txn);
        secDb.close();
        priDb.close();

        txn = txnBegin();
        assertEquals(NUM_RECS, env.truncateDatabase(txn, "testDB", true));
        assertEquals(NUM_RECS, env.truncateDatabase(txn, "testSecDB", true));
        txnCommit(txn);

        secDb = initDb();
        priDb = secDb.getPrimaryDatabase();

        txn = txnBegin();
        verifyRecords(txn, priDb, 0, false);
        verifyRecords(txn, secDb, 0, true);
        txnCommit(txn);

        secDb.close();
        priDb.close();
    }
View Full Code Here

    }

    public void testUniqueSecondaryKey()
        throws DatabaseException {

        Database priDb = openDatabase(false, "testDB", false);
        SecondaryDatabase secDb = openSecondary(priDb, false, "testSecDB",
                                                false, false);
        DatabaseEntry key;
        DatabaseEntry data;
        DatabaseEntry pkey = new DatabaseEntry();
        Transaction txn;

        /* Put {0, 0} */
        txn = txnBegin();
        key = entry(0);
        data = entry(0);
        priDb.put(txn, key, data);
        txnCommit(txn);
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(null, entry(0 + KEY_OFFSET),
                               pkey, data, null));
        assertEquals(0, TestUtils.getTestVal(pkey.getData()));
        assertEquals(0, TestUtils.getTestVal(data.getData()));

        /* Put {1, 1} */
        txn = txnBegin();
        key = entry(1);
        data = entry(1);
        priDb.put(txn, key, data);
        txnCommit(txn);
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(null, entry(1 + KEY_OFFSET),
                               pkey, data, null));
        assertEquals(1, TestUtils.getTestVal(pkey.getData()));
        assertEquals(1, TestUtils.getTestVal(data.getData()));

        /* Put {2, 0} */
        txn = txnBegin();
        key = entry(2);
        data = entry(0);
        try {
            priDb.put(txn, key, data);
            /* Expect exception because secondary key must be unique. */
            fail();
        } catch (DatabaseException e) {
            txnAbort(txn);
            /* Ensure that primary record was not inserted. */
            assertEquals(OperationStatus.NOTFOUND,
                         secDb.get(null, key, data, null));
            /* Ensure that secondary record has not changed. */
            assertEquals(OperationStatus.SUCCESS,
                         secDb.get(null, entry(0 + KEY_OFFSET),
                                   pkey, data, null));
            assertEquals(0, TestUtils.getTestVal(pkey.getData()));
            assertEquals(0, TestUtils.getTestVal(data.getData()));
        }

        /* Overwrite {1, 1} */
        txn = txnBegin();
        key = entry(1);
        data = entry(1);
        priDb.put(txn, key, data);
        txnCommit(txn);
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(null, entry(1 + KEY_OFFSET),
                               pkey, data, null));
        assertEquals(1, TestUtils.getTestVal(pkey.getData()));
        assertEquals(1, TestUtils.getTestVal(data.getData()));

        /* Modify secondary key to {1, 3} */
        txn = txnBegin();
        key = entry(1);
        data = entry(3);
        priDb.put(txn, key, data);
        txnCommit(txn);
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(null, entry(3 + KEY_OFFSET),
                               pkey, data, null));
        assertEquals(1, TestUtils.getTestVal(pkey.getData()));
        assertEquals(3, TestUtils.getTestVal(data.getData()));

        secDb.close();
        priDb.close();
    }
View Full Code Here

     */
    public void testOperationsNotAllowed()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();
        Transaction txn = txnBegin();

        /* Open secondary without a key creator. */
        try {
            env.openSecondaryDatabase(txn, "xxx", priDb, null);
            fail();
        } catch (NullPointerException expected) { }
        try {
            env.openSecondaryDatabase(txn, "xxx", priDb,
                                      new SecondaryConfig());
            fail();
        } catch (NullPointerException expected) { }
       
        /* Database operations. */

        DatabaseEntry key = entry(1);
        DatabaseEntry data = entry(2);

        try {
            secDb.getSearchBoth(txn, key, data, LockMode.DEFAULT);
            fail();
        } catch (UnsupportedOperationException expected) { }

        try {
            secDb.put(txn, key, data);
            fail();
        } catch (UnsupportedOperationException expected) { }

        try {
            secDb.putNoOverwrite(txn, key, data);
            fail();
        } catch (UnsupportedOperationException expected) { }

        try {
            secDb.putNoDupData(txn, key, data);
            fail();
        } catch (UnsupportedOperationException expected) { }

        try {
            secDb.truncate(txn, true);
            fail();
        } catch (UnsupportedOperationException expected) { }

        try {
            secDb.join(new Cursor[0], null);
            fail();
        } catch (UnsupportedOperationException expected) { }

        /* Cursor operations. */

        txnCommit(txn);
        txn = txnBeginCursor();

        SecondaryCursor cursor = null;
        try {
            cursor = secDb.openSecondaryCursor(txn, null);

            try {
                cursor.getSearchBoth(key, data, LockMode.DEFAULT);
                fail();
            } catch (UnsupportedOperationException expected) { }

            try {
                cursor.getSearchBothRange(key, data, LockMode.DEFAULT);
                fail();
            } catch (UnsupportedOperationException expected) { }

            try {
                cursor.putCurrent(data);
                fail();
            } catch (UnsupportedOperationException expected) { }

            try {
                cursor.put(key, data);
                fail();
            } catch (UnsupportedOperationException expected) { }

            try {
                cursor.putNoOverwrite(key, data);
                fail();
            } catch (UnsupportedOperationException expected) { }

            try {
                cursor.putNoDupData(key, data);
                fail();
            } catch (UnsupportedOperationException expected) { }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        txnCommit(txn);
        secDb.close();
        priDb.close();

        /* Primary with duplicates. */
        priDb = openDatabase(true, "testDBWithDups", false);
        try {
            openSecondary(priDb, true, "testSecDB", false, false);
            fail();
        } catch (IllegalArgumentException expected) {}

        priDb.close();

        /* Single secondary with two primaries.*/
        Database pri1 = openDatabase(false, "pri1", false);
        Database pri2 = openDatabase(false, "pri2", false);
        Database sec1 = openSecondary(pri1, false, "sec", false, false);
        try {
            openSecondary(pri2, false, "sec", false, false);
            fail();
        } catch (IllegalArgumentException expected) {}
        sec1.close();
        pri1.close();
        pri2.close();
    }
View Full Code Here

     */
    public void testNullLockMode()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();
        Transaction txn = txnBegin();

        DatabaseEntry key = entry(0);
        DatabaseEntry data = entry(0);
        DatabaseEntry secKey = entry(KEY_OFFSET);
        DatabaseEntry found = new DatabaseEntry();
        DatabaseEntry found2 = new DatabaseEntry();
        DatabaseEntry found3 = new DatabaseEntry();

        assertEquals(OperationStatus.SUCCESS,
                     priDb.put(txn, key, data));
        assertEquals(OperationStatus.SUCCESS,
                     priDb.put(txn, entry(1), data));
        assertEquals(OperationStatus.SUCCESS,
                     priDb.put(txn, entry(2), entry(2)));

        /* Database operations. */

        assertEquals(OperationStatus.SUCCESS,
                     priDb.get(txn, key, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     priDb.getSearchBoth(txn, key, data, null));
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(txn, secKey, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secDb.get(txn, secKey, found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secDb.getSearchBoth(txn, secKey, key, found, null));

        /* Cursor operations. */

        txnCommit(txn);
        txn = txnBeginCursor();
        Cursor cursor = priDb.openCursor(txn, null);
        SecondaryCursor secCursor = secDb.openSecondaryCursor(txn, null);

        assertEquals(OperationStatus.SUCCESS,
                     cursor.getSearchKey(key, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getSearchBoth(key, data, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getSearchKeyRange(key, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getSearchBothRange(key, data, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getFirst(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getNext(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getPrev(found, found2, null));
        assertEquals(OperationStatus.NOTFOUND,
                     cursor.getNextDup(found, found2, null));
        assertEquals(OperationStatus.NOTFOUND,
                     cursor.getPrevDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getNextNoDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getPrevNoDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     cursor.getLast(found, found2, null));

        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchKey(secKey, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchKeyRange(secKey, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getFirst(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNext(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrev(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNextDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrevDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNextNoDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrevNoDup(found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getLast(found, found2, null));

        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchKey(secKey, found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchBoth(secKey, data, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchKeyRange(secKey, found, found2, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchBothRange(secKey, data, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getFirst(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNext(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrev(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNextDup(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrevDup(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getNextNoDup(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getPrevNoDup(found, found2, found3, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getLast(found, found2, found3, null));

        secCursor.close();
        cursor.close();
        txnCommit(txn);
        secDb.close();
        priDb.close();
        env.close();
        env = null;
    }
View Full Code Here

     */
    public void testCursorState()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();
        Transaction txn = txnBegin();

        DatabaseEntry key = entry(0);
        DatabaseEntry data = entry(0);
        DatabaseEntry secKey = entry(KEY_OFFSET);
        DatabaseEntry found = new DatabaseEntry();
        DatabaseEntry found2 = new DatabaseEntry();

        assertEquals(OperationStatus.SUCCESS,
                     priDb.put(txn, key, data));

        txnCommit(txn);
        txn = txnBeginCursor();
        Cursor cursor = priDb.openCursor(txn, null);
        SecondaryCursor secCursor = secDb.openSecondaryCursor(txn, null);

        /* Check the uninitialized state for certain operations. */

        try {
            cursor.count();
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.delete();
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.putCurrent(data);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getCurrent(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getNextDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getPrevDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}

        try {
            secCursor.count();
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.delete();
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getCurrent(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getNextDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getPrevDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}

        /* Initialize, then close, then check all operations. */

        assertEquals(OperationStatus.SUCCESS,
                     cursor.getSearchKey(key, found, null));
        assertEquals(OperationStatus.SUCCESS,
                     secCursor.getSearchKey(secKey, found, null));
        secCursor.close();
        cursor.close();

        try {
            cursor.close();
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.count();
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.delete();
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.put(key, data);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.putNoOverwrite(key, data);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.putNoDupData(key, data);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.putCurrent(data);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getCurrent(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getSearchKey(key, found, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getSearchBoth(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getSearchKeyRange(key, found, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getSearchBothRange(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getFirst(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getNext(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getPrev(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getNextDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getPrevDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getNextNoDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getPrevNoDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            cursor.getLast(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}

        try {
            secCursor.close();
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.count();
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.delete();
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getCurrent(key, data, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getSearchKey(secKey, found, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getSearchKeyRange(secKey, found, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getFirst(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getNext(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getPrev(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getNextDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getPrevDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getNextNoDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getPrevNoDup(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}
        try {
            secCursor.getLast(found, found2, null);
            fail();
        } catch (DatabaseException expected) {}

        txnCommit(txn);
        secDb.close();
        priDb.close();
        env.close();
        env = null;
    }
View Full Code Here

     * Open environment, primary and secondary db
     */
    private SecondaryDatabase initDb()
        throws DatabaseException {

        Database priDb = openDatabase(false, "testDB", false);
        SecondaryDatabase secDb = openSecondary(priDb, true, "testSecDB",
                                                false, false);
        return secDb;
    }
View Full Code Here

        dbConfig.setTransactional(isTransactional);
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(allowDuplicates);
        dbConfig.setReadOnly(readOnly);
        Transaction txn = txnBegin();
        Database priDb;
        try {
            priDb = env.openDatabase(txn, name, dbConfig);
        } finally {
            txnCommit(txn);
        }
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Database$DbState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.