From e1363be005f8e91983c64614f1fd824ce00099b1 Mon Sep 17 00:00:00 2001 From: Dennis Maisenbacher Date: Thu, 7 Dec 2023 14:49:02 +0100 Subject: [PATCH] feat: Add example code for RocksDB - ZenFS application Signed-off-by: Dennis Maisenbacher Co-authored-by: Kim Taerang --- .gitignore | 2 ++ README.md | 4 +-- examples/README.md | 16 +++++++++++ examples/rw_key.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/rw_key.cc diff --git a/.gitignore b/.gitignore index 7b52ec0b..f4db2b1e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ fs/*.o fs/*.cc.d tests/results fs/version.h +examples/*.o +examples/*.cc.d diff --git a/README.md b/README.md index a4ce8321..c1ed532b 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,6 @@ ZenFS uses clang-format with Google code style. You may run the following comman before submitting a PR. ```bash -clang-format-11 -n -Werror --style=file fs/* util/zenfs.cc # Check for style issues -clang-format-11 -i --style=file fs/* util/zenfs.cc # Auto-fix the style issues +clang-format-11 -n -Werror --style=file fs/* util/zenfs.cc examples/*.cc # Check for style issues +clang-format-11 -i --style=file fs/* util/zenfs.cc examples/*.cc # Auto-fix the style issues ``` diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..dbd8d2ce --- /dev/null +++ b/examples/README.md @@ -0,0 +1,16 @@ +#Writing RocksDB applications with the ZenFS plugin + +The ZenFS plugin can also be used in your own RocksDB applications. +To help you writing your own applications you can refer to the examples in this +directory. + +To compile an example application you first need to build RocksDB with the +ZenFS plugin acording to +[this section](https://github.com/westerndigitalcorporation/zenfs#build). + +Then you can run: +``` +g++ -I../../.. -I../../../include $(pkg-config --cflags rocksdb) -c ./rw_key.cc -o ./rw_key.o +g++ -L../../.. $(pkg-config --static --libs rocksdb) ./rw_key.o -o ./rw_key +./rw_key nvmeXnX +``` diff --git a/examples/rw_key.cc b/examples/rw_key.cc new file mode 100644 index 00000000..558e105f --- /dev/null +++ b/examples/rw_key.cc @@ -0,0 +1,66 @@ +#include + +#include + +#include "rocksdb/db.h" +#include "rocksdb/env.h" +#include "rocksdb/options.h" +#include "rocksdb/utilities/options_util.h" + +int main(int argc, char* argv[]) { + if (argc != 2) { + printf("Usage:\n\t%s \nE.g.:\n\t%s nvme0n1\n", argv[0], + argv[0]); + return 1; + } + + int ret = 0; + std::string test_value; + std::string device_name = argv[1]; + rocksdb::DB* db; + static std::shared_ptr fs_env_guard; + static rocksdb::Env* fs_env = nullptr; + rocksdb::Options options; + options.create_if_missing = true; + options.use_direct_io_for_flush_and_compaction = true; + rocksdb::ConfigOptions config_options(options); + + rocksdb::Env::CreateFromUri(config_options, "", "zenfs://dev:" + device_name, + &fs_env, &fs_env_guard); + options.env = fs_env; + + // Open database + rocksdb::Status status = rocksdb::DB::Open(options, "rocksdbtest", &db); + if (!status.ok()) { + std::cerr << "Could not open the database: " << status.ToString() + << std::endl; + ret = 1; + goto out; + } + + // Write + status = + db->Put(rocksdb::WriteOptions(), "zenfs_test_var", "zenfs_test_value"); + if (!status.ok()) { + std::cerr << "Could not write test value to the database: " + << status.ToString() << std::endl; + ret = 1; + } + + // Read + status = db->Get(rocksdb::ReadOptions(), "zenfs_test_var", &test_value); + if (!status.ok()) { + std::cerr << "Could not read test value to the database: " + << status.ToString() << std::endl; + ret = 1; + goto out; + } else { + std::cout << "Value was successfully written and read: " << test_value + << std::endl; + ret = 0; + } + +out: + if (db) delete db; + return ret; +}