From 3528a884bf0a421d7d65df92fdbbd89badcacac2 Mon Sep 17 00:00:00 2001 From: Corey Kaylor Date: Wed, 29 Jul 2020 10:22:09 -0700 Subject: [PATCH] Renamed methods to be closer to the native lib counterpart --- src/LightningDB.Tests/CursorTests.cs | 95 ++++++++++++++++------- src/LightningDB.Tests/DatabaseTests.cs | 2 +- src/LightningDB.Tests/TransactionTests.cs | 6 +- src/LightningDB/LightningCursor.cs | 44 +++++------ src/LightningDB/LightningExtensions.cs | 2 +- 5 files changed, 95 insertions(+), 54 deletions(-) diff --git a/src/LightningDB.Tests/CursorTests.cs b/src/LightningDB.Tests/CursorTests.cs index e70b2b2..1018185 100644 --- a/src/LightningDB.Tests/CursorTests.cs +++ b/src/LightningDB.Tests/CursorTests.cs @@ -40,7 +40,7 @@ private static byte[][] PopulateCursorValues(LightningCursor cursor, int count = private static byte[][] PopulateMultipleCursorValues(LightningCursor cursor, string key = "TestKey") { var values = Enumerable.Range(1, 5).Select(BitConverter.GetBytes).ToArray(); - var result = cursor.PutMultiple(UTF8.GetBytes(key), values); + var result = cursor.Put(UTF8.GetBytes(key), values); Assert.Equal(MDBResultCode.Success, result); var notDuplicate = values[0]; result = cursor.Put(notDuplicate, notDuplicate, CursorPutOptions.NoDuplicateData); @@ -68,13 +68,13 @@ public void CursorShouldPutValues() } [Fact] - public void CursorShouldMoveToSpanKey() + public void CursorShouldSetSpanKey() { _env.RunCursorScenario((tx, db, c) => { var keys = PopulateCursorValues(c); var firstKey = keys.First(); - var result = c.MoveTo(firstKey.AsSpan()); + var result = c.Set(firstKey.AsSpan()); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(firstKey, current.key.CopyToNewArray()); @@ -88,7 +88,7 @@ public void CursorShouldMoveToLast() { var keys = PopulateCursorValues(c); var lastKey = keys.Last(); - var result = c.MoveToLast(); + var result = c.Last(); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(lastKey, current.key.CopyToNewArray()); @@ -102,7 +102,7 @@ public void CursorShouldMoveToFirst() { var keys = PopulateCursorValues(c); var firstKey = keys.First(); - var result = c.MoveToFirst(); + var result = c.First(); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(firstKey, current.key.CopyToNewArray()); @@ -134,7 +134,7 @@ public void CursorShouldDeleteElements() var keys = PopulateCursorValues(c).Take(2).ToArray(); for (var i = 0; i < 2; ++i) { - c.MoveNext(); + c.Next(); c.Delete(); } @@ -158,8 +158,8 @@ public void ShouldGetMultiple() { var key = UTF8.GetBytes("TestKey"); var keys = PopulateMultipleCursorValues(c); - c.MoveTo(key); - c.MoveNextDuplicate(); + c.Set(key); + c.NextDuplicate(); var (resultCode, _, value) = c.GetMultiple(); Assert.Equal(MDBResultCode.Success, resultCode); Assert.Equal(keys, value.CopyToNewArray().Split(sizeof(int)).ToArray()); @@ -173,7 +173,7 @@ public void ShouldGetNextMultiple() { var key = UTF8.GetBytes("TestKey"); var keys = PopulateMultipleCursorValues(c); - c.MoveTo(key); + c.Set(key); var (resultCode, _, value) = c.NextMultiple(); Assert.Equal(MDBResultCode.Success, resultCode); Assert.Equal(keys, value.CopyToNewArray().Split(sizeof(int)).ToArray()); @@ -186,7 +186,7 @@ public void ShouldAdvanceKeyToClosestWhenKeyNotFound() _env.RunCursorScenario((tx, db, c) => { var expected = PopulateCursorValues(c).First(); - var result = c.MoveTo(UTF8.GetBytes("key")); + var result = c.Set(UTF8.GetBytes("key")); Assert.Equal(MDBResultCode.NotFound, result); var (_, key, _) = c.GetCurrent(); Assert.Equal(expected, key.CopyToNewArray()); @@ -194,60 +194,87 @@ public void ShouldAdvanceKeyToClosestWhenKeyNotFound() } [Fact] - public void ShouldMoveToAndGet() + public void ShouldSetKeyAndGet() { _env.RunCursorScenario((tx, db, c) => { var expected = PopulateCursorValues(c).ElementAt(2); - var result = c.MoveToAndGet(expected); + var result = c.SetKey(expected); Assert.Equal(MDBResultCode.Success, result.resultCode); Assert.Equal(expected, result.key.CopyToNewArray()); }); } [Fact] - public void ShouldMoveToAndGetWithSpan() + public void ShouldSetKeyAndGetWithSpan() { _env.RunCursorScenario((tx, db, c) => { var expected = PopulateCursorValues(c).ElementAt(2); - var result = c.MoveToAndGet(expected.AsSpan()); + var result = c.SetKey(expected.AsSpan()); Assert.Equal(MDBResultCode.Success, result.resultCode); Assert.Equal(expected, result.key.CopyToNewArray()); }); } [Fact] - public void ShouldMoveTo() + public void ShouldGetBoth() { _env.RunCursorScenario((tx, db, c) => { var expected = PopulateCursorValues(c).ElementAt(2); - var result = c.MoveTo(expected, expected); + var result = c.GetBoth(expected, expected); Assert.Equal(MDBResultCode.Success, result); }, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create); } [Fact] - public void ShouldMoveToWithSpan() + public void ShouldGetBothWithSpan() { _env.RunCursorScenario((tx, db, c) => { var expected = PopulateCursorValues(c).ElementAt(2); var expectedSpan = expected.AsSpan(); - var result = c.MoveTo(expectedSpan, expectedSpan); + var result = c.GetBoth(expectedSpan, expectedSpan); Assert.Equal(MDBResultCode.Success, result); }, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create); } [Fact] - public void ShouldMoveToFirstValueAfter() + public void ShouldMoveToPrevious() + { + _env.RunCursorScenario((tx, db, c) => + { + var expected = PopulateCursorValues(c).ElementAt(2); + var expectedSpan = expected.AsSpan(); + c.GetBoth(expectedSpan, expectedSpan); + var result = c.Previous(); + Assert.Equal(MDBResultCode.Success, result); + }); + } + + [Fact] + public void ShouldSetRangeWithSpan() + { + _env.RunCursorScenario((tx, db, c) => + { + var values = PopulateCursorValues(c); + var firstAfter = values[0].AsSpan(); + var result = c.SetRange(firstAfter); + Assert.Equal(MDBResultCode.Success, result); + var current = c.GetCurrent(); + Assert.Equal(values[0], current.value.CopyToNewArray()); + }); + } + + [Fact] + public void ShouldGetBothRange() { _env.RunCursorScenario((tx, db, c) => { var key = UTF8.GetBytes("TestKey"); var values = PopulateMultipleCursorValues(c); - var result = c.MoveToFirstValueAfter(key, values[1]); + var result = c.GetBothRange(key, values[1]); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(values[1], current.value.CopyToNewArray()); @@ -255,13 +282,13 @@ public void ShouldMoveToFirstValueAfter() } [Fact] - public void ShouldMoveToFirstValueAfterWithSpan() + public void ShouldGetBothRangeWithSpan() { _env.RunCursorScenario((tx, db, c) => { var key = UTF8.GetBytes("TestKey").AsSpan(); var values = PopulateMultipleCursorValues(c); - var result = c.MoveToFirstValueAfter(key, values[1].AsSpan()); + var result = c.GetBothRange(key, values[1].AsSpan()); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(values[1], current.value.CopyToNewArray()); @@ -275,9 +302,9 @@ public void ShouldMoveToFirstDuplicate() { var key = UTF8.GetBytes("TestKey"); var values = PopulateMultipleCursorValues(c); - var result = c.MoveToFirstValueAfter(key, values[1]); + var result = c.GetBothRange(key, values[1]); Assert.Equal(MDBResultCode.Success, result); - result = c.MoveToFirstDuplicate(); + result = c.FirstDuplicate(); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(values[0], current.value.CopyToNewArray()); @@ -291,8 +318,8 @@ public void ShouldMoveToLastDuplicate() { var key = UTF8.GetBytes("TestKey"); var values = PopulateMultipleCursorValues(c); - c.MoveTo(key); - var result = c.MoveToLastDuplicate(); + c.Set(key); + var result = c.LastDuplicate(); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(values[4], current.value.CopyToNewArray()); @@ -305,7 +332,7 @@ public void ShouldMoveToNextNoDuplicate() _env.RunCursorScenario((tx, db, c) => { var values = PopulateMultipleCursorValues(c); - var result = c.MoveNextNoDuplicate(); + var result = c.NextNoDuplicate(); Assert.Equal(MDBResultCode.Success, result); var current = c.GetCurrent(); Assert.Equal(values[0], current.value.CopyToNewArray()); @@ -321,5 +348,19 @@ public void ShouldRenewSameTransaction() Assert.Equal(MDBResultCode.Success, result); }, transactionFlags: TransactionBeginFlags.ReadOnly); } + + [Fact] + public void ShouldDeleteDuplicates() + { + _env.RunCursorScenario((tx, db, c) => + { + var key = UTF8.GetBytes("TestKey"); + PopulateMultipleCursorValues(c); + c.Set(key); + c.DeleteDuplicateData(); + var result = c.Set(key); + Assert.Equal(MDBResultCode.NotFound, result); + }, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create); + } } } \ No newline at end of file diff --git a/src/LightningDB.Tests/DatabaseTests.cs b/src/LightningDB.Tests/DatabaseTests.cs index ebfbb6c..43d95cf 100644 --- a/src/LightningDB.Tests/DatabaseTests.cs +++ b/src/LightningDB.Tests/DatabaseTests.cs @@ -88,7 +88,7 @@ public void NamedDatabaseNameExistsInMaster() var db = tx.OpenDatabase(); using (var cursor = tx.CreateCursor(db)) { - cursor.MoveNext(); + cursor.Next(); Assert.Equal("customdb", UTF8.GetString(cursor.GetCurrent().key.CopyToNewArray())); } } diff --git a/src/LightningDB.Tests/TransactionTests.cs b/src/LightningDB.Tests/TransactionTests.cs index 08332d9..dc400cd 100644 --- a/src/LightningDB.Tests/TransactionTests.cs +++ b/src/LightningDB.Tests/TransactionTests.cs @@ -215,7 +215,7 @@ public void TransactionShouldSupportCustomComparer() using (var c = txn.CreateCursor(db)) { int order = 0; - while (c.MoveNext() == MDBResultCode.Success) + while (c.Next() == MDBResultCode.Success) Assert.Equal(keysSorted[order++], BitConverter.ToInt32(c.GetCurrent().key.CopyToNewArray(), 0)); } } @@ -236,13 +236,13 @@ public void TransactionShouldSupportCustomDupSorter() Array.Sort(valuesSorted, new Comparison(comparison)); using (var c = txn.CreateCursor(db)) - c.PutMultiple(BitConverter.GetBytes(123), valuesUnsorted.Select(BitConverter.GetBytes).ToArray()); + c.Put(BitConverter.GetBytes(123), valuesUnsorted.Select(BitConverter.GetBytes).ToArray()); using (var c = txn.CreateCursor(db)) { int order = 0; - while (c.MoveNext() == MDBResultCode.Success) + while (c.Next() == MDBResultCode.Success) Assert.Equal(valuesSorted[order++], BitConverter.ToInt32(c.GetCurrent().value.CopyToNewArray(), 0)); } } diff --git a/src/LightningDB/LightningCursor.cs b/src/LightningDB/LightningCursor.cs index 07b27a1..7694f18 100644 --- a/src/LightningDB/LightningCursor.cs +++ b/src/LightningDB/LightningCursor.cs @@ -50,7 +50,7 @@ public IntPtr Handle() /// /// Key /// Returns - public MDBResultCode MoveTo(byte[] key) + public MDBResultCode Set(byte[] key) { return Get(CursorOperation.Set, key).resultCode; } @@ -60,7 +60,7 @@ public MDBResultCode MoveTo(byte[] key) /// /// Key /// Returns - public MDBResultCode MoveTo(ReadOnlySpan key) + public MDBResultCode Set(ReadOnlySpan key) { return Get(CursorOperation.Set, key).resultCode; } @@ -70,7 +70,7 @@ public MDBResultCode MoveTo(ReadOnlySpan key) /// /// Key /// Returns , and key/value - public (MDBResultCode resultCode, MDBValue key, MDBValue value) MoveToAndGet(byte[] key) + public (MDBResultCode resultCode, MDBValue key, MDBValue value) SetKey(byte[] key) { return Get(CursorOperation.SetKey, key); } @@ -80,7 +80,7 @@ public MDBResultCode MoveTo(ReadOnlySpan key) /// /// Key /// Returns , and key/value - public (MDBResultCode resultCode, MDBValue key, MDBValue value) MoveToAndGet(ReadOnlySpan key) + public (MDBResultCode resultCode, MDBValue key, MDBValue value) SetKey(ReadOnlySpan key) { return Get(CursorOperation.SetKey, key); } @@ -91,7 +91,7 @@ public MDBResultCode MoveTo(ReadOnlySpan key) /// Key. /// Value /// Returns true if the key/value pair was found. - public MDBResultCode MoveTo(byte[] key, byte[] value) + public MDBResultCode GetBoth(byte[] key, byte[] value) { return Get(CursorOperation.GetBoth, key, value).resultCode; } @@ -102,7 +102,7 @@ public MDBResultCode MoveTo(byte[] key, byte[] value) /// Key. /// Value /// Returns - public MDBResultCode MoveTo(ReadOnlySpan key, ReadOnlySpan value) + public MDBResultCode GetBoth(ReadOnlySpan key, ReadOnlySpan value) { return Get(CursorOperation.GetBoth, key, value).resultCode; } @@ -113,7 +113,7 @@ public MDBResultCode MoveTo(ReadOnlySpan key, ReadOnlySpan value) /// Key /// Value /// Returns - public MDBResultCode MoveToFirstValueAfter(byte[] key, byte[] value) + public MDBResultCode GetBothRange(byte[] key, byte[] value) { return Get(CursorOperation.GetBothRange, key, value).resultCode; } @@ -124,7 +124,7 @@ public MDBResultCode MoveToFirstValueAfter(byte[] key, byte[] value) /// Key /// Value /// Returns - public MDBResultCode MoveToFirstValueAfter(ReadOnlySpan key, ReadOnlySpan value) + public MDBResultCode GetBothRange(ReadOnlySpan key, ReadOnlySpan value) { return Get(CursorOperation.GetBothRange, key, value).resultCode; } @@ -134,7 +134,7 @@ public MDBResultCode MoveToFirstValueAfter(ReadOnlySpan key, ReadOnlySpan< /// /// Key /// Returns - public MDBResultCode MoveToFirstAfter(byte[] key) + public MDBResultCode SetRange(byte[] key) { return Get(CursorOperation.SetRange, key).resultCode; } @@ -144,7 +144,7 @@ public MDBResultCode MoveToFirstAfter(byte[] key) /// /// Key /// Returns - public MDBResultCode MoveToFirstAfter(ReadOnlySpan key) + public MDBResultCode SetRange(ReadOnlySpan key) { return Get(CursorOperation.SetRange, key).resultCode; } @@ -153,7 +153,7 @@ public MDBResultCode MoveToFirstAfter(ReadOnlySpan key) /// Position at first key/data item /// /// Returns - public MDBResultCode MoveToFirst() + public MDBResultCode First() { return Get(CursorOperation.First).resultCode; } @@ -162,7 +162,7 @@ public MDBResultCode MoveToFirst() /// Position at first data item of current key. Only for MDB_DUPSORT /// /// Returns - public MDBResultCode MoveToFirstDuplicate() + public MDBResultCode FirstDuplicate() { return Get(CursorOperation.FirstDuplicate).resultCode; } @@ -171,7 +171,7 @@ public MDBResultCode MoveToFirstDuplicate() /// Position at last key/data item /// /// Returns - public MDBResultCode MoveToLast() + public MDBResultCode Last() { return Get(CursorOperation.Last).resultCode; } @@ -180,7 +180,7 @@ public MDBResultCode MoveToLast() /// Position at last data item of current key. Only for MDB_DUPSORT /// /// Returns - public MDBResultCode MoveToLastDuplicate() + public MDBResultCode LastDuplicate() { return Get(CursorOperation.LastDuplicate).resultCode; } @@ -198,7 +198,7 @@ public MDBResultCode MoveToLastDuplicate() /// Position at next data item /// /// Returns - public MDBResultCode MoveNext() + public MDBResultCode Next() { return Get(CursorOperation.Next).resultCode; } @@ -207,7 +207,7 @@ public MDBResultCode MoveNext() /// Position at next data item of current key. Only for MDB_DUPSORT /// /// Returns - public MDBResultCode MoveNextDuplicate() + public MDBResultCode NextDuplicate() { return Get(CursorOperation.NextDuplicate).resultCode; } @@ -216,7 +216,7 @@ public MDBResultCode MoveNextDuplicate() /// Position at first data item of next key. Only for MDB_DUPSORT. /// /// Returns - public MDBResultCode MoveNextNoDuplicate() + public MDBResultCode NextNoDuplicate() { return Get(CursorOperation.NextNoDuplicate).resultCode; } @@ -235,7 +235,7 @@ public MDBResultCode MoveNextNoDuplicate() /// Position at previous data item. /// /// Returns - public MDBResultCode MovePrev() + public MDBResultCode Previous() { return Get(CursorOperation.Previous).resultCode; } @@ -244,7 +244,7 @@ public MDBResultCode MovePrev() /// Position at previous data item of current key. Only for MDB_DUPSORT. /// /// Returns - public MDBResultCode MovePrevDuplicate() + public MDBResultCode PreviousDuplicate() { return Get(CursorOperation.PreviousDuplicate).resultCode; } @@ -253,7 +253,7 @@ public MDBResultCode MovePrevDuplicate() /// Position at last data item of previous key. Only for MDB_DUPSORT. /// /// Returns - public MDBResultCode MovePrevNoDuplicate() + public MDBResultCode PreviousNoDuplicate() { return Get(CursorOperation.PreviousNoDuplicate).resultCode; } @@ -364,7 +364,7 @@ public unsafe MDBResultCode Put(ReadOnlySpan key, ReadOnlySpan value /// The key operated on. /// The data items operated on. /// Returns - public unsafe MDBResultCode PutMultiple(byte[] key, byte[][] values) + public unsafe MDBResultCode Put(byte[] key, byte[][] values) { const int StackAllocateLimit = 256;//I just made up a number, this can be much more aggressive -arc @@ -455,7 +455,7 @@ private void Delete(CursorDeleteOption option) /// Delete current key/data pair. /// This function deletes the key/data range for which duplicates are found. /// - public void DeleteDuplicates() + public void DeleteDuplicateData() { Delete(CursorDeleteOption.NoDuplicateData); } diff --git a/src/LightningDB/LightningExtensions.cs b/src/LightningDB/LightningExtensions.cs index 3a2d21e..4d79505 100644 --- a/src/LightningDB/LightningExtensions.cs +++ b/src/LightningDB/LightningExtensions.cs @@ -44,7 +44,7 @@ private static string mdb_strerror(int err) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable> AsEnumerable(this LightningCursor cursor) { - while (cursor.MoveNext() == MDBResultCode.Success) + while (cursor.Next() == MDBResultCode.Success) { var current = cursor.GetCurrent(); yield return (current.key, current.value);