Rocksdb基本用法

rocksdb 用法


rocksdb 介绍

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

rocksdb用法如以下代码

#include<cstdio>
#include<string>
#include<iostream>


#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"

using namespace rocksdb;

std::string kDbPath = "/tmp/wsf_rocksdb_test";
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");
    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;
    }
}

版权声明:本文为wanshuafe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wanshuafe/p/11564148.html