Overview
TheDB class is the primary interface for interacting with a RocksDB database. It provides methods for reading, writing, and managing a persistent, versioned ordered map from keys to values. The DB is safe for concurrent access from multiple threads without any external synchronization.
Opening a Database
Open
Open the database with the specified name for reads and writes.Database configuration options
Path to the database directory
Output parameter that will store the database instance on success
Returns OK on success, or a non-OK status on error (including if the DB is already open read-write by another DB object)
Open with Column Families
Open database with column families support.Database-level options
Path to the database directory
Vector of all column families in the database. You need to open ALL column families.
Output vector of column family handles (will be same size as column_families)
Output parameter for the database instance
OpenForReadOnly
Create a read-only instance that supports reads alone.If true, returns error when WAL files exist
All DB interfaces that modify data (Put/Delete) will return error. Automatic Flush and Compactions are disabled.
OpenAsSecondary
Create a secondary instance that supports read-only operations with dynamic catch-up.Directory where the secondary instance stores its info log
Options.max_open_files should be set to -1 for secondary instances.
Write Operations
Put
Set the database entry for “key” to “value”.Write options (e.g., sync behavior)
Target column family (default column family if not specified)
The key to set
The value to store
If “key” already exists, it will be overwritten. Consider setting options.sync = true.
Delete
Remove the database entry for “key”.Returns OK on success. It is not an error if “key” did not exist in the database.
DeleteRange
Remove database entries in the range [begin_key, end_key).Start of the range (inclusive)
End of the range (exclusive)
This feature is production-ready with these caveats:
- Accumulating too many range tombstones in the memtable will degrade read performance
- Limiting max_open_files with range tombstones can degrade read performance (set to -1 when possible)
- Incompatible with row_cache
Merge
Merge the database entry for “key” with “value”.The semantics of this operation is determined by the user-provided merge_operator when opening the DB.
Write
Apply the specified updates atomically to the database.Batch of write operations to apply atomically
If
updates contains no update, WAL will still be synced if options.sync=true.Read Operations
Get
Retrieve the value for a given key.Read options (e.g., snapshot, verify_checksums)
The key to retrieve
Output parameter for the retrieved value
Optional output parameter for timestamp (if enabled)
Returns OK on success, NotFound if there is no entry for “key”, or other non-OK status on error
MultiGet
Batched key retrieval for better performance.Number of keys to lookup
Array of column family handles for each key
Array of keys to retrieve
Array for output values
Array for per-key status results
Set true if keys are already sorted by key order (optimization)
This API improves performance by batching operations in the read path. Currently optimized for block based table format with full filters.
NewIterator
Create an iterator over the database contents.Heap-allocated iterator (caller must delete). Initially invalid - must call Seek before using.
Column Family Management
CreateColumnFamily
Create a new column family.Creating many column families one-by-one is not recommended due to quadratic overheads. Use CreateColumnFamilies() or DB::Open() with create_missing_column_families=true instead.
DropColumnFamily
Drop a column family.This call only records a drop record in the manifest and prevents the column family from flushing and compacting.
DestroyColumnFamilyHandle
Release and deallocate a column family handle.ListColumnFamilies
Get the list of all column families in a database.Database Management
Close
Close the database by releasing resources.This should be called before the destructor. Does not fsync WAL files - call SyncWAL() first if needed. The DB must be freed regardless of return status.
CompactRange
Manually compact a key range.Start of range (nullptr = before all keys)
End of range (nullptr = after all keys)
This call blocks until the operation completes, fails, or is aborted (Status::Incomplete).
Flush
Flush all memtable data to SST files.GetSnapshot
Create a snapshot of the current DB state.Snapshot handle. Returns nullptr if the DB fails to take a snapshot or does not support snapshots.
ReleaseSnapshot
Release a previously acquired snapshot.Properties and Statistics
GetProperty
Get database property values.GetIntProperty
Get integer property values.Supported properties include: “rocksdb.num-immutable-mem-table”, “rocksdb.estimate-num-keys”, “rocksdb.total-sst-files-size”, and many more.