mirror of https://github.com/oxen-io/session-ios
Respond to CR.
parent
c75c3d5f9e
commit
5773b45345
@ -0,0 +1,24 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSDatabaseMigration.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
typedef BOOL (^DBRecordFilterBlock)(id record);
|
||||||
|
|
||||||
|
@class YapDatabaseConnection;
|
||||||
|
|
||||||
|
// Base class for migrations that resave all or a subset of
|
||||||
|
// records in a database collection.
|
||||||
|
@interface OWSResaveCollectionDBMigration : OWSDatabaseMigration
|
||||||
|
|
||||||
|
- (void)resaveDBCollection:(NSString *)collection
|
||||||
|
filter:(nullable DBRecordFilterBlock)filter
|
||||||
|
dbConnection:(YapDatabaseConnection *)dbConnection
|
||||||
|
completion:(OWSDatabaseMigrationCompletion)completion;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,80 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSResaveCollectionDBMigration.h"
|
||||||
|
#import <YapDatabase/YapDatabaseConnection.h>
|
||||||
|
#import <YapDatabase/YapDatabaseTransaction.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@implementation OWSResaveCollectionDBMigration
|
||||||
|
|
||||||
|
- (void)resaveDBCollection:(NSString *)collection
|
||||||
|
filter:(nullable DBRecordFilterBlock)filter
|
||||||
|
dbConnection:(YapDatabaseConnection *)dbConnection
|
||||||
|
completion:(OWSDatabaseMigrationCompletion)completion
|
||||||
|
{
|
||||||
|
OWSAssert(collection.length > 0);
|
||||||
|
OWSAssert(dbConnection);
|
||||||
|
OWSAssert(completion);
|
||||||
|
|
||||||
|
NSMutableArray<NSString *> *recordIds = [NSMutableArray new];
|
||||||
|
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
||||||
|
[recordIds addObjectsFromArray:[transaction allKeysInCollection:collection]];
|
||||||
|
DDLogInfo(@"%@ Migrating %zd records from: %@.", self.logTag, recordIds.count, collection);
|
||||||
|
}
|
||||||
|
completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
|
||||||
|
completionBlock:^{
|
||||||
|
[self resaveBatch:recordIds
|
||||||
|
collection:collection
|
||||||
|
filter:filter
|
||||||
|
dbConnection:dbConnection
|
||||||
|
completion:completion];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)resaveBatch:(NSMutableArray<NSString *> *)recordIds
|
||||||
|
collection:(NSString *)collection
|
||||||
|
filter:(nullable DBRecordFilterBlock)filter
|
||||||
|
dbConnection:(YapDatabaseConnection *)dbConnection
|
||||||
|
completion:(OWSDatabaseMigrationCompletion)completion
|
||||||
|
{
|
||||||
|
OWSAssert(recordIds);
|
||||||
|
OWSAssert(collection.length > 0);
|
||||||
|
OWSAssert(dbConnection);
|
||||||
|
OWSAssert(completion);
|
||||||
|
|
||||||
|
DDLogVerbose(@"%@ %s: %zd", self.logTag, __PRETTY_FUNCTION__, recordIds.count);
|
||||||
|
|
||||||
|
if (recordIds.count < 1) {
|
||||||
|
completion();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
||||||
|
const int kBatchSize = 1000;
|
||||||
|
for (int i = 0; i < kBatchSize && recordIds.count > 0; i++) {
|
||||||
|
NSString *messageId = [recordIds lastObject];
|
||||||
|
[recordIds removeLastObject];
|
||||||
|
id record = [transaction objectForKey:messageId inCollection:collection];
|
||||||
|
if (filter && !filter(record)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TSYapDatabaseObject *entity = (TSYapDatabaseObject *)record;
|
||||||
|
[entity saveWithTransaction:transaction];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
completionBlock:^{
|
||||||
|
// Process the next batch.
|
||||||
|
[self resaveBatch:recordIds
|
||||||
|
collection:collection
|
||||||
|
filter:filter
|
||||||
|
dbConnection:dbConnection
|
||||||
|
completion:completion];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue