【问题标题】:RocksDb: Multiple values per key (c++)RocksDb:每个键有多个值(c++)
【发布时间】:2019-08-02 01:49:39
【问题描述】:

RocksDb:每个键有多个值 (c++)

我想做什么

我正在尝试调整我的简单区块链实现以定期将区块链保存到硬盘驱动器,因此我查看了不同的数据库解决方案。我决定使用 RocksDb,因为它易于使用以及良好的文档和示例。我通读了文档,无法弄清楚如何使其适应我的用例。 我有一个班级阻止 `

class Block {
public:
    string PrevHash;
    
private:
    blockheader header; // The header of the block 
    uint32_t index; // height of this block
    std::vector<tx_data> transactions; // All transactions in the block in a vector
    std::string hash; // The hash of the block
    uint64_t timestamp; // The timestamp this block was created by the node 
    std::string data; // Extra data that can be appended to blocks (for example text or a smart contract)
                      // - The larger this feild the higher the fee and the max size is defined in config.h
};

其中包含一些变量和一个 struct tx_data 的向量。我想将此数据加载到 RocksDB 数据库中。

我尝试过的

在 google 未能返回使用一个密钥对存储多个值的任何结果后,我决定我必须将每个块数据在开头的 0xa1 中,然后在结尾的 0x2a 中括起来

*0x2a*
header
index
txns
hash
timestamp
data
*0x2a*

但决定肯定有更简单的方法。我尝试查看turtlecoin 使用的代码,一种使用rocksdb 作为其数据库的货币,但那里的代码几乎无法辨认,我听说过序列化,但似乎没有什么信息。

也许我误解了数据库的使用?

【问题讨论】:

    标签: c++ blockchain rocksdb


    【解决方案1】:

    您需要对其进行序列化。序列化是获取一组结构化数据并将其转换为一个字符串、字节数或字节向量的过程,然后可以在稍后将其反序列化回该结构。一种方法是获取块的哈希并将其用作数据库中的键,然后创建一个不包含哈希的新结构。然后编写一个函数,它接受一个 Block 结构和一个路径,并构造一个 BlockNoHash 结构并保存它。然后另一个函数从哈希中读取一个块并吐出一个块结构。基本上你可以用一个永远不会出现在数据中的字符(例如`或|)来分割每个字段,但这意味着如果一个数据被损坏,那么你就无法获得任何其他数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2012-03-05
      • 2015-08-12
      相关资源
      最近更新 更多