mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Fetch isQuoted status in bulk during conversation load.
Improves overall time to load a page of messages by ~50%.
This commit is contained in:
@@ -52,10 +52,12 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -261,6 +263,42 @@ public class MmsSmsTable extends DatabaseTable {
|
||||
return queryTables(PROJECTION, selection, order, null, true);
|
||||
}
|
||||
|
||||
public Set<Long> isQuoted(@NonNull Collection<MessageRecord> records) {
|
||||
if (records.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Map<QuoteDescriptor, MessageRecord> byQuoteDescriptor = new HashMap<>(records.size());
|
||||
List<String[]> args = new ArrayList<>(records.size());
|
||||
|
||||
for (MessageRecord record : records) {
|
||||
long timestamp = record.getDateSent();
|
||||
RecipientId author = record.isOutgoing() ? Recipient.self().getId() : record.getRecipient().getId();
|
||||
|
||||
byQuoteDescriptor.put(new QuoteDescriptor(timestamp, author), record);
|
||||
args.add(SqlUtil.buildArgs(timestamp, author));
|
||||
}
|
||||
|
||||
|
||||
String[] projection = new String[] { MessageTable.QUOTE_ID, MessageTable.QUOTE_AUTHOR };
|
||||
List<SqlUtil.Query> queries = SqlUtil.buildCustomCollectionQuery(MessageTable.QUOTE_ID + " = ? AND " + MessageTable.QUOTE_AUTHOR + " = ?", args);
|
||||
Set<Long> quotedIds = new HashSet<>();
|
||||
|
||||
for (SqlUtil.Query query : queries) {
|
||||
try (Cursor cursor = getReadableDatabase().query(MessageTable.TABLE_NAME, projection, query.getWhere(), query.getWhereArgs(), null, null, null)) {
|
||||
while (cursor.moveToNext()) {
|
||||
long timestamp = CursorUtil.requireLong(cursor, MessageTable.QUOTE_ID);
|
||||
RecipientId author = RecipientId.from(CursorUtil.requireString(cursor, MessageTable.QUOTE_AUTHOR));
|
||||
QuoteDescriptor quoteLocator = new QuoteDescriptor(timestamp, author);
|
||||
|
||||
quotedIds.add(byQuoteDescriptor.get(quoteLocator).getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return quotedIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the message has been quoted by another message.
|
||||
*/
|
||||
@@ -984,4 +1022,27 @@ public class MmsSmsTable extends DatabaseTable {
|
||||
this.threads = threads;
|
||||
}
|
||||
}
|
||||
|
||||
private static class QuoteDescriptor {
|
||||
private final long timestamp;
|
||||
private final RecipientId author;
|
||||
|
||||
private QuoteDescriptor(long timestamp, RecipientId author) {
|
||||
this.author = author;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
final QuoteDescriptor that = (QuoteDescriptor) o;
|
||||
return timestamp == that.timestamp && author.equals(that.author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(author, timestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user