Rework model read caches.

This commit is contained in:
Matthew Chen 2020-06-05 15:40:13 -03:00
parent a44f3434fe
commit ea1b62e838
12 changed files with 110 additions and 22 deletions

View File

@ -11,7 +11,6 @@ import Contacts
// TODO: We might be able to merge this with OWSFakeContactsManager.
@objc
class GRDBFullTextSearcherContactsManager: NSObject, ContactsManagerProtocol {
let signalAccountReadCache = SignalAccountReadCache()
func comparableName(for signalAccount: SignalAccount, transaction: SDSAnyReadTransaction) -> String {
return self.displayName(for: signalAccount.recipientAddress)

View File

@ -321,7 +321,6 @@ class GroupAndContactStreamTest: SignalBaseTest {
}
class TestContactsManager: NSObject, ContactsManagerProtocol {
let signalAccountReadCache = SignalAccountReadCache()
func comparableName(for signalAccount: SignalAccount, transaction: SDSAnyReadTransaction) -> String {
return signalAccount.recipientAddress.stringForDisplay
@ -395,6 +394,6 @@ class TestContactsManager: NSObject, ContactsManagerProtocol {
func avatarImage(forCNContactId contactId: String?) -> UIImage? {
return nil
}
var unknownUserLabel: String = "unknown"
}

View File

@ -52,8 +52,6 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
@implementation OWSContactsManager
@synthesize signalAccountReadCache = _signalAccountReadCache;
#pragma mark - Dependencies
- (SDSDatabaseStorage *)databaseStorage
@ -66,6 +64,11 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
return OWSProfileManager.sharedManager;
}
- (SignalAccountReadCache *)signalAccountReadCache
{
return SSKEnvironment.shared.modelReadCaches.signalAccountReadCache;
}
#pragma mark -
- (id)init
@ -83,7 +86,6 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
_allContacts = @[];
_allContactsMap = @{};
_signalAccountReadCache = [SignalAccountReadCache new];
_signalAccounts = @[];
_systemContactsFetcher = [SystemContactsFetcher new];
_systemContactsFetcher.delegate = self;

View File

@ -116,6 +116,7 @@ NS_ASSUME_NONNULL_BEGIN
BulkProfileFetch *bulkProfileFetch = [BulkProfileFetch new];
BulkUUIDLookup *bulkUUIDLookup = [BulkUUIDLookup new];
id<VersionedProfiles> versionedProfiles = [VersionedProfilesImpl new];
ModelReadCaches *modelReadCaches = [ModelReadCaches new];
EarlyMessageManager *earlyMessageManager = [EarlyMessageManager new];
[Environment setShared:[[Environment alloc] initWithAudioSession:audioSession
@ -175,6 +176,7 @@ NS_ASSUME_NONNULL_BEGIN
bulkProfileFetch:bulkProfileFetch
bulkUUIDLookup:bulkUUIDLookup
versionedProfiles:versionedProfiles
modelReadCaches:modelReadCaches
earlyMessageManager:earlyMessageManager]];
appSpecificSingletonBlock();

View File

@ -34,6 +34,11 @@ NSUInteger const SignalAccountSchemaVersion = 1;
return SSKEnvironment.shared.contactsManager;
}
- (SignalAccountReadCache *)signalAccountReadCache
{
return SSKEnvironment.shared.modelReadCaches.signalAccountReadCache;
}
#pragma mark -
+ (BOOL)shouldBeIndexedForFTS
@ -218,21 +223,21 @@ NSUInteger const SignalAccountSchemaVersion = 1;
{
[super anyDidInsertWithTransaction:transaction];
[self.contactsManager.signalAccountReadCache didInsertOrUpdateSignalAccount:self transaction:transaction];
[self.signalAccountReadCache didInsertOrUpdateSignalAccount:self transaction:transaction];
}
- (void)anyDidUpdateWithTransaction:(SDSAnyWriteTransaction *)transaction
{
[super anyDidUpdateWithTransaction:transaction];
[self.contactsManager.signalAccountReadCache didInsertOrUpdateSignalAccount:self transaction:transaction];
[self.signalAccountReadCache didInsertOrUpdateSignalAccount:self transaction:transaction];
}
- (void)anyDidRemoveWithTransaction:(SDSAnyWriteTransaction *)transaction
{
[super anyDidRemoveWithTransaction:transaction];
[self.contactsManager.signalAccountReadCache didRemoveSignalAccount:self transaction:transaction];
[self.signalAccountReadCache didRemoveSignalAccount:self transaction:transaction];
}
@end

View File

@ -62,6 +62,16 @@ const NSUInteger SignalRecipientSchemaVersion = 1;
return SSKEnvironment.shared.sessionStore;
}
+ (SignalRecipientReadCache *)signalRecipientReadCache
{
return SSKEnvironment.shared.modelReadCaches.signalRecipientReadCache;
}
- (SignalRecipientReadCache *)signalRecipientReadCache
{
return SSKEnvironment.shared.modelReadCaches.signalRecipientReadCache;
}
#pragma mark -
+ (instancetype)getOrBuildUnsavedRecipientForAddress:(SignalServiceAddress *)address
@ -171,12 +181,11 @@ const NSUInteger SignalRecipientSchemaVersion = 1;
{
OWSAssertDebug(transaction);
OWSAssertDebug(address.isValid);
SignalRecipient *_Nullable signalRecipient = [self.recipientFinder signalRecipientForAddress:address
transaction:transaction];
SignalRecipient *_Nullable signalRecipient =
[self.signalRecipientReadCache getSignalRecipientForAddress:address transaction:transaction];
if (mustHaveDevices && signalRecipient.devices.count < 1) {
return nil;
}
return signalRecipient;
}
@ -456,6 +465,27 @@ const NSUInteger SignalRecipientSchemaVersion = 1;
}
}
- (void)anyDidInsertWithTransaction:(SDSAnyWriteTransaction *)transaction
{
[super anyDidInsertWithTransaction:transaction];
[self.signalRecipientReadCache didInsertOrUpdateSignalRecipient:self transaction:transaction];
}
- (void)anyDidUpdateWithTransaction:(SDSAnyWriteTransaction *)transaction
{
[super anyDidUpdateWithTransaction:transaction];
[self.signalRecipientReadCache didInsertOrUpdateSignalRecipient:self transaction:transaction];
}
- (void)anyDidRemoveWithTransaction:(SDSAnyWriteTransaction *)transaction
{
[super anyDidRemoveWithTransaction:transaction];
[self.signalRecipientReadCache didRemoveSignalRecipient:self transaction:transaction];
}
@end
NS_ASSUME_NONNULL_END

View File

@ -10,7 +10,6 @@ NS_ASSUME_NONNULL_BEGIN
@class PhoneNumber;
@class SDSAnyReadTransaction;
@class SignalAccount;
@class SignalAccountReadCache;
@class SignalServiceAddress;
@class TSThread;
@class UIImage;
@ -55,8 +54,6 @@ NS_ASSUME_NONNULL_BEGIN
- (NSString *)comparableNameForSignalAccount:(SignalAccount *)signalAccount
transaction:(SDSAnyReadTransaction *)transaction;
@property (nonatomic, readonly) SignalAccountReadCache *signalAccountReadCache;
@property (nonatomic, readonly) NSString *unknownUserLabel;
#pragma mark - CNContacts

View File

@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
@class MessageFetcherJob;
@class MessageProcessing;
@class MessageSenderJobQueue;
@class ModelReadCaches;
@class OWS2FAManager;
@class OWSAttachmentDownloads;
@class OWSBatchMessageProcessor;
@ -107,6 +108,7 @@ NS_ASSUME_NONNULL_BEGIN
bulkProfileFetch:(BulkProfileFetch *)bulkProfileFetch
bulkUUIDLookup:(BulkUUIDLookup *)bulkUUIDLookup
versionedProfiles:(id<VersionedProfiles>)versionedProfiles
modelReadCaches:(ModelReadCaches *)modelReadCaches
earlyMessageManager:(EarlyMessageManager *)earlyMessageManager NS_DESIGNATED_INITIALIZER;
@property (nonatomic, readonly, class) SSKEnvironment *shared;
@ -165,6 +167,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) BulkProfileFetch *bulkProfileFetch;
@property (nonatomic, readonly) BulkUUIDLookup *bulkUUIDLookup;
@property (nonatomic, readonly) id<VersionedProfiles> versionedProfiles;
@property (nonatomic, readonly) ModelReadCaches *modelReadCaches;
@property (nonatomic, readonly) EarlyMessageManager *earlyMessageManager;
@property (nonatomic, readonly, nullable) OWSPrimaryStorage *primaryStorage;

View File

