RocksDB 中每个“ColumnFamilyOptions”自定义“prefix_extractor”

问题描述 投票:0回答:1

我正在寻找一种方法,为

RocksDB
数据库中的一个特定列族提供固定大小的前缀,因为该列族将用于在两个现有的“全键索引”列族之间实现连接索引

我在列族文档中没有看到任何关于此提取器的每列族专业化的具体提及,但它在 ColumnFamilyOptions 中明确定义,所以也许这是有意的。

但是我起草的尝试似乎效果不太好。它设法初始化数据库并添加具有给定描述符的列族,但第二次运行测试可执行文件时失败,无法加载具有列族描述符的数据库(代码如下):

#include <cassert>
#include <cstdint>
#include <iostream>
#include <memory>
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/table.h"


namespace rdb = rocksdb;

int main()  {

rdb::Options options;

options.write_buffer_size = (1U << 28);
options.max_open_files = (1U << 10);
options.create_if_missing = true;
options.wal_bytes_per_sync = 32768;
options.bytes_per_sync = 32768;
options.recycle_log_file_num = 8;

rdb::BlockBasedTableOptions blockbased_options;
blockbased_options.block_size = 1ULL << 17;
blockbased_options.enable_index_compression = false;
options.table_factory.reset(rdb::NewBlockBasedTableFactory(blockbased_options));
options.whole_key_filtering = false;
std::vector<rdb::ColumnFamilyDescriptor> cf_descriptors;

{
    rdb::ColumnFamilyOptions cf_normal_options;
    cf_descriptors.emplace_back("normal_cf", cf_normal_options);

    rdb::ColumnFamilyOptions cf_key_prefix_options;
    blockbased_options.whole_key_filtering = false;
    cf_pwo_edge_options.table_factory.reset(rdb::NewBlockBasedTableFactory(blockbased_options));
    cf_pwo_edge_options.prefix_extractor.reset( rdb::NewFixedPrefixTransform( sizeof(std::uint64_t) ) );
    cf_descriptors.emplace_back("key_prefix_cf", cf_key_prefix_options);
}


std::unique_ptr<rdb::DB> db;
std::vector< std::unique_ptr< rdb::ColumnFamilyHandle > > cf_safe_handles;
{
    rdb::DB* db_temp;
    std::vector<rdb::ColumnFamilyHandle*> cf_handles;
    auto s = rdb::DB::Open(options, "/tmp/test_cf", cf_descriptors, &cf_handles, &db_temp);

    if (!s.ok())
    {
        std::cout << "unable to open db with column family descriptors " << std::endl;
        s = rdb::DB::Open(options, "/tmp/test_cf", &db_temp);

        if (!s.ok())
        {
            std::cout << "unable to open uninitialized db " << std::endl;
            std::exit(1);
        }

        if (nullptr == db_temp)
        {
            std::cout << "db_temp pointer uninitialized " << std::endl;
            std::exit(1);
        }

        for (auto&& cf_desc : cf_descriptors)
        {
            rdb::ColumnFamilyHandle* cf_handle;
            db_temp->CreateColumnFamily(cf_desc.options, cf_desc.name, &cf_handle);

            cf_handles.push_back(cf_handle);
        }
    }

    db.reset(db_temp);
    for (auto&& cf_ptr : cf_handles)
    {
        cf_safe_handles.emplace_back(cf_ptr);
    }
}
// Operations
std::string value;
auto s = db->Put(rdb::WriteOptions(), cf_safe_handles[0].get(), "key1", "value1");
{
    std::string obj("phenomenal but will be removed shortly");

    s = db->Put(rdb::WriteOptions(), cf_safe_handles[1].get(), "key2", obj);
}
s = db->Get(rdb::ReadOptions(), cf_safe_handles[0].get(), "key1", &value);
assert(s.ok());
std::cout << " key1 := " << value << std::endl;

s = db->Get(rdb::ReadOptions(), cf_safe_handles[1].get(), "key2", &value);
std::cout << " key2 := " << value << std::endl;

assert(s.ok());

};
c++ key-value-store rocksdb
1个回答
0
投票

当您拨打

rdb::DB::Open(options, "/tmp/test_cf", cf_descriptors, &cf_handles, &db_temp);
但未获得“正常”状态时,状态中显示的消息是什么?

© www.soinside.com 2019 - 2024. All rights reserved.