Skip to content

Commit

Permalink
fix: Aligned disposable patterns (#746)
Browse files Browse the repository at this point in the history
* fix: Aligned disposable patterns

* Lucene.Net.Analysis.Util.CharArrayMap: Reverted changes to avoid conflict with other work that has been done to CharArrayMap

* SWEEP: Added comments to indicate places where we have diverged from Lucene to implement the disposable pattern properly.

* Lucene.Net.Codecs.Memory.MemoryDocValuesConsumer: Changed code order of Dispose() and Dispose(bool) to align with other classes

Co-authored-by: Shad Storhaug <[email protected]>
  • Loading branch information
nikcio and NightOwl888 authored Nov 12, 2022
1 parent 4b09c0a commit cf259c9
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Lucene version compatibility level 4.8.1
// Lucene version compatibility level 4.8.1
namespace Lucene.Net.Analysis.Miscellaneous
{
/*
Expand Down Expand Up @@ -96,6 +96,7 @@ protected override void Dispose(bool disposing)
{
suffix.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

public override void End()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Lucene version compatibility level 4.8.1
// Lucene version compatibility level 4.8.1
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Util;

Expand Down Expand Up @@ -182,6 +182,7 @@ protected override void Dispose(bool disposing)
prefix.Dispose();
suffix.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

public override void Reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected override void Dispose(bool disposing)
this.isDisposing = false;
#endif
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected override void Dispose(bool disposing)
}
Console.WriteLine();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

public override bool SupportsParams => true;
Expand Down
1 change: 1 addition & 0 deletions src/Lucene.Net.Benchmark/ByTask/Tasks/TaskSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected override void Dispose(bool disposing)
}
RunData.DocMaker.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

private void InitTasksArray()
Expand Down
13 changes: 11 additions & 2 deletions src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,17 @@ private void EncodeValues(int count)

public void Dispose()
{
this.counts.Dispose();
this.ords.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.counts.Dispose();
this.ords.Dispose();
}
}

// LUCENENET: Remove() not supported in .NET
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Facet/Taxonomy/WriterCache/CollisionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ internal virtual int GetMemoryUsage()
return memoryUsage;
}

private class EntryEnumerator : IEnumerator<Entry>
private sealed class EntryEnumerator : IEnumerator<Entry> // LUCENENET: Marked sealed
{
internal Entry next; // next entry to return
internal int index; // current slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected internal virtual void Scan(int scanDetailLevel)
/// <summary>
/// Used for <see cref="VNode.children"/>.
/// </summary>
private class VNodeCellEnumerator : IEnumerator<VNode>
private sealed class VNodeCellEnumerator : IEnumerator<VNode>// LUCENENET: Marked sealed
{
internal readonly IEnumerator<Cell> cellIter;
private readonly VNode vNode;
Expand Down
1 change: 1 addition & 0 deletions src/Lucene.Net/Analysis/TokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected override void Dispose(bool disposing)
{
m_input.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Lucene.Net/Analysis/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected override void Dispose(bool disposing)
inputPending = ILLEGAL_STATE_READER;
m_input = ILLEGAL_STATE_READER;
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

/// <summary>
Expand Down
13 changes: 11 additions & 2 deletions src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using J2N.Runtime.CompilerServices;
using J2N.Runtime.CompilerServices;
using Lucene.Net.Diagnostics;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -94,7 +94,16 @@ internal class ConsumerAndSuffix : IDisposable

public void Dispose()
{
Consumer.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Consumer.Dispose();
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ internal class FieldsConsumerAndSuffix : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
Consumer.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Consumer.Dispose();
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/Lucene.Net/Index/IndexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,16 @@ public virtual void Release(ReadersAndUpdates rld, bool assertInfoLive)

public void Dispose()
{
DropAll(false);
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
DropAll(false);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Index/PrefixCodedTerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal PrefixCodedTermsIterator(RAMFile buffer)
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
Expand Down
1 change: 1 addition & 0 deletions src/Lucene.Net/Store/FSDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ protected override void Dispose(bool disposing)
}
}
}
//base.Dispose(disposing); // LUCENENET: No need to call base class, we are not using the functionality of BufferedIndexOutput
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Lucene.Net/Support/IO/SafeTextWriterWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ protected override void Dispose(bool disposing)
{
textWriter.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}
}
}
21 changes: 15 additions & 6 deletions src/Lucene.Net/Support/Index/TaskMergeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,23 @@ private void Run(CancellationToken cancellationToken)

public void Dispose()
{
if (_isDisposed)
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
return;
}
if (_isDisposed)
{
return;
}

_isDisposed = true;
_lock.Dispose();
_cancellationTokenSource.Dispose();
_isDisposed = true;
_lock.Dispose();
_cancellationTokenSource.Dispose();
}
}

public override string ToString()
Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net/Util/PrintStreamInfoStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using J2N.Threading.Atomic;
using J2N.Threading.Atomic;
using Lucene.Net.Support.IO;
using System;
using System.IO;
Expand Down Expand Up @@ -90,6 +90,7 @@ protected override void Dispose(bool disposing)
{
m_stream.Dispose();
}
base.Dispose(disposing); // LUCENENET specific - disposable pattern requires calling the base class implementation
}

public virtual bool IsSystemStream => isSystemStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ private static void HandleUnexpectedArg(CommandLineApplication command, string[]
}
}

private class CommandArgumentEnumerator : IEnumerator<CommandArgument>
private sealed class CommandArgumentEnumerator : IEnumerator<CommandArgument>
{
private readonly IEnumerator<CommandArgument> _enumerator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ protected override void Dispose(bool disposing)
{
this.reader?.Dispose();
}
base.Dispose(disposing);
}
}
}

0 comments on commit cf259c9

Please sign in to comment.