Skip to content

Commit

Permalink
Wrapping EPG save in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamothe committed May 10, 2011
1 parent c36efa2 commit 74093cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions common/epg_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
* e
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
Expand All @@ -37,7 +37,7 @@ void EpgEvents::add_epg_event(const EpgEvent& epg_event)
builder->add_field_value("duration", epg_event.duration);

Glib::RefPtr<const Set> parameters = Set::create();
Glib::RefPtr<const Set> set_epg_event_id = Set::create();
Glib::RefPtr<const Set> set_epg_event_id = Set::create();
data_connection->statement_execute_non_select(builder->get_statement(), parameters, set_epg_event_id);

int epg_event_id = set_epg_event_id->get_holder_value("+0").get_int();
Expand Down
6 changes: 3 additions & 3 deletions common/epg_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ typedef std::list<EpgEvent> EpgEventList;
class EpgEvents
{
public:
static void add_epg_event(const EpgEvent& epg_event);
static void update_epg_event(const EpgEvent& epg_event);
static void add_epg_event(const EpgEvent& epg_event);
static void update_epg_event(const EpgEvent& epg_event);
static gboolean get_current(guint channe_id, EpgEvent& epg_event);
static EpgEventList get_all(time_t start_time = 0, time_t end_time = -1);
static EpgEvent get(int epg_event_id);
static void load(Glib::RefPtr<DataModelIter> iter, EpgEvent& epg_event);
static EpgEventList search(const Glib::ustring& text, gboolean search_description);
static EpgEventList search(const String& text, gboolean search_description);
};

#endif
51 changes: 37 additions & 14 deletions common/epg_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,17 @@ class EpgCache
class EpgEntry
{
public:
guint version_number;
guint event_id;
guint channel_id;
EpgEvent epg_event;
gboolean saved;
};

std::list<EpgEntry> events;
public:
void add(guint version_number, guint event_id, guint channel_id)
void add(EpgEvent& epg_event, gboolean saved)
{
EpgEntry epg_entry;
epg_entry.version_number = version_number;
epg_entry.event_id = event_id;
epg_entry.channel_id = channel_id;
epg_entry.epg_event = epg_event;
epg_entry.saved = saved;
events.push_back(epg_entry);
}

Expand All @@ -187,14 +185,31 @@ class EpgCache
for (std::list<EpgEntry>::iterator i = events.begin(); i != events.end(); i++)
{
EpgEntry epg_entry = *i;
if (epg_entry.event_id == event_id && epg_entry.channel_id == channel_id)
if (epg_entry.epg_event.event_id == event_id && epg_entry.epg_event.channel_id == channel_id)
{
return epg_entry.version_number;
return epg_entry.epg_event.version_number;
}
}

return 0;
}

void save()
{
g_debug("Saving EPG events");
data_connection->begin_transaction("EPG", TRANSACTION_ISOLATION_READ_COMMITTED);
for (std::list<EpgEntry>::iterator i = events.begin(); i != events.end(); i++)
{
EpgEntry epg_entry = *i;
if (!epg_entry.saved)
{
EpgEvents::add_epg_event(epg_entry.epg_event);
epg_entry.saved = true;
}
}
data_connection->commit_transaction("EPG");
g_debug("EPG events saved");
}
};

void EpgThread::run()
Expand Down Expand Up @@ -253,9 +268,11 @@ void EpgThread::run()
for (EpgEventList::iterator i = epg_events.begin(); i != epg_events.end(); i++)
{
EpgEvent& epg_event = *i;
epg_cache.add(epg_event.version_number, epg_event.event_id, epg_event.channel_id);
epg_cache.add(epg_event, true);
}


time_t last_save = time(NULL);

guint frequency = frontend.get_frontend_parameters().frequency;
while (!is_terminated())
{
Expand Down Expand Up @@ -348,14 +365,20 @@ void EpgThread::run()

epg_event.texts.push_back(epg_event_text);
}

EpgEvents::add_epg_event(epg_event);
}

epg_cache.add(event.version_number, event.event_id, channel_id);
g_debug("Adding event %d to EPG cache", epg_event.event_id);
epg_cache.add(epg_event, false);
}
}
}

time_t now = time(NULL);
if (now - last_save > 10)
{
epg_cache.save();
last_save = now;
}
}
}
catch(const Glib::Exception& ex)
Expand Down

0 comments on commit 74093cc

Please sign in to comment.