From 03168741d8e8e0fa2916236f282c1d63115ed9bf Mon Sep 17 00:00:00 2001 From: tohidemyname Date: Wed, 5 Jun 2024 10:36:20 +0800 Subject: [PATCH] Fix SegmentInfos replace doesn't update userData https://github.com/apache/lucenenet/issues/947 --- src/Lucene.Net.Tests/Index/TestIndexWriter.cs | 59 +++++++++++++++++++ src/Lucene.Net/Index/SegmentInfos.cs | 3 +- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs index 8cf738904c..ed36930e87 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexWriter.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexWriter.cs @@ -816,6 +816,65 @@ public virtual void TestMaxThreadPriority() } } + private Dictionary GetCommitData(IndexWriter writer) + { + Dictionary data = new Dictionary(); + 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() + { + { "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() + { + {"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() { diff --git a/src/Lucene.Net/Index/SegmentInfos.cs b/src/Lucene.Net/Index/SegmentInfos.cs index c071399e63..792159648c 100644 --- a/src/Lucene.Net/Index/SegmentInfos.cs +++ b/src/Lucene.Net/Index/SegmentInfos.cs @@ -1452,6 +1452,7 @@ internal void Replace(SegmentInfos other) { RollbackSegmentInfos(other.AsList()); lastGeneration = other.lastGeneration; + userData = other.userData; } /// @@ -1632,4 +1633,4 @@ internal int IndexOf(SegmentCommitInfo si) return segments.IndexOf(si); } } -} \ No newline at end of file +}