Skip to content
Browse files

Optimized drill down conversation query

git-svn-id: http://naan.net/svn/trunk/TwitterFon@1661 542e9493-1a22-0410-9183-c10453d2b9ee
  • Loading branch information...
1 parent 1a95da1 commit da23210abf4788e793ebd2f086ca7b691cf80430 kaz committed
Showing with 23 additions and 11 deletions.
  1. +23 −11 Classes/Models/Status.m
View
34 Classes/Models/Status.m
@@ -280,31 +280,43 @@ - (BOOL)hasConversation
- (int)getConversation:(NSMutableArray*)messages
{
NSMutableDictionary *hash = [NSMutableDictionary dictionary];
- NSMutableArray *array = [NSMutableArray array];
- static char *sql = "SELECT * FROM statuses WHERE id = ? OR in_reply_to_status_id = ?";
- Statement *stmt = [DBConnection statementWithQuery:sql];
+ NSMutableArray *idArray = [NSMutableArray array];
+ NSMutableArray *replyArray = [NSMutableArray array];
int count = 1;
[messages addObject:self];
- [array addObject:self];
+ [idArray addObject:[NSNumber numberWithLongLong:self.tweetId]];
+ [replyArray addObject:[NSNumber numberWithLongLong:self.inReplyToStatusId]];
+
[hash setObject:self forKey:[NSString stringWithFormat:@"%lld", self.tweetId]];
INIT_STOPWATCH(s);
- while ([array count]) {
- Status *sts = [array lastObject];
- [array removeLastObject];
- [stmt bindInt64:sts.inReplyToStatusId forIndex:1];
- [stmt bindInt64:sts.tweetId forIndex:2];
-
+ while ([idArray count]) {
+
+ NSString *replies = [idArray componentsJoinedByString:@","];
+ NSString *ids = [replyArray componentsJoinedByString:@","];
+
+ [idArray removeAllObjects];
+ [replyArray removeAllObjects];
+
+ NSString *sql = [NSString stringWithFormat:@"SELECT * FROM statuses WHERE id IN (%@) OR in_reply_to_status_id IN (%@)", ids, replies];
+ Statement *stmt = [DBConnection statementWithQuery:[sql UTF8String]];
+
+ //NSLog(@"Exec %@", sql);
while ([stmt step] == SQLITE_ROW) {
NSString *idStr = [NSString stringWithFormat:@"%lld", [stmt getInt64:0]];
+ //NSLog(@"Found %@", idStr);
if (![hash objectForKey:idStr]) {
Status *s = [Status initWithStatement:stmt type:TWEET_TYPE_FRIENDS];
[hash setObject:s forKey:idStr];
[messages addObject:s];
- [array addObject:s];
+ [idArray addObject:[NSNumber numberWithLongLong:s.tweetId]];
+ if (s.inReplyToStatusId) {
+ [replyArray addObject:[NSNumber numberWithLongLong:s.inReplyToStatusId]];
+ }
+
// Up to 100 messages
if (++count >= 100) break;
}

0 comments on commit da23210

Please sign in to comment.
Something went wrong with that request. Please try again.