diff options
Diffstat (limited to 'p2pvr/daemon/schedules.cpp')
-rw-r--r-- | p2pvr/daemon/schedules.cpp | 111 |
1 files changed, 56 insertions, 55 deletions
diff --git a/p2pvr/daemon/schedules.cpp b/p2pvr/daemon/schedules.cpp index 90a42a9..f343e91 100644 --- a/p2pvr/daemon/schedules.cpp +++ b/p2pvr/daemon/schedules.cpp @@ -1,34 +1,35 @@ -#include <pch.hpp> #include "schedules.h" -#include <rdbmsDataSource.h> #include <logger.h> #include <Ice/Ice.h> -#include <sqlVariableBinder.h> -#include <sqlMergeTask.h> -#include "p2Helpers.h" #include "commonHelpers.h" -#include "containerIterator.h" -#include "resources.h" #include <boost/date_time/posix_time/posix_time.hpp> #include <factory.impl.h> #include <slicer/db/sqlSelectDeserializer.h> #include <slicer/db/sqlInsertSerializer.h> #include <slicer/db/sqlUpdateSerializer.h> +#include <slicer/db/sqlTablePatchSerializer.h> +#include <tablepatch.h> #include <p2pvr-int.h> -#include <privateExecContext.h> -ResourceString(Schedules_getCandidates, sql_Schedules_getCandidates); -ResourceString(Schedules_delete, sql_Schedules_delete); -ResourceString(Schedules_selectAll, sql_Schedules_selectAll); -ResourceString(Schedules_selectById, sql_Schedules_selectById); -ResourceString(Schedules_scheduledToRecord, sql_Schedules_scheduledToRecord); +#include "sql/Schedules_getCandidates.sql.h" +#include "sql/Schedules_delete.sql.h" +#include "sql/Schedules_selectAll.sql.h" +#include "sql/Schedules_selectById.sql.h" +#include "sql/Schedules_scheduledToRecord.sql.h" -std::string Schedules::SchedulerAlgorithm; +namespace po = boost::program_options; -DECLARE_OPTIONS(Schedules, "P2PVR Scheduler options") -("p2pvr.scheduler.algorithm", Options::value(&SchedulerAlgorithm, "BitDumb"), - "Implementation of episode group scheduler problem solver") -END_OPTIONS() +IceTray::Logging::LoggerPtr Schedules::logger(LOGMANAGER()->getLogger<Schedules>()); + +Schedules::Options::Options() : + IceTray::Options("P2PVR Scheduler options") +{ +} + +ICETRAY_OPTIONS(Schedules::Options, + ("p2pvr.scheduler.algorithm", po::value(&SchedulerAlgorithm)->default_value("BitDumb"), + "Implementation of episode group scheduler problem solver") +) Showing::Showing(unsigned int e, unsigned int t, unsigned int sc, datetime start, datetime stop, int p, const Episode * ep) : episode(ep), @@ -162,6 +163,11 @@ EpisodeGroup::SuggestWithFeedback(const Showings & showings) } } +Schedules::Schedules(IceTray::DatabasePoolPtr db) : + IceTray::AbstractDatabaseClient(db) +{ +} + void Schedules::GetEpisodeIntersects(Episodes & all, Episodes & grouped) { @@ -171,7 +177,7 @@ Schedules::GetEpisodeIntersects(Episodes & all, Episodes & grouped) for (const auto & gs : ge->showings) { for (const auto & as : ae->showings) { if (gs->period.intersects(as->period)) { - Logger()->messagebf(LOG_DEBUG, " added %s", ae->what); + logger->messagebf(LOG::DEBUG, " added %s", ae->what); grouped.push_back(ae); all.erase(aei); GetEpisodeIntersects(all, grouped); @@ -191,8 +197,7 @@ Schedules::DoReschedule(const Ice::Current & ice) unsigned int tunerCount = devs->TunerCount(); // Load list from database - auto episodes = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, P2PVR::ScheduleCandidates>( - *Select(Schedules_getCandidates).second); + auto episodes = fetch<P2PVR::ScheduleCandidates>(P2PVR::sql::Schedules_getCandidates); Episodes scheduleList; Showings allShowings; @@ -209,9 +214,9 @@ Schedules::DoReschedule(const Ice::Current & ice) cur->showings.push_back(s); allShowings.push_back(s); } - Logger()->messagebf(LOG_DEBUG, "%d episodes created, %s showings", scheduleList.size(), allShowings.size()); + logger->messagebf(LOG::DEBUG, "%d episodes created, %s showings", scheduleList.size(), allShowings.size()); for (const auto & e : scheduleList) { - Logger()->messagebf(LOG_DEBUG, " %s", e->what); + logger->messagebf(LOG::DEBUG, " %s", e->what); for (const auto & s : e->showings) { s->priority += 1 - minPriority; e->priority += s->priority; @@ -223,7 +228,7 @@ Schedules::DoReschedule(const Ice::Current & ice) // Solve while (!scheduleList.empty()) { auto work = scheduleList.begin(); - Logger()->messagebf(LOG_DEBUG, "start %s", (*work)->what); + logger->messagebf(LOG::DEBUG, "start %s", (*work)->what); Episodes group; group.push_back(*work); scheduleList.erase(work); @@ -234,17 +239,17 @@ Schedules::DoReschedule(const Ice::Current & ice) return a->what < b->what; }); - Logger()->messagebf(LOG_DEBUG, "group created with %d episodes", group.size()); + logger->messagebf(LOG::DEBUG, "group created with %d episodes", group.size()); double total = 1; // Measure and add the optional to not record for (const auto & e : group) { - Logger()->messagebf(LOG_DEBUG, " %d * %d:%s", e->showings.size(), e->priority, e->what); + logger->messagebf(LOG::DEBUG, " %d * %d:%s", e->showings.size(), e->priority, e->what); e->showings.push_back(NULL); total *= e->showings.size(); } - Logger()->messagebf(LOG_DEBUG, "group complexity of %d options", total); + logger->messagebf(LOG::DEBUG, "group complexity of %d options", total); - EpisodeGroupPtr sched = EpisodeGroupPtr(EpisodeGroupFactory::createNew(SchedulerAlgorithm, group)); + EpisodeGroupPtr sched = EpisodeGroupPtr(EpisodeGroupFactory::createNew(options->SchedulerAlgorithm, group)); sched->tuners = tunerCount; std::set<ShowingPtr> selected; for (const auto & s : sched->Solve()) { @@ -252,17 +257,17 @@ Schedules::DoReschedule(const Ice::Current & ice) } for (const auto & c : group) { - Logger()->messagebf(LOG_DEBUG, "Episode %s, %d options", c->what, c->showings.size()); + logger->messagebf(LOG::DEBUG, "Episode %s, %d options", c->what, c->showings.size()); for (const auto & i : c->showings) { if (selected.find(i) != selected.end()) { - Logger()->messagebf(LOG_DEBUG, " %s - %s (%d) <-", i->startTime, i->stopTime, i->transportStreamId); + logger->messagebf(LOG::DEBUG, " %s - %s (%d) <-", i->startTime, i->stopTime, i->transportStreamId); } else if (i) { - Logger()->messagebf(LOG_DEBUG, " %s - %s (%d)", i->startTime, i->stopTime, i->transportStreamId); + logger->messagebf(LOG::DEBUG, " %s - %s (%d)", i->startTime, i->stopTime, i->transportStreamId); } } } - Logger()->message(LOG_DEBUG, "----------"); + logger->message(LOG::DEBUG, "----------"); for (const auto & c : group) { bool found = false; for (const auto & i : c->showings) { @@ -282,14 +287,11 @@ Schedules::DoReschedule(const Ice::Current & ice) } } - TxHelper tx(this); - PrivateExecContext ec; - SqlMergeTask mergeRecords("postgres", "record"); - auto rcs = CreateColumns<P2PVR::ScheduledToRecordPtr>(boost::bind(&DatabaseClient::SqlMergeColumnsInserter, &mergeRecords, _1, _2)); - mergeRecords.sources.insert(new ContainerIterator<P2PVR::ScheduledToRecordList>(&records, rcs)); - mergeRecords.loadComplete(this); - mergeRecords.execute(&ec); - tx.Commit(); + auto dbc = db->get(); + DB::TransactionScope tx(dbc.get()); + DB::TablePatch mergeRecords; + mergeRecords.dest = "record"; + Slicer::SerializeAny<Slicer::SqlTablePatchSerializer>(records, dbc.get(), mergeRecords); auto recorder = P2PVR::RecorderPrx::checkedCast(ice.adapter->createProxy(ice.adapter->getCommunicator()->stringToIdentity("Recorder"))); recorder->RefreshSchedules(); @@ -298,25 +300,25 @@ Schedules::DoReschedule(const Ice::Current & ice) void Schedules::DeleteSchedule(int id, const Ice::Current & ice) { - TxHelper tx(this); - Modify(Schedules_delete, id).second->execute(); + auto dbc = db->get(); + auto del = P2PVR::sql::Schedules_delete.modify(dbc.get()); + del->bindParamI(0, id); + del->execute(); DoReschedule(ice); } P2PVR::ScheduleList Schedules::GetSchedules(const Ice::Current &) { - Logger()->message(LOG_DEBUG, __PRETTY_FUNCTION__); - return Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, P2PVR::ScheduleList>( - *Select(Schedules_selectAll).second); + logger->message(LOG::DEBUG, __PRETTY_FUNCTION__); + return fetch<P2PVR::ScheduleList>(P2PVR::sql::Schedules_selectAll); } P2PVR::SchedulePtr Schedules::GetSchedule(int id, const Ice::Current &) { - Logger()->messagebf(LOG_DEBUG, "%s(%d)", __PRETTY_FUNCTION__, id); - auto schedules = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, P2PVR::ScheduleList>( - *Select(Schedules_selectById, id).second); + logger->messagebf(LOG::DEBUG, "%s(%d)", __PRETTY_FUNCTION__, id); + auto schedules = fetch<P2PVR::ScheduleList>(P2PVR::sql::Schedules_selectById, id); if (schedules.empty()) throw P2PVR::NotFound(); return schedules.front(); } @@ -324,16 +326,15 @@ Schedules::GetSchedule(int id, const Ice::Current &) P2PVR::ScheduledToRecordList Schedules::GetScheduledToRecord(const Ice::Current &) { - Logger()->message(LOG_DEBUG, __PRETTY_FUNCTION__); - return Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, P2PVR::ScheduledToRecordList>( - *Select(Schedules_scheduledToRecord).second); + logger->message(LOG::DEBUG, __PRETTY_FUNCTION__); + return fetch<P2PVR::ScheduledToRecordList>(P2PVR::sql::Schedules_scheduledToRecord); } Ice::Int Schedules::NewSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) { - TxHelper tx(this); - Store<Slicer::SqlFetchIdInsertSerializer>(s, "schedules"); + auto dbc = db->get(); + Slicer::SerializeAny<Slicer::SqlFetchIdInsertSerializer>(s, dbc.get(), "schedules"); DoReschedule(ice); return s->ScheduleId; } @@ -341,8 +342,8 @@ Schedules::NewSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) void Schedules::UpdateSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) { - TxHelper tx(this); - Store<Slicer::SqlUpdateSerializer>(s, "schedules"); + auto dbc = db->get(); + Slicer::SerializeAny<Slicer::SqlUpdateSerializer>(s, dbc.get(), "schedules"); DoReschedule(ice); } |