Skip to content

Commit c6f579e

Browse files
committed
Fixed regression from span refactor
1 parent 7958629 commit c6f579e

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/LightningDB.Tests/CursorTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using LightningDB.Native;
56
using Xunit;
67
using static System.Text.Encoding;
@@ -225,5 +226,27 @@ public void ShouldMoveNextMultiple()
225226
Assert.Equal(original, result);
226227
}
227228
}
229+
230+
[Fact]
231+
public void ShouldAdvanceKeyToClosestWhenKeyNotFound()
232+
{
233+
var keys = new List<string>
234+
{
235+
"one",
236+
"two/1",
237+
"two/2",
238+
"two/3"
239+
};
240+
_db = _txn.OpenDatabase(configuration: new DatabaseConfiguration { Flags = DatabaseOpenFlags.DuplicatesFixed | DatabaseOpenFlags.Create });
241+
foreach (var key in keys)
242+
{
243+
_txn.Put(_db, Encoding.UTF8.GetBytes(key), null);
244+
}
245+
246+
using var cursor = _txn.CreateCursor(_db);
247+
cursor.MoveTo(UTF8.GetBytes("two"));
248+
var result = cursor.GetCurrent().Key.CopyToNewArray();
249+
Assert.Equal("two/1", UTF8.GetString(result));
250+
}
228251
}
229252
}

src/LightningDB/LightningCursor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,15 @@ private bool Get(CursorOperation operation, byte[] key)
246246
private unsafe bool Get(CursorOperation operation, ReadOnlySpan<byte> key)
247247
{
248248
_currentValue = default;
249-
#warning should this update _currentKey?
250249
fixed (byte* keyPtr = key)
251250
{
252251
var findKey = new MDBValue(key.Length, keyPtr);
253-
return mdb_cursor_get(_handle, ref findKey, ref _currentValue, operation) == 0;
252+
var found = mdb_cursor_get(_handle, ref findKey, ref _currentValue, operation) == 0;
253+
if (found)
254+
{
255+
_currentKey = findKey;
256+
}
257+
return found;
254258
}
255259
}
256260

0 commit comments

Comments
 (0)