RocksDB安装与使用


安装

系统为:Ubuntu18.04

Dependencies:

sudo apt-get install libgflags-dev \
		     libsnappy-dev \
		     zlib1g-dev \
	             libbz2-dev \
	             liblz4-dev \
		     libzstd-dev

参考文档

使用

create_db.cpp

#include <assert.h>
#include "rocksdb/db.h"
int main()
{
  rocksdb::DB* db;
  rocksdb::Options options;
  options.create_if_missing = true;
  rocksdb::Status status = rocksdb::DB::Open(options, "/home/ubuntu/rocksdb_test/testdb", &db);
  assert(status.ok());
  return 0;
}

CMakeLists.txt

# Passed
# PROJECT(CREATEDB)
# LINK_LIBRARIES("/home/ubuntu/3rd/rocksdb/librocksdb.a")
# INCLUDE_DIRECTORIES(/home/ubuntu/3rd/rocksdb/include)

# find_library(LZ4_LIB lz4)
# message(STATUS "finding ${LZ4_LIB}")

# ADD_EXECUTABLE(create_db create_db.cpp)
# TARGET_LINK_LIBRARIES(create_db ${LZ4_LIB} -lpthread -lsnappy -lz -lbz2 -lzstd -ldl)

PROJECT(CREATEDB)
LINK_LIBRARIES("/home/ubuntu/3rd/rocksdb/librocksdb.a")
INCLUDE_DIRECTORIES(/home/ubuntu/3rd/rocksdb/include)

ADD_EXECUTABLE(create_db create_db.cpp)
TARGET_LINK_LIBRARIES(create_db z4 pthread snappy z bz2 zstd dl)

运行create_db创建数据库

(base) ubuntu@VM-8-7-ubuntu:~/rocksdb_test$ tree  testdb/
testdb/
├── 000005.log
├── CURRENT
├── IDENTITY
├── LOCK
├── LOG
├── MANIFEST-000004
└── OPTIONS-000007

0 directories, 7 files

全局数据库对象的使用

dbadapter.cpp

#include "rocksdb/db.h"
#include <dbadapter.h>
#include <string>
#include <iostream>
using namespace std;

DBTool::DBTool(void){
    write_option = rocksdb::WriteOptions();
    read_option = rocksdb::ReadOptions();
    options.create_if_missing = true;
    path = "/home/ubuntu/rocksdb_test/testdb";
    cout << "初始化数据库配置" << endl;
}

void DBTool::open(){
    status = rocksdb::DB::Open(options, path, &db);
    if (status.ok()){
        cout << "UPM数据库打开成功" << endl;
    }else{
        cerr << "UPM数据库打开失败" << endl;
    }
}

void DBTool::close(){
    status = db->Close();
    cout << "数据库关闭" << endl;
    if (status.ok()){
        // rocksdb::Status status = rocksdb::DB::DestoryDB(path, options);
        status = DestroyDB(path, options);
        cout << "数据库删除成功" << endl;
        delete db;
    } else{
        cerr << "close db error" << endl;
    }
}

bool DBTool::update(const string& k, const string& v){
    status = db->Put(write_option, k, v);
    if ( status.ok() ){
        return true;
    }else{
        cerr << "数据库出错,更新失败" << endl;
        return false;
    }
}

void DBTool::get(const string& k, string* result){
    status = db->Get(read_option, k, result);
    if ( !status.ok() ){
        cerr << status.code() << endl;
    }
}
DBTool dbtool = DBTool();

dbadapter.h

#include "rocksdb/db.h"
#include <string>
#include <iostream>
using namespace std;

// #ifndef DB_TOOL
// #define DB_TOOL
class DBTool{
public:
    DBTool();
    void open();
    void close();
    // ~DBTool(); // 如果将close放在其中进行自动销毁,则会error:pthread lock: Invalid argument

    bool update(const string& k, const string& v);
    void get(const string& k, string* result);

private:
    rocksdb::DB* db;
    rocksdb::Options options;
    rocksdb::WriteOptions write_option;
    rocksdb::ReadOptions read_option;
    string path;
    rocksdb::Status status;
};

extern DBTool dbtool;
// #endif

create_db.cpp

#include <assert.h>
#include <iostream>
#include <string>
#include <dbadapter.h>
using namespace std;

class A{
public:
    string index;
    string a;
    A(const string& index, const string& a):index(index), a(a){
      update_a(a);
    }

    void update_a(const string &value){
      if (dbtool.update(index, value)){
        cout << "更新成功" << endl;
      }else{
        cerr << "更新失败" << endl;
      }
    }
};

int main()
{
  dbtool.open();
  A a = A("1", "hello");
  string result;
  dbtool.get("1", &result);
  cout << result << endl;
  a.update_a("world");
  dbtool.get("1", &result);
  cout << result << endl;
  dbtool.close();

  return 0;
}

CMakeLists

# Passed
PROJECT(CREATEDB)
find_library(LZ4_LIB lz4)
message(STATUS "finding ${LZ4_LIB}")
LINK_LIBRARIES("/home/ubuntu/3rd/rocksdb-6.22.1/librocksdb_debug.a")
INCLUDE_DIRECTORIES(/home/ubuntu/3rd/rocksdb-6.22.1/include)
INCLUDE_DIRECTORIES(./)

ADD_EXECUTABLE(create_db create_db.cpp dbadapter.cpp)
TARGET_LINK_LIBRARIES(create_db ${LZ4_LIB} pthread snappy z bz2 zstd dl)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM