Skip to content

Commit

Permalink
RavenDB-22964 Fix MoveNext for BackwardIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Lwiel committed Oct 8, 2024
1 parent 6ba5b09 commit 0f26be9
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Voron/Data/Lookups/Lookup.Iterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ public bool MoveNext(out long value)
return false;
}
ref var state = ref _cursor._stk[_cursor._pos];

// LastMatch equal to -1 is an indicator that we didn't find seeked key (or its successor doesn't exist)
if (state.LastMatch == -1)
{
value = default;
return false;
}

while (true)
{
Debug.Assert(state.Header->PageFlags.HasFlag(LookupPageFlags.Leaf));
Expand Down Expand Up @@ -407,6 +415,16 @@ public bool MoveNext<T>(out T key, out long value, out bool hasPreviousValue)
return false;
}
ref var state = ref _cursor._stk[_cursor._pos];

// LastMatch equal to -1 is an indicator that we didn't find seeked key (or its successor doesn't exist)
if (state.LastMatch == -1)
{
value = default;
key = default;
hasPreviousValue = false;
return false;
}

while (true)
{
Debug.Assert(state.Header->PageFlags.HasFlag(LookupPageFlags.Leaf));
Expand Down
115 changes: 115 additions & 0 deletions test/SlowTests/Issues/RavenDB-22964.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Linq;
using FastTests;
using Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;

namespace SlowTests.Issues;

public class RavenDB_22964 : RavenTestBase
{
public RavenDB_22964(ITestOutputHelper output) : base(output)
{
}

[RavenTheory(RavenTestCategory.Corax | RavenTestCategory.Querying)]
[RavenData(SearchEngineMode = RavenSearchEngineMode.Corax, DatabaseMode = RavenDatabaseMode.All)]
public void TestOrderByUsingBackwardIterator(Options options)
{
using (var store = GetDocumentStore(options))
{
using (var session = store.OpenSession())
{
var t1 = new TestObject() { Value = 1 };
var t2 = new TestObject() { Value = 3 };
var t3 = new TestObject() { Value = 4 };
var t4 = new TestObject() { Value = 5 };

session.Store(t1);
session.Store(t2);
session.Store(t3);
session.Store(t4);
session.SaveChanges();

long min = 2;
long max = 4;

var r1 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value < max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(1, r1.Count);
Assert.Equal(3, r1[0].Value);

var r2 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value <= max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(2, r2.Count);
Assert.Equal(4, r2[0].Value);
Assert.Equal(3, r2[1].Value);

var r3 = session.Query<TestObject>()
.Where(x => x.Value >= min && x.Value <= max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(2, r3.Count);
Assert.Equal(4, r3[0].Value);
Assert.Equal(3, r3[1].Value);

min = 0;

var r4 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value < max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(2, r4.Count);
Assert.Equal(3, r4[0].Value);
Assert.Equal(1, r4[1].Value);

max = 7;

var r5 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value < max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(4, r5.Count);
Assert.Equal(5, r5[0].Value);
Assert.Equal(4, r5[1].Value);
Assert.Equal(3, r5[2].Value);
Assert.Equal(1, r5[3].Value);

min = 10;
max = 15;

var r6 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value < max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(0, r6.Count);

min = -5;
max = -2;

var r7 = session.Query<TestObject>()
.Where(x => x.Value > min && x.Value < max)
.OrderByDescending(x => x.Value)
.ToList();

Assert.Equal(0, r7.Count);
}
}
}

public class TestObject
{
public string Id { get; set; }
public required long Value { get; set; }
}
}

0 comments on commit 0f26be9

Please sign in to comment.