/[xmltv_parser]/trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs
ViewVC logotype

Diff of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 213 by william, Sun Mar 17 01:01:10 2013 UTC revision 219 by william, Sun Mar 17 01:43:24 2013 UTC
# Line 22  namespace GBPVRProgramDatabaseFixer Line 22  namespace GBPVRProgramDatabaseFixer
22          List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }          List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
23          List<SQLLITE.ICHANNEL> Channels { get; }          List<SQLLITE.ICHANNEL> Channels { get; }
24          IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);          IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
25          List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);          List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
26    
27          void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);          void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);
28          void UpdateGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs);          void UpdateGBPVRPrograms(List<IOldNewProgram> programs);
29    
30      }      }
31      public class SQLLITE : ISQLLITE      public class SQLLITE : ISQLLITE
32      {      {
33          private class OldNewProgram : IOldNewProgram          private class OldNewProgram : IOldNewProgram
34          {          {
35              public OldNewProgram() : this(new PROGRAMME(), new PROGRAMME()) { }              public OldNewProgram() : this(new PROGRAMME()) { }
36                public OldNewProgram(IPROGRAMME _old) : this(_old,_old) {  }
37              public OldNewProgram(IPROGRAMME _old, IPROGRAMME _new) { OldProgram = _old; NewProgram = _new; }              public OldNewProgram(IPROGRAMME _old, IPROGRAMME _new) { OldProgram = _old; NewProgram = _new; }
38    
39              public IPROGRAMME OldProgram { get; private set; }              public IPROGRAMME OldProgram { get; private set; }
# Line 347  namespace GBPVRProgramDatabaseFixer Line 348  namespace GBPVRProgramDatabaseFixer
348              return range;              return range;
349          }          }
350    
351          public List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)          public List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
352          {          {
353              source_invalid = new List<IPROGRAMME>();              source_invalid = new List<IPROGRAMME>();
354              List<IPROGRAMME> source_valid = new List<IPROGRAMME>();              List<IOldNewProgram> source_valid = new List<IOldNewProgram>();
355              gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();              gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
356              xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();              xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();
357              gbpvr_programs.TrimExcess();              gbpvr_programs.TrimExcess();
# Line 411  namespace GBPVRProgramDatabaseFixer Line 412  namespace GBPVRProgramDatabaseFixer
412                              throw new Exception(string.Format("Program oid '{0}' was not properly updated.", updated_program.oid));                              throw new Exception(string.Format("Program oid '{0}' was not properly updated.", updated_program.oid));
413                          }                          }
414                          //source_valid.Add(new OldNewProgram(program, updated_program));                          //source_valid.Add(new OldNewProgram(program, updated_program));
415                          source_valid.Add(updated_program);                          source_valid.Add(new OldNewProgram(program, updated_program));
416                      }                      }
417                      else                      else
418                      {                      {
419                          // data is the same                          // data is the same
420                          //source_valid.Add(new OldNewProgram(program, program));                          //source_valid.Add(new OldNewProgram(program, program));
421                          source_valid.Add(program);                          source_valid.Add(new OldNewProgram(program));
422                      }                      }
423                  }                  }
424                  index++;                  index++;
# Line 823  namespace GBPVRProgramDatabaseFixer Line 824  namespace GBPVRProgramDatabaseFixer
824    
825          public void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs)          public void RemoveOldGBPVRPrograms(List<SQLLITE.IPROGRAMME> programs)
826          {                      {            
827              //gLog.Info.WriteLine("Removing {0} gbpvr programs.", programs.Count);              gLog.Info.WriteLine("Removing {0} gbpvr programs.", programs.Count);
828              //double total = programs.Count;              int ChunkSize = 100;
             //double index = 0;  
             //double progress = 0;  
             //Stopwatch st = new Stopwatch();  
             //st.Start();  
             //foreach (var program in programs)  
             //{  
             //    progress = 100.0 * (index / total);  
             //    gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing GBPVR Program ({0:00}%) oid='{1}'", (int)progress, program.oid)));  
             //    if (!RemoveProgramDatabaseEntry(program))  
             //    {  
             //        gLog.Error.WriteLine("Failed to remove program with oid: {0}", program.oid);  
             //    }  
             //    index++;  
             //}  
             ////Stopwatch st = new Stopwatch();  
             ////st.Start();  
             ////if (!RemoveAllProgramDatabaseEntries())  
             ////{  
             ////    gLog.Error.WriteLine("Failed to remove one or more program(s)");  
             ////}  
             //st.Stop();  
             //gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds", st.Elapsed.TotalSeconds);  
   
             int ChunkSize = 1024;  
829              var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);              var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);
   
830              double total = programs.Count;              double total = programs.Count;
831              double index = 0;              double index = 0;
832              double progress = 0;              double progress = 0;
# Line 863  namespace GBPVRProgramDatabaseFixer Line 839  namespace GBPVRProgramDatabaseFixer
839                  {                  {
840                      gLog.Error.WriteLine("Failed to remove one or more program(s)");                      gLog.Error.WriteLine("Failed to remove one or more program(s)");
841                  }                  }
842                  gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing {0} gbpvr programs ({1} of {2}) {3:00}%", p.Count(), index, total, (int)progress)));                  gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
843                  index += (double)p.Count();                  index += (double)p.Count();
844              }              }
845                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Removing old gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
846              st1.Stop();              st1.Stop();
847              gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);              gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
848          }          }
849          public void UpdateGBPVRPrograms(List<IPROGRAMME> programs)          public void UpdateGBPVRPrograms(List<IOldNewProgram> programs)
850          {          {
851              int ChunkSize = 1024;              gLog.Info.WriteLine("Updating {0} gbpvr programs.", programs.Count);
852              var ChunkList = programs.Chunk<IPROGRAMME>(ChunkSize);              int ChunkSize = 1;
853                var ChunkList = programs.Chunk<IOldNewProgram>(ChunkSize);
854    
855              double total = programs.Count;              double total = programs.Count;
856              double index = 0;              double index = 0;
# Line 881  namespace GBPVRProgramDatabaseFixer Line 859  namespace GBPVRProgramDatabaseFixer
859              st1.Start();              st1.Start();
860              foreach (var p in ChunkList)              foreach (var p in ChunkList)
861              {              {
                 //Stopwatch st2 = new Stopwatch();  
                 //st2.Start();  
862                  progress = 100.0 * (index / total);                  progress = 100.0 * (index / total);
                 //gLog.Warn.WriteLine("Inserting {0} gbpvr programs ({1} of {2})", p.Count(), index, total);    
863                  if (!UpdateProgramEntryDatabase(p))                  if (!UpdateProgramEntryDatabase(p))
864                  {                  {
865                      gLog.Error.WriteLine("Failed to update one or more program(s)");                      gLog.Error.WriteLine("Failed to update one or more program(s)");
866                  }                  }
867                  //st2.Stop();                  gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
                 //gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds", st2.Elapsed.TotalSeconds);  
                 //gLog.Warn.WriteLine(System.Environment.NewLine);  
                 gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating {0} gbpvr programs ({1} of {2}) {3:00}%", p.Count(), index, total, (int)progress)));  
868                  index += (double)p.Count();                                  index += (double)p.Count();                
869              }              }
870                gLog.ReportProgress(this, new ReportProgressEventArgs((int)progress, string.Format("Updating gbpvr programs ({0} of {1}) {2:00}%", index, total, (int)progress)));
871              st1.Stop();              st1.Stop();
872              gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);              gLog.Warn.WriteLine("    operation took: {0:0.00000} seconds overall", st1.Elapsed.TotalSeconds);
873          }          }
# Line 979  namespace GBPVRProgramDatabaseFixer Line 952  namespace GBPVRProgramDatabaseFixer
952              }              }
953              return result;              return result;
954          }          }
955          private bool UpdateProgramEntryDatabase(IEnumerable<IPROGRAMME> list)          private bool UpdateProgramEntryDatabase(IEnumerable<IOldNewProgram> list)
956          {          {
957              bool result = false;              bool result = false;
958              try              try
# Line 1088  namespace GBPVRProgramDatabaseFixer Line 1061  namespace GBPVRProgramDatabaseFixer
1061          }          }
1062          #endregion          #endregion
1063          #region Multi-Update Command Support          #region Multi-Update Command Support
1064          private string BuildGBPVRMultiUpdateCommand(IEnumerable<IPROGRAMME> list)          private string BuildGBPVRMultiUpdateCommand(IEnumerable<IOldNewProgram> list)
1065          {          {
1066              StringBuilder builder = new StringBuilder();              StringBuilder builder = new StringBuilder();
1067              /*              /*
# Line 1100  namespace GBPVRProgramDatabaseFixer Line 1073  namespace GBPVRProgramDatabaseFixer
1073              builder.AppendLine("begin transaction;");              builder.AppendLine("begin transaction;");
1074              foreach (var t in list)              foreach (var t in list)
1075              {              {
1076                  builder.AppendLine(BuildGBPVRSingleInsertCommand(t));                  if (!t.OldProgram.Equals(t.NewProgram))
1077                    {
1078                        // only update the entry if it is different
1079                        builder.AppendLine(BuildGBPVRSingleUpdateCommand(t.NewProgram));
1080                    }                
1081              }              }
1082              builder.AppendLine("end transaction;");              builder.AppendLine("end transaction;");
1083              return builder.ToString();              return builder.ToString();

Legend:
Removed from v.213  
changed lines
  Added in v.219

  ViewVC Help
Powered by ViewVC 1.1.22