Skip to content

Commit

Permalink
Replace SetOnce Get/Set with Value, apache#1069
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 21, 2024
1 parent a739a80 commit 9b863cc
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 86 deletions.
3 changes: 2 additions & 1 deletion src/Lucene.Net.TestFramework/Index/MockRandomMergePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, Segment
int numSegments/* = segmentInfos.Count*/; // LUCENENET: IDE0059: Remove unnecessary value assignment

JCG.List<SegmentCommitInfo> segments = new JCG.List<SegmentCommitInfo>();
ICollection<SegmentCommitInfo> merging = base.m_writer.Get().MergingSegments;
ICollection<SegmentCommitInfo> merging = base.m_writer.Value?.MergingSegments
?? throw new InvalidOperationException("The writer has not been initialized"); // LUCENENET specific - throw exception if writer is null

foreach (SegmentCommitInfo sipc in segmentInfos.Segments)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Tests/Index/TestIndexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2877,7 +2877,7 @@ public virtual void TestMergeAllDeleted()
SetOnce<IndexWriter> iwRef = new SetOnce<IndexWriter>();
iwc.SetInfoStream(new TestPointInfoStream(iwc.InfoStream, new TestPointAnonymousClass(iwRef)));
IndexWriter evilWriter = new IndexWriter(dir, iwc);
iwRef.Set(evilWriter);
iwRef.Value = evilWriter;
for (int i = 0; i < 1000; i++)
{
AddDoc(evilWriter);
Expand Down Expand Up @@ -2905,11 +2905,11 @@ public void Apply(string message)
{
if ("startCommitMerge".Equals(message, StringComparison.Ordinal))
{
iwRef.Get().KeepFullyDeletedSegments = false;
iwRef.Value!.KeepFullyDeletedSegments = false;
}
else if ("startMergeInit".Equals(message, StringComparison.Ordinal))
{
iwRef.Get().KeepFullyDeletedSegments = true;
iwRef.Value!.KeepFullyDeletedSegments = true;
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Lucene.Net.Tests/Util/TestSetOnce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override void Run()
try
{
Sleep(RAND.Next(10)); // sleep for a short time
set.Set(new Integer(Convert.ToInt32(Name.Substring(2), CultureInfo.InvariantCulture)));
set.Value = new Integer(Convert.ToInt32(Name.Substring(2), CultureInfo.InvariantCulture));
success = true;
}
catch (Exception e) when (e.IsInterruptedException())
Expand All @@ -76,24 +76,24 @@ public override void Run()
public virtual void TestEmptyCtor()
{
SetOnce<Integer> set = new SetOnce<Integer>();
Assert.IsNull(set.Get());
Assert.IsNull(set.Value);
}

[Test]
public virtual void TestSettingCtor()
{
SetOnce<Integer> set = new SetOnce<Integer>(new Integer(5));
Assert.AreEqual(5, set.Get().value);
Assert.Throws<AlreadySetException>(() => set.Set(new Integer(7)));
Assert.AreEqual(5, set.Value?.value);
Assert.Throws<AlreadySetException>(() => set.Value = new Integer(7));
}

[Test]
public virtual void TestSetOnce_mem()
{
SetOnce<Integer> set = new SetOnce<Integer>();
set.Set(new Integer(5));
Assert.AreEqual(5, set.Get().value);
Assert.Throws<AlreadySetException>(() => set.Set(new Integer(7)));
set.Value = new Integer(5);
Assert.AreEqual(5, set.Value.value);
Assert.Throws<AlreadySetException>(() => set.Value = new Integer(7));
}

[Test]
Expand Down Expand Up @@ -124,7 +124,7 @@ public virtual void TestSetMultiThreaded()
if (t.success)
{
int expectedVal = Convert.ToInt32(t.Name.Substring(2));
Assert.AreEqual(expectedVal, t.set.Get().value, "thread " + t.Name);
Assert.AreEqual(expectedVal, t.set.Value?.value, "thread " + t.Name);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Index/IndexWriterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static long DefaultWriteLockTimeout
/// if this config is already attached to a writer. </exception>
internal IndexWriterConfig SetIndexWriter(IndexWriter writer)
{
this.writer.Set(writer);
this.writer.Value = writer;
return this;
}

Expand Down
42 changes: 22 additions & 20 deletions src/Lucene.Net/Index/LogMergePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public abstract class LogMergePolicy : MergePolicy
public static readonly int DEFAULT_MERGE_FACTOR = 10;

/// <summary>
/// Default maximum segment size. A segment of this size
/// or larger will never be merged. </summary>
/// Default maximum segment size. A segment of this size
/// or larger will never be merged. </summary>
/// <seealso cref="MaxMergeDocs"/>
public static readonly int DEFAULT_MAX_MERGE_DOCS = int.MaxValue;

Expand Down Expand Up @@ -125,7 +125,7 @@ protected virtual bool IsVerbose
{
get
{
IndexWriter w = m_writer.Get();
IndexWriter w = m_writer.Value;
return w != null && w.infoStream.IsEnabled("LMP");
}
}
Expand All @@ -137,7 +137,7 @@ protected virtual void Message(string message)
{
if (IsVerbose)
{
m_writer.Get().infoStream.Message("LMP", message);
m_writer.Value?.infoStream.Message("LMP", message);
}
}

Expand Down Expand Up @@ -184,16 +184,17 @@ protected override void Dispose(bool disposing)
}

/// <summary>
/// Return the number of documents in the provided
/// Return the number of documents in the provided
/// <see cref="SegmentCommitInfo"/>, pro-rated by percentage of
/// non-deleted documents if
/// non-deleted documents if
/// <see cref="CalibrateSizeByDeletes"/> is set.
/// </summary>
protected virtual long SizeDocs(SegmentCommitInfo info)
{
if (m_calibrateSizeByDeletes)
{
int delCount = m_writer.Get().NumDeletedDocs(info);
int delCount = m_writer.Value?.NumDeletedDocs(info)
?? throw new InvalidOperationException("The writer has not been initialized"); // LUCENENET specific - throw exception if writer is null
if (Debugging.AssertsEnabled) Debugging.Assert(delCount <= info.Info.DocCount);
return (info.Info.DocCount - (long)delCount);
}
Expand All @@ -204,9 +205,9 @@ protected virtual long SizeDocs(SegmentCommitInfo info)
}

/// <summary>
/// Return the byte size of the provided
/// Return the byte size of the provided
/// <see cref="SegmentCommitInfo"/>, pro-rated by percentage of
/// non-deleted documents if
/// non-deleted documents if
/// <see cref="CalibrateSizeByDeletes"/> is set.
/// </summary>
protected virtual long SizeBytes(SegmentCommitInfo info)
Expand All @@ -220,7 +221,7 @@ protected virtual long SizeBytes(SegmentCommitInfo info)

/// <summary>
/// Returns <c>true</c> if the number of segments eligible for
/// merging is less than or equal to the specified
/// merging is less than or equal to the specified
/// <paramref name="maxNumSegments"/>.
/// </summary>
protected virtual bool IsMerged(SegmentInfos infos, int maxNumSegments, IDictionary<SegmentCommitInfo, bool> segmentsToMerge)
Expand Down Expand Up @@ -381,7 +382,7 @@ public override MergeSpecification FindForcedMerges(SegmentInfos infos, int maxN
if (Debugging.AssertsEnabled) Debugging.Assert(maxNumSegments > 0);
if (IsVerbose)
{
Message("findForcedMerges: maxNumSegs=" + maxNumSegments + " segsToMerge=" +
Message("findForcedMerges: maxNumSegs=" + maxNumSegments + " segsToMerge=" +
string.Format(J2N.Text.StringFormatter.InvariantCulture, "{0}", segmentsToMerge));
}

Expand Down Expand Up @@ -468,7 +469,7 @@ public override MergeSpecification FindForcedDeletesMerges(SegmentInfos segmentI

var spec = new MergeSpecification();
int firstSegmentWithDeletions = -1;
IndexWriter w = m_writer.Get();
IndexWriter w = m_writer.Value;
if (Debugging.AssertsEnabled) Debugging.Assert(w != null);
for (int i = 0; i < numSegments; i++)
{
Expand Down Expand Up @@ -545,10 +546,10 @@ public virtual int CompareTo(SegmentInfoAndLevel other)
/// <summary>
/// Checks if any merges are now necessary and returns a
/// <see cref="MergePolicy.MergeSpecification"/> if so. A merge
/// is necessary when there are more than
/// is necessary when there are more than
/// <see cref="MergeFactor"/> segments at a given level. When
/// multiple levels have too many segments, this method
/// will return multiple merges, allowing the
/// will return multiple merges, allowing the
/// <see cref="MergeScheduler"/> to use concurrency.
/// </summary>
public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, SegmentInfos infos)
Expand All @@ -564,7 +565,8 @@ public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, Segment
IList<SegmentInfoAndLevel> levels = new JCG.List<SegmentInfoAndLevel>();
var norm = (float)Math.Log(m_mergeFactor);

ICollection<SegmentCommitInfo> mergingSegments = m_writer.Get().MergingSegments;
ICollection<SegmentCommitInfo> mergingSegments = m_writer.Value?.MergingSegments
?? throw new InvalidOperationException("The writer has not been initialized"); // LUCENENET specific - throw exception if writer is null

for (int i = 0; i < numSegments; i++)
{
Expand All @@ -588,7 +590,7 @@ public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, Segment
{
extra += " [skip: too large]";
}
Message("seg=" + m_writer.Get().SegString(info) + " level=" + infoLevel.level + " size=" + String.Format(CultureInfo.InvariantCulture, "{0:0.00} MB", segBytes / 1024 / 1024.0) + extra);
Message("seg=" + m_writer.Value?.SegString(info) + " level=" + infoLevel.level + " size=" + string.Format(CultureInfo.InvariantCulture, "{0:0.00} MB", segBytes / 1024 / 1024.0) + extra);
}
}

Expand Down Expand Up @@ -696,7 +698,7 @@ public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, Segment
}
if (IsVerbose)
{
Message(" add merge=" + m_writer.Get().SegString(mergeInfos) + " start=" + start + " end=" + end);
Message(" add merge=" + m_writer.Value?.SegString(mergeInfos) + " start=" + start + " end=" + end);
}
spec.Add(new OneMerge(mergeInfos));
}
Expand Down Expand Up @@ -726,9 +728,9 @@ public override MergeSpecification FindMerges(MergeTrigger mergeTrigger, Segment
///
/// <para>The default value is <see cref="int.MaxValue"/>.</para>
///
/// <para>The default merge policy
/// <para>The default merge policy
/// (<see cref="LogByteSizeMergePolicy"/>) also allows you to set this
/// limit by net size (in MB) of the segment, using
/// limit by net size (in MB) of the segment, using
/// <see cref="LogByteSizeMergePolicy.MaxMergeMB"/>.</para>
/// </summary>
public virtual int MaxMergeDocs
Expand All @@ -752,4 +754,4 @@ public override string ToString()
return sb.ToString();
}
}
}
}
7 changes: 4 additions & 3 deletions src/Lucene.Net/Index/MergePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ protected MergePolicy(double defaultNoCFSRatio, long defaultMaxCFSSegmentSize)
/// <seealso cref="SetOnce{T}"/>
public virtual void SetIndexWriter(IndexWriter writer)
{
this.m_writer.Set(writer);
this.m_writer.Value = writer;
}

/// <summary>
Expand Down Expand Up @@ -754,7 +754,8 @@ public virtual bool UseCompoundFile(SegmentInfos infos, SegmentCommitInfo merged
protected virtual long Size(SegmentCommitInfo info)
{
long byteSize = info.GetSizeInBytes();
int delCount = m_writer.Get().NumDeletedDocs(info);
int delCount = m_writer.Value?.NumDeletedDocs(info)
?? throw new InvalidOperationException("The writer has not been initialized"); // LUCENENET specific - throw exception if writer is null
double delRatio = (info.Info.DocCount <= 0 ? 0.0f : ((float)delCount / (float)info.Info.DocCount));
if (Debugging.AssertsEnabled) Debugging.Assert(delRatio <= 1.0);
return (info.Info.DocCount <= 0 ? byteSize : (long)(byteSize * (1.0 - delRatio)));
Expand All @@ -767,7 +768,7 @@ protected virtual long Size(SegmentCommitInfo info)
/// </summary>
protected bool IsMerged(SegmentInfos infos, SegmentCommitInfo info)
{
IndexWriter w = m_writer.Get();
IndexWriter w = m_writer.Value;
if (Debugging.AssertsEnabled) Debugging.Assert(w != null);
bool hasDeletions = w.NumDeletedDocs(info) > 0;
return !hasDeletions
Expand Down
Loading

0 comments on commit 9b863cc

Please sign in to comment.