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
Properties
Count
Returns the number of operations in the batch.
public int Count { get; }
Property Value
Methods
Clear()
Clears all operations from the batch.
public WriteBatchWithIndex Clear()
Returns
Delete(ReadOnlySpan<byte>)
Queues a Delete from the default column family.
public WriteBatchWithIndex Delete(ReadOnlySpan<byte> key)
Parameters
keyReadOnlySpan<byte>
Returns
Delete(ReadOnlySpan<byte>, ColumnFamilyHandle)
Queues a Delete from the specified column family.
public WriteBatchWithIndex Delete(ReadOnlySpan<byte> key, ColumnFamilyHandle cf)
Parameters
keyReadOnlySpan<byte>cfColumnFamilyHandle
Returns
Delete(string)
Convenience overload using a UTF-8 string key.
public WriteBatchWithIndex Delete(string key)
Parameters
keystring
Returns
Delete(string, ColumnFamilyHandle)
Convenience overload using a UTF-8 string key in a column family.
public WriteBatchWithIndex Delete(string key, ColumnFamilyHandle cf)
Parameters
keystringcfColumnFamilyHandle
Returns
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
optionsDbOptionsThe database options, not read options. RocksDb needs them for the merge operator, so that a merge queued in this batch can be resolved.
keyReadOnlySpan<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
optionsDbOptionsThe database options, not read options. RocksDb needs them for the merge operator, so that a merge queued in this batch can be resolved.
keyReadOnlySpan<byte>The key to read.
cfColumnFamilyHandle
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
dbRocksDbkeyReadOnlySpan<byte>cfColumnFamilyHandleoptionsReadOptions
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
dbRocksDbkeyReadOnlySpan<byte>optionsReadOptions
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
dbRocksDbkeystringoptionsReadOptions
Returns
Merge(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Queues a Merge into the default column family.
public WriteBatchWithIndex Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value)
Parameters
keyReadOnlySpan<byte>valueReadOnlySpan<byte>
Returns
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
keyReadOnlySpan<byte>valueReadOnlySpan<byte>cfColumnFamilyHandle
Returns
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
dbRocksDbcfColumnFamilyHandleoptionsReadOptions
Returns
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
dbRocksDboptionsReadOptions
Returns
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
keyReadOnlySpan<byte>valueReadOnlySpan<byte>
Returns
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
keyReadOnlySpan<byte>valueReadOnlySpan<byte>cfColumnFamilyHandle
Returns
Put(string, string)
Convenience overload using UTF-8 string key and value.
public WriteBatchWithIndex Put(string key, string value)
Parameters
Returns
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
keystringvaluestringcfColumnFamilyHandle
Returns
PutLogData(ReadOnlySpan<byte>)
Inserts a blob of user-defined log data into the WAL.
public WriteBatchWithIndex PutLogData(ReadOnlySpan<byte> blob)
Parameters
blobReadOnlySpan<byte>
Returns
RollbackToSavePoint()
Rolls back to the most recent save point.
public WriteBatchWithIndex RollbackToSavePoint()
Returns
SetSavePoint()
Marks a save point inside the batch.
public WriteBatchWithIndex SetSavePoint()
Returns
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
keyReadOnlySpan<byte>
Returns
SingleDelete(ReadOnlySpan<byte>, ColumnFamilyHandle)
Queues a SingleDelete in the specified column family.
public WriteBatchWithIndex SingleDelete(ReadOnlySpan<byte> key, ColumnFamilyHandle cf)
Parameters
keyReadOnlySpan<byte>cfColumnFamilyHandle