diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index a4d1917174..261abbdb4f 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -50,14 +50,18 @@ import org.whispersystems.libsignal.util.Pair; import org.whispersystems.libsignal.util.guava.Optional; import java.io.Closeable; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; public class ThreadDatabase extends Database { private static final String TAG = ThreadDatabase.class.getSimpleName(); + private Map addressCache = new HashMap<>(); + public static final String TABLE_NAME = "thread"; public static final String ID = "_id"; public static final String DATE = "date"; @@ -177,6 +181,7 @@ public class ThreadDatabase extends Database { private void deleteThread(long threadId) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""}); + addressCache.remove(threadId); notifyConversationListListeners(); } @@ -191,12 +196,16 @@ public class ThreadDatabase extends Database { where = where.substring(0, where.length() - 4); db.delete(TABLE_NAME, where, null); + for (long threadId: threadIds) { + addressCache.remove(threadId); + } notifyConversationListListeners(); } private void deleteAllThreads() { SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.delete(TABLE_NAME, null, null); + addressCache.clear(); notifyConversationListListeners(); } @@ -528,6 +537,11 @@ public class ThreadDatabase extends Database { } public @Nullable Recipient getRecipientForThreadId(long threadId) { + // Loki - Cache the address. + if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) { + return Recipient.from(context, addressCache.get(threadId), false); + } + SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = null; @@ -536,6 +550,7 @@ public class ThreadDatabase extends Database { if (cursor != null && cursor.moveToFirst()) { Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS))); + addressCache.put(threadId, address); return Recipient.from(context, address, false); } } finally { diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 6715feb5ee..2cf4269ca9 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -166,6 +166,15 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { } } + @Override + public void onConfigure(SQLiteDatabase db) { + super.onConfigure(db); + // Loki: Enable Write Ahead Logging Mode, increase the cache size + // This should be disabled if we ever run into serious race condition bugs + db.enableWriteAheadLogging(); + db.execSQL("PRAGMA cache_size = 10000"); + } + @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i(TAG, "Upgrading database: " + oldVersion + ", " + newVersion); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index fee75e62b5..2b29b7916b 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1712,7 +1712,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } return Recipient.from(context, Address.fromSerialized(publicKey), false); } catch (Exception e) { - Log.d("Loki", "Failed to get primary device public key for message. " + e.getMessage()); + Log.d("Loki", "Failed to get primary device public key for " + pubKey + ". " + e.getMessage()); return Recipient.from(context, Address.fromSerialized(pubKey), false); } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index 85d8f38408..91526991e5 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -186,12 +186,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( } override fun removePairingAuthorisations(hexEncodedPublicKey: String) { - val database = databaseHelper.readableDatabase + val database = databaseHelper.writableDatabase database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey )) } fun removePairingAuthorisation(primaryDevicePublicKey: String, secondaryDevicePublicKey: String) { - val database = databaseHelper.readableDatabase + val database = databaseHelper.writableDatabase database.delete(pairingAuthorisationCache, "${Companion.primaryDevicePublicKey} = ? OR ${Companion.secondaryDevicePublicKey} = ?", arrayOf( primaryDevicePublicKey, secondaryDevicePublicKey )) } } diff --git a/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt index b1af5cc08c..9c21983fbe 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPublicChatPoller.kt @@ -10,7 +10,6 @@ import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.database.RecipientDatabase import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob import org.thoughtcrime.securesms.recipients.Recipient