Skip to content

Commit

Permalink
Optimized drill down conversation query
Browse files Browse the repository at this point in the history
git-svn-id: http://naan.net/svn/trunk/TwitterFon@1661 542e9493-1a22-0410-9183-c10453d2b9ee
  • Loading branch information
kaz committed Jan 9, 2009
1 parent 1a95da1 commit da23210
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions Classes/Models/Status.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -280,31 +280,43 @@ - (BOOL)hasConversation
- (int)getConversation:(NSMutableArray*)messages - (int)getConversation:(NSMutableArray*)messages
{ {
NSMutableDictionary *hash = [NSMutableDictionary dictionary]; NSMutableDictionary *hash = [NSMutableDictionary dictionary];
NSMutableArray *array = [NSMutableArray array];


static char *sql = "SELECT * FROM statuses WHERE id = ? OR in_reply_to_status_id = ?"; NSMutableArray *idArray = [NSMutableArray array];
Statement *stmt = [DBConnection statementWithQuery:sql]; NSMutableArray *replyArray = [NSMutableArray array];


int count = 1; int count = 1;
[messages addObject:self]; [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]]; [hash setObject:self forKey:[NSString stringWithFormat:@"%lld", self.tweetId]];


INIT_STOPWATCH(s); INIT_STOPWATCH(s);


while ([array count]) { while ([idArray count]) {
Status *sts = [array lastObject];
[array removeLastObject]; NSString *replies = [idArray componentsJoinedByString:@","];
[stmt bindInt64:sts.inReplyToStatusId forIndex:1]; NSString *ids = [replyArray componentsJoinedByString:@","];
[stmt bindInt64:sts.tweetId forIndex:2];

[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) { while ([stmt step] == SQLITE_ROW) {
NSString *idStr = [NSString stringWithFormat:@"%lld", [stmt getInt64:0]]; NSString *idStr = [NSString stringWithFormat:@"%lld", [stmt getInt64:0]];
//NSLog(@"Found %@", idStr);
if (![hash objectForKey:idStr]) { if (![hash objectForKey:idStr]) {
Status *s = [Status initWithStatement:stmt type:TWEET_TYPE_FRIENDS]; Status *s = [Status initWithStatement:stmt type:TWEET_TYPE_FRIENDS];
[hash setObject:s forKey:idStr]; [hash setObject:s forKey:idStr];
[messages addObject:s]; [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 // Up to 100 messages
if (++count >= 100) break; if (++count >= 100) break;
} }
Expand Down

0 comments on commit da23210

Please sign in to comment.