]> nmode's Git Repositories - signal-cli/commitdiff
Do recipient merge in one transaction
authorAsamK <asamk@gmx.de>
Sat, 8 Oct 2022 14:21:29 +0000 (16:21 +0200)
committerAsamK <asamk@gmx.de>
Sat, 8 Oct 2022 15:42:03 +0000 (17:42 +0200)
lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java
lib/src/main/java/org/asamk/signal/manager/storage/groups/GroupStore.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java

index 547607b303055799f28c4a3793c148341b809077..880089e7e9328d68b38fac550c370fc1e36ae924 100644 (file)
@@ -88,6 +88,7 @@ import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.file.Files;
 import java.security.SecureRandom;
 import java.nio.channels.FileLock;
 import java.nio.file.Files;
 import java.security.SecureRandom;
+import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Base64;
 import java.util.Comparator;
 import java.sql.SQLException;
 import java.util.Base64;
 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) {
index 538ff2943c0e5e5f925a4306d90895f0e81889f4..ece7cde229cc53f777b80edb3cc5dc666e832642 100644 (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 = (
                 """
                 UPDATE OR REPLACE %s
         final var sql = (
                 """
                 UPDATE OR REPLACE %s
@@ -200,17 +202,13 @@ public class GroupStore {
                 WHERE recipient_id = ?
                 """
         ).formatted(TABLE_GROUP_V1_MEMBER);
                 WHERE recipient_id = ?
                 """
         ).formatted(TABLE_GROUP_V1_MEMBER);
-        try (final var connection = database.getConnection()) {
-            try (final var statement = connection.prepareStatement(sql)) {
-                statement.setLong(1, recipientId.id());
-                statement.setLong(2, toBeMergedRecipientId.id());
-                final var updatedRows = statement.executeUpdate();
-                if (updatedRows > 0) {
-                    logger.info("Updated {} group members when merging recipients", updatedRows);
-                }
+        try (final var statement = connection.prepareStatement(sql)) {
+            statement.setLong(1, recipientId.id());
+            statement.setLong(2, toBeMergedRecipientId.id());
+            final var updatedRows = statement.executeUpdate();
+            if (updatedRows > 0) {
+                logger.info("Updated {} group members when merging recipients", updatedRows);
             }
             }
-        } catch (SQLException e) {
-            throw new RuntimeException("Failed update group store", e);
         }
     }
 
         }
     }
 
index 247a71eb8f117c5b489fb2c8bbff3d79c46a5c11..f932d49727ff720be2b28c7c285c68750e643ebd 100644 (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());
             } catch (SQLException e) {
                 throw new RuntimeException("Failed update recipient store", e);
                 deleteRecipient(connection, pair.second().get());
             } catch (SQLException 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) {}