Package jdbm

Examples of jdbm.RecordManager


     *                                      implementation.
     * @throws IllegalArgumentException if some options are invalid.
     */
    public RecordManager createRecordManager( String name, Properties options ) throws IOException
    {
        RecordManager  recman;
        String         value;
        int            cacheSize;

        recman = new BaseRecordManager( name );

View Full Code Here


    /**
     * Example main entrypoint.
     */
    public static void main( String[] args ) {
        RecordManager recman;
        long          recid;
        Tuple         tuple = new Tuple();
        TupleBrowser  browser;
        BTree         tree;
        Properties    props;

        props = new Properties();

        try {
            // open database and setup an object cache
            recman = RecordManagerFactory.createRecordManager( DATABASE, props );

            // try to reload an existing B+Tree
            recid = recman.getNamedObject( BTREE_NAME );
            if ( recid != 0 ) {
                tree = BTree.load( recman, recid );
                System.out.println( "Reloaded existing BTree with " + tree.size()
                                    + " famous people." );
            } else {
                // create a new B+Tree data structure and use a StringComparator
                // to order the records based on people's name.
                tree = BTree.createInstance( recman, new StringComparator() );
                recman.setNamedObject( BTREE_NAME, tree.getRecid() );
                System.out.println( "Created a new empty BTree" );
            }

            // insert people with their respective occupation
            System.out.println();
            for ( int i=0; i<people.length; i++ ) {
                System.out.println( "Insert: " + people[i] );
                tree.insert( people[ i ], occupations[ i ], false );
            }

            // make the data persistent in the database
            recman.commit();

            // show list of people with their occupation
            System.out.println();
            System.out.println( "Person                   Occupation       " );
            System.out.println( "------------------       ------------------" );
View Full Code Here

        throws Exception

    {

        RecordManager recman;



        recman = new BaseRecordManager( TestRecordFile.testFileName );



        // insert a 1500 byte record.

        byte[] data = TestUtil.makeRecord(1500, (byte) 1);

        long rowid = recman.insert(data);

        assertTrue("check data1",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid), 1500, (byte) 1) );





        // delete the record

        recman.delete(rowid);



        // insert a 0 byte record. Should have the same rowid.

        data = TestUtil.makeRecord(0, (byte) 2);

        long rowid2 = recman.insert(data);

        assertEquals("old and new rowid", rowid, rowid2);

        assertTrue("check data2",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid2), 0, (byte) 2) );



        // now make the record a bit bigger

        data = TestUtil.makeRecord(10000, (byte) 3);

        recman.update(rowid, data);

        assertTrue("check data3",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid), 10000, (byte) 3) );



        // .. and again

        data = TestUtil.makeRecord(30000, (byte) 4);

        recman.update(rowid, data);

        assertTrue("check data3",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid), 30000, (byte) 4) );



        // close the file

        recman.close();

    }
View Full Code Here

        throws Exception

    {

        RecordManager recman;



        // Note: We start out with an empty file

        recman = new BaseRecordManager( TestRecordFile.testFileName );



        // insert a 150000 byte record.

        byte[] data1 = TestUtil.makeRecord(150000, (byte) 1);

        long rowid1 = recman.insert(data1);

        assertTrue("check data1",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid1), 150000, (byte) 1) );



        // rollback transaction, should revert to previous state

        recman.rollback();



        // insert same 150000 byte record.

        byte[] data2 = TestUtil.makeRecord(150000, (byte) 1);

        long rowid2 = recman.insert(data2);

        assertTrue("check data2",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid2), 150000, (byte) 1) );



        assertEquals("old and new rowid", rowid1, rowid2);



        recman.commit();



        // insert a 150000 byte record.

        data1 = TestUtil.makeRecord(150000, (byte) 2);

        rowid1 = recman.insert(data1);

        assertTrue("check data1",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid1), 150000, (byte) 2) );



        // rollback transaction, should revert to previous state

        recman.rollback();



        // insert same 150000 byte record.

        data2 = TestUtil.makeRecord(150000, (byte) 2);

        rowid2 = recman.insert(data2);

        assertTrue("check data2",

               TestUtil.checkRecord( (byte[]) recman.fetch(rowid2), 150000, (byte) 2) );



        assertEquals("old and new rowid", rowid1, rowid2);



        // close the file

        recman.close();

    }
