diff --git a/CMakeLists.txt b/CMakeLists.txt index b475a2224fd..8fe5f17d5b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000000..086ba29596b --- /dev/null +++ b/main.cpp @@ -0,0 +1,83 @@ +// +// Created by on 2024/1/11. +// +#include +#include + +#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<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; +}