Do recipient merge in one transaction

This commit is contained in:
AsamK 2022-10-08 16:21:29 +02:00
parent f2b334b57a
commit 7ab013cee9
3 changed files with 18 additions and 15 deletions

View file

@ -88,6 +88,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.file.Files; import java.nio.file.Files;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Base64; import java.util.Base64;
import java.util.Comparator; import java.util.Comparator;
@ -408,9 +409,11 @@ public class SignalAccount implements Closeable {
} }
} }
private void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId) { private void mergeRecipients(
final Connection connection, RecipientId recipientId, RecipientId toBeMergedRecipientId
) throws SQLException {
getMessageCache().mergeRecipients(recipientId, toBeMergedRecipientId); getMessageCache().mergeRecipients(recipientId, toBeMergedRecipientId);
getGroupStore().mergeRecipients(recipientId, toBeMergedRecipientId); getGroupStore().mergeRecipients(connection, recipientId, toBeMergedRecipientId);
} }
public void removeRecipient(final RecipientId recipientId) { public void removeRecipient(final RecipientId recipientId) {

View file

@ -192,7 +192,9 @@ public class GroupStore {
return Stream.concat(getGroupsV2().stream(), getGroupsV1().stream()).toList(); return Stream.concat(getGroupsV2().stream(), getGroupsV1().stream()).toList();
} }
public void mergeRecipients(final RecipientId recipientId, final RecipientId toBeMergedRecipientId) { public void mergeRecipients(
final Connection connection, final RecipientId recipientId, final RecipientId toBeMergedRecipientId
) throws SQLException {
final var sql = ( final var sql = (
""" """
UPDATE OR REPLACE %s UPDATE OR REPLACE %s
@ -200,7 +202,6 @@ public class GroupStore {
WHERE recipient_id = ? WHERE recipient_id = ?
""" """
).formatted(TABLE_GROUP_V1_MEMBER); ).formatted(TABLE_GROUP_V1_MEMBER);
try (final var connection = database.getConnection()) {
try (final var statement = connection.prepareStatement(sql)) { try (final var statement = connection.prepareStatement(sql)) {
statement.setLong(1, recipientId.id()); statement.setLong(1, recipientId.id());
statement.setLong(2, toBeMergedRecipientId.id()); statement.setLong(2, toBeMergedRecipientId.id());
@ -209,9 +210,6 @@ public class GroupStore {
logger.info("Updated {} group members when merging recipients", updatedRows); logger.info("Updated {} group members when merging recipients", updatedRows);
} }
} }
} catch (SQLException e) {
throw new RuntimeException("Failed update group store", e);
}
} }
void addLegacyGroups(final Collection<GroupInfo> groups) { void addLegacyGroups(final Collection<GroupInfo> groups) {

View file

@ -569,8 +569,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
} }
if (pair.second().isPresent()) { if (pair.second().isPresent()) {
recipientMergeHandler.mergeRecipients(pair.first(), pair.second().get());
try (final var connection = database.getConnection()) { try (final var connection = database.getConnection()) {
recipientMergeHandler.mergeRecipients(connection, pair.first(), pair.second().get());
deleteRecipient(connection, pair.second().get()); deleteRecipient(connection, pair.second().get());
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException("Failed update recipient store", e); throw new RuntimeException("Failed update recipient store", e);
@ -931,7 +931,9 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
public interface RecipientMergeHandler { public interface RecipientMergeHandler {
void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId); void mergeRecipients(
final Connection connection, RecipientId recipientId, RecipientId toBeMergedRecipientId
) throws SQLException;
} }
private record RecipientWithAddress(RecipientId id, RecipientAddress address) {} private record RecipientWithAddress(RecipientId id, RecipientAddress address) {}