Rocksdb基本用法

rocksdb 用法簡單介紹

  1. RocksDB是使用C++編寫的嵌入式kv存儲引擎,其鍵值均容許使用二進制流。由Facebook基於levelDB開發, 提供向後兼容的levelDB API。 RocksDB針對Flash存儲進行優化,延遲極小。RocksDB使用LSM存儲引擎,純C++編寫。html

  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());ios

  3. 往數據庫寫入kv鍵值對 s = db->Put(WriteOptions(), "k1", "v1");c++

  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和計算。在特定的工做載荷下, 它帶來的改善是顯著的。這個選項能夠同在正向和反向迭代。spa

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

  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();htm

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();
}

原文出處:https://www.cnblogs.com/wanshuafe/p/11564148.htmlblog

相關文章
相關標籤/搜索