Skip to content

Commit

Permalink
Return key/value for cursor First, Next, Last, Previous.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyKaylor committed Jul 9, 2024
1 parent 84385ac commit e084fa0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
29 changes: 12 additions & 17 deletions src/LightningDB.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ public void CursorShouldMoveToLast()
var keys = PopulateCursorValues(c);
var lastKey = keys.Last();
var result = c.Last();
Assert.Equal(MDBResultCode.Success, result);
var current = c.GetCurrent();
Assert.Equal(lastKey, current.key.CopyToNewArray());
Assert.Equal(MDBResultCode.Success, result.resultCode);
Assert.Equal(lastKey, result.key.CopyToNewArray());
});
}

Expand All @@ -101,9 +100,8 @@ public void CursorShouldMoveToFirst()
var keys = PopulateCursorValues(c);
var firstKey = keys.First();
var result = c.First();
Assert.Equal(MDBResultCode.Success, result);
var current = c.GetCurrent();
Assert.Equal(firstKey, current.key.CopyToNewArray());
Assert.Equal(MDBResultCode.Success, result.resultCode);
Assert.Equal(firstKey, result.key.CopyToNewArray());
});
}

Expand Down Expand Up @@ -247,7 +245,7 @@ public void ShouldMoveToPrevious()
var expectedSpan = expected.AsSpan();
c.GetBoth(expectedSpan, expectedSpan);
var result = c.Previous();
Assert.Equal(MDBResultCode.Success, result);
Assert.Equal(MDBResultCode.Success, result.resultCode);
});
}

Expand Down Expand Up @@ -302,10 +300,9 @@ public void ShouldMoveToFirstDuplicate()
var values = PopulateMultipleCursorValues(c);
var result = c.GetBothRange(key, values[1]);
Assert.Equal(MDBResultCode.Success, result);
result = c.FirstDuplicate();
Assert.Equal(MDBResultCode.Success, result);
var current = c.GetCurrent();
Assert.Equal(values[0], current.value.CopyToNewArray());
var dupResult = c.FirstDuplicate();
Assert.Equal(MDBResultCode.Success, dupResult.resultCode);
Assert.Equal(values[0], dupResult.value.CopyToNewArray());
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
}

Expand All @@ -318,9 +315,8 @@ public void ShouldMoveToLastDuplicate()
var values = PopulateMultipleCursorValues(c);
c.Set(key);
var result = c.LastDuplicate();
Assert.Equal(MDBResultCode.Success, result);
var current = c.GetCurrent();
Assert.Equal(values[4], current.value.CopyToNewArray());
Assert.Equal(MDBResultCode.Success, result.resultCode);
Assert.Equal(values[4], result.value.CopyToNewArray());
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
}

Expand All @@ -331,9 +327,8 @@ public void ShouldMoveToNextNoDuplicate()
{
var values = PopulateMultipleCursorValues(c);
var result = c.NextNoDuplicate();
Assert.Equal(MDBResultCode.Success, result);
var current = c.GetCurrent();
Assert.Equal(values[0], current.value.CopyToNewArray());
Assert.Equal(MDBResultCode.Success, result.resultCode);
Assert.Equal(values[0], result.value.CopyToNewArray());
}, DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create);
}

Expand Down
10 changes: 6 additions & 4 deletions src/LightningDB.Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ public void TransactionShouldSupportCustomComparer()
using (var c = txn.CreateCursor(db))
{
var order = 0;
while (c.Next() == MDBResultCode.Success)
Assert.Equal(keysSorted[order++], BitConverter.ToInt32(c.GetCurrent().key.CopyToNewArray(), 0));
(MDBResultCode, MDBValue, MDBValue) result;
while ((result = c.Next()).Item1 == MDBResultCode.Success)
Assert.Equal(keysSorted[order++], BitConverter.ToInt32(result.Item2.CopyToNewArray(), 0));
}
}

