How would you represent a relational entity as a s

2019-09-04 14:30发布

问题:

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.

回答1:

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.



回答2:

If your datastore is able to do so (and BerkeleyDB does AFAICT) I'd just store a representation of the object attributes keyed with the object Id, without splitting the object attributes in different keys.

E.g. given:

Person
 -id:1
 -name:"john"
 -age:18
 -gender:"male"

I'd store the yaml representation in BerkleyDB with the key person_1:

--- !ruby/object:Person 
attributes: 
  id: 1
  name: john
  age: 18
  gender: male

Instead if you need to store each attribute as a key in the datastore (why?) you should make sure the key for the person record is somewhat linked to its identifying attribute, that's the id for an ActiveRecord.

In this case you'd store these keys in BerkleyDB:

person_1_name="john"; 
person_1_age=18; 
person_1_gender="male";


回答3:

Have a look at this documentation for an Annotation Type Entity:

http://www.oracle.com/technology/documentation/berkeley-db/je/java/com/sleepycat/persist/model/Entity.html