lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r805611 - in /incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache: SimpleLRUCache.cs SimpleMapCache.cs
Date Tue, 18 Aug 2009 22:40:39 GMT
Author: digy
Date: Tue Aug 18 22:40:39 2009
New Revision: 805611

URL: http://svn.apache.org/viewvc?rev=805611&view=rev
Log:
LUCENENET-190 SimpleLRUCache.patch

Modified:
    incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleLRUCache.cs
    incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleMapCache.cs

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleLRUCache.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Util/Cache/SimpleLRUCache.cs?rev=805611&r1=805610&r2=805611&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleLRUCache.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleLRUCache.cs Tue Aug 18 22:40:39
2009
@@ -15,20 +15,39 @@
  * limitations under the License.
  */
 
+using System.Collections.Generic;
 using Hashtable = System.Collections.Hashtable;
 using LinkedList = System.Collections.Generic.LinkedList<object>;
 using Math = System.Math;
 
-namespace Lucene.Net.Util.Cache
+namespace Lucene.Net.Util.Cache  
 {
-    public class SimpleLRUCache : SimpleMapCache
+    //{{DIGY}}
+    //Use SimpleLRUCache_LUCENENET_190_1 for capacity<1536 and
+    //SimpleLRUCache_LUCENENET_190_2 for capacity > 1536
+    public class SimpleLRUCache : SimpleLRUCache_LUCENENET_190_1
+    {
+        public SimpleLRUCache(int Capacity)
+            : base(Capacity)
+        {
+        }
+    }
+
+
+
+
+    /// <summary>
+    /// This class is the original port for 2.4.0
+    /// </summary>
+    [System.ComponentModel.EditorBrowsable( System.ComponentModel.EditorBrowsableState.Never)]
+    public class SimpleLRUCache_OrgPort : SimpleMapCache
     {
         private const float LOADFACTOR = 0.75f;
 
         private int cacheSize;
         private LinkedList lru;
 
-        public SimpleLRUCache(int cacheSize)
+        public SimpleLRUCache_OrgPort(int cacheSize)
             : base(null)
         {
             this.cacheSize = cacheSize;
@@ -40,7 +59,8 @@
 
         public override void Put(object key, object value)
         {
-            if (lru.Contains(key))
+            //if (lru.Contains(key))
+            if (base.map.ContainsKey(key))
             {
                 // move key to most recently used position
                 lru.Remove(key);
@@ -64,7 +84,8 @@
 
         public override object Get(object key)
         {
-            if (lru.Contains(key))
+            //if (lru.Contains(key))
+            if (base.map.ContainsKey(key))
             {
                 // if LRU data structure contains key, move the key
                 // to the "most recently used" position
@@ -75,4 +96,151 @@
             return base.Get(key);
         }
     }
+
+
+
+
+
+    /// <summary>
+    /// Implemented for LUCENENET-190. Good for capacity < 1536
+    /// </summary>
+    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
+    public class SimpleLRUCache_LUCENENET_190_1: Cache
+    {
+        System.Collections.Generic.Dictionary<object, LRUCacheValueEntry> Data = new
Dictionary<object, LRUCacheValueEntry>();
+        SortedList<long, object> TimeStamps = new SortedList<long, object>();
+
+        long TimeStamp = 0;
+        int Capacity = 1024;
+
+        public SimpleLRUCache_LUCENENET_190_1(int Capacity)
+        {
+            this.Capacity = Capacity;
+        }
+
+        public override void Put(object Key, object Value)
+        {
+            if (Get(Key) == null)
+            {
+                TimeStamp++;
+                Data.Add(Key, new LRUCacheValueEntry(TimeStamp, Value));
+                TimeStamps.Add(TimeStamp, Key);
+
+                if (Data.Count > Capacity)
+                {
+                    long key = TimeStamps.Keys[0];
+                    Data.Remove(TimeStamps[key]);
+                    TimeStamps.RemoveAt(0);
+                }
+            }
+        }
+
+        public override object Get(object Key)
+        {
+            LRUCacheValueEntry e = null;
+            Data.TryGetValue(Key, out e);
+            if (e == null) return null;
+
+            TimeStamps.Remove(e.TimeStamp);
+            e.TimeStamp = ++TimeStamp;
+            TimeStamps.Add(e.TimeStamp, Key);
+            return e.Value;
+
+        }
+
+        public override bool ContainsKey(object key)
+        {
+            return Data.ContainsKey(key);
+        }
+
+        public override void Close()
+        {
+        }
+
+        class LRUCacheValueEntry
+        {
+            public long TimeStamp = 0;
+            public object Value;
+
+            public LRUCacheValueEntry(long TimeStamp, object Value)
+            {
+                this.TimeStamp = TimeStamp;
+                this.Value = Value;
+            }
+        }
+    }
+
+
+
+    /// <summary>
+    /// Implemented for LUCENENET-190. Good for capacity > 1536
+    /// </summary>
+    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
+    public class SimpleLRUCache_LUCENENET_190_2 : Lucene.Net.Util.Cache.Cache
+    {
+        System.Collections.Generic.Dictionary<object, LRUCacheValueEntry> Data = new
Dictionary<object, LRUCacheValueEntry>();
+        System.Collections.Generic.SortedDictionary<long, object> TimeStamps = new
SortedDictionary<long, object>();
+        
+
+        long TimeStamp = 0;
+        int Capacity = 1024;
+
+        public SimpleLRUCache_LUCENENET_190_2(int Capacity)
+        {
+            this.Capacity = Capacity;
+        }
+
+        public override void Put(object Key, object Value)
+        {
+            if (Get(Key) == null)
+            {
+                TimeStamp++;
+                Data.Add(Key, new LRUCacheValueEntry(TimeStamp, Value));
+                TimeStamps.Add(TimeStamp, Key);
+
+                if (Data.Count > Capacity)
+                {
+                    SortedDictionary<long, object>.Enumerator enumTimeStamps = TimeStamps.GetEnumerator();
+                    enumTimeStamps.MoveNext();
+                    long key = enumTimeStamps.Current.Key;
+                    Data.Remove(TimeStamps[key]);
+                    TimeStamps.Remove(key);
+                }
+
+            }
+        }
+
+        public override object Get(object Key)
+        {
+            LRUCacheValueEntry e = null;
+            Data.TryGetValue(Key, out e);
+            if (e == null) return null;
+
+            TimeStamps.Remove(e.TimeStamp);
+            e.TimeStamp = ++TimeStamp;
+            TimeStamps.Add(e.TimeStamp, Key);
+            return e.Value;
+        }
+
+        class LRUCacheValueEntry
+        {
+            public long TimeStamp = 0;
+            public object Value;
+
+            public LRUCacheValueEntry(long TimeStamp, object Value)
+            {
+                this.TimeStamp = TimeStamp;
+                this.Value = Value;
+            }
+        }
+
+        public override bool ContainsKey(object key)
+        {
+            return Data.ContainsKey(key);
+        }
+
+        public override void Close()
+        {
+        }
+    }
 }

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleMapCache.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Util/Cache/SimpleMapCache.cs?rev=805611&r1=805610&r2=805611&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleMapCache.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/Cache/SimpleMapCache.cs Tue Aug 18 22:40:39
2009
@@ -15,6 +15,10 @@
  * limitations under the License.
  */
 
+//{{DIGY}}
+//This class isn't needed by the new implementation of SimpleLRUCache(LUCENENET-190) 
+//
+
 using Hashtable = System.Collections.Hashtable;
 using ICollection = System.Collections.ICollection;
 



Mime
View raw message