session-ios/SignalServiceKit/src/Storage/TSDatabaseSecondaryIndexes.m

55 lines
1.9 KiB
Mathematica
Raw Normal View History

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"
#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
+ (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];
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];
[[transaction ext:[self registerTimeStampIndexExtensionName]] enumerateKeysMatchingQuery:query usingBlock:block];
2015-12-07 03:31:43 +01:00
}
2015-12-07 03:31:43 +01:00
@end
2019-01-23 15:05:08 +01:00
NS_ASSUME_NONNULL_END