Skip to content

Commit 8ebf7f8

Browse files
committed
Updated readme for Comparers
1 parent 15018c5 commit 8ebf7f8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,40 @@ In this example:
155155
- Using a cursor, we iterate over all values associated with the key "fruit" by moving to the next duplicate entry and see the values retrieved are ordered.
156156
- Then we demonstrate doing the same thing with IEnumerable instead.
157157

158+
## Custom Key Ordering
159+
160+
LightningDB provides built-in, allocation-free comparers for custom key sorting and duplicate ordering. Use them with `CompareWith()` for keys or `FindDuplicatesWith()` for duplicate values:
161+
162+
```csharp
163+
var config = new DatabaseConfiguration
164+
{
165+
Flags = DatabaseOpenFlags.Create | DatabaseOpenFlags.DuplicatesSort
166+
};
167+
168+
// Sort keys as signed integers (negative values sort before positive)
169+
config.CompareWith(SignedIntegerComparer.Instance);
170+
171+
// Sort duplicate values in reverse order
172+
config.FindDuplicatesWith(ReverseBitwiseComparer.Instance);
173+
174+
using var db = tx.OpenDatabase(configuration: config);
175+
```
176+
177+
**Available comparers in `LightningDB.Comparers`:**
178+
179+
| Comparer | Description |
180+
|----------|-------------|
181+
| `BitwiseComparer` | Lexicographic byte comparison (default LMDB behavior) |
182+
| `ReverseBitwiseComparer` | Lexicographic descending |
183+
| `SignedIntegerComparer` | 4/8-byte signed integers with proper negative ordering |
184+
| `UnsignedIntegerComparer` | 4/8-byte unsigned integers |
185+
| `Utf8StringComparer` | Ordinal UTF-8 string comparison |
186+
| `LengthComparer` | Sort by length first, then content |
187+
| `LengthOnlyComparer` | Sort by length only |
188+
| `HashCodeComparer` | Hash-based comparison for large values |
189+
190+
Reverse variants are available for most comparers (e.g., `ReverseSignedIntegerComparer`).
191+
158192
## Additional Resources
159193

160194
For more detailed examples and advanced usage, refer to the unit tests in the [Lightning.NET](https://github.com/CoreyKaylor/Lightning.NET) repository.

0 commit comments

Comments
 (0)