[banshee] [Shuffler] Add table/method to record discards



commit 2d85e5e83465fb7cb0d1159232c59d6477419af2
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Mar 2 23:00:12 2010 -0800

    [Shuffler] Add table/method to record discards
    
    Will be used by Play Queue to record when a song was removed, so they
    can be used as anti-seeds (and avoided in general).

 .../Banshee.Collection.Database/RandomBy.cs        |    7 +----
 .../Banshee.Collection.Database/Shuffler.cs        |   21 ++++++++++++-
 .../Banshee.Database/BansheeDbFormatMigrator.cs    |   30 ++++++++++++++++++-
 3 files changed, 48 insertions(+), 10 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Collection.Database/RandomBy.cs b/src/Core/Banshee.Services/Banshee.Collection.Database/RandomBy.cs
index 57b0025..e8fb46d 100644
--- a/src/Core/Banshee.Services/Banshee.Collection.Database/RandomBy.cs
+++ b/src/Core/Banshee.Services/Banshee.Collection.Database/RandomBy.cs
@@ -44,8 +44,6 @@ namespace Banshee.Collection.Database
         protected DatabaseTrackListModel Model { get; private set; }
         protected IDatabaseTrackModelCache Cache { get; private set; }
 
-        private HyenaSqliteCommand insert_shuffle;
-
         protected Shuffler Shuffler { get; private set; }
 
         public string Id { get; private set; }
@@ -64,7 +62,6 @@ namespace Banshee.Collection.Database
         public RandomBy (string id)
         {
             Id = id;
-            insert_shuffle = new HyenaSqliteCommand ("INSERT OR REPLACE INTO CoreShuffles (ShufflerID, TrackID, LastShuffledAt) VALUES (?, ?, ?)");
         }
 
         public void SetShuffler (Shuffler shuffler)
@@ -127,9 +124,7 @@ namespace Banshee.Collection.Database
                 var track = GetShufflerTrack (after);
 
                 // Record this shuffle
-                if (track != null) {
-                    ServiceManager.DbConnection.Execute (insert_shuffle, Shuffler.DbId, track.TrackId, DateTime.Now);
-                }
+                Shuffler.RecordShuffle (track);
 
                 return track;
             }
