Skip to content

Commit

Permalink
Fix issue #2129 - reload 'last' index node in case the IndexPage has …
Browse files Browse the repository at this point in the history
…gone through a defrag
  • Loading branch information
kcsombrio committed Jan 5, 2022
1 parent a930df1 commit 03b2579
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
49 changes: 49 additions & 0 deletions LiteDB.Tests/Issues/Issue2129_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace LiteDB.Tests.Issues
{
public class Issue2129_Tests
{
[Fact]
public void TestInsertAfterDeleteAll()
{
var db = new LiteDatabase(":memory:");
var col = db.GetCollection<SwapChance>(nameof(SwapChance));
col.EnsureIndex(x => x.Accounts1to2);
col.EnsureIndex(x => x.Accounts2to1);

col.InsertBulk(this.GenerateItems());
col.DeleteAll();
col.InsertBulk(this.GenerateItems());
}

private IEnumerable<SwapChance> GenerateItems()
{
var r = new Random();
int seq = 1;
return Enumerable.Range(0, 150).Select(x => new SwapChance
{
Rarity = "Uncommon",
Template1Id = r.Next(15023),
Template2Id = r.Next(142, 188645),
Accounts1to2 = Enumerable.Range(0, 8).Select(a => Guid.NewGuid().ToString().Substring(0, 10) + ".wam").ToList(),
Accounts2to1 = Enumerable.Range(0, 6).Select(a => Guid.NewGuid().ToString().Substring(0, 10) + ".wam").ToList(),
Sequence = seq++
});
}
}

public class SwapChance
{
public ObjectId Id { get; set; }
public int Sequence { get; set; } = 0;
public string Rarity { get; set; } = string.Empty;
public int Template1Id { get; set; }
public int Template2Id { get; set; }
public List<string> Accounts1to2 { get; set; } = new List<string>();
public List<string> Accounts2to1 { get; set; } = new List<string>();
}
}
2 changes: 2 additions & 0 deletions LiteDB/Engine/Services/IndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private IndexNode AddNode(CollectionIndex index, BsonValue key, PageAddress data
{
ENSURE(last.NextNode == PageAddress.Empty, "last index node must point to null");

// reload 'last' index node in case the IndexPage has gone through a defrag
last = this.GetNode(last.Position);
last.SetNextNode(node.Position);
}

Expand Down

0 comments on commit 03b2579

Please sign in to comment.