简体   繁体   English

将C ++键/值结构转换为数据库(sqlite3)

[英]convert C++ key/value structure to database (sqlite3)

I have a C++ key/value table that looks like this: 我有一个C ++键/值表,如下所示:

class kvBucket {
   ...
   map<string, kvBucket*> buckets;
   map<string, string> keyPairs;
   ...
}
class kvTree {
   ...
   kvBucket base;
   ...
}

I'd like to convert this from an in-memory structure to one that's implemented in a database (sqlite3). 我想将其从内存结构转换为在数据库(sqlite3)中实现的结构。 I'm not sure how to represent the schema due to buckets being nested. 由于存储桶嵌套,我不确定如何表示架构。 Here's example data: 这是示例数据:

bucket | key | value
------   ---   -----
   a      v      1
  a/b     w      2
  a/b     x      3
 a/b/c    y      4
   d      z      5

In the C++ structure, I have member functions that do things like retrieving a list of buckets under another bucket, or retrieving a list of key/value pairs under a bucket path, etc. I'll need to do the same with the database. 在C ++结构中,我具有成员函数,这些成员函数可以执行以下操作:检索另一个存储桶下的存储桶列表,或检索存储桶路径下的键/值对列表,等等。我需要对数据库进行相同的操作。 Is there an efficient way to design the schema to do this? 有没有有效的方法来设计架构来做到这一点?

There's no efficient way to represent a hierarchical relationship in a relational store. 没有有效的方法来表示关系存储中的层次结构关系。 Key-value stores (you may have heard of them, they're all the rage these days) are better at it but generally don't provide SQL layers. 键值存储(您可能已经听说过,近来它们风靡一时)在此方面表现更好,但通常不提供SQL层。 Getting all the buckets under a certain path is going to require more than one query if you want to be able to move the buckets about (if you don't want to move the buckets ever, you could use the scheme you've got there and do a substring search on the bucket name for things like "a/*" ). 如果您希望能够移动各个存储桶,则将所有存储桶都放在某个路径下将需要多个查询(如果您不想移动存储桶,则可以使用已有的方案并在存储区名称上进行子字符串搜索,例如"a/*" )。

That said, you can do it even without having each bucket store its own full path: you need a self-to-self many-many relationship mapping from bucket to bucket . 就是说,即使每个存储桶都没有存储自己的完整路径,您也可以做到这一点:您需要从bucketbucket的自对多关系映射。 You need two tables, one for the buckets and one just for the mapping. 您需要两个表,一个用于存储桶,一个仅用于映射。 In the mapping table you have two columns, parent_bucket and child_bucket . 在映射表中,您有两列parent_bucketchild_bucket In the bucket table you have three, bucket_id , key , and value . bucket表中,您有三个bucket_idkeyvalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM