Skip to content

Commit

Permalink
#2146 Add check for Next/Prev length for IndexNode when searching for…
Browse files Browse the repository at this point in the history
… index in IndexService.Find
  • Loading branch information
AndreyDeev committed Dec 14, 2023
1 parent 262415a commit c2bc987
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion LiteDB/Engine/Services/IndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public IndexNode Find(CollectionIndex index, BsonValue value, bool sibling, int

for (int i = index.MaxLevel - 1; i >= 0; i--)
{
for (; cur.GetNextPrev((byte)i, order).IsEmpty == false; cur = this.GetNode(cur.GetNextPrev((byte)i, order)))
for (; NextPrevContaintsData(cur, (byte)i, order); cur = this.GetNode(cur.GetNextPrev((byte)i, order)))
{
var next = this.GetNode(cur.GetNextPrev((byte)i, order));
var diff = next.Key.CompareTo(value, _collation);
Expand All @@ -368,6 +368,21 @@ public IndexNode Find(CollectionIndex index, BsonValue value, bool sibling, int
}

return null;

bool NextPrevContaintsData(IndexNode current, byte level, int orderValue)
{
var nextPrevLength = orderValue == Query.Ascending
? current.Next.Length
: current.Prev.Length;

// TODO need to check why some index node stored at this level does not have expected Next and Prev items
if (nextPrevLength <= level)
{
return false;
}

return current.GetNextPrev(level, order).IsEmpty == false;
}
}

#endregion
Expand Down

0 comments on commit c2bc987

Please sign in to comment.