Class MergeOperator
- Namespace
- RocksDbNet
- Assembly
- RocksDb.Net.dll
User-defined merge operator that enables read-modify-write semantics
on values stored in RocksDb. Override FullMerge (and
optionally PartialMerge) to implement custom merge logic, in either
the array form or the span form, which reads the operands in place through
MergeOperands and merges without allocating.
public abstract class MergeOperator : RocksDbHandle, IDisposable
- Inheritance
-
MergeOperator
- Implements
- Inherited Members
Remarks
A merge operator is used with Merge(string, string, WriteOptions?) and similar overloads to combine new values with existing ones without a separate read step. Common use cases include counters, lists, and append-only logs.
Register a merge operator via MergeOperator or use SetUInt64AddMergeOperator() for the built-in 64-bit addition operator.
Constructors
MergeOperator(string)
Creates a merge operator with the given name.
protected MergeOperator(string name)
Parameters
namestringIdentifies this operator in RocksDb's logs and options output. Unlike a comparator name it is not enforced on reopen, so a mismatch will not be caught for you: opening a database with a different merge operator than the one that wrote its operands silently produces wrong merges.
Methods
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().
FullMerge(ReadOnlySpan<byte>, bool, ReadOnlySpan<byte>, MergeOperands, IBufferWriter<byte>)
Merges all accumulated operands with the existing value for a key, without copying either.
public virtual bool FullMerge(ReadOnlySpan<byte> key, bool hasExistingValue, ReadOnlySpan<byte> existingValue, MergeOperands operands, IBufferWriter<byte> newValue)
Parameters
keyReadOnlySpan<byte>The key being merged. Valid for the duration of the call.
hasExistingValuebooltrueif the key has a pre-existing value.existingValueReadOnlySpan<byte>The current value, valid only when
hasExistingValueistrue.operandsMergeOperandsThe operands, oldest first, read in place. See MergeOperands.
newValueIBufferWriter<byte>Where to write the result. Valid only for the duration of the call: do not keep it.
Returns
- bool
trueif the merge succeeded;falseto signal failure.
Remarks
The default copies the operands into arrays and calls the array form, then writes its result. Override this instead to merge without allocating.
FullMerge(ReadOnlySpan<byte>, bool, ReadOnlySpan<byte>, IReadOnlyList<byte[]>, out byte[]?)
Called to merge all accumulated operands with the existing value for a key.
public virtual bool FullMerge(ReadOnlySpan<byte> key, bool hasExistingValue, ReadOnlySpan<byte> existingValue, IReadOnlyList<byte[]> operands, out byte[]? newValue)
Parameters
keyReadOnlySpan<byte>The key being merged.
hasExistingValuebooltrueif the key has a pre-existing value.existingValueReadOnlySpan<byte>The current value (valid only when
hasExistingValueistrue).operandsIReadOnlyList<byte[]>The operands to merge, in chronological order. Managed copies, so they may be kept beyond the call.
newValuebyte[]Output: the result of the merge.
Returns
- bool
trueif the merge succeeded;falseto signal failure.
Exceptions
- NotSupportedException
Neither form of
FullMergeis overridden. Reported through UnhandledException, and the merge fails.
PartialMerge(ReadOnlySpan<byte>, MergeOperands, IBufferWriter<byte>)
Optional partial merge, without copying the operands or the result.
public virtual bool PartialMerge(ReadOnlySpan<byte> key, MergeOperands operands, IBufferWriter<byte> newValue)
Parameters
keyReadOnlySpan<byte>The key being merged. Valid for the duration of the call.
operandsMergeOperandsThe operands to combine, oldest first, read in place.
newValueIBufferWriter<byte>Where to write the combined operand. Valid only for the duration of the call.
Returns
Remarks
The default calls the array form, as for FullMerge.
PartialMerge(ReadOnlySpan<byte>, IReadOnlyList<byte[]>, out byte[]?)
Optional partial merge: combines a subset of operands before a full
merge. Return false to fall back to FullMerge.
public virtual bool PartialMerge(ReadOnlySpan<byte> key, IReadOnlyList<byte[]> operands, out byte[]? newValue)
Parameters
keyReadOnlySpan<byte>The key being merged.
operandsIReadOnlyList<byte[]>The operands to combine, in chronological order. Managed copies, so they may be kept beyond the call.
newValuebyte[]Output: the combined operand.