Re: [Banshee-List] Track dictionary in TrackListDatabaseModel
- From: "Gabriel Burt" <gabriel burt gmail com>
- To: banshee-list gnome org
- Subject: Re: [Banshee-List] Track dictionary in TrackListDatabaseModel
- Date: Mon, 7 Jan 2008 17:08:39 -0600
On Jan 5, 2008 5:33 AM, LCID Fire <lcid-fire gmx net> wrote:
> I was looking into TrackListDatabaseModel and wondered why the tracks
> are managed in a Dictionary?
> Currently it's defined like:
> private Dictionary<int, LibraryTrackInfo> tracks = new Dictionary<int,
> LibraryTrackInfo>();
Unless I've misunderstood what you're talking about,
TrackListDatabaseModel in trunk does not have a dictionary itself, but
has one via it's cache adapter. It is possible to implement the cache
adapter with an array, and I in fact I wrote a patch a couple weeks
ago to test it. I've attached the patch.
Gabriel
Index: src/Core/Hyena/Makefile.am
===================================================================
--- src/Core/Hyena/Makefile.am (revision 2916)
+++ src/Core/Hyena/Makefile.am (working copy)
@@ -19,12 +19,15 @@
Hyena.Data.Query/QueryTermNode.cs \
Hyena.Data.Query/QueryToken.cs \
Hyena.Data.Query/SqlQueryGenerator.cs \
+ Hyena.Data/ArrayModelCache.cs \
Hyena.Data/CacheableModelAdapter.cs \
Hyena.Data/ColumnDescription.cs \
+ Hyena.Data/DictionaryModelCache.cs \
Hyena.Data/ICacheableModel.cs \
Hyena.Data/ICareAboutView.cs \
Hyena.Data/IFilterable.cs \
Hyena.Data/IListModel.cs \
+ Hyena.Data/IModelCache.cs \
Hyena.Data/IPropertyStoreExpose.cs \
Hyena.Data/ISortable.cs \
Hyena.Data/ISortableColumn.cs \
Index: src/Core/Hyena/Hyena.Data/CacheableModelAdapter.cs
===================================================================
--- src/Core/Hyena/Hyena.Data/CacheableModelAdapter.cs (revision 2916)
+++ src/Core/Hyena/Hyena.Data/CacheableModelAdapter.cs (working copy)
@@ -34,26 +34,27 @@
public abstract class CacheableModelAdapter<T>
{
private ICacheableModel model;
- private Dictionary<int, T> cache = new Dictionary<int, T> ();
+ private IModelCache<T> cache;
- public CacheableModelAdapter (ICacheableModel model)
+ public CacheableModelAdapter (ICacheableModel model, IModelCache<T> cache)
{
this.model = model;
+ this.cache = cache;
}
- protected virtual Dictionary<int, T> Cache {
+ protected virtual IModelCache<T> Cache {
get { return cache; }
}
public virtual T GetValue (int index)
{
- if (Cache.ContainsKey (index))
- return Cache[index];
-
+ if (cache.ContainsKey (index))
+ return cache[index];
+
FetchSet (index, model.FetchCount);
- if (Cache.ContainsKey (index))
- return Cache[index];
+ if (cache.ContainsKey (index))
+ return cache[index];
return default (T);
}
@@ -71,7 +72,7 @@
protected void InvalidateManagedCache ()
{
- Cache.Clear ();
+ cache.Clear ();
}
}
}
Index: src/Core/Banshee.Services/Banshee.Collection.Database/BansheeCacheableModelAdapter.cs
===================================================================
--- src/Core/Banshee.Services/Banshee.Collection.Database/BansheeCacheableModelAdapter.cs (revision 2916)
+++ src/Core/Banshee.Services/Banshee.Collection.Database/BansheeCacheableModelAdapter.cs (working copy)
@@ -53,7 +53,8 @@
private static bool cache_initialized = false;
public BansheeCacheableModelAdapter (BansheeDbConnection connection, IDatabaseModel<T> model)
- : base ((ICacheableModel) model)
+ : base ((ICacheableModel) model, new ArrayModelCache<T> ((ICacheableModel) model))
+ //: base ((ICacheableModel) model, new DictionaryModelCache<T> ())
{
this.db_model = model;
this.connection = connection;
--- /dev/null 2007-12-22 01:09:22.000000000 -0600
+++ src/Core/Hyena/Hyena.Data/IModelCache.cs 2007-12-20 13:36:22.000000000 -0600
@@ -0,0 +1,41 @@
+//
+// IModelCache.cs
+//
+// Author:
+// Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2007 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;
+using System.Collections.Generic;
+
+namespace Hyena.Data
+{
+ public interface IModelCache<T>
+ {
+ bool ContainsKey (int i);
+ void Add (int i, T item);
+ T this[int i] { get; }
+ void Clear ();
+ }
+}
--- /dev/null 2007-12-22 01:09:22.000000000 -0600
+++ src/Core/Hyena/Hyena.Data/ArrayModelCache.cs 2007-12-20 14:02:34.000000000 -0600
@@ -0,0 +1,78 @@
+//
+// ArrayModelCache.cs
+//
+// Author:
+// Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2007 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;
+
+namespace Hyena.Data
+{
+ public class ArrayModelCache<T> : IModelCache<T>
+ {
+ private ICacheableModel model;
+ private T [] cache;
+ private int offset = -1;
+ private int limit = 0;
+
+ public ArrayModelCache (ICacheableModel model)
+ {
+ this.model = model;
+ cache = new T [model.FetchCount];
+ }
+
+ public bool ContainsKey (int i)
+ {
+ return (i >= offset &&
+ i <= (offset + limit));
+ }
+
+ public void Add (int i, T item)
+ {
+ if (cache.Length != model.FetchCount) {
+ cache = new T [model.FetchCount];
+ Clear ();
+ }
+
+ if (offset == -1 || i < offset || i >= (offset + cache.Length)) {
+ offset = i;
+ limit = 0;
+ }
+
+ cache [i - offset] = item;
+ limit++;
+ }
+
+ public T this [int i] {
+ get { return cache [i - offset]; }
+ }
+
+ public void Clear ()
+ {
+ offset = -1;
+ limit = 0;
+ }
+ }
+}
--- /dev/null 2007-12-22 01:09:22.000000000 -0600
+++ src/Core/Hyena/Hyena.Data/DictionaryModelCache.cs 2007-12-20 13:37:34.000000000 -0600
@@ -0,0 +1,61 @@
+//
+// DictionaryModelCache.cs
+//
+// Author:
+// Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2007 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;
+using System.Collections.Generic;
+
+namespace Hyena.Data
+{
+ public class DictionaryModelCache<T> : IModelCache<T>
+ {
+ private Dictionary<int, T> cache = new Dictionary<int, T> ();
+
+ public DictionaryModelCache ()
+ {
+ }
+
+ public bool ContainsKey (int i)
+ {
+ return cache.ContainsKey (i);
+ }
+
+ public void Add (int i, T item)
+ {
+ cache.Add (i, item);
+ }
+
+ public T this [int i] {
+ get { return cache [i]; }
+ }
+
+ public void Clear ()
+ {
+ cache.Clear ();
+ }
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]