6 * DB accessable external objects
11 /** @package MediaWiki */
14 * External database storage will use one (or more) separate connection pools
15 * from what the main wiki uses. If we load many revisions, such as when doing
16 * bulk backups or maintenance, we want to keep them around over the lifetime
19 * Associative array of LoadBalancer objects, indexed by cluster name.
21 global $wgExternalLoadBalancers;
22 $wgExternalLoadBalancers = array();
25 * One-step cache variable to hold base blobs; operations that
26 * pull multiple revisions may often pull multiple times from
27 * the same blob. By keeping the last-used one open, we avoid
28 * redundant unserialization and decompression overhead.
30 global $wgExternalBlobCache;
31 $wgExternalBlobCache = array();
33 class ExternalStoreDB
{
36 function &getLoadBalancer( $cluster ) {
37 global $wgExternalServers, $wgExternalLoadBalancers;
38 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
39 $wgExternalLoadBalancers[$cluster] = LoadBalancer
::newFromParams( $wgExternalServers[$cluster] );
41 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
42 return $wgExternalLoadBalancers[$cluster];
46 function &getSlave( $cluster ) {
47 $lb =& $this->getLoadBalancer( $cluster );
48 return $lb->getConnection( DB_SLAVE
);
52 function &getMaster( $cluster ) {
53 $lb =& $this->getLoadBalancer( $cluster );
54 return $lb->getConnection( DB_MASTER
);
58 function getTable( &$db ) {
59 $table = $db->getLBInfo( 'blobs table' );
60 if ( is_null( $table ) ) {
67 * Fetch data from given URL
68 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
70 function fetchFromURL($url) {
71 $path = explode( '/', $url );
74 if ( isset( $path[4] ) ) {
80 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
82 if ( $itemID !== false && $ret !== false ) {
83 return $ret->getItem( $itemID );
89 * Fetch a blob item out of the database; a cache of the last-loaded
90 * blob will be kept so that multiple loads out of a multi-item blob
91 * can avoid redundant database access and decompression.
98 function &fetchBlob( $cluster, $id, $itemID ) {
99 global $wgExternalBlobCache;
100 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
101 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
102 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
103 return $wgExternalBlobCache[$cacheID];
106 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
108 $dbr =& $this->getSlave( $cluster );
109 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
110 if ( $ret === false ) {
111 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
113 $dbw =& $this->getMaster( $cluster );
114 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
115 if( $ret === false) {
116 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
119 if( $itemID !== false && $ret !== false ) {
120 // Unserialise object; caller extracts item
121 $ret = unserialize( $ret );
124 $wgExternalBlobCache = array( $cacheID => &$ret );
129 * Insert a data item into a given cluster
131 * @param $cluster String: the cluster name
132 * @param $data String: the data item
135 function store( $cluster, $data ) {
136 $fname = 'ExternalStoreDB::store';
138 $dbw =& $this->getMaster( $cluster );
140 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
141 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
142 $id = $dbw->insertId();
143 if ( $dbw->getFlag( DBO_TRX
) ) {
144 $dbw->immediateCommit();
146 return "DB://$cluster/$id";