lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mhern...@apache.org
Subject [3/9] git commit: cleaning up xml comments and ensuring default strategy is set.
Date Wed, 20 Aug 2014 02:32:07 GMT
cleaning up xml comments and ensuring default strategy is set.


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/46f748b0
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/46f748b0
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/46f748b0

Branch: refs/heads/pcl
Commit: 46f748b03f0bf6140f070a9b4d6949d778afd899
Parents: 52e1130
Author: Michael Herndon <mherndon@michaelherndon.com>
Authored: Sun Aug 17 15:01:51 2014 -0400
Committer: Michael Herndon <mherndon@michaelherndon.com>
Committed: Sun Aug 17 15:01:51 2014 -0400

----------------------------------------------------------------------
 src/Lucene.Net.Core/Lucene.Net.Core.csproj      |   4 +-
 src/Lucene.Net.Core/Util/IPurgeStrategy.cs      |  25 ++-
 src/Lucene.Net.Core/Util/PclPurgeStrategy.cs    |  29 ++-
 .../Util/PurgeableThreadLocal.cs                | 220 +++++++++++++++++++
 4 files changed, 258 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/46f748b0/src/Lucene.Net.Core/Lucene.Net.Core.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Lucene.Net.Core.csproj b/src/Lucene.Net.Core/Lucene.Net.Core.csproj
index 903c74d..58dd3ea 100644
--- a/src/Lucene.Net.Core/Lucene.Net.Core.csproj
+++ b/src/Lucene.Net.Core/Lucene.Net.Core.csproj
@@ -56,7 +56,6 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
-    <Compile Include="Index\IIndexDocument.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Check.cs" />
     <Compile Include="Support\AtomicReferenceArray.cs" />
@@ -93,7 +92,7 @@
     <Compile Include="Util\UnicodeUtil.cs" />
     <Compile Include="Util\Version.cs" />
     <Compile Include="Util\WeakIdentityMap.cs" />
-    <Compile Include="Util\PurgableThreadLocal.cs" />
+    <Compile Include="Util\PurgeableThreadLocal.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
@@ -106,6 +105,7 @@
   <ItemGroup>
     <Folder Include="Analysis\" />
     <Folder Include="Document\" />
+    <Folder Include="Index\" />
   </ItemGroup>
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets"
/>
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')"
/>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/46f748b0/src/Lucene.Net.Core/Util/IPurgeStrategy.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/IPurgeStrategy.cs b/src/Lucene.Net.Core/Util/IPurgeStrategy.cs
index de646bb..3d3ade4 100644
--- a/src/Lucene.Net.Core/Util/IPurgeStrategy.cs
+++ b/src/Lucene.Net.Core/Util/IPurgeStrategy.cs
@@ -1,12 +1,25 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
+/*
+ * 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.
+ */
 
 namespace Lucene.Net.Util
 {
+    using System;
+    using System.Threading.Tasks;
+
 
 
     /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/46f748b0/src/Lucene.Net.Core/Util/PclPurgeStrategy.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/PclPurgeStrategy.cs b/src/Lucene.Net.Core/Util/PclPurgeStrategy.cs
index 2ad9425..1d9468a 100644
--- a/src/Lucene.Net.Core/Util/PclPurgeStrategy.cs
+++ b/src/Lucene.Net.Core/Util/PclPurgeStrategy.cs
@@ -44,23 +44,27 @@ namespace Lucene.Net.Util
 
 
         /// <summary>
-        /// add as an asynchronous operation.
+        /// Add the value asynchronously.
         /// </summary>
         /// <param name="value">The value.</param>
         /// <returns><see cref="Task" /></returns>
-        public async Task AddAsync(object value)
+        public Task AddAsync(object value)
         {
-            await this.semaphoreSlim.WaitAsync();
-
-            try
+            return new Task(async () =>
             {
-                this.hardReferences.Add(value);
-                this.weakReferences.Add(new WeakReference(value));
-            }
-            finally
-            {
-                this.semaphoreSlim.Release();
-            }
+                await this.semaphoreSlim.WaitAsync();
+
+                try
+                {
+                    this.hardReferences.Add(value);
+                    this.weakReferences.Add(new WeakReference(value));
+                }
+                finally
+                {
+                    this.semaphoreSlim.Release();
+                }
+            });
+
         }
 
         /// <summary>
