Skip to main content
SstFileWriter creates SST (Sorted String Table) files that can be efficiently ingested into a RocksDB database. This is useful for bulk loading data, as it avoids the overhead of individual Put operations.

Creating an SstFileWriter

Constructor

const EnvOptions&
Environment options for file operations.
const Options&
Database options including comparator and table options.
ColumnFamilyHandle*
Target column family. Pass nullptr if unknown.
bool
If true, hints to OS that file pages are not needed after writing.
Env::IOPriority
I/O priority for rate limiting.

Opening a File

Open

Prepares SstFileWriter to write into the specified file.
const std::string&
Path where the SST file will be created.
Temperature
Temperature hint for storage placement (kHot, kWarm, kCold).
Status
Returns OK on success.

Adding Data

Put

Adds a key-value pair to the SST file.
const Slice&
Key to add. Must be after any previously added key according to the comparator.
const Slice&
Value to associate with the key.
Status
Returns OK on success, InvalidArgument if key order is violated.

Put with Timestamp

Adds a key with user-defined timestamp.
const Slice&
User-defined timestamp. Size must match comparator expectations.

PutEntity

Adds a wide-column entity.
const WideColumns&
Wide-column entity data.

Merge

Adds a merge operand.

Delete

Adds a deletion marker.

Delete with Timestamp

Adds a deletion marker with timestamp.

DeleteRange

Adds a range deletion tombstone [begin_key, end_key).
const Slice&
Start of the range (inclusive).
const Slice&
End of the range (exclusive).

Finalizing the File

Finish

Finalizes writing and closes the SST file.
ExternalSstFileInfo*
Optional output parameter populated with information about the created file.
Status
Returns OK on success.

FileSize

Returns the current file size.
uint64_t
Current size of the SST file in bytes.

File Information

ExternalSstFileInfo

std::string
Path to the created SST file.
std::string
Smallest user key in the file.
std::string
Largest user key in the file.
SequenceNumber
Sequence number of all keys in the file (always 0 for SstFileWriter).
uint64_t
File size in bytes.
uint64_t
Number of entries in the file.

Example: Basic Usage

Example: Bulk Loading with Ingestion

Example: Range Deletions

Key Ordering Requirements

Sorted Keys Required

Keys must be added in sorted order according to the comparator:

Range Deletions Are Order-Independent

Range deletion tombstones can be added in any order:

Verification

CreatedBySstFileWriter

Checks if a file with the given table properties was created by SstFileWriter.
const TableProperties&
Table properties to check.
bool
Returns true if the file was created by SstFileWriter.

Performance Tips

Buffer Size

Larger write buffers can improve performance:

Compression

Enable compression to reduce file size:

Page Cache Invalidation

Enable page cache invalidation for better memory usage:

Common Pitfalls

Unsorted Keys

The most common error is adding keys out of order:
Always ensure keys are sorted before adding.

Forgetting to Call Finish

Always call Finish() before using the file:

Sequence Numbers

All keys in SstFileWriter files have sequence number 0. When ingested, they receive the current database sequence number.

See Also