Warm up some recipients from the contact selection screen.

fork-5.53.8
Greyson Parrelli 2021-07-19 11:56:44 -04:00
rodzic bfdebbfa5d
commit c2a6963a6d
1 zmienionych plików z 32 dodań i 10 usunięć

Wyświetl plik

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.recipients;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
@ -15,7 +16,9 @@ import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
import org.thoughtcrime.securesms.util.CursorUtil;
import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.Stopwatch;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.concurrent.FilteredExecutor;
@ -32,14 +35,15 @@ public final class LiveRecipientCache {
private static final String TAG = Log.tag(LiveRecipientCache.class);
private static final int CACHE_MAX = 1000;
private static final int CACHE_WARM_MAX = 500;
private static final int CACHE_MAX = 1000;
private static final int THREAD_CACHE_WARM_MAX = 500;
private static final int CONTACT_CACHE_WARM_MAX = 50;
private final Context context;
private final RecipientDatabase recipientDatabase;
private final Map<RecipientId, LiveRecipient> recipients;
private final LiveRecipient unknown;
private final Executor executor;
private final Executor resolveExecutor;
private final SQLiteDatabase db;
private final AtomicReference<RecipientId> localRecipientId;
@ -54,7 +58,7 @@ public final class LiveRecipientCache {
this.localRecipientId = new AtomicReference<>(null);
this.unknown = new LiveRecipient(context, Recipient.UNKNOWN);
this.db = DatabaseFactory.getInstance(context).getRawDatabase();
this.executor = new FilteredExecutor(SignalExecutors.BOUNDED, () -> !db.isDbLockedByCurrentThread());
this.resolveExecutor = new FilteredExecutor(SignalExecutors.BOUNDED, () -> !db.isDbLockedByCurrentThread());
}
@AnyThread
@ -80,7 +84,7 @@ public final class LiveRecipientCache {
final LiveRecipient toResolve = live;
MissingRecipientException prettyStackTraceError = new MissingRecipientException(toResolve.getId());
executor.execute(() -> {
resolveExecutor.execute(() -> {
try {
toResolve.resolve();
} catch (MissingRecipientException e) {
@ -123,7 +127,7 @@ public final class LiveRecipientCache {
LiveRecipient toResolve = live;
MissingRecipientException prettyStackTraceError = new MissingRecipientException(toResolve.getId());
executor.execute(() -> {
resolveExecutor.execute(() -> {
try {
toResolve.resolve();
} catch (MissingRecipientException e) {
@ -173,22 +177,40 @@ public final class LiveRecipientCache {
return;
}
executor.execute(() -> {
Stopwatch stopwatch = new Stopwatch("recipient-warm-up");
SignalExecutors.BOUNDED.execute(() -> {
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
List<Recipient> recipients = new ArrayList<>();
try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getRecentConversationList(CACHE_WARM_MAX, false, false))) {
try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getRecentConversationList(THREAD_CACHE_WARM_MAX, false, false))) {
int i = 0;
ThreadRecord record = null;
while ((record = reader.getNext()) != null && i < CACHE_WARM_MAX) {
while ((record = reader.getNext()) != null && i < THREAD_CACHE_WARM_MAX) {
recipients.add(record.getRecipient());
i++;
}
}
Log.d(TAG, "Warming up " + recipients.size() + " recipients.");
Log.d(TAG, "Warming up " + recipients.size() + " thread recipients.");
addToCache(recipients);
stopwatch.split("thread");
try (Cursor cursor = DatabaseFactory.getRecipientDatabase(context).getNonGroupContacts(false)) {
int count = 0;
while (cursor != null && cursor.moveToNext() && count < CONTACT_CACHE_WARM_MAX) {
RecipientId id = RecipientId.from(CursorUtil.requireLong(cursor, RecipientDatabase.ID));
Recipient.resolved(id);
count++;
}
Log.d(TAG, "Warmed up " + count + " contact recipient.");
stopwatch.split("contact");
stopwatch.stop(TAG);
}
});
}