Expand All @@ -217,8 +218,9 @@ public void TransactionShouldSupportCustomDupSorter()
{
var order = 0;

while (c.Next() == MDBResultCode.Success)
Assert.Equal(valuesSorted[order++], BitConverter.ToInt32(c.GetCurrent().value.CopyToNewArray(), 0));
(MDBResultCode, MDBValue, MDBValue) result;
while ((result = c.Next()).Item1 == MDBResultCode.Success)
Assert.Equal(valuesSorted[order++], BitConverter.ToInt32(result.Item3.CopyToNewArray(), 0));
}
}
}
60 changes: 30 additions & 30 deletions src/LightningDB/LightningCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,37 +153,37 @@ public MDBResultCode SetRange(ReadOnlySpan<byte> key)
/// <summary>
/// Position at first key/data item
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode First()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) First()
{
return Get(CursorOperation.First).resultCode;
return Get(CursorOperation.First);
}

/// <summary>
/// Position at first data item of current key. Only for MDB_DUPSORT
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode FirstDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) FirstDuplicate()
{
return Get(CursorOperation.FirstDuplicate).resultCode;
return Get(CursorOperation.FirstDuplicate);
}

/// <summary>
/// Position at last key/data item
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode Last()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) Last()
{
return Get(CursorOperation.Last).resultCode;
return Get(CursorOperation.Last);
}

/// <summary>
/// Position at last data item of current key. Only for MDB_DUPSORT
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode LastDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) LastDuplicate()
{
return Get(CursorOperation.LastDuplicate).resultCode;
return Get(CursorOperation.LastDuplicate);
}

/// <summary>
Expand All @@ -198,28 +198,28 @@ public MDBResultCode LastDuplicate()
/// <summary>
/// Position at next data item
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode Next()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) Next()
{
return Get(CursorOperation.Next).resultCode;
return Get(CursorOperation.Next);
}

/// <summary>
/// Position at next data item of current key. Only for MDB_DUPSORT
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode NextDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) NextDuplicate()
{
return Get(CursorOperation.NextDuplicate).resultCode;
return Get(CursorOperation.NextDuplicate);
}

/// <summary>
/// Position at first data item of next key. Only for MDB_DUPSORT.
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode NextNoDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) NextNoDuplicate()
{
return Get(CursorOperation.NextNoDuplicate).resultCode;
return Get(CursorOperation.NextNoDuplicate);
}

/// <summary>
Expand All @@ -235,28 +235,28 @@ public MDBResultCode NextNoDuplicate()
/// <summary>
/// Position at previous data item.
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode Previous()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) Previous()
{
return Get(CursorOperation.Previous).resultCode;
return Get(CursorOperation.Previous);
}

/// <summary>
/// Position at previous data item of current key. Only for MDB_DUPSORT.
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode PreviousDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) PreviousDuplicate()
{
return Get(CursorOperation.PreviousDuplicate).resultCode;
return Get(CursorOperation.PreviousDuplicate);
}

/// <summary>
/// Position at last data item of previous key. Only for MDB_DUPSORT.
/// </summary>
/// <returns>Returns <see cref="MDBResultCode"/></returns>
public MDBResultCode PreviousNoDuplicate()
/// <returns>Returns <see cref="MDBResultCode"/>, and <see cref="MDBValue"/> key/value</returns>
public (MDBResultCode resultCode, MDBValue key, MDBValue value) PreviousNoDuplicate()
{
return Get(CursorOperation.PreviousNoDuplicate).resultCode;
return Get(CursorOperation.PreviousNoDuplicate);
}

private (MDBResultCode resultCode, MDBValue key, MDBValue value) Get(CursorOperation operation)
Expand Down
8 changes: 4 additions & 4 deletions src/LightningDB/LightningExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ private static string mdb_strerror(int err)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<ValueTuple<MDBValue, MDBValue>> AsEnumerable(this LightningCursor cursor)
{
while(cursor.Next() == MDBResultCode.Success)
(MDBResultCode, MDBValue, MDBValue) result;
while((result = cursor.Next()).Item1 == MDBResultCode.Success)
{
var (resultCode, key, value) = cursor.GetCurrent();
resultCode.ThrowOnError();
yield return (key, value);
yield return (result.Item2, result.Item3);
}
result.Item1.ThrowOnReadError();
}

/// <summary>
Expand Down

0 comments on commit e084fa0

Please sign in to comment.