banshee r3023 - in trunk/banshee: . src/Core/Banshee.Core src/Core/Banshee.Core/Banshee.Base src/Core/Banshee.Services/Banshee.Library src/Core/Banshee.Services/Banshee.Sources src/Core/Hyena/Hyena.Data.Sqlite



Author: gburt
Date: Fri Jan 25 03:58:48 2008
New Revision: 3023
URL: http://svn.gnome.org/viewvc/banshee?rev=3023&view=rev

Log:
2008-01-24  Gabriel Burt  <gabriel burt gmail com>

	* src/Core/Banshee.Core/Makefile.am:
	* src/Core/Banshee.Core/Banshee.Base/RateLimiter.cs: New class that makes
	it easy to rate limit a given method.

	* src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs: Do
	the actual import on the main thread.

	* src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs: Rate limit
	the Reload method - ensuring no matter how often it's called, it won't
	actually be executed so much it degrades overall performance.

	* src/Core/Banshee.Services/Banshee.Sources/ISourceManager.cs:
	* src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs: Add setter
	for ActiveSource.

	* src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs: Use GetValue and
	SetValue methods of PropertyInfo objects instead of GetGetMethod etc.

	* src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs: For the Insert
	command, set the value of the primary key to null so Sqlite will set it
	appropriately


Added:
   trunk/banshee/src/Core/Banshee.Core/Banshee.Base/RateLimiter.cs
Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Core/Banshee.Core/Makefile.am
   trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/ISourceManager.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs
   trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs

Added: trunk/banshee/src/Core/Banshee.Core/Banshee.Base/RateLimiter.cs
==============================================================================
--- (empty file)
+++ trunk/banshee/src/Core/Banshee.Core/Banshee.Base/RateLimiter.cs	Fri Jan 25 03:58:48 2008
@@ -0,0 +1,72 @@
+//
+// RateLimiter.cs
+//
+// Author:
+//   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;
+using GLib;
+
+namespace Banshee.Base
+{
+    public class RateLimiter
+    {
+        public delegate void RateLimitedMethod ();
+
+        private RateLimitedMethod method;
+        private double min_interval_ms;
+        private DateTime last_executed = DateTime.MinValue;
+        private uint timeout_id = 0;
+
+        public RateLimiter (double min_interval_ms, RateLimitedMethod method)
+        {
+            this.min_interval_ms = min_interval_ms;
+            this.method = method;
+        }
+
+        public void Execute ()
+        {
+            if (timeout_id != 0)
+                return;
+
+            double delta = (DateTime.Now - last_executed).TotalMilliseconds;
+            if (delta >= min_interval_ms) {
+                method ();
+                last_executed = DateTime.Now;
+            } else {
+                //Console.WriteLine ("Method rate limited, setting timeout");
+                timeout_id = GLib.Timeout.Add((uint)min_interval_ms, OnRateLimitTimer);
+            }
+        }
+
+        private bool OnRateLimitTimer ()
+        {
+            timeout_id = 0;
+            method ();
+            last_executed = DateTime.Now;
+            return false;
+        }
+    }
+}

Modified: trunk/banshee/src/Core/Banshee.Core/Makefile.am
==============================================================================
--- trunk/banshee/src/Core/Banshee.Core/Makefile.am	(original)
+++ trunk/banshee/src/Core/Banshee.Core/Makefile.am	Fri Jan 25 03:58:48 2008
@@ -10,6 +10,7 @@
 	Banshee.Base/Paths.cs \
 	Banshee.Base/PlatformHacks.cs \
 	Banshee.Base/ProductInformation.cs \
