Overview
TheIterator class provides a way to sequentially scan through the contents of a RocksDB database. Iterators support both forward and backward iteration, seeking to specific keys, and accessing key-value pairs or wide-column entities.
Creating Iterators
Positioning Methods
SeekToFirst
Valid() after this call iff the source is not empty.
SeekToLast
Valid() after this call iff the source is not empty.
Seek
target. The iterator is Valid() after this call iff the source contains an entry that comes at or past target.
Target key (does not contain timestamp).
SeekForPrev
target. The iterator is Valid() after this call iff the source contains an entry that comes at or before target.
Target key (does not contain timestamp).
Navigation Methods
Next
Valid() is true iff the iterator was not positioned at the last entry.
REQUIRES: Valid()
Prev
Valid() is true iff the iterator was not positioned at the first entry.
REQUIRES: Valid()
Access Methods
Valid
!status().ok().
key
Valid()
value
kDefaultWideColumnName) if any, or an empty value otherwise.
REQUIRES: Valid()
columns
Valid()
timestamp
Valid()
status
Status::Incomplete() if non-blocking I/O was requested and the operation cannot be satisfied without doing I/O.
Advanced Methods
PrepareValue
ReadOptions::allow_unprepared_value is set, call this method before accessing the value to ensure it is prepared. Returns true on success.
REQUIRES: Valid()
Refresh
Seek*() function to get the iterator back into a valid state.
GetProperty
"rocksdb.iterator.is-key-pinned": Returns “1” if the key is pinned (valid as long as iterator is not deleted)"rocksdb.iterator.is-value-pinned": Returns “1” if the value is pinned"rocksdb.iterator.super-version-number": LSM version used by the iterator"rocksdb.iterator.internal-key": Get the user-key portion of the internal key"rocksdb.iterator.write-time": Get the estimated write time of the entry
Prepare (Experimental)
scan_opts. This includes prefetching relevant blocks from disk.
Examples
Iterator Lifecycle
- Create the iterator with
DB::NewIterator() - Position the iterator using
Seek*()methods - Navigate using
Next()orPrev() - Access data using
key(),value(), orcolumns() - Check validity with
Valid()and errors withstatus() - Delete the iterator when done
Error Handling
AllSeek*() methods clear any previous error status. After seeking, status() indicates only errors that occurred during the seek, not past errors.
Always check status() after iteration:
Thread Safety
Multiple threads can invoke const methods on an Iterator without external synchronization. However, if any thread may call a non-const method, all threads accessing the same Iterator must use external synchronization.Performance Tips
- Set
fill_cache=falsefor bulk scans to avoid polluting the block cache - Use
readahead_sizefor large sequential scans (e.g., 2MB+) - Set iteration bounds (
iterate_upper_bound) to enable optimizations - Use
prefix_same_as_startfor prefix scans with prefix bloom filters - Consider
pin_data=trueif repeatedly accessing iterator data - Enable
adaptive_readaheadfor better auto-tuning of readahead
See Also
- ReadOptions - Options for configuring iterators
- DB Scan Operations - High-level scan patterns
- Multi-Column Family Iterators - Advanced iteration patterns