From b63e5f5f9fc0fcd85adf2f93f65d61abd4cc0681 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Fri, 24 Mar 2017 10:40:48 +0100 Subject: Some caching and fix gpgme on some systems --- libdino/src/entity/conversation.vala | 26 +++++++++------ libdino/src/entity/message.vala | 4 +-- libdino/src/service/avatar_manager.vala | 16 +++++++-- libdino/src/service/database.vala | 57 ++++++++++++++++++++++++++------- 4 files changed, 78 insertions(+), 25 deletions(-) (limited to 'libdino') diff --git a/libdino/src/entity/conversation.vala b/libdino/src/entity/conversation.vala index fd226b3e..376d1cef 100644 --- a/libdino/src/entity/conversation.vala +++ b/libdino/src/entity/conversation.vala @@ -74,15 +74,23 @@ 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) - .set(db.conversation.type_, type_) - .set(db.conversation.encryption, encryption) - //.set(conversation.read_up_to, changed_conversation.read_up_to) - .set(db.conversation.active, active); - if (last_active != null) { - update.set(db.conversation.last_active, (long) last_active.to_unix()); - } else { - update.set_null(db.conversation.last_active); + .with(db.conversation.account_id, "=", account.id); + switch (sp.name) { + case "type_": + update.set(db.conversation.type_, type_); break; + case "encryption": + update.set(db.conversation.encryption, encryption); break; + case "read_up_to": + update.set(db.conversation.read_up_to, read_up_to.id); break; + case "active": + update.set(db.conversation.active, active); break; + case "last_active": + if (last_active != null) { + update.set(db.conversation.last_active, (long) last_active.to_unix()); + } else { + update.set_null(db.conversation.last_active); + } + break; } update.perform(); } diff --git a/libdino/src/entity/message.vala b/libdino/src/entity/message.vala index 602c74b6..e50a1fc2 100644 --- a/libdino/src/entity/message.vala +++ b/libdino/src/entity/message.vala @@ -140,7 +140,7 @@ public class Message : Object { private void on_update(Object o, ParamSpec sp) { Qlite.UpdateBuilder update_builder = db.message.update().with(db.message.id, "=", id); - switch (sp.get_name()) { + switch (sp.name) { case "stanza_id": update_builder.set(db.message.stanza_id, stanza_id); break; case "counterpart": @@ -166,7 +166,7 @@ public class Message : Object { update_builder.perform(); if (sp.get_name() == "real_jid") { - db.real_jid.insert() + db.real_jid.insert().or("REPLACE") .value(db.real_jid.message_id, id) .value(db.real_jid.real_jid, real_jid) .perform(); diff --git a/libdino/src/service/avatar_manager.vala b/libdino/src/service/avatar_manager.vala index 1c0a3b51..de3d86fb 100644 --- a/libdino/src/service/avatar_manager.vala +++ b/libdino/src/service/avatar_manager.vala @@ -22,6 +22,7 @@ public class AvatarManager : StreamInteractionModule, Object { private HashMap user_avatars = new HashMap(Jid.hash_func, Jid.equals_func); private HashMap vcard_avatars = new HashMap(Jid.hash_func, Jid.equals_func); private AvatarStorage avatar_storage = new AvatarStorage(get_storage_dir()); + private HashMap cached_pixbuf = new HashMap(); private const int MAX_PIXEL = 192; public static void start(StreamInteractor stream_interactor, Database db) { @@ -45,6 +46,17 @@ public class AvatarManager : StreamInteractionModule, Object { modules.add(new Xep.VCard.Module(avatar_storage)); } + private Pixbuf? get_avatar_by_hash(string hash) { + if (cached_pixbuf.has_key(hash)) { + return cached_pixbuf[hash]; + } + Pixbuf? image = avatar_storage.get_image(hash); + if (image != null) { + cached_pixbuf[hash] = image; + } + return image; + } + public Pixbuf? get_avatar(Account account, Jid jid) { Jid jid_ = jid; if (!stream_interactor.get_module(MucManager.IDENTITY).is_groupchat_occupant(jid, account)) { @@ -52,11 +64,11 @@ public class AvatarManager : StreamInteractionModule, Object { } string? user_avatars_id = user_avatars[jid_]; if (user_avatars_id != null) { - return avatar_storage.get_image(user_avatars_id); + return get_avatar_by_hash(user_avatars_id); } string? vcard_avatars_id = vcard_avatars[jid_]; if (vcard_avatars_id != null) { - return avatar_storage.get_image(vcard_avatars_id); + return get_avatar_by_hash(vcard_avatars_id); } return null; } diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 202a0930..4115bcfa 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -120,6 +120,10 @@ public class Database : Qlite.Database { public AvatarTable avatar { get; private set; } public EntityFeatureTable entity_feature { get; private set; } + public Map jid_table_cache = new HashMap(); + public Map jid_table_reverse = new HashMap(); + public Map account_table_cache = new HashMap(); + public Database(string fileName) throws DatabaseError { base(fileName, VERSION); account = new AccountTable(this); @@ -141,21 +145,26 @@ public class Database : Qlite.Database { foreach(Row row in account.select()) { Account account = new Account.from_row(this, row); ret.add(account); + account_table_cache[account.id] = account; } return ret; } public Account? get_account_by_id(int id) { - Row? row = account.row_with(account.id, id).inner; - if (row != null) { - return new Account.from_row(this, row); + if (account_table_cache.has_key(id)) { + return account_table_cache[id]; + } else { + Row? row = account.row_with(account.id, id).inner; + if (row != null) { + Account a = new Account.from_row(this, row); + account_table_cache[a.id] = a; + return a; + } + return null; } - return null; } public Gee.List get_messages(Jid jid, Account account, int count, Message? before) { - string jid_id = get_jid_id(jid).to_string(); - QueryBuilder select = message.select() .with(message.counterpart_id, "=", get_jid_id(jid)) .with(message.account_id, "=", account.id) @@ -181,10 +190,9 @@ public class Database : Qlite.Database { } public bool contains_message(Message query_message, Account account) { - int jid_id = get_jid_id(query_message.counterpart); QueryBuilder builder = message.select() .with(message.account_id, "=", account.id) - .with(message.counterpart_id, "=", jid_id) + .with(message.counterpart_id, "=", get_jid_id(query_message.counterpart)) .with(message.counterpart_resource, "=", query_message.counterpart.resourcepart) .with(message.body, "=", query_message.body) .with(message.time, "<", (long) query_message.time.add_minutes(1).to_unix()) @@ -255,16 +263,41 @@ public class Database : Qlite.Database { public int get_jid_id(Jid jid_obj) { - Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string()).inner; - return row != null ? row[jid.id] : add_jid(jid_obj); + string bare_jid = jid_obj.bare_jid.to_string(); + if (jid_table_reverse.has_key(bare_jid)) { + return jid_table_reverse[bare_jid]; + } else { + Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string()).inner; + if (row != null) { + int id = row[jid.id]; + jid_table_cache[id] = bare_jid; + jid_table_reverse[bare_jid] = id; + return id; + } else { + return add_jid(jid_obj); + } + } } public string? get_jid_by_id(int id) { - return jid.select({jid.bare_jid}).with(jid.id, "=", id)[jid.bare_jid]; + if (jid_table_cache.has_key(id)) { + return jid_table_cache[id]; + } else { + string? bare_jid = jid.select({jid.bare_jid}).with(jid.id, "=", id)[jid.bare_jid]; + if (bare_jid != null) { + jid_table_cache[id] = bare_jid; + jid_table_reverse[bare_jid] = id; + } + return bare_jid; + } } private int add_jid(Jid jid_obj) { - return (int) jid.insert().value(jid.bare_jid, jid_obj.bare_jid.to_string()).perform(); + string bare_jid = jid_obj.bare_jid.to_string(); + int id = (int) jid.insert().value(jid.bare_jid, bare_jid).perform(); + jid_table_cache[id] = bare_jid; + jid_table_reverse[bare_jid] = id; + return id; } } -- cgit v1.2.3-54-g00ecf