Iterator Class
Definition
Namespace: RocksDbSharp
public class Iterator : IDisposable
Iterator is an ordered cursor over a database or a single column family.
Because RocksDB stores keys in sorted byte order, an iterator is the primitive
for range scans, prefix scans, and reverse scans.
Remarks
Create an iterator with RocksDb.NewIterator. It
holds a native handle and an implicit snapshot of the database state at
construction time, so concurrent writes are invisible to it. Always dispose the
iterator (or wrap it in using) — a long-lived iterator pins obsolete SST
files and blocks compaction.
Scope a scan efficiently by setting iterate_lower_bound /
iterate_upper_bound on ReadOptions, or pin an older view
with a snapshot. On net6+, GetKeySpan / GetValueSpan give zero-allocation
access to the native buffers. See Iterators.
Methods
| Name | Description |
|---|---|
SeekToFirst() |
Positions at the smallest key. |
SeekToLast() |
Positions at the largest key. |
Seek(key) |
Positions at the first key >= key. |
SeekForPrev(key) |
Positions at the last key <= key. |
Next() |
Moves forward one key. |
Prev() |
Moves backward one key. |
Valid() |
Returns whether the iterator is on a valid entry. |
StringKey() |
Returns the current key as a string. |
StringValue() |
Returns the current value as a string. |
GetKeySpan() |
Returns the current key as a ReadOnlySpan<byte>. |
GetValueSpan() |
Returns the current value as a ReadOnlySpan<byte>. |
SeekToFirst
public Iterator SeekToFirst()
Positions the iterator at the smallest key.
SeekToLast
public Iterator SeekToLast()
Positions the iterator at the largest key.
Seek
public Iterator Seek(string key)
Positions the iterator at the first key greater than or equal to key.
SeekForPrev
public Iterator SeekForPrev(string key)
Positions the iterator at the last key less than or equal to key.
Next
public Iterator Next()
Moves the iterator forward by one key.
Prev
public Iterator Prev()
Moves the iterator backward by one key. Combine with
SeekToLast for descending iteration.
Valid
public bool Valid()
Returns whether the iterator is currently positioned on a valid entry. Use it as the loop condition.
StringKey
public string StringKey()
Returns the current key decoded as a UTF-8 string.
StringValue
public string StringValue()
Returns the current value decoded as a UTF-8 string.
GetKeySpan
public ReadOnlySpan<byte> GetKeySpan()
Returns the current key as a zero-allocation ReadOnlySpan<byte> over the
native buffer (net6+). The span is only valid until the next move.
GetValueSpan
public ReadOnlySpan<byte> GetValueSpan()
Returns the current value as a zero-allocation ReadOnlySpan<byte> over the
native buffer (net6+). The span is only valid until the next move.
Applies to
RocksDbSharp — see Iterators for range scans, reverse scans, bounded scans, and snapshot pinning.