r84254 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84253‎ | r84254 | r84255 >
Date:18:12, 18 March 2011
Author:hashar
Status:resolved (Comments)
Tags:
Comment:
documentation for BacklinkCache class

- still need examples
- could use peer review
- no functional changes
Modified paths:
  • /trunk/phase3/includes/BacklinkCache.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/BacklinkCache.php
@@ -1,21 +1,77 @@
22 <?php
33 /**
4 - * Class for fetching backlink lists, approximate backlink counts and partitions.
5 - * Instances of this class should typically be fetched with $title->getBacklinkCache().
 4+ * File for BacklinkCache class
 5+ * @file
 6+ */
 7+
 8+/**
 9+ * Class for fetching backlink lists, approximate backlink counts and
 10+ * partitions. This is a shared cache.
611 *
7 - * Ideally you should only get your backlinks from here when you think there is some
8 - * advantage in caching them. Otherwise it's just a waste of memory.
 12+ * Instances of this class should typically be fetched with the method
 13+ * $title->getBacklinkCache().
 14+ *
 15+ * Ideally you should only get your backlinks from here when you think
 16+ * there is some advantage in caching them. Otherwise it's just a waste
 17+ * of memory.
 18+ *
 19+ * Introduced by r47317
 20+ *
 21+ * @internal documentation reviewed on 18 Mar 2011 by hashar
 22+ *
 23+ * @author Tim Starling
 24+ * @copyright © 2009, Tim Starling, Domas Mituzas
 25+ * @copyright © 2010, Max Sem
 26+ * @copyright © 2011, Ashar Voultoiz
927 */
1028 class BacklinkCache {
 29+
 30+ /**
 31+ * Multi dimensions array representing batches. Keys are:
 32+ * > (string) links table name
 33+ * > 'numRows' : Number of rows for this link table
 34+ * > 'batches' : array( $start, $end )
 35+ *
 36+ * @see BacklinkCache::partitionResult()
 37+ * @todo Should be private
 38+ *
 39+ * Cleared with BacklinkCache::clear()
 40+ */
1141 var $partitionCache = array();
 42+
 43+ /**
 44+ * Contains the whole links from a database result.
 45+ * This is raw data that will be partitioned in $partitionCache
 46+ *
 47+ * @todo Should be private
 48+ *
 49+ * Initialized with BacklinkCache::getLinks()
 50+ * Cleared with BacklinkCache::clear()
 51+ */
1252 var $fullResultCache = array();
13 - var $title;
 53+
 54+ /**
 55+ * Local copy of a database object.
 56+ *
 57+ * Accessor: BacklinkCache::getDB()
 58+ * Mutator : BacklinkCache::setDB()
 59+ * Cleared with BacklinkCache::clear()
 60+ *
 61+ * @todo Should be private
 62+ */
1463 var $db;
1564
 65+ /**
 66+ * Local copy of a Title object
 67+ * @todo Should be private
 68+ */
 69+ var $title;
 70+
1671 const CACHE_EXPIRY = 3600;
1772
1873 /**
1974 * Create a new BacklinkCache
 75+ * @param Title $title : Title object to create a backlink cache for.
2076 */
2177 function __construct( $title ) {
2278 $this->title = $title;
@@ -23,14 +79,15 @@
2480
2581 /**
2682 * Serialization handler, diasallows to serialize the database to prevent
27 - * failures after this class is deserialized from cache with dead DB connection.
 83+ * failures after this class is deserialized from cache with dead DB
 84+ * connection.
2885 */
2986 function __sleep() {
3087 return array( 'partitionCache', 'fullResultCache', 'title' );
3188 }
3289
3390 /**
34 - * Clear locally stored data
 91+ * Clear locally stored data and database object.
3592 */
3693 function clear() {
3794 $this->partitionCache = array();
@@ -45,6 +102,11 @@
46103 $this->db = $db;
47104 }
48105
 106+ /**
 107+ * Get the slave connection to the database
 108+ * When non existing, will initialize the connection.
 109+ * @return Database object
 110+ */
49111 protected function getDB() {
50112 if ( !isset( $this->db ) ) {
51113 $this->db = wfGetDB( DB_SLAVE );
@@ -95,6 +157,7 @@
96158 return $ta;
97159 }
98160
 161+ // FIXME : make this a function?
99162 if ( !isset( $this->fullResultCache[$table] ) ) {
100163 wfDebug( __METHOD__ . ": from DB\n" );
101164 $res = $this->getDB()->select(
@@ -117,14 +180,15 @@
118181
119182 /**
120183 * Get the field name prefix for a given table
 184+ * @param $table String
121185 */
122186 protected function getPrefix( $table ) {
123187 static $prefixes = array(
124 - 'pagelinks' => 'pl',
125 - 'imagelinks' => 'il',
 188+ 'pagelinks' => 'pl',
 189+ 'imagelinks' => 'il',
126190 'categorylinks' => 'cl',
127191 'templatelinks' => 'tl',
128 - 'redirect' => 'rd',
 192+ 'redirect' => 'rd',
129193 );
130194
131195 if ( isset( $prefixes[$table] ) ) {
@@ -135,18 +199,22 @@
136200 }
137201
138202 /**
139 - * Get the SQL condition array for selecting backlinks, with a join on the page table
 203+ * Get the SQL condition array for selecting backlinks, with a join
 204+ * on the page table.
 205+ * @param $table String
140206 */
141207 protected function getConditions( $table ) {
142208 $prefix = $this->getPrefix( $table );
143209
 210+ // FIXME imagelinks and categorylinks do not rely on getNamespace,
 211+ // they could be moved up for nicer case statements
144212 switch ( $table ) {
145213 case 'pagelinks':
146214 case 'templatelinks':
147215 case 'redirect':
148216 $conds = array(
149217 "{$prefix}_namespace" => $this->title->getNamespace(),
150 - "{$prefix}_title" => $this->title->getDBkey(),
 218+ "{$prefix}_title" => $this->title->getDBkey(),
151219 "page_id={$prefix}_from"
152220 );
153221 break;
@@ -171,6 +239,8 @@
172240
173241 /**
174242 * Get the approximate number of backlinks
 243+ * @param $table String
 244+ * @return integer
175245 */
176246 public function getNumLinks( $table ) {
177247 if ( isset( $this->fullResultCache[$table] ) ) {
@@ -189,15 +259,17 @@
190260
191261 /**
192262 * Partition the backlinks into batches.
193 - * Returns an array giving the start and end of each range. The first batch has
194 - * a start of false, and the last batch has an end of false.
 263+ * Returns an array giving the start and end of each range. The firsti
 264+ * batch has a start of false, and the last batch has an end of false.
195265 *
196266 * @param $table String: the links table name
197267 * @param $batchSize Integer
198268 * @return Array
199269 */
200270 public function partition( $table, $batchSize ) {
201 - // Try cache
 271+
 272+ // 1) try this per process cache first
 273+
202274 if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
203275 wfDebug( __METHOD__ . ": got from partition cache\n" );
204276 return $this->partitionCache[$table][$batchSize]['batches'];
@@ -206,7 +278,9 @@
207279 $this->partitionCache[$table][$batchSize] = false;
208280 $cacheEntry =& $this->partitionCache[$table][$batchSize];
209281
210 - // Try full result cache
 282+
 283+ // 2) try full result cache
 284+
211285 if ( isset( $this->fullResultCache[$table] ) ) {
212286 $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
213287 wfDebug( __METHOD__ . ": got from full result cache\n" );
@@ -214,7 +288,9 @@
215289 return $cacheEntry['batches'];
216290 }
217291
218 - // Try memcached
 292+
 293+ // 3) ... fallback to memcached ...
 294+
219295 global $wgMemc;
220296
221297 $memcKey = wfMemcKey(
@@ -233,7 +309,9 @@
234310 return $cacheEntry['batches'];
235311 }
236312
237 - // Fetch from database
 313+
 314+ // 4) ... finally fetch from the slow database :(
 315+
238316 $this->getLinks( $table );
239317 $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
240318 // Save to memcached
@@ -245,6 +323,9 @@
246324
247325 /**
248326 * Partition a DB result with backlinks in it into batches
 327+ * @param $res database result
 328+ * @param $batchSize integer
 329+ * @return array @see
249330 */
250331 protected function partitionResult( $res, $batchSize ) {
251332 $batches = array();

Follow-up revisions

RevisionCommit summaryAuthorDate
r90826Fix comment in BacklinkCache::partition()...hashar11:48, 26 June 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   06:48, 17 June 2011
+ * Returns an array giving the start and end of each range. The firsti

Typo there.

+ // 1) try this per process cache first

I don't get that comment. Aren't both the cache variables "per process"?

Status & tagging log