Merge pull request #54 from RyanRory/performance

Add cache for Addresses and Messages. Make multiple insertion into one transaction.
pull/57/head
Mikunj Varsani 5 years ago committed by GitHub
commit 8569183ed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<Long, Address> 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 {

@ -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);

@ -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);
}
}

@ -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 ))
}
}

@ -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

Loading…
Cancel
Save