Reduce number of db calls to getGroup.

This commit is contained in:
Clark
2023-05-11 13:20:15 -04:00
committed by Greyson Parrelli
parent 6da36fe098
commit e9f1f781e1
14 changed files with 105 additions and 52 deletions

View File

@@ -18,6 +18,7 @@ import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.database.model.GroupRecord;
import org.thoughtcrime.securesms.groups.v2.GroupInviteLinkUrl;
import org.thoughtcrime.securesms.groups.v2.GroupLinkPassword;
import org.thoughtcrime.securesms.groups.v2.processing.GroupsV2StateProcessor;
import org.thoughtcrime.securesms.profiles.AvatarHelper;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
@@ -178,15 +179,15 @@ public final class GroupManager {
* processing deny messages.
*/
@WorkerThread
public static void updateGroupFromServer(@NonNull Context context,
@NonNull GroupMasterKey groupMasterKey,
int revision,
long timestamp,
@Nullable byte[] signedGroupChange)
public static GroupsV2StateProcessor.GroupUpdateResult updateGroupFromServer(@NonNull Context context,
@NonNull GroupMasterKey groupMasterKey,
int revision,
long timestamp,
@Nullable byte[] signedGroupChange)
throws GroupChangeBusyException, IOException, GroupNotAMemberException
{
try (GroupManagerV2.GroupUpdater updater = new GroupManagerV2(context).updater(groupMasterKey)) {
updater.updateLocalToServerRevision(revision, timestamp, signedGroupChange);
return updater.updateLocalToServerRevision(revision, timestamp, signedGroupChange);
}
}

View File

@@ -797,11 +797,11 @@ final class GroupManagerV2 {
}
@WorkerThread
void updateLocalToServerRevision(int revision, long timestamp, @Nullable byte[] signedGroupChange)
GroupsV2StateProcessor.GroupUpdateResult updateLocalToServerRevision(int revision, long timestamp, @Nullable byte[] signedGroupChange)
throws IOException, GroupNotAMemberException
{
new GroupsV2StateProcessor(context).forGroup(serviceIds, groupMasterKey)
.updateLocalGroupToRevision(revision, timestamp, getDecryptedGroupChange(signedGroupChange));
return new GroupsV2StateProcessor(context).forGroup(serviceIds, groupMasterKey)
.updateLocalGroupToRevision(revision, timestamp, getDecryptedGroupChange(signedGroupChange));
}
@WorkerThread

View File

@@ -133,7 +133,7 @@ public class GroupsV2StateProcessor {
this.latestServer = latestServer;
}
public GroupState getGroupState() {
public @NonNull GroupState getGroupState() {
return groupState;
}
@@ -229,13 +229,14 @@ public class GroupsV2StateProcessor {
@Nullable DecryptedGroupChange signedGroupChange)
throws IOException, GroupNotAMemberException
{
if (localIsAtLeast(revision)) {
Optional<GroupRecord> localRecord = groupDatabase.getGroup(groupId);
if (localIsAtLeast(localRecord, revision)) {
return new GroupUpdateResult(GroupState.GROUP_CONSISTENT_OR_AHEAD, null);
}
GlobalGroupState inputGroupState = null;
Optional<GroupRecord> localRecord = groupDatabase.getGroup(groupId);
DecryptedGroup localState = localRecord.map(g -> g.requireV2GroupProperties().getDecryptedGroup()).orElse(null);
if (signedGroupChange != null &&
@@ -540,11 +541,11 @@ public class GroupsV2StateProcessor {
/**
* @return true iff group exists locally and is at least the specified revision.
*/
private boolean localIsAtLeast(int revision) {
if (groupDatabase.isUnknownGroup(groupId) || revision == LATEST) {
private boolean localIsAtLeast(Optional<GroupRecord> localRecord, int revision) {
if (revision == LATEST || localRecord.isEmpty() || groupDatabase.isUnknownGroup(localRecord)) {
return false;
}
int dbRevision = groupDatabase.getGroup(groupId).get().requireV2GroupProperties().getGroupRevision();
int dbRevision = localRecord.get().requireV2GroupProperties().getGroupRevision();
return revision <= dbRevision;
}