« April 5, 2007 | Main | May 29, 2007 »

May 8, 2007 Archives

May 8, 2007

Berkeley DB Java Edition and JavaFX

Sun recently announced JavaFX, "a new family of Java technology-based products that will help content providers create and deploy rich Internet applications (RIA)".  They also announced "a new scripting language, JavaFX Script
[which] gives Java developers the power to quickly create content-rich
applications for the widest variety of clients, including mobile
devices, set-top boxes, desktops, even Blu-ray discs."

We've always wanted to port JE to Java ME, but for various reasons, never gotten around to it.  JavaFX Script opens the possibility of using JE in an embedded environment and so I thought it would be interesting to verify that it all works ok.  It makes perfect sense to have a lightweight, persistence, ACID storage engine in a JavaFX environment, and since Java FX Script is one of the new class of scripting languages which sit on top of a JVM (e.g. JRuby, Groovy, etc.), it would have surprised me if JE didn't play well with Java FX Script.  Indeed, it was a pretty painless task to get a simple example running.

Here's the sample code:

import java.io.File;
import java.lang.System;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.JEVersion;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

doSomeJEOps();

operation doSomeJEOps() {
var theDb : Database = initJE();
var txn : Transaction = null;

initJE();

txn = theDb.getEnvironment().beginTransaction(null, null);
for (i in [0..9]) {
put(theDb, txn, "foo{i}", "bar{i}");
}
txn.commit();

System.out.println("Retrieval by key: foo0 => {get(theDb, null, 'foo0')}");
var key : DatabaseEntry = new DatabaseEntry();
var val : DatabaseEntry = new DatabaseEntry();
var cursor : Cursor = theDb.openCursor(null, null);
System.out.println("Retrieval by cursor");
while (cursor.getNext(key, val, null) == OperationStatus.SUCCESS) {
System.out.println
("{new String(key.getData())} => {new String(val.getData())}");
}

closeJE();
}

operation initJE() {
var envConfig = null;
var env = null;
var dbConfig = null;
var db = null;
envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(true);
env = new Environment(new File("c:/temp/blort"), envConfig);
dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setTransactional(true);
db = env.openDatabase(null, "foo", dbConfig);
return db;
}

operation closeJE() {
theDb.close();
theDb.getEnvironment().close();
}

operation put(theDb:Database, txn:Transaction, theKey:String, theVal:String) {
var keyDE : DatabaseEntry = new DatabaseEntry(theKey.getBytes());
var valDE : DatabaseEntry = new DatabaseEntry(theVal.getBytes());
var status = theDb.put(txn, keyDE, valDE);
if (status <> OperationStatus.SUCCESS) {
throw status;
}
}

operation get(theDb:Database, txn:Transaction, theKey:String) {
var keyDE : DatabaseEntry = new DatabaseEntry(theKey.getBytes());
var valDE : DatabaseEntry = new DatabaseEntry();
var status = theDb.get(txn, keyDE, valDE, null);
if (status <> OperationStatus.SUCCESS) {
throw status;
}
return new String(valDE.getData());
}

and the output:
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 0.11
Retrieval by key: foo0 => bar0
Retrieval by cursor
foo0 => bar0
foo1 => bar1
foo2 => bar2
foo3 => bar3
foo4 => bar4
foo5 => bar5
foo6 => bar6
foo7 => bar7
foo8 => bar8
foo9 => bar9
init: 1.332


About May 2007

This page contains all entries posted to Charles Lamb's Blog in May 2007. They are listed from oldest to newest.

April 5, 2007 is the previous archive.

May 29, 2007 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type and Oracle