[mythtv] [PATCH] Max Episodes refinements

Kevin Kuphal kuphal at dls.net
Sun Nov 21 00:46:46 UTC 2004


This patch does the following:

* Changes Max Episode expiration from multiple queries to a single query 
which made the code alot simpler.

* Changes Max Episode behavior from keying off title to keying off 
recordid.  This addresses the problem where if you have two recording 
schedules for the same program title with different parameters (like Law 
and Order on NBC on Wednesday nights and a second schedule for Law and 
Order on A&E all the time) that the expiration code would use all Law 
and Order episodes for expiration purposes even if you had different 
expiration settings such as "keep all episodes" for NBC but "keep 3 max 
episodes" for A&E.  The A&E schedule would expire the NBC program.  Now 
it uses recordid to limit checks to the recording schedule that shows 
belong to as well as respect different max episode settings per schedule.

I'll probably have an episode preservation patch to allow ignoring of 
max episodes for certain shows but I need to get upgraded to latest CVS 
as that will include a DB change.

Kevin
-------------- next part --------------
Index: mythtv/programs/mythbackend/autoexpire.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythbackend/autoexpire.cpp,v
retrieving revision 1.16
diff -n -u -r1.16 autoexpire.cpp
--- mythtv/programs/mythbackend/autoexpire.cpp	5 Oct 2004 02:51:46 -0000	1.16
+++ mythtv/programs/mythbackend/autoexpire.cpp	21 Nov 2004 00:37:45 -0000
@@ -138,60 +138,49 @@
 
 void AutoExpire::ExpireEpisodesOverMax(void)
 {
-    QMap<QString, int> maxEpisodes;
-    QMap<QString, int>::Iterator maxIter;
+    /*
+        This query does the following:
 
-    QString fileprefix = gContext->GetFilePrefix();
-    QString querystr = "SELECT title, maxepisodes "
-                       "FROM record WHERE maxepisodes > 0 "
-                       "ORDER BY title ASC, maxepisodes DESC";
+        Selects the channel ID, starttime, maxepisodes and title for a recording
+        by joining the recorded shows and recordings tables
+        and looking for recorded show from a recording that defines max episodes
+        where the number of recorded shows is greater than the max episode limit
+        and sorts them in descending time order (earliest first)
+
+        Channel ID and starttime are used to expire.  Title is used for logging.
+
+        By using recordid, we only expire shows from the recording they were recorded
+        from.  Previously this was done by title which allowed shows to be expired even
+        if they were recorded from a recording that did not define max episodes if there
+        was an identical recording (by title) which did.
+    */
+    QString querystr = "SELECT recorded.chanid, recorded.starttime, record.maxepisodes, recorded.title "
+                       "FROM record, recorded "
+                       "WHERE record.recordid = recorded.recordid "
+                       "AND record.maxepisodes > 0 "
+                       "GROUP BY recorded.recordid "
+                       "HAVING COUNT( recorded.title ) > record.maxepisodes "
+                       "ORDER BY recorded.starttime ASC";
 
     QSqlQuery query = db->exec(querystr);
 
     if (query.isActive() && query.numRowsAffected() > 0)
     {
         while (query.next()) {
-            maxEpisodes[query.value(0).toString()] = query.value(1).toInt();
-        }
-    }
+                QString msg = QString("Expiring \"%1\" from %2, "
+                                      "too many episodes.")
+                                      .arg(query.value(3).toString())
+                                      .arg(query.value(1).toString());
+                VERBOSE(VB_GENERAL, msg);
+                gContext->LogEntry("autoexpire", LP_NOTICE, "Expired program", msg);
+
+                QString message = QString("AUTO_EXPIRE %1 %2")
+                                          .arg(query.value(0).toString())
+                                          .arg(query.value(1).toDateTime()
+                                          .toString(Qt::ISODate));
 
-    for(maxIter = maxEpisodes.begin(); maxIter != maxEpisodes.end(); maxIter++)
-    {
-        QString sqltitle(maxIter.key());
-        sqltitle.replace(QRegExp("\'"), "\\'");
-        sqltitle.replace(QRegExp("\""), "\\\"");
-
-        querystr = QString( "SELECT chanid, starttime FROM recorded "
-                            "WHERE title = \"%1\" "
-                            "ORDER BY starttime DESC;")
-                            .arg(sqltitle);
-
-        query = db->exec(querystr);
-
-        if (query.isActive() && query.numRowsAffected() > 0)
-        {
-            int found = 0;
-            while (query.next()) {
-                found++;
-
-                if (found > maxIter.data())
-                {
-                    QString msg = QString("Expiring \"%1\" from %2, "
-                                          "too many episodes.")
-                                          .arg(maxIter.key())
-                                          .arg(query.value(1).toString());
-                    VERBOSE(VB_GENERAL, msg);
-                    gContext->LogEntry("autoexpire", LP_NOTICE, "Expired program", msg);
-
-                    QString message = QString("AUTO_EXPIRE %1 %2")
-                                              .arg(query.value(0).toString())
-                                              .arg(query.value(1).toDateTime()
-                                                   .toString(Qt::ISODate));
-
-                    MythEvent me(message);
-                    gContext->dispatchNow(me);
-                }
-            }
+                MythEvent me(message);
+                gContext->dispatchNow(me);
         }
     }
 }


More information about the mythtv-dev mailing list