Package jdbm

Examples of jdbm.RecordManager


     *  Test to find differents objects in the btree. (cdaller)
     */
    @Test
    public void testFind() throws IOException
    {
        RecordManager recman;
        BTree<String, String> tree;

        recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testFind" ) );
        tree = new BTree<String, String>( recman, new StringComparator() );

        tree.insert( "test1", "value1", false );
        tree.insert( "test2", "value2", false );

        Object value = tree.find( "test1" );

        assertTrue( value instanceof String );
        assertEquals( "value1", value );

        tree.insert( "", "Empty String as key", false );

        assertEquals( "Empty String as key", tree.find( "" ) );
        assertEquals( null, tree.find( "someoneelse" ) );

        recman.close();
    }
View Full Code Here


     *  Test to insert, retrieve and remove a large amount of data. (cdaller)
     */
    @Test
    public void testLargeDataAmount() throws IOException
    {
        RecordManager recman;
        BTree<String, Object> tree;

        recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testLargeDataAmount" ) );

        // recman = new jdbm.recman.BaseRecordManager( "test" );
        tree = new BTree<String, Object>( recman, new StringComparator() );
        int iterations = 10000;

        // insert data
        for ( int count = 0; count < iterations; count++ )
        {
            assertEquals( null, tree.insert( "num" + count, Integer.valueOf( count ), false ) );
        }

        // find data
        for ( int count = 0; count < iterations; count++ )
        {
            assertEquals( Integer.valueOf( count ), tree.find( "num" + count ) );
        }

        // delete data
        for ( int count = 0; count < iterations; count++ )
        {
            assertEquals( Integer.valueOf( count ), tree.remove( "num" + count ) );
        }

        assertEquals( 0, tree.size() );

        recman.close();
    }
View Full Code Here

     * handleException().
     */
    @Test
    public void testMultithreadAccess() throws IOException, InterruptedException
    {
        RecordManager recman;
        BTree<String, Integer> tree;

        recman = RecordManagerFactory.createRecordManager( getTemporaryFile( "testMultithreadAccess" ) );
        tree = new BTree<String, Integer>( recman, new StringComparator() );
        TestThread<String, Integer>[] threadPool = ( TestThread<String, Integer>[] ) new TestThread[THREAD_NUMBER];
        String name;
        Map<String, Integer> content;

        // create content for the tree, different content for different threads!
        for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ )
        {
            name = "thread" + threadCount;
            content = new TreeMap<String, Integer>();

            for ( int contentCount = 0; contentCount < THREAD_CONTENT_SIZE; contentCount++ )
            {
                // guarantee, that keys and values do not overleap,
                // otherwise one thread removes some keys/values of
                // other threads!
                content.put( name + "_" + contentCount,
                    Integer.valueOf( threadCount * THREAD_CONTENT_SIZE + contentCount ) );
            }

            threadPool[threadCount] = new TestThread<String, Integer>( name, tree, content );
            threadPool[threadCount].start();
        }

        Thread.sleep( THREAD_RUNTIME );

        // stop threads:
        for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ )
        {
            threadPool[threadCount].setStop();
        }

        // wait until the threads really stop:
        try
        {
            for ( int threadCount = 0; threadCount < THREAD_NUMBER; threadCount++ )
            {
                threadPool[threadCount].join();
            }
        }
        catch ( InterruptedException ignore )
        {
            ignore.printStackTrace();
        }

        recman.close();
    }
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

    public void indexNamespaceFile(long characterOffset, File dataFile,
            String indexPath) throws IndexingFailure {
        // open database and setup an object cache
        try {
            RecordManager recman =
                    RecordManagerFactory.createRecordManager(indexPath,
                            new Properties());
            PrimaryTreeMap<String, String> tree =
                    recman.treeMap(IndexerConstants.INDEX_TREE_KEY);
            tree.inverseHashView(IndexerConstants.INVERSE_KEY);

            // clear out tree data since we're rebuilding the index
            tree.clear();

            BufferedReader reader =
                    new BufferedReader(new FileReader(dataFile));
            reader.skip(characterOffset);

            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] lineTokens = line.split("\\|");

                if (lineTokens.length == 2 && lineTokens[0] != null
                        && lineTokens[1] != null) {
                    tree.put(lineTokens[0], lineTokens[1]);
                }
            }
            reader.close();

            // make the data persistent in the database
            recman.commit();
            recman.close();
        } catch (IOException ie) {
            final String name = dataFile.getAbsolutePath();
            throw new IndexingFailure(name, ie);
        }
    }
View Full Code Here

    public void indexEquivalenceFile(long characterOffset, File dataFile,
            String indexPath) throws IndexingFailure {
        // open database and setup an object cache
        try {
            RecordManager recman =
                    RecordManagerFactory.createRecordManager(indexPath,
                            new Properties());
            PrimaryTreeMap<String, SkinnyUUID> tree =
                    recman.treeMap(IndexerConstants.INDEX_TREE_KEY,
                            new SkinnyUUIDSerializer());
            tree.inverseHashView(IndexerConstants.INVERSE_KEY);

            // clear out tree data since we're rebuilding the index
            tree.clear();

            BufferedReader reader =
                    new BufferedReader(new FileReader(dataFile));
            reader.skip(characterOffset);

            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] lineTokens = line.split("\\|");

                if (lineTokens.length == 2 && lineTokens[0] != null
                        && lineTokens[1] != null) {
                    tree.put(lineTokens[0],
                            SkinnyUUID.fromString(lineTokens[1]));
                }
            }
            reader.close();

            // make the data persistent in the database
            recman.commit();
            recman.close();
        } catch (IOException ie) {
            final String name = dataFile.getAbsolutePath();
            throw new IndexingFailure(name, ie);
        }
    }
View Full Code Here

    @Override
    protected void finalize() throws Throwable {
        if (tmap == null) {
            return;
        }
        RecordManager rm = tmap.getRecordManager();
        if (rm == null) {
            return;
        }

        // We don't want any exceptions propagating back up the stack
        // and causing problems finalizing this object.
        try {
            rm.close();
        } catch (IOException ioex) {
            // Assume it has been closed.
        }
    }
View Full Code Here

     */
    public synchronized void open() throws IOException {
        if (tmap != null) {
            return;
        }
        final RecordManager rm = createRecordManager(indexPath);
        if (valueSerializer == null) {
            tmap = rm.treeMap(IndexerConstants.INDEX_TREE_KEY);
        } else {
            tmap = rm.treeMap(IndexerConstants.INDEX_TREE_KEY, valueSerializer);
        }
        if (tmap.isEmpty()) {
            rm.close();
            throw new IOException("tree map is empty");
        }
        invTmap = tmap.inverseHashView(IndexerConstants.INVERSE_KEY);
    }
View Full Code Here

     */
    public synchronized void close() throws IOException {
        if (tmap == null) {
            throw new IllegalStateException("not open");
        }
        final RecordManager rm = tmap.getRecordManager();
        rm.close();
        tmap = null;
    }
View Full Code Here

    @Before
    public void setup() {
        try {
            indexPath = tempFolder.newFolder();
            RecordManager recman = RecordManagerFactory.createRecordManager(
                    indexPath.getAbsolutePath(), new Properties());
            PrimaryTreeMap<String, String> tree =
                    recman.treeMap(IndexerConstants.INDEX_TREE_KEY);
            tree.inverseHashView(IndexerConstants.INVERSE_KEY);
            tree.put(key, value);
            recman.commit();
            recman.close();
        } catch (IOException e) {
            e.printStackTrace();
            fail("Failure to create temporary folder for testing.");
        }
    }
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.