Table of Contents

Class WriteBatch

Namespace
RocksDbNet
Assembly
RocksDb.Net.dll

An atomic write batch. Apply to the database with Write(WriteBatch, WriteOptions?). Maps to rocksdb_writebatch_t.

public sealed class WriteBatch : RocksDbHandle, IDisposable
Inheritance
WriteBatch
Implements
Inherited Members

Constructors

WriteBatch()

Creates an empty write batch.

public WriteBatch()

Properties

Count

Returns the number of operations in the batch.

public int Count { get; }

Property Value

int

Methods

Clear()

Clears all operations from the batch.

public WriteBatch Clear()

Returns

WriteBatch

Delete(ReadOnlySpan<byte>)

Queues a Delete from the default column family.

public WriteBatch Delete(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

Returns

WriteBatch

Delete(ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a Delete from the specified column family.

public WriteBatch Delete(ReadOnlySpan<byte> key, ColumnFamilyHandle cf)

Parameters

key ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatch

Delete(string)

Convenience overload using a UTF-8 string key.

public WriteBatch Delete(string key)

Parameters

key string

Returns

WriteBatch

Delete(string, ColumnFamilyHandle)

Convenience overload using a UTF-8 string key in a column family.

public WriteBatch Delete(string key, ColumnFamilyHandle cf)

Parameters

key string
cf ColumnFamilyHandle

Returns

WriteBatch

DeleteRange(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Queues a DeleteRange (deletes all keys in [startKey, endKey)) in the default column family.

public WriteBatch DeleteRange(ReadOnlySpan<byte> startKey, ReadOnlySpan<byte> endKey)

Parameters

startKey ReadOnlySpan<byte>
endKey ReadOnlySpan<byte>

Returns

WriteBatch

DeleteRange(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a DeleteRange in the specified column family.

public WriteBatch DeleteRange(ReadOnlySpan<byte> startKey, ReadOnlySpan<byte> endKey, ColumnFamilyHandle cf)

Parameters

startKey ReadOnlySpan<byte>
endKey ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatch

DisposeHandle()

Releases the native handle. Called during disposal.

protected override void DisposeHandle()

Remarks

Protected rather than public: it destroys the native object without marking this instance disposed or clearing the handle, so calling it from outside and then disposing normally would free the same pointer twice. It was the most Dispose-looking member on the type. Callers want Dispose().

Entries()

Returns the operations in this batch, in the order they were queued.

public IReadOnlyList<WriteBatchEntry> Entries()

Returns

IReadOnlyList<WriteBatchEntry>

Remarks

The missing half of change-data-capture. GetUpdatesSince(ulong, WalReadOptions?) hands back a batch of everything written since a sequence number, and without this there was no way to see what was in it. With it, a batch from the write-ahead log can be inspected, filtered or translated rather than only replayed wholesale.

Column families appear as numeric ids, because that is what the batch records. Compare them against Id.

The entries are copied out during the call, so the result stays valid after the batch is modified or disposed. RocksDb hands the callbacks pointers into its own memory that are only valid for the duration of iteration, so materialising is the only safe shape; it also keeps a caller's exception from having to cross the native boundary.

Only the four kinds in WriteBatchEntryKind can be read back. The C API offers no callback for a single delete or a range delete, so a batch containing either cannot be reported faithfully and this throws rather than returning a list that quietly omits them. Use GetData() for such a batch, or keep those operations out of batches you intend to read back.

Exceptions

NotSupportedException

The batch contains a single delete or a range delete.

GetData()

Returns the serialized batch data as a byte array.

public byte[] GetData()

Returns

byte[]

Merge(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Queues a Merge into the default column family.

public WriteBatch Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>

Returns

WriteBatch

Merge(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a Merge into the specified column family.

public WriteBatch Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, ColumnFamilyHandle cf)

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatch

PopSavePoint()

Removes the most recent save point.

public WriteBatch PopSavePoint()

Returns

WriteBatch

Put(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Queues a Put into the default column family.

public WriteBatch Put(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>

Returns

WriteBatch

Put(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a Put into the specified column family.

public WriteBatch Put(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, ColumnFamilyHandle cf)

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatch

Put(string, string)

Convenience overload using UTF-8 string key and value.

public WriteBatch Put(string key, string value)

Parameters

key string
value string

Returns

WriteBatch

Put(string, string, ColumnFamilyHandle)

Convenience overload using UTF-8 string key and value in a specific column family.

public WriteBatch Put(string key, string value, ColumnFamilyHandle cf)

Parameters

key string
value string
cf ColumnFamilyHandle

Returns

WriteBatch

PutLogData(ReadOnlySpan<byte>)

Inserts a blob of user-defined log data into the WAL.

public WriteBatch PutLogData(ReadOnlySpan<byte> blob)

Parameters

blob ReadOnlySpan<byte>

Returns

WriteBatch

RollbackToSavePoint()

Rolls back to the most recent save point.

public WriteBatch RollbackToSavePoint()

Returns

WriteBatch

SetSavePoint()

Marks a save point inside the batch.

public WriteBatch SetSavePoint()

Returns

WriteBatch

SingleDelete(ReadOnlySpan<byte>)

Queues a SingleDelete. Only valid when exactly one Put exists for the key (no prior Puts remain in the database for that key).

public WriteBatch SingleDelete(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

Returns

WriteBatch

SingleDelete(ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a SingleDelete in the specified column family.

public WriteBatch SingleDelete(ReadOnlySpan<byte> key, ColumnFamilyHandle cf)

Parameters

key ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatch

VerifyChecksum()

Verifies the batch's internal checksums, catching corruption before the batch is written.

public void VerifyChecksum()

Remarks

Only meaningful when per-key protection was enabled for the batch, via ProtectionBytesPerKey. Without it there is no checksum to check and the call simply succeeds.

Exceptions

RocksDbException

The batch is corrupt.