当前位置: 首页 > 工具软件 > LevelDB > 使用案例 >

[leveldb] levelDB源码阅读

江超英
2023-12-01

[leveldb] levelDB源码阅读

cmake配置

# leveldb.cmake文件
include(FetchContent)

FetchContent_Declare(
    leveldb
  GIT_REPOSITORY https://github.com/google/leveldb.git
  GIT_TAG 1.23)
# https://gitlab.kitware.com/cmake/cmake/-/issues/20579
# 
FetchContent_MakeAvailable(leveldb)

# 导入配置即可
include(3rd/leveldb.cmake)

hello.cpp

#include <assert.h>
#include <iostream>
#include <leveldb/db.h>
#include <string.h>

// using namespace leveldb;

int main() {
  leveldb::DB *db = nullptr;
  leveldb::Options options;
  // 打开一个数据库, 不存在就创建
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  // 插入一个键值对
  status = db->Put(leveldb::WriteOptions(), "hello", "levelDB");
  assert(status.ok());

  // 读取键值对
  std::string value;
  status = db->Get(leveldb::ReadOptions(), "hello", &value);

  assert(status.ok());
  std::cout << value << '\n';

  delete db;
  return 0;
}

在对应的target上链接leveldb即可

add_executable(app app.cpp)
# ...
target_link_libraries(app PRIVATE leveldb)

reference

 类似资料: