Search code examples
rubydatabaseberkeley-dbnon-relational-database

How would you represent a relational entity as a single unit of retrievable data in BerkeleyDB?


BerkeleyDB is the database equivalent of a Ruby hashtable or a Python dictionary except that you can store multiple values for a single key.

My question is: If you wanted to store a complex datatype in a storage structure like this, how could you go about it?

In a normal relational table, if you want to represent a Person, you create a table with columns of particular data types:

Person
-id:integer
-name:string
-age:integer
-gender:string

When it's written out like this, you can see how a person might be understood as a set of key/value pairs:

id=1
name="john"; 
age=18; 
gender="male";

Decomposing the person into individual key/value pairs (name="john") is easy.

But in order to use the BerkeleyDB format to represent a Person, you would need some way of recomposing the person from its constituent key/value pairs.

For that, you would need to impose some artificial encapsulating structure to hold a Person together as a unit.

Is there a way to do this?

EDIT: As Robert Harvey's answer indicates, there is an entity persistence feature in the Java edition of BerkeleyDB. Unfortunately because I will be connnecting to BerkeleyDB from a Ruby application using Moneta, I will be using the standard edition which I believe requires me to create a custom solution in the absence of this support.


Solution

  • You can always serialize (called marshalling in Ruby) the data as a string and store that instead. The serialization can be done in several ways.

    With YAML (advantage: human readable, multiple implementation in different languages):

    require 'yaml'; str = person.to_yaml
    

    With Marshalling (Ruby-only, even Ruby version specific):

    Marshal.dump(person)
    

    This will only work if class of person is an entity which does not refer to other objects you want not included. For example, references to other persons would need to be taken care of differently.