API: Added ReferenceManager<G>.AcquireContext(), which is similar to ReferenceManager<G>.Acquire() but can be used in a using block to implicitly dereference instead of having to do it explicitly in a finally block.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/6f51ab54 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/6f51ab54 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/6f51ab54 Branch: refs/heads/master Commit: 6f51ab547cd00f3280d69b37d17c86d45f587c00 Parents: 39e6316 Author: Shad Storhaug <[email protected]> Authored: Thu May 11 13:44:46 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Fri Jul 14 01:15:57 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net/Lucene.Net.csproj | 2 + .../Support/Search/ReferenceContext.cs | 64 ++++++++++++++++++++ .../Search/ReferenceManagerExtensions.cs | 47 ++++++++++++++ 3 files changed, 113 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6f51ab54/src/Lucene.Net/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj index c0c02d3..ff96872 100644 --- a/src/Lucene.Net/Lucene.Net.csproj +++ b/src/Lucene.Net/Lucene.Net.csproj @@ -652,6 +652,8 @@ <Compile Include="Support\ICallable.cs" /> <Compile Include="Support\ICharSequence.cs" /> <Compile Include="Support\RectangularArrays.cs" /> + <Compile Include="Support\Search\ReferenceContext.cs" /> + <Compile Include="Support\Search\ReferenceManagerExtensions.cs" /> <Compile Include="Support\Threading\ICompletionService.cs" /> <Compile Include="Support\IO\IDataInput.cs" /> <Compile Include="Support\IO\IDataOutput.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6f51ab54/src/Lucene.Net/Support/Search/ReferenceContext.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/Search/ReferenceContext.cs b/src/Lucene.Net/Support/Search/ReferenceContext.cs new file mode 100644 index 0000000..c72e3f4 --- /dev/null +++ b/src/Lucene.Net/Support/Search/ReferenceContext.cs @@ -0,0 +1,64 @@ +using System; + +namespace Lucene.Net.Search +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /// <summary> + /// <see cref="ReferenceContext{T}"/> holds a reference instance and + /// ensures it is properly de-referenced from its corresponding <see cref="ReferenceManager{G}"/> + /// when <see cref="Dispose()"/> is called. This class is primarily intended + /// to be used with a using block. + /// <para/> + /// LUCENENET specific + /// </summary> + /// <typeparam name="T">The reference type</typeparam> + public class ReferenceContext<T> : IDisposable + where T : class + { + private readonly ReferenceManager<T> referenceManager; + private T reference; + + internal ReferenceContext(ReferenceManager<T> referenceManager) + { + this.referenceManager = referenceManager; + this.reference = referenceManager.Acquire(); + } + + /// <summary> + /// The reference acquired from the <see cref="ReferenceManager{G}"/>. + /// </summary> + public T Reference + { + get { return reference; } + } + + /// <summary> + /// Ensures the reference is properly de-referenced from its <see cref="ReferenceManager{G}"/>. + /// After this call, <see cref="Reference"/> will be <c>null</c>. + /// </summary> + public void Dispose() + { + if (this.reference != null) + { + this.referenceManager.Release(this.reference); + this.reference = null; + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6f51ab54/src/Lucene.Net/Support/Search/ReferenceManagerExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/Search/ReferenceManagerExtensions.cs b/src/Lucene.Net/Support/Search/ReferenceManagerExtensions.cs new file mode 100644 index 0000000..2629398 --- /dev/null +++ b/src/Lucene.Net/Support/Search/ReferenceManagerExtensions.cs @@ -0,0 +1,47 @@ +namespace Lucene.Net.Search +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + public static class ReferenceManagerExtensions + { + /// <summary> + /// Obtain the current reference. + /// <para/> + /// Like <see cref="ReferenceManager{G}.Acquire()"/>, but intended for use in a using + /// block so calling <see cref="ReferenceManager{G}.Release(G)"/> happens implicitly. + /// For example: + /// <para/> + /// <code> + /// var searcherManager = new SearcherManager(indexWriter, true, null); + /// using (var context = searcherManager.AcquireContext()) + /// { + /// IndexSearcher searcher = context.Reference; + /// + /// // use searcher... + /// } + /// </code> + /// </summary> + /// <typeparam name="T">The reference type</typeparam> + /// <param name="referenceManager">this <see cref="ReferenceManager{G}"/></param> + /// <returns>A <see cref="ReferenceContext{T}"/> instance that holds the <see cref="ReferenceContext{T}.Reference"/> and ensures it is released properly when <see cref="ReferenceContext{T}.Dispose()"/> is called.</returns> + public static ReferenceContext<T> AcquireContext<T>(this ReferenceManager<T> referenceManager) where T : class + { + return new ReferenceContext<T>(referenceManager); + } + } +}