+	Banshee.Base/RateLimiter.cs \
 	Banshee.Base/SafeUri.cs \
 	Banshee.Base/StringUtil.cs \
 	Banshee.Base/UriList.cs \

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Library/LibraryImportManager.cs	Fri Jan 25 03:58:48 2008
@@ -95,23 +95,30 @@
             try {
                 SafeUri uri = new SafeUri (path);
 
-                if (LibraryTrackInfo.ContainsUri (uri)) {
-                    IncrementProcessedCount (null);
-                    return;
+                LibraryTrackInfo track = null;
+                ThreadAssist.ProxyToMain (delegate {
+                    if (LibraryTrackInfo.ContainsUri (uri)) {
+                        IncrementProcessedCount (null);
+                        return;
+                    }
+
+                    TagLib.File file = StreamTagger.ProcessUri (uri);
+                    track = new LibraryTrackInfo ();
+                    StreamTagger.TrackInfoMerge (track, file);
+
+                    track.DateAdded = DateTime.Now;
+                    LibraryArtistInfo artist = new LibraryArtistInfo (track.ArtistName);
+                    track.ArtistId = artist.DbId;
+                    track.AlbumId = new LibraryAlbumInfo (artist, track.AlbumTitle).DbId;
+
+                    track.Save ();
+                    (ServiceManager.SourceManager.DefaultSource as LibrarySource).Reload ();
+
+                });
+                if (track != null && track.DbId > 0) {
+                    IncrementProcessedCount (String.Format ("{0} - {1}", track.DisplayArtistName, track.DisplayTrackTitle));
                 }
 
-                TagLib.File file = StreamTagger.ProcessUri (uri);
-                LibraryTrackInfo track = new LibraryTrackInfo ();
-                StreamTagger.TrackInfoMerge (track, file);
-
-                track.DateAdded = DateTime.Now;
-                LibraryArtistInfo artist = new LibraryArtistInfo (track.ArtistName);
-                track.ArtistId = artist.DbId;
-                track.AlbumId = new LibraryAlbumInfo (artist, track.AlbumTitle).DbId;
-
-                track.Save ();
-                
-                IncrementProcessedCount (String.Format ("{0} - {1}", track.DisplayArtistName, track.DisplayTrackTitle));
             } catch (Exception e) {
                 LogError (path, e);
                 IncrementProcessedCount (null);

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs	Fri Jan 25 03:58:48 2008
@@ -34,6 +34,7 @@
 using Hyena.Data;
 using Hyena.Collections;
 
+using Banshee.Base;
 using Banshee.ServiceStack;
 using Banshee.Sources;
 using Banshee.Collection;
@@ -48,6 +49,8 @@
         protected TrackListDatabaseModel track_model;
         protected AlbumListDatabaseModel album_model;
         protected ArtistListDatabaseModel artist_model;
+
+        protected RateLimiter reload_limiter;
         
         public DatabaseSource (string generic_name, string name, string id, int order) : base (generic_name, name, order)
         {
@@ -55,6 +58,7 @@
             track_model = new TrackListDatabaseModel (ServiceManager.DbConnection, uuid);
             album_model = new AlbumListDatabaseModel (track_model, ServiceManager.DbConnection, uuid);
             artist_model = new ArtistListDatabaseModel (track_model, ServiceManager.DbConnection, uuid);
+            reload_limiter = new RateLimiter (50.0, RateLimitedReload);
         }
 
 #region Public Properties
@@ -102,6 +106,11 @@
 
         public virtual void Reload ()
         {
+            reload_limiter.Execute ();
+        }
+
+        protected virtual void RateLimitedReload ()
+        {
             track_model.Reload ();
             artist_model.Reload ();
             album_model.Reload ();

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/ISourceManager.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/ISourceManager.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/ISourceManager.cs	Fri Jan 25 03:58:48 2008
@@ -39,7 +39,7 @@
     public interface ISourceManager : IDBusExportable
     {
         //event SourceEventHandler SourceUpdated; 
-        ISource ActiveSource { get; }
+        ISource ActiveSource { get; set; }
         ISource DefaultSource { get; }
         string [] Sources { get; }
     }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs	Fri Jan 25 03:58:48 2008
@@ -213,6 +213,7 @@
         
         ISource ISourceManager.ActiveSource {
             get { return ActiveSource; }
+            set { value.Activate (); }
         }
         
         public void SetActiveSource(Source source)

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	Fri Jan 25 03:58:48 2008
@@ -83,25 +83,11 @@
         {
             return field_info != null
                 ? field_info.GetValue (target)
-                : property_info.GetGetMethod (true).Invoke (target, null);
+                : property_info.GetValue (target, null);
         }
         
         public void SetValue (object target, IDataReader reader, int column)
         {
-            SetValue (target, SetValue (type, reader, column));
-        }
-        
-        public void SetValue (object target, object value)
-        {
-            if (field_info != null) {
-                field_info.SetValue (target, value);
-            } else {
-                property_info.GetSetMethod (true).Invoke (target, new object[] { value });
-            }
-        }
-        
-        private static object SetValue (Type type, IDataReader reader, int column)
-        {
             // FIXME should we insist on nullable types?
             object result;
             if (type == typeof (string)) {
@@ -117,7 +103,17 @@
                     ? reader.GetInt64 (column)
                     : 0;
             }
-            return result;
+
+            SetValue (target, result);
+        }
+        
+        public void SetValue (object target, object value)
+        {
+            if (field_info != null) {
+                field_info.SetValue (target, value);
+            } else {
+                property_info.SetValue (target, value, null);
+            }
         }
         
         public string Name {

Modified: trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs
==============================================================================
--- trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs	(original)
+++ trunk/banshee/src/Core/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs	Fri Jan 25 03:58:48 2008
@@ -247,7 +247,12 @@
         protected virtual void PrepareInsertCommand (T target)
         {
             for (int i = 0; i < columns.Count; i++) {
-                InsertCommand.Parameters[i].Value = columns[i].GetValue (target);
+                if (columns[i] != key) {
+                    InsertCommand.Parameters[i].Value = columns[i].GetValue (target);
+                } else {
+                    // On insert, the key needs to be NULL to be automatically set by Sqlite
+                    InsertCommand.Parameters[i].Value = null;
+                }
             }
         }
         



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