r47568 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47567‎ | r47568 | r47569 >
Date:18:17, 20 February 2009
Author:aaron
Status:ok
Tags:
Comment:
* Updated FRCacheUpdate to work with r47317 and break inheritance
* Don't use queue if there are less than a batch size worth of items
* Improvements to insertJobs() ranges
Modified paths:
  • /trunk/extensions/FlaggedRevs/FRCacheUpdate.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FRCacheUpdate.php
@@ -1,72 +1,129 @@
22 <?php
33
4 -class FRCacheUpdate extends HTMLCacheUpdate
5 -{
 4+class FRCacheUpdate {
 5+ public $mTitle, $mTable;
 6+ public $mRowsPerJob, $mRowsPerQuery;
 7+
 8+ public function __construct( $titleTo ) {
 9+ global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
 10+ $this->mTitle = $titleTo;
 11+ $this->mTable = 'flaggedrevs_tracking';
 12+ $this->mRowsPerJob = $wgUpdateRowsPerJob;
 13+ $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
 14+ }
 15+
616 public function doUpdate() {
717 global $wgFlaggedRevsCacheUpdates;
818 if( !isset($wgFlaggedRevsCacheUpdates) ) {
9 - $wgFlaggedRevsCacheUpdates = array();
 19+ $wgFlaggedRevsCacheUpdates = array(); // temp var
1020 }
11 - # No duplicates...
1221 $key = $this->mTitle->getPrefixedDBKey();
13 - if( isset($wgFlaggedRevsCacheUpdates[$key]) ) {
14 - return;
15 - }
 22+ if( isset($wgFlaggedRevsCacheUpdates[$key]) )
 23+ return; // No duplicates...
1624 # Fetch the IDs
17 - $cond = $this->getToCondition();
1825 $dbr = wfGetDB( DB_SLAVE );
19 - $res = $dbr->select( $this->mTable, $this->getFromField(), $cond, __METHOD__ );
20 - if( $dbr->numRows( $res ) != 0 ) {
21 - $this->insertJobs( $res );
 26+ $res = $dbr->select( $this->mTable, $this->getFromField(), $this->getToCondition(), __METHOD__ );
 27+ if( $dbr->numRows($res) > 0 ) {
 28+ # Do it right now?
 29+ if( $dbr->numRows($res) <= $this->mRowsPerJob ) {
 30+ $this->invalidateIDs( $res );
 31+ # Defer to job queue...
 32+ } else {
 33+ $this->insertJobs( $res );
 34+ }
2235 }
23 - $wgFlaggedRevsCacheUpdates[$key] = 1;
 36+ $wgFlaggedRevsCacheUpdates[$key] = 1; // No duplicates...
2437 }
2538
26 - function insertJobs( ResultWrapper $res ) {
 39+ protected function insertJobs( ResultWrapper $res ) {
2740 $numRows = $res->numRows();
 41+ if( !$numRows ) return; // sanity check
2842 $numBatches = ceil( $numRows / $this->mRowsPerJob );
29 - $realBatchSize = $numRows / $numBatches;
 43+ $realBatchSize = ceil( $numRows / $numBatches );
3044 $start = false;
3145 $jobs = array();
3246 do {
33 - for ( $i = 0; $i <= $realBatchSize - 1; $i++ ) {
 47+ $first = $last = false; // first/last page_id of this batch
 48+ # Get $realBatchSize items (or less if not enough)...
 49+ for( $i = 0; $i < $realBatchSize; $i++ ) {
3450 $row = $res->fetchRow();
 51+ # Is there another row?
3552 if( $row ) {
3653 $id = $row[0];
 54+ $last = $id; // $id is the last page_id of this batch
 55+ if( $first === false )
 56+ $first = $id; // set first page_id of this batch
 57+ # Out of rows?
3758 } else {
3859 $id = false;
3960 break;
4061 }
 62+ }
 63+ # Insert batch into the queue if there is anything there
 64+ if( $first ) {
 65+ $params = array(
 66+ 'table' => $this->mTable,
 67+ 'start' => $first,
 68+ 'end' => $last,
 69+ );
 70+ $jobs[] = new FRCacheUpdateJob( $this->mTitle, $params );
4171 }
42 - $params = array(
43 - 'table' => $this->mTable,
44 - 'start' => $start,
45 - 'end' => ( $id !== false ? $id - 1 : false ),
46 - );
47 - $jobs[] = new FRCacheUpdateJob( $this->mTitle, $params );
48 - $start = $id;
49 - } while ( $start );
 72+ $start = $id; // Where the last ID left off
 73+ } while( $start );
5074 Job::batchInsert( $jobs );
51 - }
 75+ }
5276
53 - function getPrefix() {
54 - return 'ftr';
 77+ public function getFromField() {
 78+ return 'ftr_from';
5579 }
5680
57 - function getFromField() {
58 - return $this->getPrefix() . '_from';
 81+ public function getToCondition() {
 82+ return array( 'ftr_namespace' => $this->mTitle->getNamespace(), 'ftr_title' => $this->mTitle->getDBkey() );
5983 }
 84+
 85+ /**
 86+ * Invalidate a set of IDs, right now
 87+ */
 88+ public function invalidateIDs( ResultWrapper $res ) {
 89+ global $wgUseFileCache, $wgUseSquid;
 90+ if( $res->numRows() == 0 ) return; // sanity check
6091
61 - function getToCondition() {
62 - if( $this->mTable !== 'flaggedrevs_tracking' ) {
63 - throw new MWException( 'Invalid table type in ' . __CLASS__ );
64 - }
65 - $prefix = $this->getPrefix();
66 - return array(
67 - "{$prefix}_namespace" => $this->mTitle->getNamespace(),
68 - "{$prefix}_title" => $this->mTitle->getDBkey()
69 - );
70 - }
 92+ $dbw = wfGetDB( DB_MASTER );
 93+ $timestamp = $dbw->timestamp();
 94+ $done = false;
 95+
 96+ while( !$done ) {
 97+ # Get all IDs in this query into an array
 98+ $ids = array();
 99+ for( $i = 0; $i < $this->mRowsPerQuery; $i++ ) {
 100+ $row = $res->fetchRow();
 101+ if( $row ) {
 102+ $ids[] = $row[0];
 103+ } else {
 104+ $done = true;
 105+ break;
 106+ }
 107+ }
 108+ if( count($ids) == 0 ) break;
 109+ # Update page_touched
 110+ $dbw->update( 'page', array( 'page_touched' => $timestamp ), array( 'page_id' => $ids ), __METHOD__ );
 111+ # Update static caches
 112+ if( $wgUseSquid || $wgUseFileCache ) {
 113+ $titles = Title::newFromIDs( $ids );
 114+ # Update squid cache
 115+ if( $wgUseSquid ) {
 116+ $u = SquidUpdate::newFromTitles( $titles );
 117+ $u->doUpdate();
 118+ }
 119+ # Update file cache
 120+ if( $wgUseFileCache ) {
 121+ foreach( $titles as $title ) {
 122+ HTMLFileCache::clearFileCache( $title );
 123+ }
 124+ }
 125+ }
 126+ }
 127+ }
71128 }
72129
73130 /**
@@ -90,8 +147,8 @@
91148 }
92149
93150 function run() {
94 - $update = new FRCacheUpdate( $this->title, $this->table );
95 -
 151+ $update = new FRCacheUpdate( $this->title );
 152+ # Get query conditions
96153 $fromField = $update->getFromField();
97154 $conds = $update->getToCondition();
98155 if( $this->start ) {
@@ -101,10 +158,11 @@
102159 $conds[] = "$fromField <= {$this->end}";
103160 }
104161
 162+ # Run query to get page Ids
105163 $dbr = wfGetDB( DB_SLAVE );
106164 $res = $dbr->select( $this->table, $fromField, $conds, __METHOD__ );
 165+ # Invalidate the pages
107166 $update->invalidateIDs( $res );
108 -
109167 return true;
110168 }
111169 }
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
@@ -304,7 +304,7 @@
305305 * that was just changed in some way.
306306 */
307307 public static function doCacheUpdate( $title ) {
308 - $update = new FRCacheUpdate( $title, 'flaggedrevs_tracking' );
 308+ $update = new FRCacheUpdate( $title );
309309 $update->doUpdate();
310310 return true;
311311 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r47317* Mostly reverted r41081 and related. Although the motivation (to save a quer...tstarling14:26, 16 February 2009

Status & tagging log