Skip to content

Commit

Permalink
feat: Add example code for RocksDB - ZenFS application
Browse files Browse the repository at this point in the history
Signed-off-by: Dennis Maisenbacher <[email protected]>
Co-authored-by: Kim Taerang <[email protected]>
  • Loading branch information
MaisenbacherD and kt5965 committed Dec 12, 2023
1 parent 8337de1 commit e1363be
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ fs/*.o
fs/*.cc.d
tests/results
fs/version.h
examples/*.o
examples/*.cc.d
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
16 changes: 16 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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
```
66 changes: 66 additions & 0 deletions examples/rw_key.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <assert.h>

#include <iostream>

#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 <device-name>\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<rocksdb::Env> 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;
}

0 comments on commit e1363be

Please sign in to comment.