Rocksdb基本用法

rocksdb 用法简单介绍

  1. RocksDB是使用C++编写的嵌入式kv存储引擎,其键值均允许使用二进制流。由Facebook基于levelDB开发, 提供向后兼容的levelDB API。
    RocksDB针对Flash存储进行优化,延迟极小。RocksDB使用LSM存储引擎,纯C++编写。

  2. 打开一个数据库
    rocksdb::DB* db;
    rocksdb::Options options;
    options.create_if_missing = true;
    rocksdb::Status status = rocksdb::DB::Open(options, "/tmp/testdb", &db);
    assert(status.ok());

  3. 往数据库写入kv键值对
    s = db->Put(WriteOptions(), "k1", "v1");

  4. 向数据库查询k的value
    s = db->Get(ReadOptions(), "k1", &v);

  5. 原子写入
    rocksdb::WriteBatch batch;
    batch.Delete("k1");
    batch.Put("k2", "v2");
    s = db->Write(rocksdb::WriteOptions(), &batch);

  6. 迭代器
    db->NewIterator(rocksdb::ReadOptions());
    可以通过在调用NewIterator的时候,给传入的option设定ReadOptions.iterate_upper_bound来为你的迭代范围设置一个上边界。
    通过这个设定,rocksdb就不用继续查找这个key之后的内容了。在一些情况下,可以节省一些IO和计算。在特定的工作载荷下,
    它带来的改善是显著的。这个选项可以同在正向和反向迭代。

    rocksdb::Iterator* it = db->NewIterator(rocksdb::ReadOptions());
    for (it->SeekToFirst(); it->Valid(); it->Next()) {
    cout << it->key().ToString() << ": " << it->value().ToString() << endl;
    }

  7. 事务操作
    Status s = TransactionDB::Open(options,txn_db_options,kDbPathTran,&txn_db );
    Transaction* txn = txn_db->BeginTransaction(WriteOptions());
    获得事务,就可以进行相应的op操作,如下:
    s = txn->Put("key", "value");
    s = txn->Delete("key2");
    然后对事务进行提交
    s = txn->Commit();

rocksdb实际用法如以下代码

#include<cstdio>
#include<string>
#include<iostream>
#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"
#include "rocksdb/utilities/transaction.h"
#include "rocksdb/utilities/transaction_db.h"
using namespace rocksdb;

std::string kDbPath = "/tmp/wsf_rocksdb_test";
std::string kDbPathTran = "/tmp/wsf_rocksdb_transation";

void testTransaction(){
     TransactionDB* txn_db;
     Options options;        
     TransactionDBOptions txn_db_options;
     options.create_if_missing = true;


     Status s = TransactionDB::Open(options,txn_db_options,kDbPathTran,&txn_db );
     Transaction* txn = txn_db->BeginTransaction(WriteOptions());
     s = txn->Put("key", "value");
     s = txn->Delete("key2");
     s = txn->Merge("key3", "value");
     s = txn->Commit();
     delete txn;

}

int main()
{
    DB *db;
    Options options;
    options.create_if_missing = true;

    Status s = DB::Open(options, kDbPath, &db);
    assert(s.ok());

    s = db->Put(WriteOptions(), "k1", "v1");
    assert(s.ok());

    std::string v;
    s = db->Get(ReadOptions(), "k1", &v);
    assert(s.ok());
    std::cout << "v = " << v << std::endl;

    rocksdb::WriteBatch batch;
    batch.Delete("k1");
    batch.Put("k2", "v2");
    batch.Put("k3", "v3");
    batch.Put("k4", "v4");
    s = db->Write(rocksdb::WriteOptions(), &batch);
    if (s.ok()) {
    std::string v2;
    s = db->Get(ReadOptions(), "k2", &v2);
    std::cout << "v = " << v2 << std::endl;
    s = db->Get(ReadOptions(), "k1", &v);
    if (!s.ok()) {
        std::cout << s.ToString() << std::endl;
    }
    // assert(s.ok());
    //std::cout<<"v = "<<v<<std::endl;
    }

    rocksdb::Iterator * it = db->NewIterator(rocksdb::ReadOptions());
    for (it->SeekToFirst(); it->Valid(); it->Next()) {
    std::cout << it->key().ToString() << ":" << it->value().
        ToString() << std::endl;
    }

    testTransaction();
}

posted on 2019-09-21 18:57  盛夏落木  阅读(...)  评论(...编辑  收藏

导航