WriteBatch Class
Definition
Namespace: RocksDbSharp
public class WriteBatch : IDisposable
WriteBatch groups any number of Put, Merge, Delete, and DeleteRange
operations into a single atomic write. The batch buffers operations in memory
until handed to RocksDb.Write.
Remarks
Either every operation in the batch lands or none do. Batches are also faster
than individual writes because they amortise the WAL fsync and MemTable
insertion overhead. Every overload accepts an optional ColumnFamilyHandle, so
a single batch can write across multiple column families atomically. The
mutating methods are fluent and return the batch for chaining.
ToBytes serializes the batch in the same format RocksDB writes to the WAL,
which makes it the basis for replication and persistence. See
Write batches.
Constructors
| Name | Description |
|---|---|
WriteBatch() |
Creates an empty batch. |
WriteBatch(bytes) |
Reconstructs a batch from serialized bytes. |
WriteBatch
public WriteBatch()
Creates a new empty write batch.
WriteBatch (bytes)
public WriteBatch(byte[] rep)
Reconstructs a batch from the bytes produced by ToBytes — used to
replay a batch on another instance.
Methods
| Name | Description |
|---|---|
Put(key, value, cf?) |
Inserts or overwrites a key. |
Merge(key, value, cf?) |
Appends an operand for the merge operator. |
Delete(key, cf?) |
Writes a tombstone. |
DeleteRange(start, sklen, end, eklen, cf?) |
Tombstones a contiguous half-open range. |
PutLogData(blob, len) |
Adds WAL-only metadata not applied to the MemTable. |
Clear() |
Resets the batch for reuse. |
Count() |
Returns the number of queued operations. |
SetSavePoint() |
Marks a rollback point. |
RollbackToSavePoint() |
Drops operations queued since the last save point. |
ToBytes() |
Serializes the batch for replication or persistence. |
Iterate(state, putCb, deleteCb) |
Walks every operation in the batch. |
Put
public WriteBatch Put(string key, string value, ColumnFamilyHandle cf = null)
Queues an insert or overwrite. Span overloads (net6+) are supported.
Merge
public WriteBatch Merge(string key, string value, ColumnFamilyHandle cf = null)
Queues a merge operand for the configured merge operator.
Delete
public WriteBatch Delete(string key, ColumnFamilyHandle cf = null)
Queues a tombstone for the key.
DeleteRange
public WriteBatch DeleteRange(byte[] startKey, ulong sklen, byte[] endKey, ulong eklen, ColumnFamilyHandle cf = null)
Tombstones every key in the half-open range [startKey, endKey) in a single
record — far cheaper than many individual deletes.
PutLogData
public WriteBatch PutLogData(byte[] blob, ulong len)
Adds metadata that travels in the WAL but isn't applied to the MemTable.
Clear
public WriteBatch Clear()
Resets the batch so it can be reused.
Count
public int Count()
Returns the number of operations queued in the batch.
SetSavePoint
public void SetSavePoint()
Marks the current position so the tail of the batch can be rolled back.
RollbackToSavePoint
public void RollbackToSavePoint()
Drops every operation queued since the last SetSavePoint.
ToBytes
public byte[] ToBytes()
Returns the serialized batch — exactly the WAL record format — so it can be shipped across the wire and replayed elsewhere.
Iterate
public void Iterate(IntPtr state, PutDelegate put, DeletedDelegate deleted)
Walks every operation in the batch via callbacks, useful for inspection or selective replay.
Applies to
RocksDbSharp — see Write batches for worked examples, including WriteBatchWithIndex and WAL-streaming replication.