6 * DB accessable external objects
10 /** @package MediaWiki */
13 * External database storage will use one (or more) separate connection pools
14 * from what the main wiki uses. If we load many revisions, such as when doing
15 * bulk backups or maintenance, we want to keep them around over the lifetime
18 * Associative array of LoadBalancer objects, indexed by cluster name.
20 global $wgExternalLoadBalancers;
21 $wgExternalLoadBalancers = array();
24 * One-step cache variable to hold base blobs; operations that
25 * pull multiple revisions may often pull multiple times from
26 * the same blob. By keeping the last-used one open, we avoid
27 * redundant unserialization and decompression overhead.
29 global $wgExternalBlobCache;
30 $wgExternalBlobCache = array();
32 class ExternalStoreDB
{
35 function &getLoadBalancer( $cluster ) {
36 global $wgExternalServers, $wgExternalLoadBalancers;
37 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
38 $wgExternalLoadBalancers[$cluster] = LoadBalancer
::newFromParams( $wgExternalServers[$cluster] );
40 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
41 return $wgExternalLoadBalancers[$cluster];
45 function &getSlave( $cluster ) {
46 $lb =& $this->getLoadBalancer( $cluster );
47 return $lb->getConnection( DB_SLAVE
);
51 function &getMaster( $cluster ) {
52 $lb =& $this->getLoadBalancer( $cluster );
53 return $lb->getConnection( DB_MASTER
);
57 function getTable( &$db ) {
58 $table = $db->getLBInfo( 'blobs table' );
59 if ( is_null( $table ) ) {
66 * Fetch data from given URL
67 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
69 function fetchFromURL($url) {
70 $path = explode( '/', $url );
73 if ( isset( $path[4] ) ) {
79 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
81 if ( $itemID !== false && $ret !== false ) {
82 return $ret->getItem( $itemID );
88 * Fetch a blob item out of the database; a cache of the last-loaded
89 * blob will be kept so that multiple loads out of a multi-item blob
90 * can avoid redundant database access and decompression.
97 function &fetchBlob( $cluster, $id, $itemID ) {
98 global $wgExternalBlobCache;
99 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
100 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
101 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
102 return $wgExternalBlobCache[$cacheID];
105 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
107 $dbr =& $this->getSlave( $cluster );
108 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
109 if ( $ret === false ) {
110 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
112 $dbw =& $this->getMaster( $cluster );
113 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
114 if( $ret === false) {
115 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
118 if( $itemID !== false && $ret !== false ) {
119 // Unserialise object; caller extracts item
120 $ret = unserialize( $ret );
123 $wgExternalBlobCache = array( $cacheID => &$ret );
128 * Insert a data item into a given cluster
130 * @param $cluster String: the cluster name
131 * @param $data String: the data item
134 function store( $cluster, $data ) {
135 $fname = 'ExternalStoreDB::store';
137 $dbw =& $this->getMaster( $cluster );
139 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
140 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
141 $id = $dbw->insertId();
142 if ( $dbw->getFlag( DBO_TRX
) ) {
143 $dbw->immediateCommit();
145 return "DB://$cluster/$id";