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