Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add example code for RocksDB - ZenFS application #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading