[Cpan-forum-commit] rev 280 - in trunk: . lib/CPAN lib/CPAN/Forum lib/CPAN/Forum/DB
svn at pti.co.il
svn at pti.co.il
Sat Jul 21 18:16:39 EEST 2007
Author: gabor
Date: 2007-07-21 18:16:39 +0300 (Sat, 21 Jul 2007)
New Revision: 280
Modified:
trunk/
trunk/lib/CPAN/Forum.pm
trunk/lib/CPAN/Forum/DB/Posts.pm
trunk/lib/CPAN/Forum/DB/Tags.pm
trunk/lib/CPAN/Forum/DBI.pm
Log:
avoid calling thread_count for each post on the listing by creating one
statement to count all thread at once
Property changes on: trunk
___________________________________________________________________
Name: svk:merge
- 7bc34947-122d-0410-bc5a-f898d2bb5f81:/local/cpan-forum:4272
8c4c90e1-83eb-0310-96eb-e7cb62807872:/local/cpan-forum:12752
+ 7bc34947-122d-0410-bc5a-f898d2bb5f81:/local/cpan-forum:4273
8c4c90e1-83eb-0310-96eb-e7cb62807872:/local/cpan-forum:12752
Modified: trunk/lib/CPAN/Forum/DB/Posts.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Posts.pm 2007-07-21 15:16:10 UTC (rev 279)
+++ trunk/lib/CPAN/Forum/DB/Posts.pm 2007-07-21 15:16:39 UTC (rev 280)
@@ -13,7 +13,6 @@
__PACKAGE__->set_sql(latest => "SELECT __ESSENTIAL__ FROM __TABLE__ ORDER BY DATE DESC LIMIT %s");
-#__PACKAGE__->set_sql(count_thread => "SELECT count(*) FROM __TABLE__ WHERE thread=%s");
__PACKAGE__->set_sql(count_where => "SELECT count(*) FROM __TABLE__ WHERE %s='%s'");
__PACKAGE__->set_sql(count_like => "SELECT count(*) FROM __TABLE__ WHERE %s LIKE '%s'");
#__PACKAGE__->add_constraint('subject_too_long', subject => sub { length $_[0] <= 70 and $_[0] !~ /</});
@@ -127,12 +126,26 @@
ORDER BY cnt DESC";
return $self->_fetch_arrayref_of_hashes($sql);
}
-sub count_threads {
+
+# returns the number of entries in a single thread
+sub count_thread {
my ($self, $thread_id) = @_;
my $sql = "SELECT count(*) FROM posts WHERE thread=?";
return $self->_fetch_single_value($sql, $thread_id);
}
+# returns a hashref where the keys are the given thread ids
+# the values are the number of entries in the given thread
+sub count_threads {
+ my ($self, @thread_ids) = @_;
+ return {} if not @thread_ids;
+ # TODO check if they are all numbers?
+
+ my $ids = join ",", @thread_ids;
+ my $sql = "SELECT thread, COUNT(*) cnt FROM posts WHERE thread in ($ids) GROUP BY thread";
+ return $self->_selectall_hashref($sql, 'thread');
+}
+
sub stat_posts_by_group {
my ($self, $limit) = @_;
my $sql = qq{
Modified: trunk/lib/CPAN/Forum/DB/Tags.pm
===================================================================
--- trunk/lib/CPAN/Forum/DB/Tags.pm 2007-07-21 15:16:10 UTC (rev 279)
+++ trunk/lib/CPAN/Forum/DB/Tags.pm 2007-07-21 15:16:39 UTC (rev 280)
@@ -35,6 +35,7 @@
my $ar = $sth->fetchall_arrayref;
my @names = map { {name => $_->[0]} } @$ar;
return \@names;
+ #return $self->_fetch_arrayref_of_hashes($sql, $uid, $group_id);
}
sub get_tags_of_module {
Modified: trunk/lib/CPAN/Forum/DBI.pm
===================================================================
--- trunk/lib/CPAN/Forum/DBI.pm 2007-07-21 15:16:10 UTC (rev 279)
+++ trunk/lib/CPAN/Forum/DBI.pm 2007-07-21 15:16:39 UTC (rev 280)
@@ -98,5 +98,10 @@
return $self->_fetch_single_value("SELECT COUNT(*) FROM $table");
}
+sub _selectall_hashref {
+ my ($self, $sql, $key, @args) = @_;
+ my $dbh = CPAN::Forum::DBI::db_Main();
+ return $dbh->selectall_hashref($sql, $key, undef, @args);
+}
1;
Modified: trunk/lib/CPAN/Forum.pm
===================================================================
--- trunk/lib/CPAN/Forum.pm 2007-07-21 15:16:10 UTC (rev 279)
+++ trunk/lib/CPAN/Forum.pm 2007-07-21 15:16:39 UTC (rev 280)
@@ -733,9 +733,12 @@
my ($self, $it) = @_;
my @resp;
+ my @threads = map {$_->thread} @$it;
+ my $threads = CPAN::Forum::DB::Posts->count_threads(@threads);
+
foreach my $post (@$it) {
-#warn "called too many times";
- my $thread_count = CPAN::Forum::DB::Posts->count_threads($post->thread);
+#warn "called for each post";
+ my $thread_count = $threads->{$post->thread}{cnt};
push @resp, {
subject => _subject_escape($post->subject),
id => $post->id,
@@ -1075,7 +1078,7 @@
"in request",
);
}
- my $thread_count = CPAN::Forum::DB::Posts->count_threads($post->thread);
+ my $thread_count = CPAN::Forum::DB::Posts->count_thread($post->thread);
if ($thread_count > 1) {
$t->param(thread_id => $post->thread);
$t->param(thread_count => $thread_count);
More information about the Cpan-forum-commit
mailing list