[hyena] Data.Sqlite: bring two generic classes from Banshee codebase



commit 7b464ecc93537314c5ddfc1fedd140cb0a25b2f1
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Tue May 27 01:40:01 2014 +0200

    Data.Sqlite: bring two generic classes from Banshee codebase
    
    These classes were called MigoModelProvider and MigoItem but
    didn't have anything specific to Migo, just generic DB &
    caching related features. Therefore they are now renamed to
    CacheableSqliteModelProvider and CacheableItem respectively,
    and we take the opportunity to do some cleanup to them (such
    as converting properties to auto-properties, remove unused
    usings, adding a readonly keyword, etc.)
    
    It's good to move them here in order to avoid unnecessary
    coupling on Migo from some extensions.

 Hyena.Data.Sqlite/Hyena.Data.Sqlite.csproj         |    2 +
 .../Hyena.Data.Sqlite/CacheableItem.cs             |   45 ++++++++
 .../CacheableSqliteModelProvider.cs                |  112 ++++++++++++++++++++
 Hyena.Data.Sqlite/Makefile.am                      |    2 +
 Hyena/Hyena.Data/ICacheableItem.cs                 |    2 -
 5 files changed, 161 insertions(+), 2 deletions(-)
---
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite.csproj b/Hyena.Data.Sqlite/Hyena.Data.Sqlite.csproj
index 023569f..c224a06 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite.csproj
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite.csproj
@@ -68,6 +68,8 @@
     <Compile Include="Hyena.Metrics\MemorySampleStore.cs" />
     <Compile Include="Hyena.Metrics\Sample.cs" />
     <Compile Include="Hyena.Data.Sqlite\Tests\SqliteTests.cs" />
+    <Compile Include="Hyena.Data.Sqlite\CacheableSqliteModelProvider.cs" />
+    <Compile Include="Hyena.Data.Sqlite\CacheableItem.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Posix" />
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableItem.cs 
b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableItem.cs
new file mode 100644
index 0000000..3a85771
--- /dev/null
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableItem.cs
@@ -0,0 +1,45 @@
+//
+// CacheableItem.cs
+//
+// Authors:
+//   Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2008 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using Hyena.Data;
+
+namespace Hyena.Data.Sqlite
+{
+    public abstract class CacheableItem<T> : ICacheableItem
+    {
+        public object CacheEntryId {
+            get; set;
+        }
+
+        public long CacheModelId {
+            get; set;
+        }
+
+        public abstract long DbId { get; protected set;}
+    }
+}
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableSqliteModelProvider.cs 
b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableSqliteModelProvider.cs
new file mode 100644
index 0000000..0c0d086
--- /dev/null
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/CacheableSqliteModelProvider.cs
@@ -0,0 +1,112 @@
+//
+// CacheableSqliteModelProvider.cs
+//
+// Authors:
+//   Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2008 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.Collections.Generic;
+
+using Hyena.Data;
+
+namespace Hyena.Data.Sqlite
+{
+    // Caches all results retrieved from the database, such that any subsequent retrieval will
+    // return the same instance.
+    public class CacheableSqliteModelProvider<T> : SqliteModelProvider<T> where T : CacheableItem<T>, 
ICacheableItem, new()
+    {
+        private readonly Dictionary<long, T> full_cache = new Dictionary<long, T> ();
+
+        public CacheableSqliteModelProvider (HyenaSqliteConnection connection, string table_name) : base 
(connection, table_name)
+        {
+        }
+
+        public CacheableSqliteModelProvider (HyenaSqliteConnection connection, string table_name, bool 
check) : base (connection, table_name, check)
+        {
+        }
+
+        public void ClearCache ()
+        {
+            full_cache.Clear ();
+        }
+
+        #region Overrides
+
+        public override T FetchSingle (long id)
+        {
+            return GetCached (id) ?? CacheResult (base.FetchSingle (id));
+        }
+
+        public override void Save (T target)
+        {
+            base.Save (target);
+            if (!full_cache.ContainsKey (target.DbId)) {
+                full_cache[target.DbId] = target;
+            }
+        }
+
+        public override T Load (IDataReader reader)
+        {
+            return GetCached (PrimaryKeyFor (reader)) ?? CacheResult (base.Load (reader));
+        }
+
+        public override void Delete (long id)
+        {
+            full_cache.Remove (id);
+            base.Delete (id);
+        }
+
+        public override void Delete (IEnumerable<T> items)
+        {
+            foreach (T item in items) {
+                if (item != null)
+                    full_cache.Remove (PrimaryKeyFor (item));
+            }
+
+            base.Delete (items);
+        }
+
+        #endregion
+
+        #region Utility Methods
+
+        private T GetCached (long id)
+        {
+            if (full_cache.ContainsKey (id)) {
+                return full_cache[id];
+            } else {
+                return null;
+            }
+        }
+
+        private T CacheResult (T item)
+        {
+            full_cache[item.DbId] = item;
+            return item;
+        }
+
+        #endregion
+
+    }
+}
diff --git a/Hyena.Data.Sqlite/Makefile.am b/Hyena.Data.Sqlite/Makefile.am
index 9abb9ef..c0a5524 100644
--- a/Hyena.Data.Sqlite/Makefile.am
+++ b/Hyena.Data.Sqlite/Makefile.am
@@ -4,6 +4,8 @@ LINK = -r:Mono.Posix -r:System -r:System.Core -r:$(DIR_BIN)/Hyena.dll
 
 SOURCES =  \
        Hyena.Data.Sqlite/ArrayDataReader.cs \
+       Hyena.Data.Sqlite/CacheableItem.cs \
+       Hyena.Data.Sqlite/CacheableSqliteModelProvider.cs \
        Hyena.Data.Sqlite/DatabaseColumn.cs \
        Hyena.Data.Sqlite/DatabaseColumnAttribute.cs \
        Hyena.Data.Sqlite/HyenaSqliteCommand.cs \
diff --git a/Hyena/Hyena.Data/ICacheableItem.cs b/Hyena/Hyena.Data/ICacheableItem.cs
index e370a18..6d6b2b3 100644
--- a/Hyena/Hyena.Data/ICacheableItem.cs
+++ b/Hyena/Hyena.Data/ICacheableItem.cs
@@ -26,8 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using Hyena.Data;
-
 namespace Hyena.Data
 {
     public interface ICacheableItem


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]