Modify TSStorageManager to use separate shared read and write connections.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 04ef06ce95
commit daae31d30e

@ -244,8 +244,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSArray<NSString *> *)textSecureIdentifiers { - (NSArray<NSString *> *)textSecureIdentifiers {
__block NSMutableArray *identifiers = [NSMutableArray array]; __block NSMutableArray *identifiers = [NSMutableArray array];
[[TSStorageManager sharedManager] [[TSStorageManager sharedManager].dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
for (PhoneNumber *number in self.parsedPhoneNumbers) { for (PhoneNumber *number in self.parsedPhoneNumbers) {
if ([SignalRecipient recipientWithTextSecureIdentifier:number.toE164 withTransaction:transaction]) { if ([SignalRecipient recipientWithTextSecureIdentifier:number.toE164 withTransaction:transaction]) {
[identifiers addObject:number.toE164]; [identifiers addObject:number.toE164];

@ -122,8 +122,8 @@ NS_ASSUME_NONNULL_BEGIN
} }
NSMutableSet *recipientIds = [NSMutableSet set]; NSMutableSet *recipientIds = [NSMutableSet set];
[[TSStorageManager sharedManager] [[TSStorageManager sharedManager].dbReadConnection
.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
NSArray *allRecipientKeys = [transaction allKeysInCollection:[SignalRecipient collection]]; NSArray *allRecipientKeys = [transaction allKeysInCollection:[SignalRecipient collection]];
[recipientIds addObjectsFromArray:allRecipientKeys]; [recipientIds addObjectsFromArray:allRecipientKeys];
}]; }];
@ -135,7 +135,7 @@ NS_ASSUME_NONNULL_BEGIN
[recipientIds minusSet:matchedIds]; [recipientIds minusSet:matchedIds];
// Cleaning up unregistered identifiers // Cleaning up unregistered identifiers
[[TSStorageManager sharedManager].dbConnection [[TSStorageManager sharedManager].dbReadWriteConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (NSString *identifier in recipientIds) { for (NSString *identifier in recipientIds) {
SignalRecipient *recipient = SignalRecipient *recipient =
@ -185,14 +185,13 @@ NS_ASSUME_NONNULL_BEGIN
} }
// Insert or update contact attributes // Insert or update contact attributes
[[TSStorageManager sharedManager] [[TSStorageManager sharedManager].dbReadWriteConnection
.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (NSString *identifier in attributesForIdentifier) { for (NSString *identifier in attributesForIdentifier) {
SignalRecipient *recipient = SignalRecipient *recipient =
[SignalRecipient recipientWithTextSecureIdentifier:identifier withTransaction:transaction]; [SignalRecipient recipientWithTextSecureIdentifier:identifier withTransaction:transaction];
if (!recipient) { if (!recipient) {
recipient = [[SignalRecipient alloc] initWithTextSecureIdentifier:identifier recipient = [[SignalRecipient alloc] initWithTextSecureIdentifier:identifier relay:nil];
relay:nil];
} }
NSDictionary *attributes = [attributesForIdentifier objectForKey:identifier]; NSDictionary *attributes = [attributesForIdentifier objectForKey:identifier];

@ -37,7 +37,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier + (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier
{ {
__block SignalRecipient *recipient; __block SignalRecipient *recipient;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
recipient = [self recipientWithTextSecureIdentifier:textSecureIdentifier withTransaction:transaction]; recipient = [self recipientWithTextSecureIdentifier:textSecureIdentifier withTransaction:transaction];
}]; }];
return recipient; return recipient;

@ -131,7 +131,7 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (void)enumerateInteractionsUsingBlock:(void (^)(TSInteraction *interaction))block - (void)enumerateInteractionsUsingBlock:(void (^)(TSInteraction *interaction))block
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self enumerateInteractionsWithTransaction:transaction [self enumerateInteractionsWithTransaction:transaction
usingBlock:^( usingBlock:^(
TSInteraction *interaction, YapDatabaseReadTransaction *transaction) { TSInteraction *interaction, YapDatabaseReadTransaction *transaction) {
@ -172,7 +172,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSUInteger)numberOfInteractions - (NSUInteger)numberOfInteractions
{ {
__block NSUInteger count; __block NSUInteger count;
[[self dbConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName]; YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
count = [interactionsByThread numberOfItemsInGroup:self.uniqueId]; count = [interactionsByThread numberOfItemsInGroup:self.uniqueId];
}]; }];
@ -237,7 +237,7 @@ NS_ASSUME_NONNULL_BEGIN
- (TSInteraction *) lastInteraction { - (TSInteraction *) lastInteraction {
__block TSInteraction *last; __block TSInteraction *last;
[TSStorageManager.sharedManager.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){ [TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
last = [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId]; last = [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId];
}]; }];
return last; return last;
@ -246,7 +246,7 @@ NS_ASSUME_NONNULL_BEGIN
- (TSInteraction *)lastInteractionForInbox - (TSInteraction *)lastInteractionForInbox
{ {
__block TSInteraction *last = nil; __block TSInteraction *last = nil;
[TSStorageManager.sharedManager.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[[transaction ext:TSMessageDatabaseViewExtensionName] [[transaction ext:TSMessageDatabaseViewExtensionName]
enumerateRowsInGroup:self.uniqueId enumerateRowsInGroup:self.uniqueId
withOptions:NSEnumerationReverse withOptions:NSEnumerationReverse
@ -397,7 +397,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate - (void)updateWithMutedUntilDate:(NSDate *)mutedUntilDate
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestThread:transaction [self applyChangeToSelfAndLatestThread:transaction
changeBlock:^(TSThread *thread) { changeBlock:^(TSThread *thread) {
[thread setMutedUntilDate:mutedUntilDate]; [thread setMutedUntilDate:mutedUntilDate];

@ -76,7 +76,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(contactId.length > 0); OWSAssert(contactId.length > 0);
__block TSContactThread *thread; __block TSContactThread *thread;
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [self getOrCreateThreadWithContactId:contactId transaction:transaction]; thread = [self getOrCreateThreadWithContactId:contactId transaction:transaction];
}]; }];

@ -91,7 +91,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(groupModel.groupId.length > 0); OWSAssert(groupModel.groupId.length > 0);
__block TSGroupThread *thread; __block TSGroupThread *thread;
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [self getOrCreateThreadWithGroupModel:groupModel transaction:transaction]; thread = [self getOrCreateThreadWithGroupModel:groupModel transaction:transaction];
}]; }];
return thread; return thread;

@ -1,4 +1,6 @@
// Copyright © 2016 Open Whisper Systems. All rights reserved. //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSReadReceipt.h" #import "OWSReadReceipt.h"
#import <YapDatabase/YapDatabase.h> #import <YapDatabase/YapDatabase.h>
@ -104,7 +106,7 @@ NSString *const OWSReadReceiptColumnSenderId = @"senderId";
stringWithFormat:@"WHERE %@ = ? AND %@ = ?", OWSReadReceiptColumnSenderId, OWSReadReceiptColumnTimestamp]; stringWithFormat:@"WHERE %@ = ? AND %@ = ?", OWSReadReceiptColumnSenderId, OWSReadReceiptColumnTimestamp];
YapDatabaseQuery *query = [YapDatabaseQuery queryWithFormat:queryFormat, senderId, @(timestamp)]; YapDatabaseQuery *query = [YapDatabaseQuery queryWithFormat:queryFormat, senderId, @(timestamp)];
[[self dbConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
[[transaction ext:OWSReadReceiptIndexOnSenderIdAndTimestamp] [[transaction ext:OWSReadReceiptIndexOnSenderIdAndTimestamp]
enumerateKeysAndObjectsMatchingQuery:query enumerateKeysAndObjectsMatchingQuery:query
usingBlock:^(NSString *collection, NSString *key, id object, BOOL *stop) { usingBlock:^(NSString *collection, NSString *key, id object, BOOL *stop) {

@ -94,7 +94,8 @@ NSString *const OWSReadReceiptsProcessorMarkedMessageAsReadNotification =
// Always mark the message specified by the read receipt as read. // Always mark the message specified by the read receipt as read.
[interactionsToMarkAsRead addObject:message]; [interactionsToMarkAsRead addObject:message];
[self.storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.storageManager.dbReadWriteConnection readWriteWithBlock:^(
YapDatabaseReadWriteTransaction *transaction) {
[[TSDatabaseView unseenDatabaseViewExtension:transaction] [[TSDatabaseView unseenDatabaseViewExtension:transaction]
enumerateRowsInGroup:message.uniqueThreadId enumerateRowsInGroup:message.uniqueThreadId
usingBlock:^(NSString *collection, usingBlock:^(NSString *collection,

@ -387,7 +387,7 @@ NS_ASSUME_NONNULL_BEGIN
if (transaction) { if (transaction) {
updateDataStore(transaction); updateDataStore(transaction);
} else { } else {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
updateDataStore(transaction); updateDataStore(transaction);
}]; }];
} }
@ -458,7 +458,7 @@ NS_ASSUME_NONNULL_BEGIN
if (transaction) { if (transaction) {
updateDataStore(transaction); updateDataStore(transaction);
} else { } else {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
updateDataStore(transaction); updateDataStore(transaction);
}]; }];
} }

@ -78,7 +78,7 @@ NSString *const TSIncomingMessageWasReadOnThisDeviceNotification = @"TSIncomingM
+ (nullable instancetype)findMessageWithAuthorId:(NSString *)authorId timestamp:(uint64_t)timestamp + (nullable instancetype)findMessageWithAuthorId:(NSString *)authorId timestamp:(uint64_t)timestamp
{ {
__block TSIncomingMessage *foundMessage; __block TSIncomingMessage *foundMessage;
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// In theory we could build a new secondaryIndex for (authorId,timestamp), but in practice there should // In theory we could build a new secondaryIndex for (authorId,timestamp), but in practice there should
// be *very* few (millisecond) timestamps with multiple authors. // be *very* few (millisecond) timestamps with multiple authors.
[TSDatabaseSecondaryIndexes [TSDatabaseSecondaryIndexes

@ -245,7 +245,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
{ {
OWSAssert(error); OWSAssert(error);
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestOutgoingMessage:transaction [self applyChangeToSelfAndLatestOutgoingMessage:transaction
changeBlock:^(TSOutgoingMessage *message) { changeBlock:^(TSOutgoingMessage *message) {
[message setMessageState:TSOutgoingMessageStateUnsent]; [message setMessageState:TSOutgoingMessageStateUnsent];
@ -256,7 +256,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithMessageState:(TSOutgoingMessageState)messageState - (void)updateWithMessageState:(TSOutgoingMessageState)messageState
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithMessageState:messageState transaction:transaction]; [self updateWithMessageState:messageState transaction:transaction];
}]; }];
} }
@ -274,7 +274,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript - (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestOutgoingMessage:transaction [self applyChangeToSelfAndLatestOutgoingMessage:transaction
changeBlock:^(TSOutgoingMessage *message) { changeBlock:^(TSOutgoingMessage *message) {
[message setHasSyncedTranscript:hasSyncedTranscript]; [message setHasSyncedTranscript:hasSyncedTranscript];
@ -295,7 +295,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithCustomMessage:(NSString *)customMessage - (void)updateWithCustomMessage:(NSString *)customMessage
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithCustomMessage:customMessage transaction:transaction]; [self updateWithCustomMessage:customMessage transaction:transaction];
}]; }];
} }
@ -312,14 +312,14 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithWasDelivered - (void)updateWithWasDelivered
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithWasDeliveredWithTransaction:transaction]; [self updateWithWasDeliveredWithTransaction:transaction];
}]; }];
} }
- (void)updateWithWasSentAndDelivered - (void)updateWithWasSentAndDelivered
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestOutgoingMessage:transaction [self applyChangeToSelfAndLatestOutgoingMessage:transaction
changeBlock:^(TSOutgoingMessage *message) { changeBlock:^(TSOutgoingMessage *message) {
[message setMessageState:TSOutgoingMessageStateSentToService]; [message setMessageState:TSOutgoingMessageStateSentToService];
@ -397,7 +397,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
- (void)updateWithSentRecipient:(NSString *)contactId - (void)updateWithSentRecipient:(NSString *)contactId
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithSentRecipient:contactId transaction:transaction]; [self updateWithSentRecipient:contactId transaction:transaction];
}]; }];
} }

