Skip to content

Commit

Permalink
Fix SegmentInfos replace doesn't update userData
Browse files Browse the repository at this point in the history
  • Loading branch information
tohidemyname committed Jun 5, 2024
1 parent 38a7b53 commit 0316874
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/Lucene.Net.Tests/Index/TestIndexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,65 @@ public virtual void TestMaxThreadPriority()
}
}

private Dictionary<String, String> GetCommitData(IndexWriter writer)
{
Dictionary<String, String> data = new Dictionary<String, String>();
foreach (var ent in writer.CommitData)
{
data.Put(ent.Key, ent.Value);
}
return data;
}

[Test]
public void testGetCommitDataFromOldSnapshot()
{
Directory dir = NewDirectory();
IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMaxBufferedDocs(2).SetMergePolicy(NewLogMergePolicy());
conf.SetIndexDeletionPolicy(new SnapshotDeletionPolicy(NoDeletionPolicy.INSTANCE));
IndexWriter writer = new IndexWriter(dir, conf);
writer.SetCommitData(
new Dictionary<String, String>()
{
{ "key", "value" },
});
assertEquals("value", GetCommitData(writer).GetValueOrDefault("key"));
writer.Commit();
// Snapshot this commit to open later
IndexCommit indexCommit =
((SnapshotDeletionPolicy)writer.Config.IndexDeletionPolicy).Snapshot();
writer.Dispose();

// Modify the commit data and commit on close so the most recent commit data is different
conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMaxBufferedDocs(2).SetMergePolicy(NewLogMergePolicy());
conf.SetIndexDeletionPolicy(new SnapshotDeletionPolicy(NoDeletionPolicy.INSTANCE));
writer = new IndexWriter(dir, conf);
writer.SetCommitData(
new Dictionary<String, String>()
{
{"key", "value2" },
});

assertEquals("value2", GetCommitData(writer).GetValueOrDefault("key"));
writer.Dispose();

// validate that when opening writer from older snapshotted index commit, the old commit data is
// visible
conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMaxBufferedDocs(2).SetMergePolicy(NewLogMergePolicy());
conf.SetIndexDeletionPolicy(new SnapshotDeletionPolicy(NoDeletionPolicy.INSTANCE));
writer =
new IndexWriter(
dir,
conf
.SetOpenMode(OpenMode.APPEND)
.SetIndexCommit(indexCommit));
assertEquals("value", GetCommitData(writer).GetValueOrDefault("key"));
writer.Dispose();

dir.Dispose();
}


[Test]
public virtual void TestVariableSchema()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net/Index/SegmentInfos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,7 @@ internal void Replace(SegmentInfos other)
{
RollbackSegmentInfos(other.AsList());
lastGeneration = other.lastGeneration;
userData = other.userData;
}

/// <summary>
Expand Down Expand Up @@ -1632,4 +1633,4 @@ internal int IndexOf(SegmentCommitInfo si)
return segments.IndexOf(si);
}
}
}
}

0 comments on commit 0316874

Please sign in to comment.