about photos bookshelf portfolio blog home
Begin main content

JRuby + OODBMS

It was only last year, as I was playing around with Smalltalk that I discovered the joy of an object database.

Naturally as I began playing with JRuby I wondered about object database options for it. One of the great things about the Java linkup is the wealth of available libraries and products and sure enough I found a number of open source OO database options. GOODS (which I have used with Smalltalk) is an option, but like some others it relies on a bytecode pre-processor, which makes me nervous given how JRuby plays with the bytecode.

Playing with db4o (which has both GPL and commercial licenses) has been pretty successful for the very little time I have invested.

Unsurprisingly I discovered that trying to store a pure Ruby object yielded an ObjectNotStorableException exception. In JRuby extending Java classes is easy. I haven't discovered how to properly extend the constructor, so I ended up with the following class:

require 'java'

class Song < java.lang.Object
  attr_writer :name, :artist, :duration
  
  def to_s
    "Song: #@name--#@artist (#@duration)"
  end
end
Creating a song object and storing it in a db4o database is simple:

include_class 'com.db4o.Db4o'

db = Db4o.openFile("/tmp/test_db")

song = Song.new
song.name = "Bicylops"
song.artist = "Fleck"
song.duration = 260
puts song

db.set(song)
I can then pull song objects out of the database using QBE:

# make an unconstrained template object for QBE
song_proto = Song.new
song_proto.name = ""
song_proto.artist = ""
song_proto.duration = 0

results = db.get(song_proto)

results.each {|s| puts s}
Unfortunately I don't get a good look at the objects since they appear as a generic proxy object. to_s yields:

(G) org.jruby.javasupport.proxy.gen.Object$Proxy0

Ruby has no concept of casting or blessing the object into a different class, so I suspect that I'll have to take care of that by wrapping the db4o api with some Java to do the casting.

I can confirm that something like the objects I'm storing are ending up in the database file with the venerable strings command. Surprisingly the database binary file contains strings that look exactly like that generated by to_s. Perhaps it stores a stringified version of each object? Or perhaps to_s is proffered by JRuby as a default serialisation method.

Anyway, as I said I haven't invested much time on this (too busy watching the Monaco F1 right now...) and getting it useable may be a bit of work.

One weird thing is that the first time I run code like the above for a particular database file I get an ObjectNotStorableException exception. The db file is created though, and further runs work as described above.

01:09 AM, 28 May 2007 by Mark Aufflick Permalink

ProjectSpace

Hi!
What do you think a about having a ProjectSpace in db4o to document JRuby + db4o related stuff?
Perhaps some contributors from the db4o community interested in JRuby can join you and help to make JRuby objects easily storable in db4o.
This would be a valuable contribution to the db4o community!
If you are interested contact me through this page.
Best regards!
German

by Unregistered Visitor on 05/29/07

Hybrid RDBMS Architecture

A hybrid architecture that integrates OODBMS and ORDBMS concepts in to RDBMS provides a far more effective solution for dealing with real life complex data scenarios Visit HRDBMS

by Unregistered Visitor on 11/07/07

Add comment