View Full Code Here

     * Test case courtesy of Derek Dick (mailto:ddick@users.sourceforge.net)
     */
    public void testRollback1()
        throws Exception
    {
        RecordManager recman;
        long root;

        // Note: We start out with an empty file
        recman = new BaseRecordManager( TestRecordFile.testFileName );

  root = recman.getNamedObject( "xyz" );
            
        HTree tree = null;
        if ( root == 0 ) {
            // create a new one
            tree = HTree.createInstance( recman );
            root = tree.getRecid();
            recman.setNamedObject( "xyz", root );
            recman.commit();
        } else {
            tree = HTree.load( recman, root );
        }

        tree.put( "Foo", "Bar" );
        tree.put( "Fo", "Fum" );

        recman.commit();

        tree.put( "Hello", "World" );

        recman.rollback();

        tree = HTree.load( recman, root );
        assertTrue( tree.get( "Foo" ).equals( "Bar" ) );
        assertTrue( tree.get( "Fo" ).equals( "Fum" ) );
        assertTrue( tree.get( "Hello" ) == null );
View Full Code Here

     * Test case courtesy of Derek Dick (mailto:ddick@users.sourceforge.net)
     */
    public void testRollback2()
        throws Exception
    {
        RecordManager recman;
        long root;

        // Note: We start out with an empty file
        recman = new BaseRecordManager( TestRecordFile.testFileName );

  root = recman.getNamedObject( "xyz" );

        HTree tree = null;
        if ( root == 0 ) {
            // create a new one
            tree = HTree.createInstance( recman );
            root = tree.getRecid();
            recman.setNamedObject( "xyz", root );
            recman.commit();
        } else {
            tree = HTree.load( recman, root );
        }

        tree.put( "hello", "world" );
        tree.put( "goodnight", "gracie" );
        recman.commit();

        tree.put( "derek", "dick" );
        recman.rollback();

        assertTrue( tree.get( "derek" ) == null );
        assertTrue( tree.get( "goodnight" ).equals( "gracie" ) );
        assertTrue( tree.get( "hello" ).equals( "world" ) );
    }
View Full Code Here

     */

    public void testBasics() throws Exception {

        RecordManager recman;



        recman = RecordManagerFactory.createRecordManager( TestRecordFile.testFileName );



        // as this code is meant to test data structure calculcations

        // and stuff like that, we may want to disable transactions

        // that just slow us down.

        //  mgr.disableTransactions();



        RecordData[] d = new RecordData[RECORDS];

        int recordCount = 0, rootCount = 0;

        int inserts = 0, updates = 0, deletes = 0, fetches = 0;

        int rootgets = 0, rootsets = 0;

        int slot = -1;



        try {



            for (int i = 0; i < ROUNDS; i++) {

                if ((i % RPPROMILLE) == 0)

                    System.out.print("\rComplete: "

                        + i/RPPROMILLE + "/1000th");



                // close and re-open a couple of times during the

                // test, in order to check flushing etcetera.

                if ((i % (ROUNDS / 5)) == 0) {

                    System.out.print(" (reopened at round "

                    + i/RPPROMILLE + ")");

                    recman.close();

                    recman = RecordManagerFactory.createRecordManager( TestRecordFile.testFileName );

                    //        recman.disableTransactions();

                }



                // generate a random number and assign ranges to operations:

                // 0-10 = insert, 20 = delete, 30-50 = update, 51 = set root,

                // 52 = get root, rest = fetch.

                int op = rnd.nextInt(100);

                if (op <= 10) {

                    // INSERT RECORD

                    if (recordCount == RECORDS) {

                        i -= 1;

                        continue;

                    }



                    slot = 0;

                    while (d[slot] != null)

                        slot++;



                    d[slot] = new RecordData(0, rnd.nextInt(MAXSIZE),

                    (byte) rnd.nextInt());

                    d[slot].rowid =

                        recman.insert(TestUtil.makeRecord(d[slot].size,

                    d[slot].b));

                    recordCount++;

                    inserts++;

                }

                else if (op == 20) {

                    // DELETE RECORD

                    if (recordCount == 0) {

                        i -= 1;

                        continue;

                    }



                    slot = getRandomAllocatedSlot(d);

                    recman.delete(d[slot].rowid);

                    d[slot] = null;

                    recordCount--;

                    deletes++;

                }

                else if (op <= 50) {

                    // UPDATE RECORD

                    if (recordCount == 0) {

                        i -= 1;

                        continue;

                    }



                    slot = getRandomAllocatedSlot(d);

                    d[slot].size = rnd.nextInt(MAXSIZE);

                    d[slot].b = (byte) rnd.nextInt();

                    recman.update(d[slot].rowid,

                    TestUtil.makeRecord(d[slot].size,

                    d[slot].b));

                    updates++;

                }

                else if (op == 51) {

                    // SET ROOT

                    int root = rnd.nextInt(FileHeader.NROOTS);

                    roots[root] = rnd.nextLong();

                    recman.setRoot(root, roots[root]);

                    rootsets++;

                }

                else if (op == 52) {

                    // GET ROOT

                    if (rootCount == 0) {

                        i -= 1;

                        continue;

                    }



                    int root = getRandomAllocatedRoot();

                    assertEquals("root", roots[root], recman.getRoot(root));

                    rootgets++;

                }

                else {

                    // FETCH RECORD

                    if (recordCount == 0) {

                        i -= 1;

                        continue;

                    }



                    slot = getRandomAllocatedSlot(d);

                    byte[] data = (byte[]) recman.fetch(d[slot].rowid, ByteArraySerializer.INSTANCE );

                    assertTrue("fetch round=" + i + ", slot=" + slot

                    + ", " + d[slot],

                    TestUtil.checkRecord(data, d[slot].size, d[slot].b));

                    fetches++;

                }

            }

            recman.close();

        } catch (Throwable e) {

            e.printStackTrace();

View Full Code Here

     */

    public void testInserts() throws Exception {

        RecordManager recman;



        recman = RecordManagerFactory.createRecordManager( TestRecordFile.testFileName );



        int inserts = 0;

        long start = System.currentTimeMillis();

        try {



            long stop = 0;

            while (true) {



                recman.insert(TestUtil.makeRecord(rnd.nextInt(MAXSIZE),

                                               (byte) rnd.nextInt()));

                inserts++;

                if ((inserts % 25) == 0) {

                    stop = System.currentTimeMillis();

                    if (stop - start >= 60000)

                        break;

                }

            }

            recman.close();

            System.out.println("Inserts: " + inserts + " in "

                               + (stop - start) + " millisecs");

View Full Code Here

     */

    private long[] makeRows() throws Exception {

        RecordManager recman;

        Properties    options;



        options = new Properties();

        options.setProperty( RecordManagerOptions.DISABLE_TRANSACTIONS, "true" );



        recman = RecordManagerFactory.createRecordManager( TestRecordFile.testFileName,

                                                           options );



        long[] retval = new long[RECORDS];

        System.out.print("Creating test database");

        long start = System.currentTimeMillis();

        try {

            for (int i = 0; i < RECORDS; i++) {

                retval[i] = recman.insert(TestUtil

                                       .makeRecord(rnd.nextInt(MAXSIZE),

                                                   (byte) rnd.nextInt()));

                if ((i % 100) == 0)

                    System.out.print(".");

            }

            recman.close();

        } catch (Throwable e) {

            e.printStackTrace();

View Full Code Here

     */

    public void testFetches() throws Exception {

        RecordManager recman;



        long[] rowids = makeRows();



        recman = RecordManagerFactory.createRecordManager( TestRecordFile.testFileName );



        int fetches = 0;

        long start = System.currentTimeMillis();

        try {



            long stop = 0;

            while (true) {

                recman.fetch( rowids[ rnd.nextInt( RECORDS ) ] );

                fetches++;

                if ((fetches % 25) == 0) {

                    stop = System.currentTimeMillis();

                    if (stop - start >= 60000)

                        break;

                }

            }

            recman.close();

            System.out.println("Fetches: " + fetches + " in "

                               + (stop - start) + " millisecs");

View Full Code Here

TOP

Related Classes of jdbm.RecordManager

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.