mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Matlab
		
	
| 
											8 years ago
										 | // | ||
|  | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | #import "OWSResaveCollectionDBMigration.h" | ||
|  | #import <YapDatabase/YapDatabaseConnection.h> | ||
|  | #import <YapDatabase/YapDatabaseTransaction.h> | ||
| 
											5 years ago
										 | #import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h> | ||
| 
											8 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
|  | @implementation OWSResaveCollectionDBMigration | ||
|  | 
 | ||
|  | - (void)resaveDBCollection:(NSString *)collection | ||
|  |                     filter:(nullable DBRecordFilterBlock)filter | ||
|  |               dbConnection:(YapDatabaseConnection *)dbConnection | ||
|  |                 completion:(OWSDatabaseMigrationCompletion)completion | ||
|  | { | ||
| 
											7 years ago
										 |     OWSAssertDebug(collection.length > 0); | ||
|  |     OWSAssertDebug(dbConnection); | ||
|  |     OWSAssertDebug(completion); | ||
| 
											8 years ago
										 | 
 | ||
|  |     NSMutableArray<NSString *> *recordIds = [NSMutableArray new]; | ||
| 
											5 years ago
										 |     [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { | ||
| 
											8 years ago
										 |         [recordIds addObjectsFromArray:[transaction allKeysInCollection:collection]]; | ||
| 
											7 years ago
										 |         OWSLogInfo(@"Migrating %lu records from: %@.", (unsigned long)recordIds.count, collection); | ||
| 
											8 years ago
										 |     } | ||
| 
											5 years ago
										 |     completion:^{ | ||
|  |         [self resaveBatch:recordIds | ||
|  |                collection:collection | ||
|  |                    filter:filter | ||
|  |              dbConnection:dbConnection | ||
|  |                completion:completion]; | ||
|  |     }]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)resaveBatch:(NSMutableArray<NSString *> *)recordIds | ||
|  |          collection:(NSString *)collection | ||
|  |              filter:(nullable DBRecordFilterBlock)filter | ||
|  |        dbConnection:(YapDatabaseConnection *)dbConnection | ||
|  |          completion:(OWSDatabaseMigrationCompletion)completion | ||
|  | { | ||
| 
											7 years ago
										 |     OWSAssertDebug(recordIds); | ||
|  |     OWSAssertDebug(collection.length > 0); | ||
|  |     OWSAssertDebug(dbConnection); | ||
|  |     OWSAssertDebug(completion); | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     OWSLogVerbose(@"%lu", (unsigned long)recordIds.count); | ||
| 
											8 years ago
										 | 
 | ||
|  |     if (recordIds.count < 1) { | ||
|  |         completion(); | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { | ||
| 
											8 years ago
										 |         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]; | ||
|  |         } | ||
|  |     } | ||
| 
											5 years ago
										 |     completion:^{ | ||
|  |         // Process the next batch. | ||
|  |         [self resaveBatch:recordIds | ||
|  |                collection:collection | ||
|  |                    filter:filter | ||
|  |              dbConnection:dbConnection | ||
|  |                completion:completion]; | ||
|  |     }]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |