Package org.xtreemfs.babudb.api

Examples of org.xtreemfs.babudb.api.BabuDB


public class SimpleDemo {
   
    public static void main(String[] args) throws InterruptedException {
        try {
            // start the database
            BabuDB databaseSystem = BabuDBFactory.createBabuDB(new BabuDBConfig("myDatabase/", "myDatabase/",
                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
            databaseSystem.shutdown();
        } catch (BabuDBException ex) {
            ex.printStackTrace();
        }
    }
View Full Code Here


        ConfigBuilder builder1 = new ConfigBuilder();
        builder1.setDataPath("/tmp/babudb1", "/tmp/babudb1/log").setLogAppendSyncMode(SyncMode.SYNC_WRITE);
        builder1.addPlugin("myBabuDBPath/pluginConfig1.properties");
        BabuDBConfig config1 = builder1.build();

        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();
        byte[] result0 = babuDB0.getDatabaseManager().getDatabase("myDatabase")
                .lookup(0, "testKey".getBytes(), null).get();
       
        if ("testValue".equals(new String(result0))) {
            System.out.println("Successfully retrieved \"testValue\" from \"myDatabase\" at the " +
                "first BabuDB instance.");
        } else {
            System.err.println(new String(result0) + " did not match expected \"testValue\"!");
            System.err.println("Running the sample application has caused a failure!");
            System.exit(1);
        }
       
        if ("testValue".equals(new String(result1))) {
            System.out.println("Successfully retrieved \"testValue\" from \"myDatabase\" at the " +
                "second BabuDB instance.");
        } else {
            System.err.println(new String(result1) + " did not match expected \"testValue\"!");
            System.err.println("Running the sample application has caused a failure!");
            System.exit(1);
        }
       
        System.out.println("Shutting down the BabuDB instances ... ");
       
        babuDB1.shutdown();
        babuDB0.shutdown();
       
        System.out.println("The sample application has been executed successfully.");
        System.exit(0);
    }
View Full Code Here

     *             if an error occurs while dumping the database
     */
    public static void dumpDB(String dbDir, String logDir, boolean compression, RecordFormatter formatter,
        PrintStream out) throws BabuDBException {
       
        BabuDB databaseSystem = BabuDBFactory.createBabuDB(new ConfigBuilder().setDataPath(dbDir, logDir)
                .setCompressed(compression).build());
       
        out.println("<BabuDB version=\"" + BabuDBFactory.BABUDB_VERSION + "\">");
       
        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++;
                }
               
                out.println("\t\t</index>");
            }
           
            out.println("\t</database>");
           
        }
       
        out.println("</BabuDB>");
       
        databaseSystem.shutdown();
    }
View Full Code Here

    public void tearDown() throws Exception {
    }
   
    @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();
View Full Code Here

        final int maxValSize = 16384;
       
        // FSUtils.delTree(new File(dbDir));
       
        // start the database
        final BabuDB databaseSystem = BabuDBFactory.createBabuDB(new BabuDBConfig(dbDir, dbDir, numThreads,
                maxLogFileSize, 10, SyncMode.ASYNC, 1000, 0, false, 16, 1024 * 1024 * 256));
        DatabaseManager dbm = databaseSystem.getDatabaseManager();
       
        // for (int i = 0; i < numDBs; i++)
        // dbm.createDatabase("DB" + i, numIndices);
        dbm.createDatabase("blub", numIndices);
       
        final Database[] dbs = new Database[numDBs];
        for (int i = 0; i < dbs.length; i++)
            dbs[i] = dbm.getDatabase("DB" + i);
       
        Thread writeThread = new Thread() {
           
            public void run() {
               
                try {
                   
                    System.out.println("starting write thread...");
                   
                    // insert a random entry in a random database
                   
                    for (int i = 0; i < NUM_INSERTS; i++) {
                       
                        int db = (int) (Math.random() * numDBs);
                        byte[] key = randomBytes(minKeySize, maxKeySize);
                        byte[] val = randomBytes(minValSize, maxValSize);
                        int index = (int) (Math.random() * numIndices);
                       
                        dbs[db].singleInsert(index, key, val, null).get();
                       
                        if (i % 1000 == 0) {
                            System.out.print(".");
                            Thread.sleep(5000);
                        }
                       
                        if (i % 10000 == 9999) {
                            System.out.println("\ncheckpoint...");
                            databaseSystem.getCheckpointer().checkpoint();
                            System.out.println("done");
                        }
                       
                        if (i % 40000 == 39999)
                            System.exit(0);
                       
                    }
                   
                    System.out.println("write thread finished");
                   
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        };
       
        Thread readThread = new Thread() {
           
            // public void run() {
            //
            // try {
            //
            // System.out.println("starting read thread...");
            //
            // // insert a random entry in a random database
            //
            // for (int i = 0; i < NUM_READS; i++) {
            //
            // int db = (int) (Math.random() * numDBs);
            // byte[] key = new byte[0];
            // int index = (int) (Math.random() * numIndices);
            //
            // Iterator<Entry<byte[], byte[]>> it = dbs[db]
            // .prefixLookup(index, key, null).get();
            //
            // synchronized (lock) {
            // while (it.hasNext())
            // it.next();
            // }
            // }
            //
            // System.out.println("read thread finished");
            //
            // } catch (Throwable th) {
            // th.printStackTrace();
            // }
            // }
        };
       
        writeThread.start();
        readThread.start();
       
        writeThread.join();
        readThread.join();
       
        // create a checkpoint for faster start-ups
        // databaseSystem.getCheckpointer().checkpoint();
       
        // shutdown database
        databaseSystem.shutdown();
       
    }
View Full Code Here

TOP

Related Classes of org.xtreemfs.babudb.api.BabuDB

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.