您如何将关系实体表示为BerkeleyDB中的单个可检索数据单元? [英] How would you represent a relational entity as a single unit of retrievable data in BerkeleyDB?

查看:194
本文介绍了您如何将关系实体表示为BerkeleyDB中的单个可检索数据单元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BerkeleyDB 是与Ruby散列表或Python字典等价的数据库,但可以存储单个键的多个值。



我的问题是:如果你想在这样的存储结构中存储复杂的数据类型,你该怎么办? p>

在正常的关系表中,如果你想表示一个Person,你创建一个包含特定数据类型列的表:

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

当这样写出时,你可以看到一个人如何被理解为一组键/ / p>

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

将人分解成单个键/值对(name =john)很容易



但是为了使用BerkeleyDB格式来表示一个Person,你需要一些方法,值对。



为此,您需要强加一些人工封装结构来将一个Person作为一个单元。



有办法吗?



编辑:Robert Harvey answer表示,在 BerkeleyDB的Java版本。很遗憾,因为我将使用 Moneta 从Ruby应用程序连接到BerkeleyDB,我将使用我相信需要我的标准版创建一个自定义解决方案。

解决方案

您可以随时将数据序列化字符串和存储。



使用YAML(优点:人类可读,多种语言的多种实现):

  require'yaml';使用Marshalling(仅限Ruby,甚至特定于Ruby版本):

  Marshal.dump(person)


b $ b

这只有当类的人是一个实体,不涉及其他对象,你想不包括的工作。例如,对其他人的提及需要有所不同。


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.

解决方案

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.

这篇关于您如何将关系实体表示为BerkeleyDB中的单个可检索数据单元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