From f60c85749674f38b8e5fee222cd89fcc46ba2688 Mon Sep 17 00:00:00 2001 From: Peng Huang Date: Mon, 7 Feb 2011 11:33:37 -0500 Subject: [PATCH] Storing user database to disk, if engine idles more than 60 seconds. BUG=none TEST=manual Review URL: http://codereview.appspot.com/4127050 --- src/PYDatabase.cc | 22 ++++++++++++++++------ src/PYDatabase.h | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/PYDatabase.cc b/src/PYDatabase.cc index 4411d45..d6810ce 100644 --- a/src/PYDatabase.cc +++ b/src/PYDatabase.cc @@ -36,6 +36,7 @@ namespace PY { #define DB_COLUMN_S0 (3) #define DB_PREFETCH_LEN (6) +#define DB_BACKUP_TIMEOUT (60) std::unique_ptr Database::m_instance; @@ -185,14 +186,16 @@ Query::fill (PhraseArray &phrases, gint count) } Database::Database (void) - : m_db (NULL), - m_timeout_id (0) + : m_db (NULL) + , m_timeout_id (0) + , m_timer (g_timer_new ()) { open (); } Database::~Database (void) { + g_timer_destroy (m_timer); if (m_timeout_id != 0) { saveUserDB (); g_source_remove (m_timeout_id); @@ -431,7 +434,11 @@ Database::timeoutCallback (gpointer data) { Database *self = static_cast (data); - if (self->saveUserDB ()) { + /* Get elapsed time since last modification of database. */ + guint elapsed = (guint)g_timer_elapsed (self->m_timer, NULL); + + if (elapsed >= DB_BACKUP_TIMEOUT && + self->saveUserDB ()) { self->m_timeout_id = 0; return FALSE; } @@ -442,12 +449,15 @@ Database::timeoutCallback (gpointer data) void Database::modified (void) { + /* Restart the timer */ + g_timer_start (m_timer); + if (m_timeout_id != 0) return; - m_timeout_id = g_timeout_add (60000, // one minute - Database::timeoutCallback, - static_cast (this)); + m_timeout_id = g_timeout_add_seconds (DB_BACKUP_TIMEOUT, + Database::timeoutCallback, + static_cast (this)); } inline static gboolean diff --git a/src/PYDatabase.h b/src/PYDatabase.h index 8a474e1..70566e5 100644 --- a/src/PYDatabase.h +++ b/src/PYDatabase.h @@ -94,6 +94,7 @@ private: String m_sql; /* sql stmt */ String m_buffer; /* temp buffer */ guint m_timeout_id; + GTimer *m_timer; private: static std::unique_ptr m_instance; -- 2.7.4