aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service
diff options
context:
space:
mode:
Diffstat (limited to 'libdino/src/service')
-rw-r--r--libdino/src/service/avatar_manager.vala16
-rw-r--r--libdino/src/service/database.vala57
2 files changed, 59 insertions, 14 deletions
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<Jid, string> user_avatars = new HashMap<Jid, string>(Jid.hash_func, Jid.equals_func);
private HashMap<Jid, string> vcard_avatars = new HashMap<Jid, string>(Jid.hash_func, Jid.equals_func);
private AvatarStorage avatar_storage = new AvatarStorage(get_storage_dir());
+ private HashMap<string, Pixbuf> cached_pixbuf = new HashMap<string, Pixbuf>();
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<int, string> jid_table_cache = new HashMap<int, string>();
+ public Map<string, int> jid_table_reverse = new HashMap<string, int>();
+ public Map<int, Account> account_table_cache = new HashMap<int, Account>();
+
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<Message> 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;
}
}