BerkeleyDb QuickStart

Here’s a basic set of methods I wrote while trying to familizrize myself with some of BerkeleyDb‘s API methods. Interesting to see how it can be used as an object store and the use of a hashmap facade to query. FWIW, we didn’t want to annotate the POJOs with @Entity, hence we used serial entity binding.

Ze maven dependencies

    <dependencies>
        <dependency>
            <groupId>berkeleydb</groupId>
            <artifactId>je</artifactId>
            <version>3.2.76</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>

Ze code …

import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.LongBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.collections.StoredSortedMap;
import com.sleepycat.je.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.Serializable;
import java.util.*;

public class DbApp {


    private static final String CLASS_CATALOG_DB = "ClassCatalog_Db";
    private EnvironmentConfig envConfig_ = null;
    private Environment env_ = null;
    private DatabaseConfig dbConfig_ = null;
    private Database dataDb_ = null;
    private Database classCatalogDb_ = null;


    private final File envHome_;
    private final String dbName;
    private StoredClassCatalog classCatalog_;
    private SerialBinding myDataBinding;
    private EntryBinding keyBinding;
    private SortedMap <Long, MyData> map;


    public DbApp(String envLocation, String dbName) throws Exception {
        envHome_ = new File(envLocation);
        if (envHome_.exists()) {
            FileUtils.cleanDirectory(envHome_);
        } else {
            FileUtils.forceMkdir(envHome_);
        }
        this.dbName = dbName;
        createEnv();
        createDbHandle();
        createClassCatalogAndBindings();
    }

    private void createEnv() throws Exception {
            final EnvironmentConfig envConfig_ = new EnvironmentConfig();
            envConfig_.setTransactional(false);
            envConfig_.setAllowCreate(true);
            envConfig_.setLocking(true);
            this.env_ = new Environment(envHome_, envConfig_);
    }


    private void createDbHandle() throws Exception {
        dbConfig_ = new DatabaseConfig();
        dbConfig_.setTransactional(false);
        dbConfig_.setAllowCreate(true);
        dbConfig_.setDeferredWrite(true);
        dataDb_ = env_.openDatabase(null, dbName, dbConfig_);
    }

    private void createClassCatalogAndBindings() throws DatabaseException {
        classCatalogDb_ = env_.openDatabase(null, CLASS_CATALOG_DB, dbConfig_);
        classCatalog_ = new StoredClassCatalog(classCatalogDb_);
        myDataBinding = new SerialBinding(classCatalog_, MyData.class);
        keyBinding = TupleBinding.getPrimitiveBinding(Long.class);
        map = new StoredSortedMap(dataDb_, keyBinding, myDataBinding, true);

    }

    public void store(MyData mydata) throws Exception {
        final DatabaseEntry key = new DatabaseEntry();
        final DatabaseEntry data = new DatabaseEntry();
        LongBinding.longToEntry(mydata.getSsn(), key);
        myDataBinding.objectToEntry(mydata, data);
        dataDb_.put(null, key, data);
    }

    public List<MyData> getAll() throws Exception {
        List<MyData> dataList = new ArrayList<MyData>();
        final DatabaseEntry key = new DatabaseEntry();
        final DatabaseEntry data = new DatabaseEntry();
        final Cursor cursor = dataDb_.openCursor(null, null);
        try {
            while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                MyData mydata = (MyData) (myDataBinding.entryToObject(data));
                dataList.add(mydata);
            }
            return dataList;
        } finally {
            cursor.close();
        }
    }

    public MyData getHead() throws Exception {
        final DatabaseEntry key = new DatabaseEntry();
        final DatabaseEntry data = new DatabaseEntry();
        final Cursor cursor = dataDb_.openCursor(null, null);
        MyData myData = null;
        try {
            cursor.getFirst(key, data, LockMode.RMW);
            if (data.getData() == null) {
                return null;
            }
            myData = (MyData) (myDataBinding.entryToObject(data));
        } finally {
            cursor.close();
        }
        return myData;
    }


    public MyData query(Long keyData) throws Exception {  
        return map.get(keyData);
    }

    public MyData poll() throws Exception {

        final DatabaseEntry key = new DatabaseEntry();
        final DatabaseEntry data = new DatabaseEntry();


        final Cursor cursor = dataDb_.openCursor(null, null);
        MyData myData = null;
        try {
            cursor.getFirst(key, data, LockMode.RMW);
            if (data.getData() == null) {
                return null;
            }
            myData = (MyData) (myDataBinding.entryToObject(data));
        } finally {
            cursor.close();
        }
        return myData;

    }

    public static void main(String[] args) throws Exception {

        DbApp app = new DbApp("/home/vinayb/junk/abc/bdb_env", "mydata_db");
        MyData data1 = new MyData(1, "me", 21, "121 some street");
        MyData data2 = new MyData(2, "he", 22, "122 some street");
        MyData data3 = new MyData(3, "she", 23, "123 some street");

        app.store(data1);
        app.store(data2);
        app.store(data3);

        List<MyData> dataList = app.getAll();
        for (MyData data : dataList) {
            System.out.println(data);
        }
        System.out.println("app.poll() = " + app.poll());
        MyData data2Retrieved = app.query(2L);
        System.err.println("id = 2: data: " +  data2Retrieved);
    }


    private static class MyData implements Serializable {
        private long ssn;
        private String name;
        private int age;
        private String address;

        private MyData(long ssn, String name, int age, String address) {
            this.ssn = ssn;
            this.name = name;
            this.age = age;
            this.address = address;
        }

        public long getSsn() {
            return ssn;
        }

        public void setSsn(long ssn) {
            this.ssn = ssn;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "MyData{" +
                    "ssn=" + ssn +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s