[epiphany/wip/google-safe-browsing: 18/29] gsb-storage: Store metadata values as integers
- From: Gabriel Ivașcu <gabrielivascu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/wip/google-safe-browsing: 18/29] gsb-storage: Store metadata values as integers
- Date: Tue, 26 Sep 2017 17:57:52 +0000 (UTC)
commit 78a75a7534a89e1730617b67d9ece8b2706c7f86
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date: Fri Sep 22 16:47:11 2017 +0300
gsb-storage: Store metadata values as integers
lib/safe-browsing/ephy-gsb-service.c | 16 +++--
lib/safe-browsing/ephy-gsb-storage.c | 126 ++++++++++++++++++----------------
lib/safe-browsing/ephy-gsb-storage.h | 9 ++-
3 files changed, 82 insertions(+), 69 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-service.c b/lib/safe-browsing/ephy-gsb-service.c
index e24805a..157208e 100644
--- a/lib/safe-browsing/ephy-gsb-service.c
+++ b/lib/safe-browsing/ephy-gsb-service.c
@@ -300,7 +300,7 @@ ephy_gsb_service_update_thread (GTask *task,
next_update_time = CURRENT_TIME + (gint64)ceil (duration);
}
- ephy_gsb_storage_set_next_update_time (self->storage, next_update_time);
+ ephy_gsb_storage_set_metadata (self->storage, "next_update_time", next_update_time);
g_object_unref (msg);
json_node_unref (body_node);
@@ -412,19 +412,21 @@ static void
ephy_gsb_service_constructed (GObject *object)
{
EphyGSBService *self = EPHY_GSB_SERVICE (object);
- gint64 interval;
+ gint64 next_update_time;
+ gint64 now = CURRENT_TIME;
G_OBJECT_CLASS (ephy_gsb_service_parent_class)->constructed (object);
if (!ephy_gsb_storage_is_operable (self->storage))
return;
- interval = ephy_gsb_storage_get_next_update_time (self->storage) - CURRENT_TIME;
-
- if (interval <= 0)
- ephy_gsb_service_update (self);
+ next_update_time = ephy_gsb_storage_get_metadata (self->storage,
+ "next_update_time",
+ now);
+ if (next_update_time > now)
+ ephy_gsb_service_schedule_update (self, next_update_time - now);
else
- ephy_gsb_service_schedule_update (self, interval);
+ ephy_gsb_service_update (self);
}
static void
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 0b75776..da3cb82 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -36,11 +36,11 @@
*/
#define BATCH_SIZE 199
-/* Update schema version if you:
+/* Increment schema version if you:
* 1) Modify the database table structure.
* 2) Add new threat lists below.
*/
-#define SCHEMA_VERSION "1.0"
+#define SCHEMA_VERSION 1
/* The available Linux threat lists of Google Safe Browsing API v4.
* The format is {THREAT_TYPE, PLATFORM_TYPE, THREAT_ENTRY_TYPE}.
@@ -153,6 +153,7 @@ ephy_gsb_storage_end_transaction (EphyGSBStorage *self)
static gboolean
ephy_gsb_storage_init_metadata_table (EphyGSBStorage *self)
{
+ EphySQLiteStatement *statement;
GError *error = NULL;
const char *sql;
@@ -163,8 +164,8 @@ ephy_gsb_storage_init_metadata_table (EphyGSBStorage *self)
return TRUE;
sql = "CREATE TABLE metadata ("
- "name VARCHAR NOT NULL PRIMARY KEY,"
- "value VARCHAR NOT NULL"
+ "key VARCHAR NOT NULL PRIMARY KEY,"
+ "value INTEGER NOT NULL"
")";
ephy_sqlite_connection_execute (self->db, sql, &error);
if (error) {
@@ -173,10 +174,27 @@ ephy_gsb_storage_init_metadata_table (EphyGSBStorage *self)
return FALSE;
}
- sql = "INSERT INTO metadata (name, value) VALUES"
- "('schema_version', '"SCHEMA_VERSION"'),"
- "('next_update_at', strftime('%s', 'now'))";
- ephy_sqlite_connection_execute (self->db, sql, &error);
+ sql = "INSERT INTO metadata (key, value) VALUES"
+ "('schema_version', ?),"
+ "('next_update_time', (CAST(strftime('%s', 'now') AS INT)))";
+ statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
+ if (error) {
+ g_warning ("Failed to create metadata insert statement: %s", error->message);
+ g_error_free (error);
+ return FALSE;
+ }
+
+ ephy_sqlite_statement_bind_int64 (statement, 0, SCHEMA_VERSION, &error);
+ if (error) {
+ g_warning ("Failed to bind int64 in metadata insert statement: %s", error->message);
+ g_error_free (error);
+ g_object_unref (statement);
+ return FALSE;
+ }
+
+ ephy_sqlite_statement_step (statement, &error);
+ g_object_unref (statement);
+
if (error) {
g_warning ("Failed to insert initial data into metadata table: %s", error->message);
g_error_free (error);
@@ -406,41 +424,17 @@ ephy_gsb_storage_init_db (EphyGSBStorage *self)
return success;
}
-static gboolean
+static inline gboolean
ephy_gsb_storage_check_schema_version (EphyGSBStorage *self)
{
- EphySQLiteStatement *statement;
- GError *error = NULL;
- gboolean success;
- const char *schema_version;
- const char *sql;
+ gint64 schema_version;
g_assert (EPHY_IS_GSB_STORAGE (self));
g_assert (EPHY_IS_SQLITE_CONNECTION (self->db));
- sql = "SELECT value FROM metadata WHERE name='schema_version'";
- statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
- if (error) {
- g_warning ("Failed to create select schema version statement: %s", error->message);
- g_error_free (error);
- return FALSE;
- }
-
- ephy_sqlite_statement_step (statement, &error);
+ schema_version = ephy_gsb_storage_get_metadata (self, "schema_version", 0);
- if (error) {
- g_warning ("Failed to retrieve schema version: %s", error->message);
- g_error_free (error);
- g_object_unref (statement);
- return FALSE;
- }
-
- schema_version = ephy_sqlite_statement_get_column_as_string (statement, 0);
- success = g_strcmp0 (schema_version, SCHEMA_VERSION) == 0;
-
- g_object_unref (statement);
-
- return success;
+ return schema_version == SCHEMA_VERSION;
}
static void
@@ -562,67 +556,81 @@ ephy_gsb_storage_is_operable (EphyGSBStorage *self)
}
gint64
-ephy_gsb_storage_get_next_update_time (EphyGSBStorage *self)
+ephy_gsb_storage_get_metadata (EphyGSBStorage *self,
+ const char *key,
+ gint64 default_value)
{
EphySQLiteStatement *statement;
GError *error = NULL;
- const char *next_update_at;
const char *sql;
- gint64 next_update_time;
+ gint64 value;
g_assert (EPHY_IS_GSB_STORAGE (self));
- g_assert (self->is_operable);
+ g_assert (EPHY_IS_SQLITE_CONNECTION (self->db));
+ g_assert (key);
- sql = "SELECT value FROM metadata WHERE name='next_update_at'";
+ sql = "SELECT value FROM metadata WHERE key=?";
statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
if (error) {
- g_warning ("Failed to create select next update statement: %s", error->message);
+ g_warning ("Failed to create select metadata statement: %s", error->message);
g_error_free (error);
- return G_MAXINT64;
+ return default_value;
}
- ephy_sqlite_statement_step (statement, &error);
+ ephy_sqlite_statement_bind_string (statement, 0, key, &error);
if (error) {
- g_warning ("Failed to retrieve next update time: %s", error->message);
+ g_warning ("Failed to bind key as string in select metadata statement: %s", error->message);
g_error_free (error);
g_object_unref (statement);
- return G_MAXINT64;
+ return default_value;
}
- next_update_at = ephy_sqlite_statement_get_column_as_string (statement, 0);
- sscanf (next_update_at, "%ld", &next_update_time);
+ ephy_sqlite_statement_step (statement, &error);
+ if (error) {
+ g_warning ("Failed to execute select metadata statement: %s", error->message);
+ g_error_free (error);
+ g_object_unref (statement);
+ return default_value;
+ }
+ value = ephy_sqlite_statement_get_column_as_int64 (statement, 0);
g_object_unref (statement);
- return next_update_time;
+ return value;
}
void
-ephy_gsb_storage_set_next_update_time (EphyGSBStorage *self,
- gint64 next_update_time)
+ephy_gsb_storage_set_metadata (EphyGSBStorage *self,
+ const char *key,
+ gint64 value)
{
EphySQLiteStatement *statement;
GError *error = NULL;
const char *sql;
- char *value;
g_assert (EPHY_IS_GSB_STORAGE (self));
g_assert (self->is_operable);
+ g_assert (key);
- sql = "UPDATE metadata SET value=? WHERE name='next_update_at'";
+ sql = "UPDATE metadata SET value=? WHERE key=?";
statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
if (error) {
- g_warning ("Failed to create update next update time statement: %s", error->message);
+ g_warning ("Failed to create update metadata statement: %s", error->message);
g_error_free (error);
return;
}
- value = g_strdup_printf ("%ld", next_update_time);
- ephy_sqlite_statement_bind_string (statement, 0, value, &error);
- g_free (value);
+ ephy_sqlite_statement_bind_int64 (statement, 0, value, &error);
+ if (error) {
+ g_warning ("Failed to bind value as int64 in update metadata statement: %s", error->message);
+ g_error_free (error);
+ g_object_unref (statement);
+ return;
+ }
+ ephy_sqlite_statement_bind_string (statement, 1, key, &error);
if (error) {
- g_warning ("Failed to bind string in next update time statement: %s", error->message);
+ g_warning ("Failed to bind key as string in update metadata statement: %s", error->message);
g_error_free (error);
g_object_unref (statement);
return;
@@ -632,7 +640,7 @@ ephy_gsb_storage_set_next_update_time (EphyGSBStorage *self,
g_object_unref (statement);
if (error) {
- g_warning ("Failed to update next update time: %s", error->message);
+ g_warning ("Failed to execute update metadata statement: %s", error->message);
g_error_free (error);
}
}
diff --git a/lib/safe-browsing/ephy-gsb-storage.h b/lib/safe-browsing/ephy-gsb-storage.h
index 5fbd685..ed41a7e 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -32,9 +32,12 @@ G_DECLARE_FINAL_TYPE (EphyGSBStorage, ephy_gsb_storage, EPHY, GSB_STORAGE, GObje
EphyGSBStorage *ephy_gsb_storage_new (const char *db_path);
gboolean ephy_gsb_storage_is_operable (EphyGSBStorage *self);
-gint64 ephy_gsb_storage_get_next_update_time (EphyGSBStorage *self);
-void ephy_gsb_storage_set_next_update_time (EphyGSBStorage *self,
- gint64 next_update_time);
+gint64 ephy_gsb_storage_get_metadata (EphyGSBStorage *self,
+ const char *key,
+ gint64 default_value);
+void ephy_gsb_storage_set_metadata (EphyGSBStorage *self,
+ const char *key,
+ gint64 value);
GList *ephy_gsb_storage_get_threat_lists (EphyGSBStorage *self);
char *ephy_gsb_storage_compute_checksum (EphyGSBStorage *self,
EphyGSBThreatList *list);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]