Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2 /**
3 *
4 *
5 * DB accessable external objects
6 *
7 */
8
9
10
11 /**
12 * External database storage will use one (or more) separate connection pools
13 * from what the main wiki uses. If we load many revisions, such as when doing
14 * bulk backups or maintenance, we want to keep them around over the lifetime
15 * of the script.
16 *
17 * Associative array of LoadBalancer objects, indexed by cluster name.
18 */
19 global $wgExternalLoadBalancers;
20 $wgExternalLoadBalancers = array();
21
22 /**
23 * One-step cache variable to hold base blobs; operations that
24 * pull multiple revisions may often pull multiple times from
25 * the same blob. By keeping the last-used one open, we avoid
26 * redundant unserialization and decompression overhead.
27 */
28 global $wgExternalBlobCache;
29 $wgExternalBlobCache = array();
30
31 class ExternalStoreDB {
32
33 /** @todo Document.*/
34 function &getLoadBalancer( $cluster ) {
35 global $wgExternalServers, $wgExternalLoadBalancers;
36 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
37 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
38 }
39 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
40 return $wgExternalLoadBalancers[$cluster];
41 }
42
43 /** @todo Document.*/
44 function &getSlave( $cluster ) {
45 $lb =& $this->getLoadBalancer( $cluster );
46 return $lb->getConnection( DB_SLAVE );
47 }
48
49 /** @todo Document.*/
50 function &getMaster( $cluster ) {
51 $lb =& $this->getLoadBalancer( $cluster );
52 return $lb->getConnection( DB_MASTER );
53 }
54
55 /** @todo Document.*/
56 function getTable( &$db ) {
57 $table = $db->getLBInfo( 'blobs table' );
58 if ( is_null( $table ) ) {
59 $table = 'blobs';
60 }
61 return $table;
62 }
63
64 /**
65 * Fetch data from given URL
66 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
67 */
68 function fetchFromURL($url) {
69 $path = explode( '/', $url );
70 $cluster = $path[2];
71 $id = $path[3];
72 if ( isset( $path[4] ) ) {
73 $itemID = $path[4];
74 } else {
75 $itemID = false;
76 }
77
78 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
79
80 if ( $itemID !== false && $ret !== false ) {
81 return $ret->getItem( $itemID );
82 }
83 return $ret;
84 }
85
86 /**
87 * Fetch a blob item out of the database; a cache of the last-loaded
88 * blob will be kept so that multiple loads out of a multi-item blob
89 * can avoid redundant database access and decompression.
90 * @param $cluster
91 * @param $id
92 * @param $itemID
93 * @return mixed
94 * @private
95 */
96 function &fetchBlob( $cluster, $id, $itemID ) {
97 global $wgExternalBlobCache;
98 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
99 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
100 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
101 return $wgExternalBlobCache[$cacheID];
102 }
103
104 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
105
106 $dbr =& $this->getSlave( $cluster );
107 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
108 if ( $ret === false ) {
109 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
110 // Try the master
111 $dbw =& $this->getMaster( $cluster );
112 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
113 if( $ret === false) {
114 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
115 }
116 }
117 if( $itemID !== false && $ret !== false ) {
118 // Unserialise object; caller extracts item
119 $ret = unserialize( $ret );
120 }
121
122 $wgExternalBlobCache = array( $cacheID => &$ret );
123 return $ret;
124 }
125
126 /**
127 * Insert a data item into a given cluster
128 *
129 * @param $cluster String: the cluster name
130 * @param $data String: the data item
131 * @return string URL
132 */
133 function store( $cluster, $data ) {
134 $fname = 'ExternalStoreDB::store';
135
136 $dbw =& $this->getMaster( $cluster );
137
138 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
139 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
140 $id = $dbw->insertId();
141 if ( $dbw->getFlag( DBO_TRX ) ) {
142 $dbw->immediateCommit();
143 }
144 return "DB://$cluster/$id";
145 }
146 }
147 ?>