Skip to content

Instantly share code, notes, and snippets.

@dotslash
Created December 24, 2020 19:28
Show Gist options
  • Save dotslash/6136f74b595af38cb316e773f94bcefa to your computer and use it in GitHub Desktop.
Save dotslash/6136f74b595af38cb316e773f94bcefa to your computer and use it in GitHub Desktop.
// osx instructions on Mojave (as of 2020-12-27)
// brew install llvm
// brew insall rocksdb
// llvm-g++ -std=c++14 -L /usr/local/Cellar/rocksdb/6.14.6_1/lib -lrocksdb rocks_example.cc
#include "rocksdb/db.h"
#include <iostream>
#include <cassert>
#include <sstream>
void assert_status(rocksdb::Status status, std::string msg ) {
if (!status.ok()) {
std::cout << msg << ". Exiting with failure. status -> " << status.ToString() << std::endl;
assert(false);
}
}
rocksdb::Status log_failure(rocksdb::Status status, std::string msg ) {
if (!status.ok()) {
std::cout << msg << ". Status -> " << status.ToString() << std::endl;
}
return status;
}
std::unique_ptr<rocksdb::DB> get_db(std::string dbfile) {
rocksdb::Options options;
options.create_if_missing = true;
rocksdb::DB* db_raw_ptr;
rocksdb::Status status =
rocksdb::DB::Open(options, dbfile, &db_raw_ptr);
std::unique_ptr<rocksdb::DB> db(db_raw_ptr);
assert_status(status, "Failed to create db");
return db;
}
int main() {
std::unique_ptr<rocksdb::DB> db = get_db("/tmp/testdb");
assert_status(
db->Put(rocksdb::WriteOptions(), "k1", "v1"),
"Failed to write k1 = v1"
);
std::string val;
assert_status(
db->Get(rocksdb::ReadOptions(), "k1", &val),
"Failed to read k1"
);
std::cout << "Value for k1 is " << val << std::endl;
log_failure(
db->Get(rocksdb::ReadOptions(), "k2", &val),
"Failed to read k2"
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment