From 175596644e6f23231878845bd4ed56b1165f49f5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 24 Oct 2015 14:58:54 +0100 Subject: Use slicer for inserts and updates --- p2pvr/daemon/recordings.cpp | 10 ++++------ p2pvr/daemon/recordings.h | 2 +- p2pvr/daemon/schedules.cpp | 27 +++++++++++++++------------ p2pvr/daemon/schedules.h | 3 ++- p2pvr/daemon/sql/Recordings_insert.sql | 2 -- p2pvr/daemon/sql/Recordings_insertNewId.sql | 2 -- p2pvr/daemon/sql/Schedules_insert.sql | 2 -- p2pvr/daemon/sql/Schedules_insertNewId.sql | 1 - p2pvr/daemon/sql/Schedules_update.sql | 10 ---------- p2pvr/daemon/unittests/mockScheduler.cpp | 7 ++++++- p2pvr/daemon/unittests/mockScheduler.h | 3 ++- p2pvr/daemon/unittests/testRecordings.cpp | 5 +++-- p2pvr/daemon/unittests/testSched.cpp | 12 +++++------- p2pvr/ice/p2pvr.ice | 6 +++++- 14 files changed, 43 insertions(+), 49 deletions(-) delete mode 100644 p2pvr/daemon/sql/Recordings_insert.sql delete mode 100644 p2pvr/daemon/sql/Recordings_insertNewId.sql delete mode 100644 p2pvr/daemon/sql/Schedules_insert.sql delete mode 100644 p2pvr/daemon/sql/Schedules_insertNewId.sql delete mode 100644 p2pvr/daemon/sql/Schedules_update.sql diff --git a/p2pvr/daemon/recordings.cpp b/p2pvr/daemon/recordings.cpp index 4a3e6d2..547b3bf 100644 --- a/p2pvr/daemon/recordings.cpp +++ b/p2pvr/daemon/recordings.cpp @@ -5,21 +5,19 @@ #include #include #include +#include -ResourceString(Recording_Insert, sql_Recordings_insert); -ResourceString(Recording_InsertNewId, sql_Recordings_insertNewId); ResourceString(Recording_Delete, sql_Recordings_delete); ResourceString(Recording_GetStorage, sql_Recordings_getStorage); ResourceString(Recording_GetAll, sql_Recordings_getAll); -int +Ice::Int Recordings::NewRecording(const P2PVR::RecordingPtr & r, const Ice::Current &) { Logger()->messagebf(LOG_INFO, "%s: Creating new recording %s at %s", __PRETTY_FUNCTION__, r->Guid, r->StorageAddress); TxHelper tx(this); - auto insert = Modify(Recording_Insert, r->StorageAddress, r->Guid, r->ScheduleId, r->EventUid); - insert.second->execute(); - r->RecordingId = SelectScalar(Recording_InsertNewId); + auto db = dataSource("postgres")->getWritable(); + Slicer::SerializeAny(r, db.get(), "recordings"); Logger()->messagebf(LOG_INFO, "%s: Created recording Id: %d", __PRETTY_FUNCTION__, r->RecordingId); return r->RecordingId; } diff --git a/p2pvr/daemon/recordings.h b/p2pvr/daemon/recordings.h index 0f18a36..fc7863a 100644 --- a/p2pvr/daemon/recordings.h +++ b/p2pvr/daemon/recordings.h @@ -7,7 +7,7 @@ class Recordings : public DatabaseClient, public P2PVR::Recordings { public: - int NewRecording(const P2PVR::RecordingPtr & rec, const Ice::Current &); + Ice::Int NewRecording(const P2PVR::RecordingPtr & rec, const Ice::Current &); void DeleteRecording(int recordingId, const Ice::Current &); P2PVR::RecordingList GetRecordings(const Ice::Current &); }; diff --git a/p2pvr/daemon/schedules.cpp b/p2pvr/daemon/schedules.cpp index 19d277f..6a985c3 100644 --- a/p2pvr/daemon/schedules.cpp +++ b/p2pvr/daemon/schedules.cpp @@ -12,14 +12,13 @@ #include #include #include +#include +#include #include #include #include ResourceString(Schedules_getCandidates, sql_Schedules_getCandidates); -ResourceString(Schedules_insert, sql_Schedules_insert); -ResourceString(Schedules_insertNewId, sql_Schedules_insertNewId); -ResourceString(Schedules_update, sql_Schedules_update); ResourceString(Schedules_delete, sql_Schedules_delete); ResourceString(Schedules_selectAll, sql_Schedules_selectAll); ResourceString(Schedules_selectById, sql_Schedules_selectById); @@ -331,20 +330,24 @@ Schedules::GetScheduledToRecord(const Ice::Current &) *Select(Schedules_scheduledToRecord).second); } -int -Schedules::UpdateSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) +Ice::Int +Schedules::NewSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) { TxHelper tx(this); - if (s->ScheduleId == 0) { - Modify(Schedules_insert, s->ServiceId, s->EventUid, s->Title, s->Search, s->Priority, s->Early, s->Late, s->Repeats).second->execute(); - s->ScheduleId = SelectScalar(Schedules_insertNewId); - } - else { - Modify(Schedules_update, s->ServiceId, s->EventUid, s->Title, s->Search, s->Priority, s->Early, s->Late, s->Repeats, s->ScheduleId).second->execute(); - } + auto db = dataSource("postgres")->getWritable(); + Slicer::SerializeAny(s, db.get(), "schedules"); DoReschedule(ice); return s->ScheduleId; } +void +Schedules::UpdateSchedule(const P2PVR::SchedulePtr & s, const Ice::Current & ice) +{ + TxHelper tx(this); + auto db = dataSource("postgres")->getWritable(); + Slicer::SerializeAny(s, db.get(), "schedules"); + DoReschedule(ice); +} + INSTANTIATEFACTORY(EpisodeGroup, const Episodes &); diff --git a/p2pvr/daemon/schedules.h b/p2pvr/daemon/schedules.h index 07f2c23..2669449 100644 --- a/p2pvr/daemon/schedules.h +++ b/p2pvr/daemon/schedules.h @@ -67,7 +67,8 @@ class Schedules : public P2PVR::Schedules, public DatabaseClient { P2PVR::SchedulePtr GetSchedule(int id, const Ice::Current &); P2PVR::ScheduleList GetSchedules(const Ice::Current &); P2PVR::ScheduledToRecordList GetScheduledToRecord(const Ice::Current &); - int UpdateSchedule(const P2PVR::SchedulePtr &, const Ice::Current &); + Ice::Int NewSchedule(const P2PVR::SchedulePtr &, const Ice::Current &); + void UpdateSchedule(const P2PVR::SchedulePtr &, const Ice::Current &); void DoReschedule(const Ice::Current &); INITOPTIONS; diff --git a/p2pvr/daemon/sql/Recordings_insert.sql b/p2pvr/daemon/sql/Recordings_insert.sql deleted file mode 100644 index ba0b9e2..0000000 --- a/p2pvr/daemon/sql/Recordings_insert.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO recordings(storageAddress, guid, scheduleId, eventUid) -VALUES(?, ?, ?, ?) diff --git a/p2pvr/daemon/sql/Recordings_insertNewId.sql b/p2pvr/daemon/sql/Recordings_insertNewId.sql deleted file mode 100644 index 0583b49..0000000 --- a/p2pvr/daemon/sql/Recordings_insertNewId.sql +++ /dev/null @@ -1,2 +0,0 @@ -SELECT currval('recordings_recordingid_seq'); - diff --git a/p2pvr/daemon/sql/Schedules_insert.sql b/p2pvr/daemon/sql/Schedules_insert.sql deleted file mode 100644 index 70c95bd..0000000 --- a/p2pvr/daemon/sql/Schedules_insert.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO schedules(serviceId, eventUid, title, search, priority, early, late, repeats) -VALUES(?, ?, ?, ?, ?, ?, ?, ?) diff --git a/p2pvr/daemon/sql/Schedules_insertNewId.sql b/p2pvr/daemon/sql/Schedules_insertNewId.sql deleted file mode 100644 index f66acd5..0000000 --- a/p2pvr/daemon/sql/Schedules_insertNewId.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT currval('schedules_scheduleid_seq'); diff --git a/p2pvr/daemon/sql/Schedules_update.sql b/p2pvr/daemon/sql/Schedules_update.sql deleted file mode 100644 index cf3ce49..0000000 --- a/p2pvr/daemon/sql/Schedules_update.sql +++ /dev/null @@ -1,10 +0,0 @@ -UPDATE schedules SET - serviceId = ?, - eventUid = ?, - title = ?, - search = ?, - priority = ?, - early = ?, - late = ?, - repeats = ? -WHERE scheduleId = ? diff --git a/p2pvr/daemon/unittests/mockScheduler.cpp b/p2pvr/daemon/unittests/mockScheduler.cpp index 2677df7..193630d 100644 --- a/p2pvr/daemon/unittests/mockScheduler.cpp +++ b/p2pvr/daemon/unittests/mockScheduler.cpp @@ -29,8 +29,13 @@ MockScheduler::GetScheduledToRecord(const::Ice::Current &) } Ice::Int +MockScheduler::NewSchedule(const::P2PVR::SchedulePtr &, const::Ice::Current &) +{ + return 1; +} + +void MockScheduler::UpdateSchedule(const::P2PVR::SchedulePtr &, const::Ice::Current &) { - return 0; } diff --git a/p2pvr/daemon/unittests/mockScheduler.h b/p2pvr/daemon/unittests/mockScheduler.h index 9176bb6..e90567b 100644 --- a/p2pvr/daemon/unittests/mockScheduler.h +++ b/p2pvr/daemon/unittests/mockScheduler.h @@ -10,7 +10,8 @@ class MockScheduler : public P2PVR::Schedules { P2PVR::SchedulePtr GetSchedule(::Ice::Int, const ::Ice::Current&) override; P2PVR::ScheduleList GetSchedules(const ::Ice::Current&) override; P2PVR::ScheduledToRecordList GetScheduledToRecord(const ::Ice::Current&) override; - Ice::Int UpdateSchedule(const ::P2PVR::SchedulePtr&, const ::Ice::Current&) override; + Ice::Int NewSchedule(const ::P2PVR::SchedulePtr&, const ::Ice::Current&) override; + void UpdateSchedule(const ::P2PVR::SchedulePtr&, const ::Ice::Current&) override; }; #endif diff --git a/p2pvr/daemon/unittests/testRecordings.cpp b/p2pvr/daemon/unittests/testRecordings.cpp index ff8f676..7415cb1 100644 --- a/p2pvr/daemon/unittests/testRecordings.cpp +++ b/p2pvr/daemon/unittests/testRecordings.cpp @@ -68,9 +68,10 @@ BOOST_AUTO_TEST_CASE( recordings_addAndDelete ) auto guid = boost::lexical_cast(boost::uuids::random_generator()()); auto rec = P2PVR::RecordingPtr(new P2PVR::Recording(0, "", guid, 0, event->EventUid)); - auto id = r->NewRecording(rec); + rec->RecordingId = r->NewRecording(rec); + BOOST_REQUIRE_EQUAL(218, rec->RecordingId); r->GetRecordings(); - r->DeleteRecording(id); + r->DeleteRecording(rec->RecordingId); } BOOST_AUTO_TEST_SUITE_END() diff --git a/p2pvr/daemon/unittests/testSched.cpp b/p2pvr/daemon/unittests/testSched.cpp index 9afdaad..45414aa 100644 --- a/p2pvr/daemon/unittests/testSched.cpp +++ b/p2pvr/daemon/unittests/testSched.cpp @@ -79,11 +79,10 @@ BOOST_AUTO_TEST_CASE( sc_crud ) P2PVR::SchedulePtr schedule = new P2PVR::Schedule(); schedule->Search = "Top Gear"; - auto id = sc->UpdateSchedule(schedule); - BOOST_REQUIRE(id > 0); - schedule->ScheduleId = id; + schedule->ScheduleId = sc->NewSchedule(schedule); + BOOST_REQUIRE_EQUAL(187, schedule->ScheduleId); - auto fetched = sc->GetSchedule(id); + auto fetched = sc->GetSchedule(schedule->ScheduleId); BOOST_REQUIRE_EQUAL(schedule->ScheduleId, fetched->ScheduleId); BOOST_REQUIRE_EQUAL(*schedule->Search, *fetched->Search); BOOST_REQUIRE_EQUAL(schedule->Priority, fetched->Priority); @@ -94,10 +93,9 @@ BOOST_AUTO_TEST_CASE( sc_crud ) schedule->Search = "Top Gear Special"; schedule->Early.Minutes = 5; schedule->Late.Minutes = 15; - auto updatedId = sc->UpdateSchedule(schedule); + sc->UpdateSchedule(schedule); - fetched = sc->GetSchedule(id); - BOOST_REQUIRE_EQUAL(updatedId, id); + fetched = sc->GetSchedule(schedule->ScheduleId); BOOST_REQUIRE_EQUAL(schedule->ScheduleId, fetched->ScheduleId); BOOST_REQUIRE_EQUAL(*schedule->Search, *fetched->Search); BOOST_REQUIRE_EQUAL(schedule->Priority, fetched->Priority); diff --git a/p2pvr/ice/p2pvr.ice b/p2pvr/ice/p2pvr.ice index 8f746d1..0c9cce3 100644 --- a/p2pvr/ice/p2pvr.ice +++ b/p2pvr/ice/p2pvr.ice @@ -8,6 +8,7 @@ module P2PVR { ["project2:type"] class Event extends DVBSI::Event { + ["slicer:db:auto","slicer:db:pkey"] int EventUid; bool Current = true; }; @@ -19,6 +20,7 @@ module P2PVR { // Something that we have recorded. ["project2:type"] class Recording { + ["slicer:db:auto","slicer:db:pkey"] int RecordingId; string StorageAddress; string Guid; @@ -30,6 +32,7 @@ module P2PVR { // Something that defines what we would like to record. ["project2:type"] class Schedule { + ["slicer:db:auto","slicer:db:pkey"] int ScheduleId = 0; optional(1) int ServiceId; optional(2) int EventUid; @@ -96,7 +99,8 @@ module P2PVR { idempotent ScheduleList GetSchedules(); ["project2:rows"] idempotent ScheduledToRecordList GetScheduledToRecord(); - idempotent int UpdateSchedule(Schedule newSchedule); + idempotent int NewSchedule(Schedule newSchedule); + idempotent void UpdateSchedule(Schedule newSchedule); ["project2:task"] idempotent void DoReschedule(); }; -- cgit v1.2.3