Before performing any operations, you need to open a RocksDB database:
#include "rocksdb/db.h"#include "rocksdb/options.h"using ROCKSDB_NAMESPACE::DB;using ROCKSDB_NAMESPACE::Options;using ROCKSDB_NAMESPACE::Status;std::unique_ptr<DB> db;Options options;// Optimize RocksDB. This is the easiest way to get RocksDB to perform welloptions.IncreaseParallelism();options.OptimizeLevelStyleCompaction();// Create the DB if it's not already presentoptions.create_if_missing = true;// Open DBStatus s = DB::Open(options, "/path/to/db", &db);assert(s.ok());
The IncreaseParallelism() and OptimizeLevelStyleCompaction() methods are the easiest way to get good performance from RocksDB. They set reasonable defaults for most workloads.
For better performance, use PinnableSlice to avoid unnecessary memory copies:
using ROCKSDB_NAMESPACE::PinnableSlice;PinnableSlice pinnable_val;Status s = db->Get(ReadOptions(), db->DefaultColumnFamily(), "key2", &pinnable_val);assert(s.ok());assert(pinnable_val == "value");// Reset PinnableSlice after each use and before each reusepinnable_val.Reset();
Always call Reset() on a PinnableSlice before reusing it. The Slice pointed by pinnable_val is not valid after calling Reset().
RocksDB allows you to apply multiple operations atomically using WriteBatch:
using ROCKSDB_NAMESPACE::WriteBatch;WriteBatch batch;batch.Delete("key1");batch.Put("key2", "value");Status s = db->Write(WriteOptions(), &batch);assert(s.ok());
After the batch is applied:
// key1 has been deletedStatus s = db->Get(ReadOptions(), "key1", &value);assert(s.IsNotFound());// key2 has the new values = db->Get(ReadOptions(), "key2", &value);assert(s.ok());assert(value == "value");
1
Create a WriteBatch
Instantiate a WriteBatch object to hold multiple operations.
2
Add operations
Add Put, Delete, or other operations to the batch.
3
Apply atomically
Call db->Write() to apply all operations atomically.
All RocksDB operations return a Status object that indicates success or failure:
Status s = db->Put(WriteOptions(), "key", "value");if (s.ok()) { // Success} else if (s.IsNotFound()) { // Key not found (for Get operations)} else if (s.IsCorruption()) { // Data corruption detected} else if (s.IsIOError()) { // I/O error occurred} else { // Other error std::cerr << "Error: " << s.ToString() << std::endl;}
Here’s a complete example demonstrating basic operations:
#include <cstdio>#include <memory>#include <string>#include "rocksdb/db.h"#include "rocksdb/options.h"#include "rocksdb/slice.h"using ROCKSDB_NAMESPACE::DB;using ROCKSDB_NAMESPACE::Options;using ROCKSDB_NAMESPACE::PinnableSlice;using ROCKSDB_NAMESPACE::ReadOptions;using ROCKSDB_NAMESPACE::Status;using ROCKSDB_NAMESPACE::WriteBatch;using ROCKSDB_NAMESPACE::WriteOptions;int main() { std::unique_ptr<DB> db; Options options; // Optimize RocksDB. This is the easiest way to get RocksDB to perform well options.IncreaseParallelism(); options.OptimizeLevelStyleCompaction(); // create the DB if it's not already present options.create_if_missing = true; // open DB Status s = DB::Open(options, "/tmp/rocksdb_simple_example", &db); assert(s.ok()); // Put key-value s = db->Put(WriteOptions(), "key1", "value"); assert(s.ok()); std::string value; // get value s = db->Get(ReadOptions(), "key1", &value); assert(s.ok()); assert(value == "value"); // atomically apply a set of updates { WriteBatch batch; batch.Delete("key1"); batch.Put("key2", value); s = db->Write(WriteOptions(), &batch); } s = db->Get(ReadOptions(), "key1", &value); assert(s.IsNotFound()); db->Get(ReadOptions(), "key2", &value); assert(value == "value"); return 0;}