Table of Contents

Class WriteBatchWithIndex

Namespace
RocksDbNet
Assembly
RocksDb.Net.dll

An atomic write batch that also indexes its own contents, so it can be read back before it is applied. Maps to rocksdb_writebatch_wi_t.

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

Remarks

Apply it with Write(WriteBatchWithIndex, WriteOptions?). To read pending writes before applying them, use GetFromBatch(DbOptions, ReadOnlySpan<byte>) for the batch alone, or GetFromBatchAndDb(RocksDb, ReadOnlySpan<byte>, ReadOptions?) to see the batch layered over the committed database.

Constructors

WriteBatchWithIndex(int, bool)

Creates an empty write batch.

public WriteBatchWithIndex(int reservedBytes = 0, bool overwriteKeys = true)

Parameters

reservedBytes int
overwriteKeys bool

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 WriteBatchWithIndex Clear()

Returns

WriteBatchWithIndex

Delete(ReadOnlySpan<byte>)

Queues a Delete from the default column family.

public WriteBatchWithIndex Delete(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

Returns

WriteBatchWithIndex

Delete(ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a Delete from the specified column family.

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

Parameters

key ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatchWithIndex

Delete(string)

Convenience overload using a UTF-8 string key.

public WriteBatchWithIndex Delete(string key)

Parameters

key string

Returns

WriteBatchWithIndex

Delete(string, ColumnFamilyHandle)

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

public WriteBatchWithIndex Delete(string key, ColumnFamilyHandle cf)

Parameters

key string
cf ColumnFamilyHandle

Returns

WriteBatchWithIndex

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().

GetData()

Returns the serialized batch data as a byte array.

public byte[] GetData()

Returns

byte[]

GetFromBatch(DbOptions, ReadOnlySpan<byte>)

Reads a key from this batch alone, ignoring the database, or returns null if the batch does not mention it.

public byte[]? GetFromBatch(DbOptions options, ReadOnlySpan<byte> key)

Parameters

options DbOptions

The database options, not read options. RocksDb needs them for the merge operator, so that a merge queued in this batch can be resolved.

key ReadOnlySpan<byte>

The key to read.

Returns

byte[]

Remarks

Throws when the batch holds a merge for this key that cannot be resolved on its own, for instance because there is no base value here and no merge operator configured. Use GetFromBatchAndDb(RocksDb, ReadOnlySpan<byte>, ReadOptions?) when the base value is in the database.

GetFromBatch(DbOptions, ReadOnlySpan<byte>, ColumnFamilyHandle)

Reads a key from this batch alone, ignoring the database, or returns null if the batch does not mention it.

public byte[]? GetFromBatch(DbOptions options, ReadOnlySpan<byte> key, ColumnFamilyHandle cf)

Parameters

options DbOptions

The database options, not read options. RocksDb needs them for the merge operator, so that a merge queued in this batch can be resolved.

key ReadOnlySpan<byte>

The key to read.

cf ColumnFamilyHandle

Returns

byte[]

Remarks

Throws when the batch holds a merge for this key that cannot be resolved on its own, for instance because there is no base value here and no merge operator configured. Use GetFromBatchAndDb(RocksDb, ReadOnlySpan<byte>, ReadOptions?) when the base value is in the database.

GetFromBatchAndDb(RocksDb, ReadOnlySpan<byte>, ColumnFamilyHandle, ReadOptions?)

Reads a key, seeing this batch's queued writes on top of what is already in db, or returns null if neither has it.

public byte[]? GetFromBatchAndDb(RocksDb db, ReadOnlySpan<byte> key, ColumnFamilyHandle cf, ReadOptions? options = null)

Parameters

db RocksDb
key ReadOnlySpan<byte>
cf ColumnFamilyHandle
options ReadOptions

Returns

byte[]

Remarks

This is read-your-own-writes, and the reason to choose an indexed batch over a plain WriteBatch. A queued put shadows the stored value, a queued delete hides it, and a queued merge is resolved against it using the database's merge operator.

A snapshot in options applies to the database side only. This batch's own writes are always visible, since they are not in the database yet.

GetFromBatchAndDb(RocksDb, ReadOnlySpan<byte>, ReadOptions?)

Reads a key, seeing this batch's queued writes on top of what is already in db, or returns null if neither has it.

public byte[]? GetFromBatchAndDb(RocksDb db, ReadOnlySpan<byte> key, ReadOptions? options = null)

Parameters

db RocksDb
key ReadOnlySpan<byte>
options ReadOptions

Returns

byte[]

Remarks

This is read-your-own-writes, and the reason to choose an indexed batch over a plain WriteBatch. A queued put shadows the stored value, a queued delete hides it, and a queued merge is resolved against it using the database's merge operator.

A snapshot in options applies to the database side only. This batch's own writes are always visible, since they are not in the database yet.

GetStringFromBatchAndDb(RocksDb, string, ReadOptions?)

Reads a UTF-8 key from this batch and the database.

public string? GetStringFromBatchAndDb(RocksDb db, string key, ReadOptions? options = null)

Parameters

db RocksDb
key string
options ReadOptions

Returns

string

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

Queues a Merge into the default column family.

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

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>

Returns

WriteBatchWithIndex

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

Queues a Merge into the specified column family.

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

Parameters

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

Returns

WriteBatchWithIndex

NewIteratorWithBase(RocksDb, ColumnFamilyHandle, ReadOptions?)

Creates an iterator over db with this batch's queued writes overlaid.

public Iterator NewIteratorWithBase(RocksDb db, ColumnFamilyHandle cf, ReadOptions? options = null)

Parameters

db RocksDb
cf ColumnFamilyHandle
options ReadOptions

Returns

Iterator

Remarks

The iterator merges the two sources, so it shows what the data would look like if the batch were applied. Queued puts shadow stored values, and queued deletes hide them.

Do not modify the batch while the iterator is positioned; RocksDb invalidates the current key and value. Dispose the iterator before both the batch and the database.

NewIteratorWithBase(RocksDb, ReadOptions?)

Creates an iterator over db with this batch's queued writes overlaid.

public Iterator NewIteratorWithBase(RocksDb db, ReadOptions? options = null)

Parameters

db RocksDb
options ReadOptions

Returns

Iterator

Remarks

The iterator merges the two sources, so it shows what the data would look like if the batch were applied. Queued puts shadow stored values, and queued deletes hide them.

Do not modify the batch while the iterator is positioned; RocksDb invalidates the current key and value. Dispose the iterator before both the batch and the database.

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

Queues a Put into the default column family.

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

Parameters

key ReadOnlySpan<byte>
value ReadOnlySpan<byte>

Returns

WriteBatchWithIndex

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

Queues a Put into the specified column family.

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

Parameters

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

Returns

WriteBatchWithIndex

Put(string, string)

Convenience overload using UTF-8 string key and value.

public WriteBatchWithIndex Put(string key, string value)

Parameters

key string
value string

Returns

WriteBatchWithIndex

Put(string, string, ColumnFamilyHandle)

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

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

Parameters

key string
value string
cf ColumnFamilyHandle

Returns

WriteBatchWithIndex

PutLogData(ReadOnlySpan<byte>)

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

public WriteBatchWithIndex PutLogData(ReadOnlySpan<byte> blob)

Parameters

blob ReadOnlySpan<byte>

Returns

WriteBatchWithIndex

RollbackToSavePoint()

Rolls back to the most recent save point.

public WriteBatchWithIndex RollbackToSavePoint()

Returns

WriteBatchWithIndex

SetSavePoint()

Marks a save point inside the batch.

public WriteBatchWithIndex SetSavePoint()

Returns

WriteBatchWithIndex

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 WriteBatchWithIndex SingleDelete(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

Returns

WriteBatchWithIndex

SingleDelete(ReadOnlySpan<byte>, ColumnFamilyHandle)

Queues a SingleDelete in the specified column family.

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

Parameters

key ReadOnlySpan<byte>
cf ColumnFamilyHandle

Returns

WriteBatchWithIndex