-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add example code for RocksDB - ZenFS application
Signed-off-by: Dennis Maisenbacher <[email protected]> Co-authored-by: Kim Taerang <[email protected]>
- Loading branch information
1 parent
8337de1
commit e1363be
Showing
4 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ fs/*.o | |
fs/*.cc.d | ||
tests/results | ||
fs/version.h | ||
examples/*.o | ||
examples/*.cc.d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |