mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
54 lines
1.9 KiB
Objective-C
54 lines
1.9 KiB
Objective-C
//
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "TSDatabaseSecondaryIndexes.h"
|
|
#import "OWSStorage.h"
|
|
#import "TSInteraction.h"
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
#define TSTimeStampSQLiteIndex @"messagesTimeStamp"
|
|
|
|
@implementation TSDatabaseSecondaryIndexes
|
|
|
|
+ (NSString *)registerTimeStampIndexExtensionName
|
|
{
|
|
return @"idx";
|
|
}
|
|
|
|
+ (YapDatabaseSecondaryIndex *)registerTimeStampIndex {
|
|
YapDatabaseSecondaryIndexSetup *setup = [[YapDatabaseSecondaryIndexSetup alloc] init];
|
|
[setup addColumn:TSTimeStampSQLiteIndex withType:YapDatabaseSecondaryIndexTypeReal];
|
|
|
|
YapDatabaseSecondaryIndexWithObjectBlock block =
|
|
^(YapDatabaseReadTransaction *transaction, NSMutableDictionary *dict, NSString *collection, NSString *key, id object) {
|
|
|
|
if ([object isKindOfClass:[TSInteraction class]]) {
|
|
TSInteraction *interaction = (TSInteraction *)object;
|
|
|
|
[dict setObject:@(interaction.timestamp) forKey:TSTimeStampSQLiteIndex];
|
|
}
|
|
};
|
|
|
|
YapDatabaseSecondaryIndexHandler *handler = [YapDatabaseSecondaryIndexHandler withObjectBlock:block];
|
|
|
|
YapDatabaseSecondaryIndex *secondaryIndex =
|
|
[[YapDatabaseSecondaryIndex alloc] initWithSetup:setup handler:handler versionTag:nil];
|
|
|
|
return secondaryIndex;
|
|
}
|
|
|
|
|
|
+ (void)enumerateMessagesWithTimestamp:(uint64_t)timestamp
|
|
withBlock:(void (^)(NSString *collection, NSString *key, BOOL *stop))block
|
|
usingTransaction:(YapDatabaseReadTransaction *)transaction
|
|
{
|
|
NSString *formattedString = [NSString stringWithFormat:@"WHERE %@ = %lld", TSTimeStampSQLiteIndex, timestamp];
|
|
YapDatabaseQuery *query = [YapDatabaseQuery queryWithFormat:formattedString];
|
|
[[transaction ext:[self registerTimeStampIndexExtensionName]] enumerateKeysMatchingQuery:query usingBlock:block];
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|