@ -423,7 +423,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[messages addObject:[TSErrorMessage nonblockingIdentityChangeInThread:groupThread recipientId:recipientId]]; [messages addObject:[TSErrorMessage nonblockingIdentityChangeInThread:groupThread recipientId:recipientId]];
} }
[self.storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.storageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (TSMessage *message in messages) { for (TSMessage *message in messages) {
[message saveWithTransaction:transaction]; [message saveWithTransaction:transaction];
} }
@ -452,7 +452,8 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
@synchronized(self) @synchronized(self)
{ {
NSMutableArray<NSString *> *recipientIds = [NSMutableArray new]; NSMutableArray<NSString *> *recipientIds = [NSMutableArray new];
[self.storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.storageManager.dbReadWriteConnection readWriteWithBlock:^(
YapDatabaseReadWriteTransaction *transaction) {
[transaction enumerateKeysAndObjectsInCollection:OWSIdentityManager_QueuedVerificationStateSyncMessages [transaction enumerateKeysAndObjectsInCollection:OWSIdentityManager_QueuedVerificationStateSyncMessages
usingBlock:^(NSString *_Nonnull recipientId, usingBlock:^(NSString *_Nonnull recipientId,
id _Nonnull object, id _Nonnull object,
@ -731,7 +732,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
isLocalChange:isLocalChange]]; isLocalChange:isLocalChange]];
} }
[self.storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.storageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (TSMessage *message in messages) { for (TSMessage *message in messages) {
[message saveWithTransaction:transaction]; [message saveWithTransaction:transaction];
} }

@ -116,7 +116,7 @@ NSUInteger TSCallCurrentSchemaVersion = 1;
_callType = callType; _callType = callType;
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];
// redraw any thread-related unread count UI. // redraw any thread-related unread count UI.

@ -525,7 +525,7 @@ NS_ASSUME_NONNULL_BEGIN
NSString *recipientId = incomingEnvelope.source; NSString *recipientId = incomingEnvelope.source;
__block TSThread *thread; __block TSThread *thread;
[[TSStorageManager sharedManager].dbConnection [[TSStorageManager sharedManager].dbReadWriteConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction]; thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction];
}]; }];

