mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 01:07:47 +00:00
Merge pull request #54 from RyanRory/performance
Add cache for Addresses and Messages. Make multiple insertion into one transaction.
This commit is contained in:
commit
8569183ed8
@ -50,14 +50,18 @@ import org.whispersystems.libsignal.util.Pair;
|
|||||||
import org.whispersystems.libsignal.util.guava.Optional;
|
import org.whispersystems.libsignal.util.guava.Optional;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class ThreadDatabase extends Database {
|
public class ThreadDatabase extends Database {
|
||||||
|
|
||||||
private static final String TAG = ThreadDatabase.class.getSimpleName();
|
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 TABLE_NAME = "thread";
|
||||||
public static final String ID = "_id";
|
public static final String ID = "_id";
|
||||||
public static final String DATE = "date";
|
public static final String DATE = "date";
|
||||||
@ -177,6 +181,7 @@ public class ThreadDatabase extends Database {
|
|||||||
private void deleteThread(long threadId) {
|
private void deleteThread(long threadId) {
|
||||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||||
db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""});
|
db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""});
|
||||||
|
addressCache.remove(threadId);
|
||||||
notifyConversationListListeners();
|
notifyConversationListListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,12 +196,16 @@ public class ThreadDatabase extends Database {
|
|||||||
where = where.substring(0, where.length() - 4);
|
where = where.substring(0, where.length() - 4);
|
||||||
|
|
||||||
db.delete(TABLE_NAME, where, null);
|
db.delete(TABLE_NAME, where, null);
|
||||||
|
for (long threadId: threadIds) {
|
||||||
|
addressCache.remove(threadId);
|
||||||
|
}
|
||||||
notifyConversationListListeners();
|
notifyConversationListListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteAllThreads() {
|
private void deleteAllThreads() {
|
||||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||||
db.delete(TABLE_NAME, null, null);
|
db.delete(TABLE_NAME, null, null);
|
||||||
|
addressCache.clear();
|
||||||
notifyConversationListListeners();
|
notifyConversationListListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -528,6 +537,11 @@ public class ThreadDatabase extends Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @Nullable Recipient getRecipientForThreadId(long threadId) {
|
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();
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
|
|
||||||
@ -536,6 +550,7 @@ public class ThreadDatabase extends Database {
|
|||||||
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
||||||
|
addressCache.put(threadId, address);
|
||||||
return Recipient.from(context, address, false);
|
return Recipient.from(context, address, false);
|
||||||
}
|
}
|
||||||
} finally {
|
} 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
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
Log.i(TAG, "Upgrading database: " + oldVersion + ", " + 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);
|
return Recipient.from(context, Address.fromSerialized(publicKey), false);
|
||||||
} catch (Exception e) {
|
} 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);
|
return Recipient.from(context, Address.fromSerialized(pubKey), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,12 +186,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun removePairingAuthorisations(hexEncodedPublicKey: String) {
|
override fun removePairingAuthorisations(hexEncodedPublicKey: String) {
|
||||||
val database = databaseHelper.readableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey ))
|
database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey ))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removePairingAuthorisation(primaryDevicePublicKey: String, secondaryDevicePublicKey: String) {
|
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 ))
|
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.crypto.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.database.Address
|
import org.thoughtcrime.securesms.database.Address
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.database.RecipientDatabase
|
|
||||||
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
||||||
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
|
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
|
Loading…
x
Reference in New Issue
Block a user