0

Rocksdb 允许按键对记录进行排序,但我想按值对记录进行排序。有可能这样做吗?

4

1 回答 1

1

RocksDB 不直接支持二级索引,但是你可以在 RocksDB 之上构建自己的二级索引。这是一个非常简单的示例,使用BatchWrite

// create 2 ColumnFamily, one for data, one for index
Status s = db->CreateColumnFamily(cf_options, "Data", &data_handle);
Status s = db->CreateColumnFamily(cf_options, "Index", &index_handle);

// Use batch to guarantee consistency between data and index
WriteBatch batch;
batch.Put(data_handle, "key1", "value1");
batch.Put(index_handle, "value1", "key1");

db->Write(WriteOptions(), &batch);

std::string result;
s = db_->Get(ReadOptions(), data_handle, "key1", &result);
// index ColumnFamily is sorted by value
s = db_->Get(ReadOptions(), index_handle, "value1", &result);

这是一个过于简化的示例,实际上您需要处理许多其他事情,例如重复的键或值、空间放大等。现有的二级索引实现供您参考,例如: mokurokumyrocks等。

于 2020-09-11T19:37:56.040 回答