Merge branch 'charlesmchen/removeYdb3'

This commit is contained in:
Matthew Chen 2019-08-14 09:12:40 -03:00
commit dcea0c0865
14 changed files with 54 additions and 329 deletions

View File

@ -212,7 +212,7 @@ static NSTimeInterval launchStartedAt;
OWSLogWarn(@"application: didFinishLaunchingWithOptions.");
[Cryptography seedRandom];
// XXX - careful when moving this. It must happen before we initialize OWSPrimaryStorage.
// XXX - careful when moving this. It must happen before we load YDB and/or GRDB.
[self verifyDBKeysAvailableBeforeBackgroundLaunch];
// We need to do this _after_ we set up logging, when the keychain is unlocked,
@ -324,6 +324,8 @@ static NSTimeInterval launchStartedAt;
return;
}
// GRDB TODO: We should consult YDB and GRDB. This should be done via storage coordinator,
// which knows which checks we need to do.
if (![OWSPrimaryStorage isDatabasePasswordAccessible]) {
OWSLogInfo(@"exiting because we are in the background and the database password is not accessible.");

View File

@ -21,10 +21,6 @@ public class LongTextViewController: OWSViewController {
return SDSDatabaseStorage.shared
}
var uiDatabaseConnection: YapDatabaseConnection {
return OWSPrimaryStorage.shared().uiDatabaseConnection
}
// MARK: - Properties
@objc
@ -78,8 +74,8 @@ public class LongTextViewController: OWSViewController {
let uniqueId = self.viewItem.interaction.uniqueId
do {
try uiDatabaseConnection.read { transaction in
guard TSInteraction.anyFetch(uniqueId: uniqueId, transaction: transaction.asAnyRead) != nil else {
try databaseStorage.uiReadThrows { transaction in
guard TSInteraction.anyFetch(uniqueId: uniqueId, transaction: transaction) != nil else {
Logger.error("Message was deleted")
throw LongTextViewError.messageWasDeleted
}

View File

@ -33,8 +33,6 @@ class MessageDetailViewController: OWSViewController {
// MARK: Properties
let uiDatabaseConnection: YapDatabaseConnection
var bubbleView: UIView?
let mode: MessageMetadataViewMode
@ -85,7 +83,6 @@ class MessageDetailViewController: OWSViewController {
self.viewItem = viewItem
self.message = message
self.mode = mode
self.uiDatabaseConnection = OWSPrimaryStorage.shared().uiDatabaseConnection
self.conversationStyle = ConversationStyle(thread: thread)
super.init(nibName: nil, bundle: nil)
@ -204,8 +201,8 @@ class MessageDetailViewController: OWSViewController {
lazy var thread: TSThread = {
var thread: TSThread?
self.uiDatabaseConnection.read { transaction in
thread = self.message.thread(transaction: transaction.asAnyRead)
databaseStorage.uiRead { transaction in
thread = self.message.thread(transaction: transaction)
}
return thread!
}()

View File

@ -78,8 +78,6 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
// TODO: Observe Reachability.
@interface OWSBackup () <OWSBackupJobDelegate>
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
// This property should only be accessed on the main thread.
@property (nonatomic, nullable) OWSBackupExportJob *backupExportJob;
@ -101,8 +99,6 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
@implementation OWSBackup
@synthesize dbConnection = _dbConnection;
- (SDSDatabaseStorage *)databaseStorage
{
return SDSDatabaseStorage.shared;
@ -113,7 +109,7 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
static SDSKeyValueStore *keyValueStore = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
keyValueStore = [[SDSKeyValueStore alloc] initWithCollection:@"OWSPrimaryStorage_OWSBackupCollection"];
keyValueStore = [[SDSKeyValueStore alloc] initWithCollection:@"OWSBackupCollection"];
});
return keyValueStore;
}
@ -183,25 +179,8 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
});
}
- (YapDatabaseConnection *)dbConnection
{
@synchronized(self) {
if (!_dbConnection) {
_dbConnection = self.primaryStorage.newDatabaseConnection;
}
return _dbConnection;
}
}
#pragma mark - Dependencies
- (OWSPrimaryStorage *)primaryStorage
{
OWSAssertDebug(SSKEnvironment.shared.primaryStorage);
return SSKEnvironment.shared.primaryStorage;
}
- (TSAccountManager *)tsAccountManager
{
OWSAssertDebug(SSKEnvironment.shared.tsAccountManager);
@ -746,8 +725,12 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
- (NSArray<NSString *> *)attachmentRecordNamesForLazyRestore
{
NSMutableArray<NSString *> *recordNames = [NSMutableArray new];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
id ext = [transaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
// GRDB TODO:
if (transaction.transitional_yapReadTransaction == nil) {
return;
}
id ext = [transaction.transitional_yapReadTransaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
if (!ext) {
OWSFailDebug(@"Could not load database view.");
return;
@ -764,7 +747,7 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
TSAttachmentPointer *attachmentPointer = object;
OWSBackupFragment *_Nullable lazyRestoreFragment =
[attachmentPointer lazyRestoreFragmentWithTransaction:transaction.asAnyRead];
[attachmentPointer lazyRestoreFragmentWithTransaction:transaction];
if (lazyRestoreFragment == nil) {
OWSFailDebug(
@"Invalid object: %@ in collection:%@", [object class], collection);
@ -779,8 +762,12 @@ NSError *OWSBackupErrorWithDescription(NSString *description)
- (NSArray<NSString *> *)attachmentIdsForLazyRestore
{
NSMutableArray<NSString *> *attachmentIds = [NSMutableArray new];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
id ext = [transaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
// GRDB TODO:
if (transaction.transitional_yapReadTransaction == nil) {
return;
}
id ext = [transaction.transitional_yapReadTransaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
if (!ext) {
OWSFailDebug(@"Could not load database view.");
return;

View File

@ -8,10 +8,6 @@ import XCTest
class MantlePerfTest: SignalBaseTest {
var primaryStorage: OWSPrimaryStorage {
return SSKEnvironment.shared.primaryStorage
}
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

View File

@ -19,10 +19,10 @@
#import <SignalMessaging/SignalMessaging-Swift.h>
#import <SignalServiceKit/PhoneNumber.h>
#import <SignalServiceKit/SignalAccount.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
#import <SignalServiceKit/TSAccountManager.h>
#import <SignalServiceKit/TSContactThread.h>
#import <SignalServiceKit/TSThread.h>
#import <YapDatabase/YapDatabase.h>
NS_ASSUME_NONNULL_BEGIN
@ -30,12 +30,12 @@ NS_ASSUME_NONNULL_BEGIN
ThreadViewHelperDelegate,
ContactsViewHelperDelegate,
UISearchBarDelegate,
NewNonContactConversationViewControllerDelegate>
NewNonContactConversationViewControllerDelegate,
SDSDatabaseStorageObserver>
@property (nonatomic, readonly) ContactsViewHelper *contactsViewHelper;
@property (nonatomic, readonly) FullTextSearcher *fullTextSearcher;
@property (nonatomic, readonly) ThreadViewHelper *threadViewHelper;
@property (nonatomic, readonly) YapDatabaseConnection *uiDatabaseConnection;
@property (nonatomic, readonly) OWSTableViewController *tableViewController;
@ -72,19 +72,7 @@ NS_ASSUME_NONNULL_BEGIN
_threadViewHelper = [ThreadViewHelper new];
_threadViewHelper.delegate = self;
_uiDatabaseConnection = [[OWSPrimaryStorage sharedManager] newDatabaseConnection];
#ifdef DEBUG
_uiDatabaseConnection.permittedTransactions = YDB_AnyReadTransaction;
#endif
[_uiDatabaseConnection beginLongLivedReadTransaction];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yapDatabaseModified:)
name:YapDatabaseModifiedNotification
object:OWSPrimaryStorage.sharedManager.dbNotificationObject];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yapDatabaseModifiedExternally:)
name:YapDatabaseModifiedExternallyNotification
object:nil];
[self.databaseStorage addDatabaseStorageObserver:self];
[self createViews];
@ -124,21 +112,29 @@ NS_ASSUME_NONNULL_BEGIN
self.tableViewController.tableView.estimatedRowHeight = 60;
}
- (void)yapDatabaseModifiedExternally:(NSNotification *)notification
#pragma mark - SDSDatabaseStorageObserver
- (void)databaseStorageDidUpdateWithChange:(SDSDatabaseStorageChange *)change
{
OWSAssertIsOnMainThread();
OWSAssertDebug(AppReadiness.isAppReady);
OWSLogVerbose(@"");
[self.uiDatabaseConnection beginLongLivedReadTransaction];
[self updateTableContents];
}
- (void)yapDatabaseModified:(NSNotification *)notification
- (void)databaseStorageDidUpdateExternally
{
OWSAssertIsOnMainThread();
[self.uiDatabaseConnection beginLongLivedReadTransaction];
OWSAssertDebug(AppReadiness.isAppReady);
[self updateTableContents];
}
- (void)databaseStorageDidReset
{
OWSAssertIsOnMainThread();
OWSAssertDebug(AppReadiness.isAppReady);
[self updateTableContents];
}

View File

@ -250,8 +250,6 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
@synchronized (self) {
__block NSString *_Nullable result;
// GRDB TODO: Until GRDB migration is complete, we need to load this from YDB,
//
// * YAPDBJobRecordFinder uses a secondary index.
// * Yaps views and indices enumerate all (per whitelist or blacklist) entities when building or updating the
// index. Views and indices can be built or re-built on launch.
@ -261,20 +259,10 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
// * OWSOutgoingSyncMessage extends TSOutgoingMessage whose deserialization initializer initWithCoder uses
// TSAccountManager.localNumber.
// * TSAccountManager.localNumber is persisted in the database.
// * When we load TSAccountManager.localNumber we use the "current" database which might be GRDB. GRDB might not
// be populated because the migration hasn't occurred yet.
//
// GRDB TODO: GRDB_MIGRATION_COMPLETE might eventually be replaced by a flag set at runtime.
#ifdef GRDB_MIGRATION_COMPLETE
// * When we load TSAccountManager.localNumber we use the "current" database which might be GRDB.
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
result = [self.keyValueStore getString:TSAccountManager_RegisteredNumberKey transaction:transaction];
}];
#else
[self.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
result =
[self.keyValueStore getString:TSAccountManager_RegisteredNumberKey transaction:transaction.asAnyRead];
}];
#endif
return result;
}
}

View File

@ -11,7 +11,6 @@
#import "OWSFileSystem.h"
#import "OWSMessageSender.h"
#import "OWSOutgoingNullMessage.h"
#import "OWSPrimaryStorage.h"
#import "OWSRecipientIdentity.h"
#import "OWSVerificationStateChangeMessage.h"
#import "OWSVerificationStateSyncMessage.h"
@ -30,7 +29,7 @@
NS_ASSUME_NONNULL_BEGIN
// Storing our own identity key
NSString *const OWSPrimaryStorageIdentityKeyStoreIdentityKey = @"TSStorageManagerIdentityKeyStoreIdentityKey";
NSString *const kIdentityKeyStore_IdentityKey = @"TSStorageManagerIdentityKeyStoreIdentityKey";
// Don't trust an identity for sending to unless they've been around for at least this long
const NSTimeInterval kIdentityKeyStoreNonBlockingSecondsThreshold = 5.0;
@ -128,7 +127,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
{
[self.databaseQueue writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
[self.ownIdentityKeyValueStore setObject:[Curve25519 generateKeyPair]
key:OWSPrimaryStorageIdentityKeyStoreIdentityKey
key:kIdentityKeyStore_IdentityKey
transaction:transaction];
}];
}
@ -185,7 +184,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
{
OWSAssertDebug(transaction);
id _Nullable object =
[self.ownIdentityKeyValueStore getObject:OWSPrimaryStorageIdentityKeyStoreIdentityKey transaction:transaction];
[self.ownIdentityKeyValueStore getObject:kIdentityKeyStore_IdentityKey transaction:transaction];
if ([object isKindOfClass:[ECKeyPair class]]) {
return (ECKeyPair *)object;
} else {
@ -951,7 +950,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
NSMutableArray<NSString *> *identityKeysToRemove = [NSMutableArray new];
for (NSString *key in [self.ownIdentityKeyValueStore allKeysWithTransaction:transaction]) {
if ([key isEqualToString:OWSPrimaryStorageIdentityKeyStoreIdentityKey]) {
if ([key isEqualToString:kIdentityKeyStore_IdentityKey]) {
// Don't delete our own key.
return;
}

View File

@ -24,7 +24,6 @@
#import "OWSMessageSender.h"
#import "OWSMessageUtils.h"
#import "OWSOutgoingReceiptManager.h"
#import "OWSPrimaryStorage.h"
#import "OWSReadReceiptManager.h"
#import "OWSRecordTranscriptJob.h"
#import "OWSSyncGroupsRequestMessage.h"

View File

@ -7,7 +7,6 @@
#import "OWSCensorshipConfiguration.h"
#import "OWSError.h"
#import "OWSHTTPSecurityPolicy.h"
#import "OWSPrimaryStorage.h"
#import "TSAccountManager.h"
#import "TSConstants.h"
#import <AFNetworking/AFHTTPSessionManager.h>
@ -15,13 +14,11 @@
NS_ASSUME_NONNULL_BEGIN
NSString *const kOWSPrimaryStorage_isCensorshipCircumventionManuallyActivated
NSString *const kisCensorshipCircumventionManuallyActivatedKey
= @"kTSStorageManager_isCensorshipCircumventionManuallyActivated";
NSString *const kOWSPrimaryStorage_isCensorshipCircumventionManuallyDisabled
NSString *const kisCensorshipCircumventionManuallyDisabledKey
= @"kTSStorageManager_isCensorshipCircumventionManuallyDisabled";
NSString *const kOWSPrimaryStorage_ManualCensorshipCircumventionDomain
= @"kTSStorageManager_ManualCensorshipCircumventionDomain";
NSString *const kOWSPrimaryStorage_ManualCensorshipCircumventionCountryCode
NSString *const kManualCensorshipCircumventionCountryCodeKey
= @"kTSStorageManager_ManualCensorshipCircumventionCountryCode";
NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
@ -120,7 +117,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
{
__block BOOL result;
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
result = [self.keyValueStore getBool:kOWSPrimaryStorage_isCensorshipCircumventionManuallyActivated
result = [self.keyValueStore getBool:kisCensorshipCircumventionManuallyActivatedKey
defaultValue:NO
transaction:transaction];
}];
@ -130,9 +127,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
- (void)setIsCensorshipCircumventionManuallyActivated:(BOOL)value
{
[self.databaseStorage writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
[self.keyValueStore setBool:value
key:kOWSPrimaryStorage_isCensorshipCircumventionManuallyActivated
transaction:transaction];
[self.keyValueStore setBool:value key:kisCensorshipCircumventionManuallyActivatedKey transaction:transaction];
}];
[self updateIsCensorshipCircumventionActive];
@ -142,7 +137,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
{
__block BOOL result;
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
result = [self.keyValueStore getBool:kOWSPrimaryStorage_isCensorshipCircumventionManuallyDisabled
result = [self.keyValueStore getBool:kisCensorshipCircumventionManuallyDisabledKey
defaultValue:NO
transaction:transaction];
}];
@ -152,9 +147,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
- (void)setIsCensorshipCircumventionManuallyDisabled:(BOOL)value
{
[self.databaseStorage writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
[self.keyValueStore setBool:value
key:kOWSPrimaryStorage_isCensorshipCircumventionManuallyDisabled
transaction:transaction];
[self.keyValueStore setBool:value key:kisCensorshipCircumventionManuallyDisabledKey transaction:transaction];
}];
[self updateIsCensorshipCircumventionActive];
@ -399,8 +392,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
{
__block NSString *_Nullable result;
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
result = [self.keyValueStore getString:kOWSPrimaryStorage_ManualCensorshipCircumventionCountryCode
transaction:transaction];
result = [self.keyValueStore getString:kManualCensorshipCircumventionCountryCodeKey transaction:transaction];
}];
return result;
}
@ -408,9 +400,7 @@ NSString *const kNSNotificationName_IsCensorshipCircumventionActiveDidChange =
- (void)setManualCensorshipCircumventionCountryCode:(nullable NSString *)value
{
[self.databaseStorage writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
[self.keyValueStore setString:value
key:kOWSPrimaryStorage_ManualCensorshipCircumventionCountryCode
transaction:transaction];
[self.keyValueStore setString:value key:kManualCensorshipCircumventionCountryCodeKey transaction:transaction];
}];
}

View File

@ -2,7 +2,6 @@
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
#import "OWSPrimaryStorage.h"
#import <AxolotlKit/SignedPreKeyStore.h>
NS_ASSUME_NONNULL_BEGIN

View File

@ -17,31 +17,16 @@ NS_ASSUME_NONNULL_BEGIN
// This will break Swift usage and generate warnings for Obj-C usage.
// Note: this functionality can still be accessed (for example by the
// SDS model extensions) via the ydb_ methods of TSYapDatabaseObject.
+ (NSUInteger)numberOfKeysInCollection NS_UNAVAILABLE;
+ (NSUInteger)numberOfKeysInCollectionWithTransaction:(YapDatabaseReadTransaction *)transaction NS_UNAVAILABLE;
+ (void)removeAllObjectsInCollection NS_UNAVAILABLE;
+ (NSArray *)allObjectsInCollection NS_UNAVAILABLE;
+ (void)enumerateCollectionObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block NS_UNAVAILABLE;
+ (void)enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction
usingBlock:(void (^)(id object, BOOL *stop))block NS_UNAVAILABLE;
- (YapDatabaseConnection *)dbReadConnection NS_UNAVAILABLE;
+ (YapDatabaseConnection *)dbReadConnection NS_UNAVAILABLE;
- (YapDatabaseConnection *)dbReadWriteConnection NS_UNAVAILABLE;
+ (YapDatabaseConnection *)dbReadWriteConnection NS_UNAVAILABLE;
- (OWSPrimaryStorage *)primaryStorage NS_UNAVAILABLE;
+ (OWSPrimaryStorage *)primaryStorage NS_UNAVAILABLE;
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID
transaction:(YapDatabaseReadTransaction *)transaction NS_UNAVAILABLE;
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID NS_UNAVAILABLE;
- (void)save NS_UNAVAILABLE;
- (void)reload NS_UNAVAILABLE;
- (void)reloadWithTransaction:(YapDatabaseReadTransaction *)transaction NS_UNAVAILABLE;
- (void)reloadWithTransaction:(YapDatabaseReadTransaction *)transaction
ignoreMissing:(BOOL)ignoreMissing NS_UNAVAILABLE;
- (void)saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock NS_UNAVAILABLE;
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction NS_UNAVAILABLE;
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction NS_UNAVAILABLE;
- (void)remove NS_UNAVAILABLE;
@end

View File

@ -42,40 +42,14 @@ NS_ASSUME_NONNULL_BEGIN
*
* @return The number of keys in the classes collection.
*/
+ (NSUInteger)numberOfKeysInCollection;
+ (NSUInteger)numberOfKeysInCollectionWithTransaction:(YapDatabaseReadTransaction *)transaction;
/**
* Removes all objects in the classes collection.
*/
+ (void)removeAllObjectsInCollection;
/**
* A memory intesive method to get all objects in the collection. You should prefer using enumeration over this method
* whenever feasible. See `enumerateObjectsInCollectionUsingBlock`
*
* @return All objects in the classes collection.
*/
+ (NSArray *)allObjectsInCollection;
/**
* Enumerates all objects in collection.
*/
+ (void)enumerateCollectionObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block;
+ (void)enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction
usingBlock:(void (^)(id object, BOOL *stop))block;
/**
* @return Shared database connections for reading and writing.
*/
- (YapDatabaseConnection *)dbReadConnection;
+ (YapDatabaseConnection *)dbReadConnection;
- (YapDatabaseConnection *)dbReadWriteConnection;
+ (YapDatabaseConnection *)dbReadWriteConnection;
- (OWSPrimaryStorage *)primaryStorage;
+ (OWSPrimaryStorage *)primaryStorage;
@property (nonatomic, readonly) SDSDatabaseStorage *databaseStorage;
@property (class, nonatomic, readonly) SDSDatabaseStorage *databaseStorage;
@ -90,32 +64,13 @@ NS_ASSUME_NONNULL_BEGIN
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID
transaction:(YapDatabaseReadTransaction *)transaction
NS_SWIFT_NAME(fetch(uniqueId:transaction:));
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID NS_SWIFT_NAME(fetch(uniqueId:));
/**
* Saves the object with the shared readWrite connection.
*
* This method will block if another readWrite transaction is open.
*/
- (void)save;
/**
* Assign the latest persisted values from the database.
*/
- (void)reload;
- (void)reloadWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (void)reloadWithTransaction:(YapDatabaseReadTransaction *)transaction ignoreMissing:(BOOL)ignoreMissing;
/**
* Saves the object with the shared readWrite connection - does not block.
*
* Be mindful that this method may clobber other changes persisted
* while waiting to open the readWrite transaction.
*
* @param completionBlock is called on the main thread
*/
- (void)saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock;
/**
* Saves the object with the provided transaction
*
@ -129,7 +84,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSString *uniqueId;
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)remove;
#pragma mark - Write Hooks
@ -149,25 +103,16 @@ NS_ASSUME_NONNULL_BEGIN
// GRDB TODO: Ensure these ydb_ methods are only be used before
// and during the ydb-to-grdb migration.
+ (NSUInteger)ydb_numberOfKeysInCollection;
+ (NSUInteger)ydb_numberOfKeysInCollectionWithTransaction:(YapDatabaseReadTransaction *)transaction;
+ (void)ydb_removeAllObjectsInCollection;
+ (NSArray *)ydb_allObjectsInCollection;
+ (void)ydb_enumerateCollectionObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block;
+ (void)ydb_enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction
usingBlock:(void (^)(id object, BOOL *stop))block;
+ (nullable instancetype)ydb_fetchObjectWithUniqueID:(NSString *)uniqueID
transaction:(YapDatabaseReadTransaction *)transaction
NS_SWIFT_NAME(ydb_fetch(uniqueId:transaction:));
+ (nullable instancetype)ydb_fetchObjectWithUniqueID:(NSString *)uniqueID NS_SWIFT_NAME(ydb_fetch(uniqueId:));
- (void)ydb_save;
- (void)ydb_reload;
- (void)ydb_reloadWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (void)ydb_reloadWithTransaction:(YapDatabaseReadTransaction *)transaction ignoreMissing:(BOOL)ignoreMissing;
- (void)ydb_saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock;
- (void)ydb_saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)ydb_removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)ydb_remove;
@end

View File

@ -60,74 +60,13 @@ NS_ASSUME_NONNULL_BEGIN
[transaction setObject:self forKey:self.uniqueId inCollection:[[self class] collection]];
}
- (void)save
{
[[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self saveWithTransaction:transaction];
}];
}
- (void)saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock
{
[[self dbReadWriteConnection] asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[self saveWithTransaction:transaction];
}
completionBlock:completionBlock];
}
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
[transaction removeObjectForKey:self.uniqueId inCollection:[[self class] collection]];
}
- (void)remove
{
[[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self removeWithTransaction:transaction];
}];
}
- (YapDatabaseConnection *)dbReadConnection
{
return [[self class] dbReadConnection];
}
- (YapDatabaseConnection *)dbReadWriteConnection
{
return [[self class] dbReadWriteConnection];
}
- (OWSPrimaryStorage *)primaryStorage
{
return [[self class] primaryStorage];
}
#pragma mark Class Methods
+ (YapDatabaseConnection *)dbReadConnection
{
OWSJanksUI();
// We use TSYapDatabaseObject's dbReadWriteConnection (not OWSPrimaryStorage's
// dbReadConnection) for consistency, since we tend to [TSYapDatabaseObject
// save] and want to write to the same connection we read from. To get true
// consistency, we'd want to update entities by reading & writing from within
// the same transaction, but that'll be a big refactor.
return self.dbReadWriteConnection;
}
+ (YapDatabaseConnection *)dbReadWriteConnection
{
OWSJanksUI();
return SSKEnvironment.shared.objectReadWriteConnection;
}
+ (OWSPrimaryStorage *)primaryStorage
{
return [OWSPrimaryStorage sharedManager];
}
- (SDSDatabaseStorage *)databaseStorage
{
return SDSDatabaseStorage.shared;
@ -143,36 +82,11 @@ NS_ASSUME_NONNULL_BEGIN
return NSStringFromClass([self class]);
}
+ (NSUInteger)numberOfKeysInCollection
{
__block NSUInteger count;
[[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
count = [self numberOfKeysInCollectionWithTransaction:transaction];
}];
return count;
}
+ (NSUInteger)numberOfKeysInCollectionWithTransaction:(YapDatabaseReadTransaction *)transaction
{
return [transaction numberOfKeysInCollection:[self collection]];
}
+ (NSArray *)allObjectsInCollection
{
__block NSMutableArray *all = [[NSMutableArray alloc] initWithCapacity:[self numberOfKeysInCollection]];
[self enumerateCollectionObjectsUsingBlock:^(id object, BOOL *stop) {
[all addObject:object];
}];
return [all copy];
}
+ (void)enumerateCollectionObjectsUsingBlock:(void (^)(id object, BOOL *stop))block
{
[[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[self enumerateCollectionObjectsWithTransaction:transaction usingBlock:block];
}];
}
+ (void)enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction
usingBlock:(void (^)(id object, BOOL *stop))block
{
@ -185,37 +99,14 @@ NS_ASSUME_NONNULL_BEGIN
[transaction enumerateRowsInCollection:[self collection] usingBlock:yapBlock];
}
+ (void)removeAllObjectsInCollection
{
[[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:[self collection]];
}];
}
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID
transaction:(YapDatabaseReadTransaction *)transaction
{
return [transaction objectForKey:uniqueID inCollection:[self collection]];
}
+ (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID
{
__block id _Nullable object = nil;
[[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:uniqueID inCollection:[self collection]];
}];
return object;
}
#pragma mark Reload
- (void)reload
{
[self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
[self reloadWithTransaction:transaction];
}];
}
- (void)reloadWithTransaction:(YapDatabaseReadTransaction *)transaction
{
[self reloadWithTransaction:transaction ignoreMissing:NO];
@ -273,11 +164,6 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - YDB Deprecation
+ (NSUInteger)ydb_numberOfKeysInCollection
{
return [self numberOfKeysInCollection];
}
+ (NSUInteger)ydb_numberOfKeysInCollectionWithTransaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
@ -285,21 +171,6 @@ NS_ASSUME_NONNULL_BEGIN
return [self numberOfKeysInCollectionWithTransaction:transaction];
}
+ (void)ydb_removeAllObjectsInCollection
{
[self removeAllObjectsInCollection];
}
+ (NSArray *)ydb_allObjectsInCollection
{
return [self allObjectsInCollection];
}
+ (void)ydb_enumerateCollectionObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block
{
return [self enumerateCollectionObjectsUsingBlock:block];
}
+ (void)ydb_enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction
usingBlock:(void (^)(id object, BOOL *stop))block
{
@ -316,21 +187,6 @@ NS_ASSUME_NONNULL_BEGIN
return [self fetchObjectWithUniqueID:uniqueID transaction:transaction];
}
+ (nullable instancetype)ydb_fetchObjectWithUniqueID:(NSString *)uniqueID
{
return [self fetchObjectWithUniqueID:uniqueID];
}
- (void)ydb_save
{
[self save];
}
- (void)ydb_reload
{
[self reload];
}
- (void)ydb_reloadWithTransaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
@ -345,11 +201,6 @@ NS_ASSUME_NONNULL_BEGIN
[self reloadWithTransaction:transaction ignoreMissing:ignoreMissing];
}
- (void)ydb_saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock
{
[self saveAsyncWithCompletionBlock:completionBlock];
}
- (void)ydb_saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
@ -364,11 +215,6 @@ NS_ASSUME_NONNULL_BEGIN
[self removeWithTransaction:transaction];
}
- (void)ydb_remove
{
[self remove];
}
@end
NS_ASSUME_NONNULL_END