From 9728e832b18e8bbfcc0e011b5b1da9afe6021bf3 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sun, 23 Apr 2017 10:23:11 +0200 Subject: [PATCH] qlite/libdino: optimize db access --- libdino/src/entity/conversation.vala | 8 +------- libdino/src/service/database.vala | 3 +++ qlite/src/database.vala | 5 ++++- qlite/src/table.vala | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libdino/src/entity/conversation.vala b/libdino/src/entity/conversation.vala index 7e8b1424..e820af4d 100644 --- a/libdino/src/entity/conversation.vala +++ b/libdino/src/entity/conversation.vala @@ -88,13 +88,7 @@ public class Conversation : Object { } private void on_update(Object o, ParamSpec sp) { - var update = db.conversation.update().with(db.conversation.jid_id, "=", db.get_jid_id(counterpart)) - .with(db.conversation.account_id, "=", account.id); - if (counterpart.resourcepart != null) { - update.with(db.conversation.resource, "=", counterpart.resourcepart); - } else { - update.with_null(db.conversation.resource); - } + var update = db.conversation.update().with(db.conversation.id, "=", id); switch (sp.name) { case "type-": update.set(db.conversation.type_, type_); break; diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index a74ac056..021d1c21 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -110,6 +110,8 @@ public class Database : Qlite.Database { internal EntityFeatureTable(Database db) { base(db, "entity_feature"); init({entity, feature}); + unique({entity, feature}, "IGNORE"); + index("entity_feature_idx", {entity}); } } @@ -135,6 +137,7 @@ public class Database : Qlite.Database { avatar = new AvatarTable(this); entity_feature = new EntityFeatureTable(this); init({ account, jid, message, real_jid, conversation, avatar, entity_feature }); + exec("PRAGMA synchronous=0"); } public override void migrate(long oldVersion) { diff --git a/qlite/src/database.vala b/qlite/src/database.vala index 0427df16..cc46ee21 100644 --- a/qlite/src/database.vala +++ b/qlite/src/database.vala @@ -77,6 +77,9 @@ public class Database { meta_table.update().with(meta_name, "=", "version").set(meta_int_val, expected_version).perform(); } } + foreach (Table t in tables) { + t.post(); + } } internal int errcode() { @@ -136,7 +139,7 @@ public class Database { return statement; } - internal void exec(string sql) throws DatabaseError { + public void exec(string sql) throws DatabaseError { ensure_init(); if (db.exec(sql) != OK) { throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())"); diff --git a/qlite/src/table.vala b/qlite/src/table.vala index b4f2fee7..bd3fcc36 100644 --- a/qlite/src/table.vala +++ b/qlite/src/table.vala @@ -7,6 +7,7 @@ public class Table { public string name { get; private set; } protected Column[]? columns; private string constraints = ""; + private string[] post_statements = {}; public Table(Database db, string name) { this.db = db; @@ -32,6 +33,22 @@ public class Table { } } + public void add_post_statement(string stmt) { + post_statements += stmt; + } + + public void index(string index_name, Column[] columns, bool unique = false) { + string stmt = @"CREATE $(unique ? "UNIQUE" : "") INDEX IF NOT EXISTS $index_name ON $name ("; + bool first = true; + foreach (Column c in columns) { + if (!first) stmt += ", "; + stmt += c.name; + first = false; + } + stmt += ")"; + add_post_statement(stmt); + } + private void ensure_init() throws DatabaseError { if (columns == null) throw new DatabaseError.NOT_INITIALIZED(@"Table $name was not initialized, call init()"); } @@ -114,6 +131,12 @@ public class Table { db.exec(@"DROP TABLE _$(name)_$old_version"); } } + + internal void post() throws DatabaseError { + foreach (string stmt in post_statements) { + db.exec(stmt); + } + } } } \ No newline at end of file