@ -91,7 +91,7 @@ OWSSignalServiceProtosVerifiedState OWSVerificationStateToProtoState(OWSVerifica
{ {
changeBlock(self); changeBlock(self);
[[self class].dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { [[self class].dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
OWSRecipientIdentity *latest = [[self class] fetchObjectWithUniqueID:self.uniqueId transaction:transaction]; OWSRecipientIdentity *latest = [[self class] fetchObjectWithUniqueID:self.uniqueId transaction:transaction];
if (latest == nil) { if (latest == nil) {
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];

@ -148,7 +148,7 @@ NSString *const TSStorageManagerKeyPrekeyCurrentSignedPrekeyId = @"currentSigned
NSDate *firstPrekeyUpdateFailureDate = [self firstPrekeyUpdateFailureDate]; NSDate *firstPrekeyUpdateFailureDate = [self firstPrekeyUpdateFailureDate];
NSUInteger prekeyUpdateFailureCount = [self prekeyUpdateFailureCount]; NSUInteger prekeyUpdateFailureCount = [self prekeyUpdateFailureCount];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
__block int i = 0; __block int i = 0;
DDLogInfo(@"%@ SignedPreKeys Report:", tag); DDLogInfo(@"%@ SignedPreKeys Report:", tag);

@ -1,4 +1,6 @@
// Copyright (c) 2016 Open Whisper Systems. All rights reserved. //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSOrphanedDataCleaner.h" #import "OWSOrphanedDataCleaner.h"
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
@ -32,7 +34,7 @@
- (NSArray<NSString *> *)orphanedInteractionIds - (NSArray<NSString *> *)orphanedInteractionIds
{ {
NSMutableArray *interactionIds = [NSMutableArray new]; NSMutableArray *interactionIds = [NSMutableArray new];
[[TSInteraction dbConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [[TSInteraction dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
[TSInteraction enumerateCollectionObjectsWithTransaction:transaction [TSInteraction enumerateCollectionObjectsWithTransaction:transaction
usingBlock:^(TSInteraction *interaction, BOOL *stop) { usingBlock:^(TSInteraction *interaction, BOOL *stop) {
TSThread *thread = [TSThread TSThread *thread = [TSThread

@ -53,7 +53,7 @@
- (void)storePhoneNumber:(NSString *)phoneNumber - (void)storePhoneNumber:(NSString *)phoneNumber
{ {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:phoneNumber [transaction setObject:phoneNumber
forKey:TSStorageRegisteredNumberKey forKey:TSStorageRegisteredNumberKey
inCollection:TSStorageUserAccountCollection]; inCollection:TSStorageUserAccountCollection];
@ -61,9 +61,8 @@
} }
+ (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey { + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey {
YapDatabaseConnection *dbConn = [[self sharedManager] dbConnection]; TSStorageManager *sharedManager = self.sharedManager;
[sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[dbConn readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:authToken forKey:TSStorageServerAuthToken inCollection:TSStorageUserAccountCollection]; [transaction setObject:authToken forKey:TSStorageServerAuthToken inCollection:TSStorageUserAccountCollection];
[transaction setObject:signalingKey [transaction setObject:signalingKey
forKey:TSStorageServerSignalingKey forKey:TSStorageServerSignalingKey

@ -61,7 +61,8 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable SignedPreKeyRecord *)signedPreKeyRecordForKey:(NSString *)key inCollection:(NSString *)collection; - (nullable SignedPreKeyRecord *)signedPreKeyRecordForKey:(NSString *)key inCollection:(NSString *)collection;
- (void)purgeCollection:(NSString *)collection; - (void)purgeCollection:(NSString *)collection;
@property (nullable, nonatomic, readonly) YapDatabaseConnection *dbConnection; @property (nullable, nonatomic, readonly) YapDatabaseConnection *dbReadConnection;
@property (nullable, nonatomic, readonly) YapDatabaseConnection *dbReadWriteConnection;
@end @end

@ -156,7 +156,8 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
if (!_database) { if (!_database) {
return NO; return NO;
} }
_dbConnection = self.newDatabaseConnection; _dbReadConnection = self.newDatabaseConnection;
_dbReadWriteConnection = self.newDatabaseConnection;
return YES; return YES;
} }
@ -388,19 +389,19 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
#pragma mark - convenience methods #pragma mark - convenience methods
- (void)purgeCollection:(NSString *)collection { - (void)purgeCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:collection]; [transaction removeAllObjectsInCollection:collection];
}]; }];
} }
- (void)setObject:(id)object forKey:(NSString *)key inCollection:(NSString *)collection { - (void)setObject:(id)object forKey:(NSString *)key inCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction setObject:object forKey:key inCollection:collection]; [transaction setObject:object forKey:key inCollection:collection];
}]; }];
} }
- (void)removeObjectForKey:(NSString *)string inCollection:(NSString *)collection { - (void)removeObjectForKey:(NSString *)string inCollection:(NSString *)collection {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeObjectForKey:string inCollection:collection]; [transaction removeObjectForKey:string inCollection:collection];
}]; }];
} }
@ -408,7 +409,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
- (id)objectForKey:(NSString *)key inCollection:(NSString *)collection { - (id)objectForKey:(NSString *)key inCollection:(NSString *)collection {
__block NSString *object; __block NSString *object;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:key inCollection:collection]; object = [transaction objectForKey:key inCollection:collection];
}]; }];
@ -419,7 +420,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
{ {
__block NSDictionary *object; __block NSDictionary *object;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:key inCollection:collection]; object = [transaction objectForKey:key inCollection:collection];
}]; }];
@ -479,7 +480,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
- (int)incrementIntForKey:(NSString *)key inCollection:(NSString *)collection - (int)incrementIntForKey:(NSString *)key inCollection:(NSString *)collection
{ {
__block int value = 0; __block int value = 0;
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
value = [[transaction objectForKey:key inCollection:collection] intValue]; value = [[transaction objectForKey:key inCollection:collection] intValue];
value++; value++;
[transaction setObject:@(value) forKey:key inCollection:collection]; [transaction setObject:@(value) forKey:key inCollection:collection];
@ -503,7 +504,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
} }
- (void)deleteThreadsAndMessages { - (void)deleteThreadsAndMessages {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:[TSThread collection]]; [transaction removeAllObjectsInCollection:[TSThread collection]];
[transaction removeAllObjectsInCollection:[SignalRecipient collection]]; [transaction removeAllObjectsInCollection:[SignalRecipient collection]];
[transaction removeAllObjectsInCollection:[TSInteraction collection]]; [transaction removeAllObjectsInCollection:[TSInteraction collection]];
@ -529,7 +530,8 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
- (void)resetSignalStorage - (void)resetSignalStorage
{ {
self.database = nil; self.database = nil;
_dbConnection = nil; _dbReadConnection = nil;
_dbReadWriteConnection = nil;
[self deletePasswordFromKeychain]; [self deletePasswordFromKeychain];

@ -58,10 +58,12 @@
usingBlock:(void (^)(id object, BOOL *stop))block; usingBlock:(void (^)(id object, BOOL *stop))block;
/** /**
* @return A shared database connection. * @return Shared database connections for reading and writing.
*/ */
- (YapDatabaseConnection *)dbConnection; - (YapDatabaseConnection *)dbReadConnection;
+ (YapDatabaseConnection *)dbConnection; + (YapDatabaseConnection *)dbReadConnection;
- (YapDatabaseConnection *)dbReadWriteConnection;
+ (YapDatabaseConnection *)dbReadWriteConnection;
- (TSStorageManager *)storageManager; - (TSStorageManager *)storageManager;
+ (TSStorageManager *)storageManager; + (TSStorageManager *)storageManager;

@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 16/11/14. //
// Copyright (c) 2014 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSYapDatabaseObject.h" #import "TSYapDatabaseObject.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
@ -31,7 +32,7 @@
- (void)save - (void)save
{ {
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];
}]; }];
} }
@ -43,7 +44,7 @@
- (void)touch - (void)touch
{ {
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self touchWithTransaction:transaction]; [self touchWithTransaction:transaction];
}]; }];
} }
@ -55,14 +56,19 @@
- (void)remove - (void)remove
{ {
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self removeWithTransaction:transaction]; [self removeWithTransaction:transaction];
}]; }];
} }
- (YapDatabaseConnection *)dbConnection - (YapDatabaseConnection *)dbReadConnection
{ {
return [[self class] dbConnection]; return [[self class] dbReadConnection];
}
- (YapDatabaseConnection *)dbReadWriteConnection
{
return [[self class] dbReadWriteConnection];
} }
- (TSStorageManager *)storageManager - (TSStorageManager *)storageManager
@ -72,9 +78,19 @@
#pragma mark Class Methods #pragma mark Class Methods
+ (YapDatabaseConnection *)dbConnection + (YapDatabaseConnection *)dbReadConnection
{
// We use TSStorageManager's dbReadWriteConnection (not its 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 storageManager].dbReadWriteConnection;
}
+ (YapDatabaseConnection *)dbReadWriteConnection
{ {
return [self storageManager].dbConnection; return [self storageManager].dbReadWriteConnection;
} }
+ (TSStorageManager *)storageManager + (TSStorageManager *)storageManager
@ -90,7 +106,7 @@
+ (NSUInteger)numberOfKeysInCollection + (NSUInteger)numberOfKeysInCollection
{ {
__block NSUInteger count; __block NSUInteger count;
[[self dbConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) { [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
count = [self numberOfKeysInCollectionWithTransaction:transaction]; count = [self numberOfKeysInCollectionWithTransaction:transaction];
}]; }];
return count; return count;
@ -112,7 +128,7 @@
+ (void)enumerateCollectionObjectsUsingBlock:(void (^)(id object, BOOL *stop))block + (void)enumerateCollectionObjectsUsingBlock:(void (^)(id object, BOOL *stop))block
{ {
[[self dbConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) { [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
[self enumerateCollectionObjectsWithTransaction:transaction usingBlock:block]; [self enumerateCollectionObjectsWithTransaction:transaction usingBlock:block];
}]; }];
} }
@ -131,7 +147,7 @@
+ (void)removeAllObjectsInCollection + (void)removeAllObjectsInCollection
{ {
[[self dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[transaction removeAllObjectsInCollection:[self collection]]; [transaction removeAllObjectsInCollection:[self collection]];
}]; }];
} }
@ -144,7 +160,7 @@
+ (instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID + (instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID
{ {
__block id object; __block id object;
[[self dbConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) { [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
object = [transaction objectForKey:uniqueID inCollection:[self collection]]; object = [transaction objectForKey:uniqueID inCollection:[self collection]];
}]; }];
return object; return object;

Loading…
Cancel
Save