Skip to content

Commit

Permalink
add debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyuecai committed May 26, 2024
1 parent eb1a67e commit e45bf29
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,12 @@ if(WITH_TESTS)
endif()
endif()

# 搜索指定目录下源文件
#file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_executable(myTest "main.cpp")
target_link_libraries(myTest ${ROCKSDB_STATIC_LIB})


if(WITH_BENCHMARK_TOOLS)
add_executable(db_bench${ARTIFACT_SUFFIX}
tools/simulated_hybrid_file_system.cc
Expand Down
83 changes: 83 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// Created by on 2024/1/11.
//
#include <iostream>
#include <string>

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

using ROCKSDB_NAMESPACE::DB;
using ROCKSDB_NAMESPACE::Options;
using ROCKSDB_NAMESPACE::PinnableSlice;
using ROCKSDB_NAMESPACE::ReadOptions;
using ROCKSDB_NAMESPACE::Status;
using ROCKSDB_NAMESPACE::WriteBatch;
using ROCKSDB_NAMESPACE::WriteOptions;

// 参考资料:https://blog.csdn.net/dlf123321/article/details/135534752

// rocksdb存储路径
std::string kDBPath="c://myrocksdb";

int main()
{
DB* db;
Options options;
options.IncreaseParallelism();
//文件夹没有数据就创建
options.create_if_missing=true;
// 打开数据库,加载数据到内存
Status s=DB::Open(options,kDBPath,&db);
assert(s.ok());
// 写key-value
s=db->Put(WriteOptions(),"key01","value");
assert(s.ok());

std::string value;
s=db->Get(ReadOptions(),"key01",&value);
assert(s.ok());
std::cout<<"get value "<< value<<std::endl;
assert(value=="value");
// 管道,原子方式更新
{
WriteBatch batch;
batch.Delete("key01");
batch.Put("key02",value);
s=db->Write(WriteOptions(),&batch);
}
s=db->Get(ReadOptions(),"key01",&value);
assert(s.IsNotFound());
s=db->Get(ReadOptions(),"key02",&value);
assert(value=="value");

{
PinnableSlice pinnable_val;
// 列族方式读取
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
}

{
std::string string_val;
PinnableSlice pinnable_val(&string_val);
// 列族方式读取
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
assert(pinnable_val.IsPinned() || string_val == "value");
}

PinnableSlice pinnable_val;
s=db->Get(ReadOptions(),db->DefaultColumnFamily(),"key01",&pinnable_val);
assert(s.IsNotFound());

pinnable_val.Reset();
db->Get(ReadOptions(),db->DefaultColumnFamily(),"key02",&pinnable_val);
assert(pinnable_val=="value");
pinnable_val.Reset();

delete db;

return 0;
}

0 comments on commit e45bf29

Please sign in to comment.