Package org.xtreemfs.babudb.api.database

Examples of org.xtreemfs.babudb.api.database.Database


                2, 1024 * 1024 * 16, 5 * 60, SyncMode.SYNC_WRITE, 0, 0, false, 16, 1024 * 1024 * 512));
            DatabaseManager dbm = databaseSystem.getDatabaseManager();
           
            // create a new database called myDB
            dbm.createDatabase("myDB", 2);
            Database db = dbm.getDatabase("myDB");
           
            // create an insert group for atomic inserts
            DatabaseInsertGroup group = db.createInsertGroup();
           
            // insert one key in each index
            group.addInsert(0, "Key1".getBytes(), "Value1".getBytes());
            group.addInsert(1, "Key2".getBytes(), "Value2".getBytes());
           
            // and execute group insert
            db.insert(group, null).get();
           
            // now do a lookup
            byte[] result = db.lookup(0, "Key1".getBytes(), null).get();
           
            // create a checkpoint for faster start-ups
            databaseSystem.getCheckpointer().checkpoint();
           
            // shutdown database
View Full Code Here


        BabuDB babuDB0 = BabuDBFactory.createBabuDB(config0);
        BabuDB babuDB1 = BabuDBFactory.createBabuDB(config1);
       
        System.out.println("Creating database \"myDatabase\" ... ");
       
        Database myDb = null;
        try {
            myDb = babuDB0.getDatabaseManager().getDatabase("myDatabase");
        } catch (BabuDBException exc) {
            myDb = babuDB0.getDatabaseManager().createDatabase("myDatabase", 1);
        }
       
        System.out.println("Inserting \"testValue\" for \"testKey\" into \"myDatabase\" ... ");
       
        myDb.singleInsert(0, "testKey".getBytes(), "testValue".getBytes(), null).get();
       
        System.out.println("Retrieving the test values from both BabuDB instance ... ");
       
        byte[] result1 = babuDB1.getDatabaseManager().getDatabase("myDatabase")
                .lookup(0, "testKey".getBytes(), null).get();
View Full Code Here

     */
    @Test
    public void testBasicIO() throws Exception {
              
        // create some DBs
        Database test0 = babu0.getDatabaseManager().createDatabase("0", 3);   
        Database test1 = babu1.getDatabaseManager().createDatabase("1", 3);
        Database test2 = babu2.getDatabaseManager().createDatabase("2", 3);
               
        // retrieve the databases
        test0 = babu2.getDatabaseManager().getDatabase("0");
        test1 = babu0.getDatabaseManager().getDatabase("1");
        test2 = babu1.getDatabaseManager().getDatabase("2");
       
        // make some inserts
        final AtomicInteger notReady = new AtomicInteger(3);
        final Object context0 = new Object();
        DatabaseInsertGroup ig = test0.createInsertGroup();
        ig.addInsert(0, "bla00".getBytes(), "blub00".getBytes());
        ig.addInsert(1, "bla01".getBytes(), "blub01".getBytes());
        ig.addInsert(2, "bla02".getBytes(), "blub02".getBytes());
        test0.insert(ig, context0).registerListener(new DatabaseRequestListener<Object>() {
            @Override
            public void finished(Object result, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                assertEquals(context0, context);
            }
           
            @Override
            public void failed(BabuDBException error, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                fail("This should not happen.");
            }
        });
       
        final Object context1 = new Object();
        ig = test1.createInsertGroup();
        ig.addInsert(0, "bla10".getBytes(), "blub10".getBytes());
        ig.addInsert(1, "bla11".getBytes(), "blub11".getBytes());
        ig.addInsert(2, "bla12".getBytes(), "blub12".getBytes());
        test1.insert(ig, context1).registerListener(new DatabaseRequestListener<Object>() {
            @Override
            public void finished(Object result, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                assertEquals(context1, context);
            }
           
            @Override
            public void failed(BabuDBException error, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                fail("This should not happen.");
            }
        });
       
        final Object context2 = new Object();
        ig = test2.createInsertGroup();
        ig.addInsert(0, "bla20".getBytes(), "blub20".getBytes());
        ig.addInsert(1, "bla21".getBytes(), "blub21".getBytes());
        ig.addInsert(2, "bla22".getBytes(), "blub22".getBytes());
        test2.insert(ig, context2).registerListener(new DatabaseRequestListener<Object>() {
            @Override
            public void finished(Object result, Object context) {
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                assertEquals(context2, context);
            }
           
            @Override
            public void failed(BabuDBException error, Object context) {
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                fail("This should not happen.");
            }
        });
       
        // wait for the inserts to finish
        synchronized (notReady) {
            if (notReady.get() > 0) {
                notReady.wait();
            }
        }
       
        // retrieve the databases
        test0 = babu1.getDatabaseManager().getDatabase("0");
        test1 = babu2.getDatabaseManager().getDatabase("1");
        test2 = babu0.getDatabaseManager().getDatabase("2");
       
        // make some lookups
        byte[] res = test0.lookup(0, "bla00".getBytes(), test0).get();
        assertNotNull(res);
        assertEquals("blub00", new String(res));
        assertNull(test0.lookup(0, "bla20".getBytes(), test0).get());

        res = test0.lookup(1, "bla01".getBytes(), test0).get();
        assertNotNull(res);
        assertEquals("blub01", new String(res));
        assertNull(test0.lookup(1, "bla20".getBytes(), test0).get());
       
        res = test1.lookup(0, "bla10".getBytes(), test0).get();
        assertNotNull(res);
        assertEquals("blub10", new String(res));
        assertNull(test0.lookup(0, "bla20".getBytes(), test0).get());
       
        res = test2.lookup(0, "bla20".getBytes(), test0).get();
        assertNotNull(res);
        assertEquals("blub20", new String(res));
        assertNull(test0.lookup(0, "bla01".getBytes(), test0).get());
    }
View Full Code Here

    @Test
    public void testPrefixLookups() throws Exception {
       
        // create some DBs
        babu0.getDatabaseManager().createDatabase("prefixLookup", 1);
        Database test0 = babu2.getDatabaseManager().getDatabase("prefixLookup");
        Database test1 = babu0.getDatabaseManager().getDatabase("prefixLookup");
        Database test2 = babu1.getDatabaseManager().getDatabase("prefixLookup");
       
        // make some inserts on database 'test1'
        final AtomicInteger notReady = new AtomicInteger(2);
        final Object context0 = new Object();
        DatabaseInsertGroup ig = test1.createInsertGroup();
        ig.addInsert(0, "bla0".getBytes(), "blub0".getBytes());
        ig.addInsert(0, "bla1".getBytes(), "blub1".getBytes());
        ig.addInsert(0, "bla2".getBytes(), "blub2".getBytes());
        test1.insert(ig, context0).registerListener(new DatabaseRequestListener<Object>() {
            @Override
            public void finished(Object result, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                assertEquals(context0, context);
            }
           
            @Override
            public void failed(BabuDBException error, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                fail("This should not happen.");
            }
        });
       
        final Object context1 = new Object();
        ig = test1.createInsertGroup();
        ig.addInsert(0, "yagga0".getBytes(), "yagga0".getBytes());
        ig.addInsert(0, "yagga1".getBytes(), "yagga1".getBytes());
        ig.addInsert(0, "yagga2".getBytes(), "yagga2".getBytes());
        test1.insert(ig, context1).registerListener(new DatabaseRequestListener<Object>() {
            @Override
            public void finished(Object result, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                assertEquals(context1, context);
            }
           
            @Override
            public void failed(BabuDBException error, Object context) {
               
                if (notReady.decrementAndGet() == 0) {
                    synchronized (notReady) {
                        notReady.notify();
                    }
                }
                fail("This should not happen.");
            }
        });
       
        // wait for the inserts to finish
        synchronized (notReady) {
            if (notReady.get() > 0) {
                notReady.wait();
            }
        }
       
    /*
     * prefix
     */
       
        // perform a prefix lookup on database 'test0'
        ResultSet<byte[], byte[]> result = test0.prefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        Entry<byte[], byte[]> next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertFalse(result.hasNext());
       
        // perform a prefix lookup on database 'test1'
        result = test1.prefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertFalse(result.hasNext());
       
        // perform a prefix lookup on database 'test2'
        result = test2.prefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertFalse(result.hasNext());
       
        // perform an empty prefix lookup on database 'test0'
        result = test0.prefixLookup(0, null, null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        next = result.next();
        assertEquals("yagga0", new String(next.getKey()));
        assertEquals("yagga0", new String(next.getValue()));
        next = result.next();
        assertEquals("yagga1", new String(next.getKey()));
        assertEquals("yagga1", new String(next.getValue()));
        next = result.next();
        assertEquals("yagga2", new String(next.getKey()));
        assertEquals("yagga2", new String(next.getValue()));
        assertFalse(result.hasNext());
       
    /*
     * prefix reverse
     */
       
        // perform a prefix lookup on database 'test0'
        result = test0.reversePrefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertFalse(result.hasNext());
       
        // perform a prefix lookup on database 'test1'
        result = test1.reversePrefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla1", new String(next.getKey()));
        assertEquals("blub1", new String(next.getValue()));
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla0", new String(next.getKey()));
        assertEquals("blub0", new String(next.getValue()));
        assertFalse(result.hasNext());
       
        // perform a prefix lookup on database 'test2'
        result = test2.reversePrefixLookup(0, "bla".getBytes(), null).get();
        assertTrue(result.hasNext());
        next = result.next();
        assertEquals("bla2", new String(next.getKey()));
        assertEquals("blub2", new String(next.getValue()));
        assertTrue(result.hasNext());
View Full Code Here

     */
    @Test
    public void testRestart() throws Exception {
       
        // retrieve the databases
        Database test0 = babu1.getDatabaseManager().getDatabase("0");
        Database test1 = babu2.getDatabaseManager().getDatabase("1");
        Database test2 = babu0.getDatabaseManager().getDatabase("2");
       
        // make some lookups
        byte[] res = test0.lookup(0, "bla00".getBytes(), test0).get();
        assertNotNull(res);
        assertEquals("blub00", new String(res));
       
        assertNull(test0.lookup(0, "bla20".getBytes(), test0).get());
       
        res = test1.lookup(0, "bla10".getBytes(), test1).get();
        assertNotNull(res);
        assertEquals("blub10", new String(res));
       
        assertNull(test1.lookup(0, "bla20".getBytes(), test1).get());
       
        res = test2.lookup(0, "bla20".getBytes(), test2).get();
        assertNotNull(res);
        assertEquals("blub20", new String(res));
       
        assertNull(test2.lookup(0, "bla10".getBytes(), test2).get());
    }
View Full Code Here

        Map<String, Database> dbs = databaseSystem.getDatabaseManager().getDatabases();
       
        long entryCount = 0;
        for (Entry<String, Database> entry : dbs.entrySet()) {
           
            Database db = entry.getValue();
            out.println("\t<database name=\"" + db.getName() + "\">");
           
            int numIndices = db.getComparators().length;
            for (int i = 0; i < numIndices; i++) {
               
                out.println("\t\t<index number=\"" + i + "\">");
               
                Iterator<Entry<byte[], byte[]>> it = db.prefixLookup(i, new byte[0], null).get();
                while (it.hasNext()) {
                    Entry<byte[], byte[]> next = it.next();
                    out.println("\t\t\t<key>");
                    out.println("\t\t\t\t" + formatter.formatKey(next.getKey(), db.getName(), i));
                    out.println("\t\t\t</key>");
                    out.println("\t\t\t<value>");
                    out.println("\t\t\t\t"
                        + formatter.formatValue(next.getValue(), next.getKey(), db.getName(), i));
                    out.println("\t\t\t</value>");
                   
                    entryCount++;
                }
               
View Full Code Here

                    public boolean accept(File pathname) {
                        return !pathname.isDirectory();
                    }
                });
               
                Database db = babuDB.getDatabaseManager().createDatabase(dbDir.getName(), indexFiles.length);
                for (File indexFile : indexFiles) {
                   
                    int indexId = Integer.parseInt(indexFile.getName());
                   
                    IndexFileIterator it = new IndexFileIterator(indexFile);
                    while (it.hasNext()) {
                        Entry<byte[], byte[]> next = it.next();
                        if (next == null) {
                            throw new BabuDBException(ErrorCode.INTERNAL_ERROR,
                                "database conversion failed, dump corrupted");
                        }
                       
                        db.singleInsert(indexId, next.getKey(), next.getValue(), null).get();
                    }
                   
                    it.destroy();
                }
               
View Full Code Here

    @Test
    public void testConsistentWithSingleDB() throws Exception {
        BabuDB database = BabuDBFactory.createBabuDB(new BabuDBConfig(origDir, origDir, 1, 0, 0,
            SyncMode.SYNC_WRITE, 0, 0, compression, maxNumRecs, maxBlockFileSize));
       
        Database db = database.getDatabaseManager().createDatabase("test", numIndices);
       
        // fill database with random entries
        for (int i = 0; i < numIndices; i++) {
            for (int e = 0; e < 100; e++) {
                int randnum = (int) (Math.random() * Integer.MAX_VALUE);
                final String key = String.valueOf(randnum);
                final String value = "VALUE_" + randnum;
                db.singleInsert(i, key.getBytes(), value.getBytes(),null).get();
            }
        }
       
        System.out.println("Creating backup database...");
        database.getDatabaseManager().dumpAllDatabases(backupDir);
             
        BabuDB babuBackup = BabuDBFactory.createBabuDB(new BabuDBConfig(backupDir, backupDir, 1, 0, 0,
            SyncMode.SYNC_WRITE, 0, 0, compression, maxNumRecs, maxBlockFileSize));
       
        Database backupDB = babuBackup.getDatabaseManager().getDatabase("test");
       
        // check entries
        for (int i = 0; i < numIndices; i++) {
            System.out.println("checking index " + i);
            Iterator<Entry<byte[], byte[]>> values = db.prefixLookup(i, "1".getBytes(),null).get();
           
            while (values.hasNext()) {
                Entry<byte[], byte[]> e = values.next();
                byte[] v = backupDB.lookup(i, e.getKey(),null).get();
                assertNotNull(v);
                assertEquals(v.length, e.getValue().length);
                for (int p = 0; p < v.length; p++)
                    assertEquals(v[p], e.getValue()[p]);
            }
            System.out.println("checking complete.");
        }

        System.out.println("test inserts in the backup db");
       
        // fill database with random entries
        for (int i = 0; i < numIndices; i++) {
            for (int e = 0; e < 100; e++) {
                int randnum = (int) (Math.random() * Integer.MAX_VALUE);
                final String key = String.valueOf(randnum);
                final String value = "VALUE_" + randnum;
                backupDB.singleInsert(i, key.getBytes(), value.getBytes(),null).get();
            }
        }

        System.out.println("shutting down databases...");
        database.shutdown();
        backupDB.shutdown();
    }
View Full Code Here

    public void testShutdownRestart() throws Exception {
       
        database = BabuDBFactory.createBabuDB(new BabuDBConfig(baseDir, baseDir, 0, 0, 0, SyncMode.ASYNC, 0,
            0, COMPRESSION, maxNumRecs, maxBlockFileSize, !MMAP, -1, LOG_LEVEL));
       
        Database db = database.getDatabaseManager().createDatabase("test", 3);
        DatabaseInsertGroup ig = db.createInsertGroup();
        ig.addInsert(0, "Yagga".getBytes(), "Brabbel".getBytes());
        ig.addInsert(1, "Brabbel".getBytes(), "Blupp".getBytes());
        ig.addInsert(2, "Blupp".getBytes(), "Blahh".getBytes());
        db.insert(ig, null).get();
       
        Database db2 = database.getDatabaseManager().createDatabase("test2", 1);
        ig = db2.createInsertGroup();
        ig.addInsert(0, "test".getBytes(), "test".getBytes());
        db2.insert(ig, null).get();
        database.getDatabaseManager().deleteDatabase("test2");
       
        database.shutdown();
       
        database = BabuDBFactory.createBabuDB(new BabuDBConfig(baseDir, baseDir, 0, 0, 0, SyncMode.ASYNC, 0,
View Full Code Here

        final int NUMIDX = 5;
       
        database = BabuDBFactory.createBabuDB(new BabuDBConfig(baseDir, baseDir, 1, 0, 0, SyncMode.ASYNC, 0,
            0, compression, maxNumRecs, maxBlockFileSize));
       
        Database db = database.getDatabaseManager().createDatabase("testDB", NUMIDX);
       
        // fill database with random entries
        for (int i = 0; i < NUMIDX; i++) {
            for (int e = 0; e < 100; e++) {
                int randnum = (int) (Math.random() * Integer.MAX_VALUE);
                final String key = String.valueOf(randnum);
                final String value = "VALUE_" + randnum;
                db.singleInsert(i, key.getBytes(), value.getBytes(),null).get();
            }
        }
       
        System.out.println("starting copy");
        // copy database
        database.getDatabaseManager().copyDatabase("testDB", "copyDB");
       
        System.out.println("copy complete");
        // check entries
        for (int i = 0; i < NUMIDX; i++) {
            System.out.println("checking index " + i);
            Iterator<Entry<byte[], byte[]>> values = db.prefixLookup(i, "1".getBytes(),null).get();
           
            while (values.hasNext()) {
                Entry<byte[], byte[]> e = values.next();
                byte[] v = database.getDatabaseManager().getDatabase("copyDB").lookup(i, e.getKey(),null).get();
                assertNotNull(v);
View Full Code Here

TOP

Related Classes of org.xtreemfs.babudb.api.database.Database

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.