r103665 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103664‎ | r103665 | r103666 >
Date:11:53, 19 November 2011
Author:yuvipanda
Status:resolved (Comments)
Tags:
Comment:
Initial insertion of ShortURL entries done in batches
Modified paths:
  • /trunk/extensions/ShortUrl/populateShortUrlTable.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ShortUrl/populateShortUrlTable.php
@@ -26,31 +26,42 @@
2727 public function execute() {
2828 $rowCount = 0;
2929 $dbr = wfGetDB( DB_SLAVE );
30 - $res = $dbr->select(
31 - 'page',
32 - array( 'page_namespace', 'page_title' ),
33 - array(),
34 - __METHOD__
35 - );
 30+
 31+ $last_processed_id = 0;
 32+
3633 $insertBuffer = array();
3734
38 - foreach( $res as $row ) {
39 - $rowData = array(
40 - 'su_namespace' => $row->page_namespace,
41 - 'su_title' => $row->page_title
 35+ while( true ) {
 36+ $res = $dbr->select(
 37+ 'page',
 38+ array( 'page_id', 'page_namespace', 'page_title' ),
 39+ array( "page_id > " . $last_processed_id ),
 40+ __METHOD__,
 41+ array( 'LIMIT' => 100 )
4242 );
43 - array_push( $insertBuffer, $rowData );
44 - if( count( $insertBuffer ) % 100 == 0 ) {
45 - $this->insertRows( $insertBuffer );
46 - $insertBuffer = array();
 43+ if( $res->numRows() == 0 ) {
 44+ break;
 45+ }
 46+
 47+ foreach( $res as $row ) {
 48+ $rowCount++;
 49+
 50+ $rowData = array(
 51+ 'su_namespace' => $row->page_namespace,
 52+ 'su_title' => $row->page_title
 53+ );
 54+ $this->output( $insertBuffer );
 55+ array_push( $insertBuffer, $rowData );
 56+
 57+ $last_processed_id = $row->page_id;
4758 }
48 - $this->output( $rowCount . " titles done\n" );
4959
50 - $rowCount++;
51 - }
52 - if( count( $insertBuffer ) > 0 ) {
5360 $this->insertRows( $insertBuffer );
 61+ $insertBuffer = array();
 62+ wfWaitForSlaves(); // 'Kill' lag
 63+ $this->output( $rowCount . " titles done\n" );
5464 }
 65+ $this->output( "Done\n" );
5566 }
5667 }
5768

Sign-offs

UserFlagDate
YuviPandainspected21:23, 3 December 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r104219Slight code cleanup, plus potential fix for non-mysql databasesyuvipanda04:44, 25 November 2011

Comments

#Comment by Nikerabbit (talk | contribs)   05:16, 21 November 2011

Space indentation in the $insertBuffer line.

#Comment by Catrope (talk | contribs)   19:07, 23 November 2011
+				$this->output( $insertBuffer );

Why are you passing an array to $this->output() ?!?

+				array_push( $insertBuffer, $rowData );

IMO $insertBuffer[] = $rowData; is clearer.

Other things:

  • the code is much clearer if you put $insertBuffer = array(); at the start of the while loop, rather than at the end
  • your query is missing an ORDER BY page_id. This is not a problem in practice on MySQL, but may well be one on other DBMSes
#Comment by YuviPanda (talk | contribs)   04:44, 25 November 2011

Ugh, debug call left in. Fixed all in r104219.

Status & tagging log