@ -52,6 +52,7 @@ static SSKEnvironment *sharedSSKEnvironment;
@property (nonatomic) BulkProfileFetch *bulkProfileFetch;
@property (nonatomic) BulkUUIDLookup *bulkUUIDLookup;
@property (nonatomic) id<VersionedProfiles> versionedProfiles;
@property (nonatomic) ModelReadCaches *modelReadCaches;
@property (nonatomic) EarlyMessageManager *earlyMessageManager;
@end
@ -110,6 +111,7 @@ static SSKEnvironment *sharedSSKEnvironment;
bulkProfileFetch:(BulkProfileFetch *)bulkProfileFetch
bulkUUIDLookup:(BulkUUIDLookup *)bulkUUIDLookup
versionedProfiles:(id<VersionedProfiles>)versionedProfiles
modelReadCaches:(ModelReadCaches *)modelReadCaches
earlyMessageManager:(EarlyMessageManager *)earlyMessageManager
{
self = [super init];
@ -162,6 +164,7 @@ static SSKEnvironment *sharedSSKEnvironment;
OWSAssertDebug(bulkProfileFetch);
OWSAssertDebug(versionedProfiles);
OWSAssertDebug(bulkUUIDLookup);
OWSAssertDebug(modelReadCaches);
OWSAssertDebug(earlyMessageManager);
_contactsManager = contactsManager;
@ -210,6 +213,7 @@ static SSKEnvironment *sharedSSKEnvironment;
_bulkProfileFetch = bulkProfileFetch;
_versionedProfiles = versionedProfiles;
_bulkUUIDLookup = bulkUUIDLookup;
_modelReadCaches = modelReadCaches;
_earlyMessageManager = earlyMessageManager;
return self;

View File

@ -81,11 +81,6 @@ public class FakeContactsManager: NSObject, ContactsManagerProtocol {
return nil
}
private let _signalAccountReadCache = SignalAccountReadCache()
public var signalAccountReadCache: SignalAccountReadCache {
return _signalAccountReadCache
}
public var unknownUserLabel: String {
"Unknown"
}

View File

@ -108,6 +108,7 @@ NS_ASSUME_NONNULL_BEGIN
BulkProfileFetch *bulkProfileFetch = [BulkProfileFetch new];
BulkUUIDLookup *bulkUUIDLookup = [BulkUUIDLookup new];
id<VersionedProfiles> versionedProfiles = [MockVersionedProfiles new];
ModelReadCaches *modelReadCaches = [ModelReadCaches new];
EarlyMessageManager *earlyMessageManager = [EarlyMessageManager new];
self = [super initWithContactsManager:contactsManager
@ -156,6 +157,7 @@ NS_ASSUME_NONNULL_BEGIN
bulkProfileFetch:bulkProfileFetch
bulkUUIDLookup:bulkUUIDLookup
versionedProfiles:versionedProfiles
modelReadCaches:modelReadCaches
earlyMessageManager:earlyMessageManager];
if (!self) {

View File

@ -276,8 +276,8 @@ public class SignalAccountReadCache: NSObject {
cache = ModelReadCache(keyBlock: {
$0.recipientAddress
},
readBlock: { (address, transaction) in
return accountFinder.signalAccount(for: address, transaction: transaction)
readBlock: { (address, transaction) in
return accountFinder.signalAccount(for: address, transaction: transaction)
})
}
@ -301,3 +301,53 @@ public class SignalAccountReadCache: NSObject {
cache.didInsertOrUpdate(value: signalAccount, transaction: transaction)
}
}
// MARK: -
@objc
public class SignalRecipientReadCache: NSObject {
private let cache: ModelReadCache<SignalServiceAddress, SignalRecipient>
@objc
public override init() {
let recipientFinder = AnySignalRecipientFinder()
cache = ModelReadCache(keyBlock: {
$0.address
},
readBlock: { (address, transaction) in
return recipientFinder.signalRecipient(for: address, transaction: transaction)
})
}
@objc(getSignalRecipientForAddress:transaction:)
public func getSignalRecipient(address: SignalServiceAddress, transaction: SDSAnyReadTransaction) -> SignalRecipient? {
return cache.getValue(for: address, transaction: transaction)
}
@objc
public func getSignalRecipientWithSneakyTransaction(address: SignalServiceAddress) -> SignalRecipient? {
return cache.getValueWithSneakyTransaction(for: address)
}
@objc(didRemoveSignalRecipient:transaction:)
public func didRemove(signalRecipient: SignalRecipient, transaction: SDSAnyWriteTransaction) {
cache.didRemove(value: signalRecipient, transaction: transaction)
}
@objc(didInsertOrUpdateSignalRecipient:transaction:)
public func didInsertOrUpdate(signalRecipient: SignalRecipient, transaction: SDSAnyWriteTransaction) {
cache.didInsertOrUpdate(value: signalRecipient, transaction: transaction)
}
}
// MARK: -
@objc
public class ModelReadCaches: NSObject {
// UserProfileReadCache is only used within the profile manager.
@objc
public let signalAccountReadCache = SignalAccountReadCache()
@objc
public let signalRecipientReadCache = SignalRecipientReadCache()
}