Skip to content

Commit

Permalink
Renamed methods to be closer to the native lib counterpart
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyKaylor committed Jul 29, 2020
1 parent 70c53ec commit 3528a88
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 54 deletions.
95 changes: 68 additions & 27 deletions src/LightningDB.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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();
}

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -186,82 +186,109 @@ 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());
});
}

[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());
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
}

[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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/LightningDB.Tests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/LightningDB.Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -236,13 +236,13 @@ public void TransactionShouldSupportCustomDupSorter()
Array.Sort(valuesSorted, new Comparison<int>(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));
}
}
Expand Down
Loading

0 comments on commit 3528a88

Please sign in to comment.