Clean up some corner cases in storage syncing.

This commit is contained in:
Greyson Parrelli 2020-04-22 13:47:32 -04:00
parent 57467bb338
commit 7734cd2c8f
2 changed files with 17 additions and 11 deletions

View File

@ -202,8 +202,8 @@ public class StorageSyncJob extends BaseJob {
List<RecipientSettings> pendingUpdates = recipientDatabase.getPendingRecipientSyncUpdates(); List<RecipientSettings> pendingUpdates = recipientDatabase.getPendingRecipientSyncUpdates();
List<RecipientSettings> pendingInsertions = recipientDatabase.getPendingRecipientSyncInsertions(); List<RecipientSettings> pendingInsertions = recipientDatabase.getPendingRecipientSyncInsertions();
List<RecipientSettings> pendingDeletions = recipientDatabase.getPendingRecipientSyncDeletions(); List<RecipientSettings> pendingDeletions = recipientDatabase.getPendingRecipientSyncDeletions();
Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context);
Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context); Optional<SignalAccountRecord> pendingAccountInsert = StorageSyncHelper.getPendingAccountSyncInsert(context);
Optional<SignalAccountRecord> pendingAccountUpdate = StorageSyncHelper.getPendingAccountSyncUpdate(context);
Set<RecipientId> archivedRecipients = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients(); Set<RecipientId> archivedRecipients = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients();
Optional<LocalWriteResult> localWriteResult = StorageSyncHelper.buildStorageUpdatesForLocal(localManifestVersion, Optional<LocalWriteResult> localWriteResult = StorageSyncHelper.buildStorageUpdatesForLocal(localManifestVersion,
allLocalStorageKeys, allLocalStorageKeys,

View File

@ -83,23 +83,29 @@ public final class StorageSyncHelper {
@NonNull Optional<SignalAccountRecord> accountInsert, @NonNull Optional<SignalAccountRecord> accountInsert,
@NonNull Set<RecipientId> archivedRecipients) @NonNull Set<RecipientId> archivedRecipients)
{ {
Set<StorageId> completeKeys = new LinkedHashSet<>(currentLocalKeys); if (accountUpdate.isPresent() && accountInsert.isPresent()) {
throw new AssertionError("Cannot update and insert an account at the same time!");
}
Set<StorageId> completeIds = new LinkedHashSet<>(currentLocalKeys);
Set<SignalStorageRecord> storageInserts = new LinkedHashSet<>(); Set<SignalStorageRecord> storageInserts = new LinkedHashSet<>();
Set<ByteBuffer> storageDeletes = new LinkedHashSet<>(); Set<ByteBuffer> storageDeletes = new LinkedHashSet<>();
Map<RecipientId, byte[]> storageKeyUpdates = new HashMap<>(); Map<RecipientId, byte[]> storageKeyUpdates = new HashMap<>();
for (RecipientSettings insert : inserts) { for (RecipientSettings insert : inserts) {
storageInserts.add(StorageSyncModels.localToRemoteRecord(insert, archivedRecipients)); storageInserts.add(StorageSyncModels.localToRemoteRecord(insert, archivedRecipients));
completeIds.add(StorageId.forContact(insert.getStorageId()));
} }
if (accountInsert.isPresent()) { if (accountInsert.isPresent()) {
storageInserts.add(SignalStorageRecord.forAccount(accountInsert.get())); storageInserts.add(SignalStorageRecord.forAccount(accountInsert.get()));
completeIds.add(accountInsert.get().getId());
} }
for (RecipientSettings delete : deletes) { for (RecipientSettings delete : deletes) {
byte[] key = Objects.requireNonNull(delete.getStorageId()); byte[] key = Objects.requireNonNull(delete.getStorageId());
storageDeletes.add(ByteBuffer.wrap(key)); storageDeletes.add(ByteBuffer.wrap(key));
completeKeys.remove(StorageId.forContact(key)); completeIds.remove(StorageId.forContact(key));
} }
for (RecipientSettings update : updates) { for (RecipientSettings update : updates) {
@ -108,8 +114,8 @@ public final class StorageSyncHelper {
storageInserts.add(StorageSyncModels.localToRemoteRecord(update, newKey, archivedRecipients)); storageInserts.add(StorageSyncModels.localToRemoteRecord(update, newKey, archivedRecipients));
storageDeletes.add(ByteBuffer.wrap(oldKey)); storageDeletes.add(ByteBuffer.wrap(oldKey));
completeKeys.remove(StorageId.forContact(oldKey)); completeIds.remove(StorageId.forContact(oldKey));
completeKeys.add(StorageId.forContact(newKey)); completeIds.add(StorageId.forContact(newKey));
storageKeyUpdates.put(update.getId(), newKey); storageKeyUpdates.put(update.getId(), newKey);
} }
@ -119,18 +125,18 @@ public final class StorageSyncHelper {
storageInserts.add(SignalStorageRecord.forAccount(StorageId.forAccount(newKey), accountUpdate.get())); storageInserts.add(SignalStorageRecord.forAccount(StorageId.forAccount(newKey), accountUpdate.get()));
storageDeletes.add(ByteBuffer.wrap(oldKey)); storageDeletes.add(ByteBuffer.wrap(oldKey));
completeKeys.remove(StorageId.forAccount(oldKey)); completeIds.remove(StorageId.forAccount(oldKey));
completeKeys.add(StorageId.forAccount(newKey)); completeIds.add(StorageId.forAccount(newKey));
storageKeyUpdates.put(Recipient.self().getId(), newKey); storageKeyUpdates.put(Recipient.self().getId(), newKey);
} }
if (storageInserts.isEmpty() && storageDeletes.isEmpty()) { if (storageInserts.isEmpty() && storageDeletes.isEmpty()) {
return Optional.absent(); return Optional.absent();
} else { } else {
List<byte[]> contactDeleteBytes = Stream.of(storageDeletes).map(ByteBuffer::array).toList(); List<byte[]> storageDeleteBytes = Stream.of(storageDeletes).map(ByteBuffer::array).toList();
List<StorageId> completeKeysBytes = new ArrayList<>(completeKeys); List<StorageId> completeIdsBytes = new ArrayList<>(completeIds);
SignalStorageManifest manifest = new SignalStorageManifest(currentManifestVersion + 1, completeKeysBytes); SignalStorageManifest manifest = new SignalStorageManifest(currentManifestVersion + 1, completeIdsBytes);
WriteOperationResult writeOperationResult = new WriteOperationResult(manifest, new ArrayList<>(storageInserts), contactDeleteBytes); WriteOperationResult writeOperationResult = new WriteOperationResult(manifest, new ArrayList<>(storageInserts), storageDeleteBytes);
return Optional.of(new LocalWriteResult(writeOperationResult, storageKeyUpdates)); return Optional.of(new LocalWriteResult(writeOperationResult, storageKeyUpdates));
} }