@@ -82,6 +86,7 @@ namespace Lucene.Net.Util
 
                 GC.Collect();
 
+                // let GC do it's thing.
                 await Task.Delay(1000);
 
                 foreach (var reference in this.weakReferences)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/46f748b0/src/Lucene.Net.Core/Util/PurgeableThreadLocal.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/PurgeableThreadLocal.cs b/src/Lucene.Net.Core/Util/PurgeableThreadLocal.cs
new file mode 100644
index 0000000..bb02e40
--- /dev/null
+++ b/src/Lucene.Net.Core/Util/PurgeableThreadLocal.cs
@@ -0,0 +1,220 @@
+/*
+ * 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.
+ */
+
+namespace Lucene.Net.Util
+{
+    using System;
+    using System.Reflection;
+    using System.Threading;
+
+    /// <summary>
+    ///     Replacement for Java's CloseableThreadLocal. Java's ThreadLocal keeps objects
alive even after a thread dies.
+    ///     CloseableThreadLocal was created in order to purge objects that are still in
memory even though the
+    ///     threads are already dead.
+    /// </summary>
+    /// <remarks>
+    ///     <para>
+    ///         .NET's <see cref="System.Threading.ThreadLocal{T}" /> implements <see
cref="IDisposable" />,
+    ///         which is similar to Closeable in Java.  The point of Lucene's CloseableThreadLocal
is not that it
+    ///         is disposable, but that it purges objects for dead threads rather than waiting
for Java's garbage
+    ///         collection to do it.
+    ///     </para>
+    ///     <para>
+    ///         PCL, portable class libraries, currently does not support the <see cref="System.Threading.Thread"
/>
+    ///         class. In order to get around this, <see cref="IPurgeStrategy" /> was
created so that different
+    ///         purge strategies can be swapped out.  The default strategy can't investigate
threads, so the
+    ///         default <see cref="PclPurgeStrategy" /> instead clears all internal
hard references, calls
+    ///         <see cref="GC.Collect()" />, gives <see cref="GC" /> time to
work,
+    ///         and then determines which references are dead and purges them. It would be
a good idea to dispose
+    ///         of any <see cref="System.Threading.Tasks.Task" />
+    ///     </para>
+    ///     <para>
+    ///         The default strategy can be replaced with one similar to Java Version's that
loops through threads
+    ///         to see which threads are dead and purge the related objects. <see cref="IPurgeStrategy.AddAsync"
/>
+    ///         is purposely instructed to run synchronously so that a strategy can get the
current thread when
+    ///         a value is added.
+    ///     </para>
+    /// </remarks>
+    /// <typeparam name="T"></typeparam>
+    public class PurgeableThreadLocal<T> : IDisposable
+    {
+        private readonly bool isValueType;
+        public IPurgeStrategy PurgeStrategy = null;
+        private bool isDisposed;
+        private ThreadLocal<WeakReference> threadLocal;
+
+
+        /// <summary>
+        ///     Initializes static members of the <see cref="PurgeableThreadLocal{T}"
/> class.
+        /// </summary>
+        static PurgeableThreadLocal()
+        {
+            DefaultPurgeStrategy = new PclPurgeStrategy();
+        }
+
+        /// <summary>
+        ///     Initializes a new instance of the <see cref="PurgeableThreadLocal{T}"
/> class.
+        /// </summary>
+        /// <param name="valueFactory">The value factory.</param>
+        /// <param name="trackAllValues">if set to <c>true</c> [track all
values].</param>
+        public PurgeableThreadLocal(Func<T> valueFactory = null, bool trackAllValues
= false)
+        {
+            this.isValueType = typeof (T).GetTypeInfo().IsValueType;
+            this.PurgeStrategy = DefaultPurgeStrategy;
+            if (valueFactory != null)
+            {
+                this.threadLocal = new ThreadLocal<WeakReference>(() =>
+                {
+                    var value = valueFactory();
+                    if (this.PurgeStrategy != null)
+                        this.PurgeStrategy.AddAsync(value).RunSynchronously();
+
+                    return new WeakReference(value, trackAllValues);
+                });
+                return;
+            }
+
+            this.threadLocal = new ThreadLocal<WeakReference>(trackAllValues);
+        }
+
+        /// <summary>
+        ///     Gets or sets the default purge strategy.
+        /// </summary>
+        /// <value>The default purge strategy.</value>
+        public static IPurgeStrategy DefaultPurgeStrategy { get; set; }
+
+        /// <summary>
+        ///     Gets or sets the value.
+        /// </summary>
+        /// <value>The value.</value>
+        public T Value
+        {
+            get
+            {
+                this.CheckDisposed();
+                WeakReference reference = this.threadLocal.Value;
+                if (reference == null || !reference.IsAlive)
+                {
+                    T value = default(T);
+
+                    // check for value type. 
+                    // ReSharper disable once CompareNonConstrainedGenericWithNull
+                    if (this.isValueType || value != null)
+                        this.Set(value);
+
+                    return value;
+                }
+
+
+                this.Purge();
+                return (T) reference.Target;
+            }
+            set { this.Set(value); }
+        }
+
+        /// <summary>
+        ///     Gets or sets a value indicating whether this instance is alive.
+        /// </summary>
+        /// <value><c>true</c> if this instance is alive; otherwise, <c>false</c>.</value>
+        public bool IsAlive
+        {
+            get
+            {
+                this.CheckDisposed();
+                WeakReference reference = this.threadLocal.Value;
+                return reference != null && reference.IsAlive;
+            }
+            // ReSharper disable once ValueParameterNotUsed
+            set
+            {
+                this.CheckDisposed();
+                if (value == false)
+                    this.threadLocal.Value = null;
+            }
+        }
+
+
+        /// <summary>
+        ///     Performs application-defined tasks associated with freeing, releasing, or
resetting unmanaged resources.
+        /// </summary>
+        public void Dispose()
+        {
+            GC.SuppressFinalize(this);
+            this.Dispose(true);
+        }
+
+        /// <summary>
+        ///     Purges this instance.
+        /// </summary>
+        protected void Purge()
+        {
+            if (this.PurgeStrategy != null)
+                this.PurgeStrategy.PurgeAsync();
+        }
+
+        private void Set(T value)
+        {
+            this.CheckDisposed();
+            this.threadLocal.Value = new WeakReference(value);
+            if (this.PurgeStrategy != null)
+                this.PurgeStrategy.AddAsync(value).RunSynchronously();
+
+            this.Purge();
+        }
+
+        /// <summary>
+        ///     Checks to see if <see cref="PurgeableThreadLocal{T}" /> has already
been disposed.
+        /// </summary>
+        /// <exception cref="System.ObjectDisposedException"></exception>
+        private void CheckDisposed()
+        {
+            if (this.isDisposed)
+                throw new ObjectDisposedException(this.GetType().Name);
+        }
+
+        /// <summary>
+        ///     Releases unmanaged and - optionally - managed resources.
+        /// </summary>
+        /// <param name="dispose">
+        ///     <c>true</c> to release both managed and unmanaged resources;
<c>false</c> to release only
+        ///     unmanaged resources.
+        /// </param>
+        protected virtual void Dispose(bool dispose)
+        {
+            if (this.isDisposed)
+                return;
+
+            if (this.PurgeStrategy != null)
+            {
+                this.PurgeStrategy.Dispose();
+                this.PurgeStrategy = null;
+            }
+
+            this.threadLocal.Dispose();
+            this.threadLocal = null;
+            this.isDisposed = true;
+        }
+
+        /// <summary>
+        ///     Finalizes an instance of the <see cref="PurgeableThreadLocal{T}" />
class.
+        /// </summary>
+        ~PurgeableThreadLocal()
+        {
+            this.Dispose(false);
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message