LevelDB is a key-value store developed by google and originally written in c++. It stores the keys and values in arbitrary byte arrays and data is sorted by key. LevelDB developed based on the idea of Log Structured Merge(LSM) Tree which provides higher insertion rate than B+ Tree.
The LSM tree is a data structure designed to provide low-cost indexing for files experiencing a high rate of updates. This tree splits the logical tree into several physical pieces so that the most-recently-updated portion of data is in a tree that fits entirely in memory. Once the in-memory tree reaches a threshold size, a new in-memory tree will be created and the old tree synced to disk. Once written to disk, trees are read-only, though they are merged in the background with other on-disk trees to minimize the cost of reads.
Figure: Illustration of LSM Trees (image collected from internet)
In this blog I will explain how to use LevelDB in your java application. If you want to know more about levelDB, you can go through the following links:
[2] Original C++ Version: https://github.com/google/leveldb
[3] Java Version: https://github.com/dain/leveldb
(1) At first download the the levelDB[3] and merge it with your java project. Or you can the download the levelDB jars from here and add the jars with your java projects.
(2) To create a levelDB, you need to specify the location where you want to persist your data. You can look on the following code:
static DB ldb;public static String dbLocation = "C:\\dataset\\leveldb";
static File ldbDir;
public static void createOrOpenDB() throws Exception {
ldbDir = new File(dbLocation, "ldb");
if(!ldbDir.exists()) { ldbDir.mkdirs(); }
Options options = new Options();
options.createIfMissing(true);
ldb = factory.open(ldbDir, options);
}
(3) Closing a levelDB
public static void destroyDB() {
try {
ldb.close();
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
(4) You can insert into, retrieve and delete data from levelDB using put(), get() and delete() function. Suppose, we will read data from a text file and write into levelDB store. As levelDB stores the data as byte array. You need to convert both key and value into byte array. You can look on the following code. [you can use text file provided with jars]
private static void writeData() {
File fileObj = new File("small.txt");
try {
System.out.println("Writing data into LevelDB...");
Scanner sc = new Scanner(fileObj);
while(sc.hasNextLine()) {
String value = sc.nextLine();
int key = Integer.parseInt(value.split(",")[0]);
insertIntoDB(key, value);
}
System.out.println("Successfully written data into LevelDB");
sc.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void insertIntoDB(int key, String value) {
try{
byte[] bValue = value.getBytes();
ByteBuffer buf = ByteBuffer.allocate(4);
buf.putInt(key);
byte[] bKey = buf.array();
ldb.put(bKey, bValue); //put data into levelDB store
}
catch (Exception e) {
e.printStackTrace();
}
}
(5) Reading data from levelDB
private static void readFromDB() throws IOException {
DBIterator it = ldb.iterator();
try {
for(it.seekToFirst(); it.hasNext(); it.next()) {
int key = ByteBuffer.wrap(it.peekNext().getKey()).getInt();
String value = asString(it.peekNext().getValue());
System.out.println("< " + key + ", " + value + ">");
}
}
finally {
System.out.println("Read Successfully");
it.close();
}
}
Thanks...Mahbub.