2015-12-07 03:31:43 +01:00
|
|
|
//
|
2019-01-23 15:05:08 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2015-12-07 03:31:43 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "TSDatabaseSecondaryIndexes.h"
|
2018-04-23 23:07:13 +02:00
|
|
|
#import "OWSStorage.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
#import "TSInteraction.h"
|
|
|
|
|
2019-01-23 15:05:08 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
#define TSTimeStampSQLiteIndex @"messagesTimeStamp"
|
|
|
|
|
|
|
|
@implementation TSDatabaseSecondaryIndexes
|
|
|
|
|
2018-04-24 16:02:20 +02:00
|
|
|
+ (NSString *)registerTimeStampIndexExtensionName
|
|
|
|
{
|
|
|
|
return @"idx";
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
+ (YapDatabaseSecondaryIndex *)registerTimeStampIndex {
|
|
|
|
YapDatabaseSecondaryIndexSetup *setup = [[YapDatabaseSecondaryIndexSetup alloc] init];
|
|
|
|
[setup addColumn:TSTimeStampSQLiteIndex withType:YapDatabaseSecondaryIndexTypeReal];
|
|
|
|
|
|
|
|
YapDatabaseSecondaryIndexWithObjectBlock block =
|
2016-03-27 22:27:31 +02:00
|
|
|
^(YapDatabaseReadTransaction *transaction, NSMutableDictionary *dict, NSString *collection, NSString *key, id object) {
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
if ([object isKindOfClass:[TSInteraction class]]) {
|
|
|
|
TSInteraction *interaction = (TSInteraction *)object;
|
|
|
|
|
|
|
|
[dict setObject:@(interaction.timestamp) forKey:TSTimeStampSQLiteIndex];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
YapDatabaseSecondaryIndexHandler *handler = [YapDatabaseSecondaryIndexHandler withObjectBlock:block];
|
|
|
|
|
2018-04-24 15:56:24 +02:00
|
|
|
YapDatabaseSecondaryIndex *secondaryIndex =
|
|
|
|
[[YapDatabaseSecondaryIndex alloc] initWithSetup:setup handler:handler versionTag:nil];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
return secondaryIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)enumerateMessagesWithTimestamp:(uint64_t)timestamp
|
|
|
|
withBlock:(void (^)(NSString *collection, NSString *key, BOOL *stop))block
|
2018-04-09 19:58:12 +02:00
|
|
|
usingTransaction:(YapDatabaseReadTransaction *)transaction
|
|
|
|
{
|
2015-12-07 03:31:43 +01:00
|
|
|
NSString *formattedString = [NSString stringWithFormat:@"WHERE %@ = %lld", TSTimeStampSQLiteIndex, timestamp];
|
|
|
|
YapDatabaseQuery *query = [YapDatabaseQuery queryWithFormat:formattedString];
|
2018-04-24 16:02:20 +02:00
|
|
|
[[transaction ext:[self registerTimeStampIndexExtensionName]] enumerateKeysMatchingQuery:query usingBlock:block];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
2017-06-15 19:37:10 +02:00
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@end
|
2019-01-23 15:05:08 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|