diff --git a/src/Core/Banshee.Services/Banshee.Collection.Database/Shuffler.cs b/src/Core/Banshee.Services/Banshee.Collection.Database/Shuffler.cs
index 754d8c5..89fada9 100644
--- a/src/Core/Banshee.Services/Banshee.Collection.Database/Shuffler.cs
+++ b/src/Core/Banshee.Services/Banshee.Collection.Database/Shuffler.cs
@@ -45,6 +45,9 @@ namespace Banshee.Collection.Database
     {
         public static readonly Shuffler Playback = new Shuffler () { Id = "playback", DbId = 0 };
 
+        private static HyenaSqliteCommand add_shuffle_cmd = new HyenaSqliteCommand ("INSERT OR REPLACE INTO CoreShuffles (ShufflerID, TrackID, LastShuffledAt) VALUES (?, ?, ?)");
+        private static HyenaSqliteCommand add_discard_cmd = new HyenaSqliteCommand ("INSERT OR REPLACE INTO CoreShuffleDiscards (ShufflerID, TrackID, LastDiscardedAt) VALUES (?, ?, ?)");
+
         private DateTime random_began_at = DateTime.MinValue;
         private DateTime last_random = DateTime.MinValue;
         private List<RandomBy> random_modes;
@@ -90,7 +93,7 @@ namespace Banshee.Collection.Database
 
                 if (random_by != null) {
                     if (!random_by.GetType ().AssemblyQualifiedName.Contains ("Banshee.Service")) {
-                        Log.DebugFormat ("Loaded RandomBy: {0}", random_by.Id);
+                        Log.DebugFormat ("Loaded new mode into {0} shuffler: {1}", this.Id, random_by.Id);
                     }
                     var handler = RandomModeAdded;
                     if (handler != null) {
@@ -107,7 +110,7 @@ namespace Banshee.Collection.Database
                 }
 
                 if (random_by != null) {
-                    Log.DebugFormat ("Removed RandomBy: {0}", random_by.Id);
+                    Log.DebugFormat ("Removed mode from {0} shuffler: {1}", this.Id, random_by.Id);
                     var handler = RandomModeRemoved;
                     if (handler != null) {
                         handler (random_by);
@@ -153,6 +156,20 @@ namespace Banshee.Collection.Database
             }
         }
 
+        public void RecordShuffle (DatabaseTrackInfo track)
+        {
+            if (track != null) {
+                ServiceManager.DbConnection.Execute (add_shuffle_cmd, this.DbId, track.TrackId, DateTime.Now);
+            }
+        }
+
+        public void RecordDiscard (DatabaseTrackInfo track)
+        {
+            if (track != null) {
+                ServiceManager.DbConnection.Execute (add_discard_cmd, this.DbId, track.TrackId, DateTime.Now);
+            }
+        }
+
         private TrackInfo GetRandomTrack (string mode, bool repeat, bool resetSinceTime)
         {
             foreach (var r in random_modes) {
diff --git a/src/Core/Banshee.Services/Banshee.Database/BansheeDbFormatMigrator.cs b/src/Core/Banshee.Services/Banshee.Database/BansheeDbFormatMigrator.cs
index 38891f4..37e9bf5 100644
--- a/src/Core/Banshee.Services/Banshee.Database/BansheeDbFormatMigrator.cs
+++ b/src/Core/Banshee.Services/Banshee.Database/BansheeDbFormatMigrator.cs
@@ -56,7 +56,7 @@ namespace Banshee.Database
         // NOTE: Whenever there is a change in ANY of the database schema,
         //       this version MUST be incremented and a migration method
         //       MUST be supplied to match the new version number
-        protected const int CURRENT_VERSION = 39;
+        protected const int CURRENT_VERSION = 40;
         protected const int CURRENT_METADATA_VERSION = 7;
 
 #region Migration Driver
@@ -809,7 +809,7 @@ namespace Banshee.Database
         {
             Execute(@"
                 CREATE TABLE CoreShuffles (
-                    ShufflerId           INTEGER,
+                    ShufflerId          INTEGER,
                     TrackID             INTEGER,
                     LastShuffledAt      INTEGER,
                     CONSTRAINT one_entry_per_track UNIQUE (ShufflerID, TrackID)
@@ -890,6 +890,22 @@ namespace Banshee.Database
 
 #endregion
 
+        [DatabaseVersion (40)]
+        private bool Migrate_40 ()
+        {
+            Execute(@"
+                CREATE TABLE CoreShuffleDiscards (
+                    ShufflerId           INTEGER,
+                    TrackID              INTEGER,
+                    LastDiscardedAt      INTEGER,
+                    CONSTRAINT one_entry_per_track UNIQUE (ShufflerID, TrackID)
+                )
+            ");
+            Execute("CREATE INDEX CoreShuffleDiscardsIndex ON CoreShuffleDiscards (ShufflerId, TrackID, LastDiscardedAt)");
+            return true;
+        }
+
+
 #pragma warning restore 0169
 
 #region Fresh database setup
@@ -1117,6 +1133,16 @@ namespace Banshee.Database
                     Id              TEXT UNIQUE
                 )
             ");
+
+            Execute(@"
+                CREATE TABLE CoreShuffleDiscards (
+                    ShufflerId           INTEGER,
+                    TrackID              INTEGER,
+                    LastDiscardedAt      INTEGER,
+                    CONSTRAINT one_entry_per_track UNIQUE (ShufflerID, TrackID)
+                )
+            ");
+            Execute("CREATE INDEX CoreShuffleDiscardsIndex ON CoreShuffleDiscards (ShufflerId, TrackID, LastDiscardedAt)");
         }
 
 #endregion



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