Table of Contents

Class Comparator

Namespace
RocksDbNet
Assembly
RocksDb.Net.dll

User-defined comparator for controlling the sort order of keys in a RocksDb database. Override Compare(ReadOnlySpan<byte>, ReadOnlySpan<byte>) to define custom key ordering.

public abstract class Comparator : RocksDbHandle, IDisposable
Inheritance
Comparator
Implements
Inherited Members

Remarks

Every database uses a comparator to determine the ordering of keys. The default comparator uses bytewise (lexicographic) ordering. To use a custom comparator, create a subclass and pass it to Comparator.

Important: Once a database has been created with a given comparator, every subsequent open must use a comparator with the same name and semantics.

Constructors

Comparator(string)

Creates a comparator with the given name.

protected Comparator(string name)

Parameters

name string

Identifies the ordering this comparator implements. RocksDb records it in the database and refuses to reopen with a comparator whose name differs, which is what stops data being read back in an order it was not written in. Change the name only when the ordering itself changes, and never reuse a name for different semantics.

Methods

Compare(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Compares two keys and returns their relative ordering.

public abstract int Compare(ReadOnlySpan<byte> keyA, ReadOnlySpan<byte> keyB)

Parameters

keyA ReadOnlySpan<byte>

The first key.

keyB ReadOnlySpan<byte>

The second key.

Returns

int

A negative value if keyA is less than keyB, zero if they are equal, or a positive value if keyA is greater than keyB.

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