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
9 * Associative array of LoadBalancer objects, indexed by cluster name.
11 global $wgExternalLoadBalancers;
12 $wgExternalLoadBalancers = array();
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.
20 global $wgExternalBlobCache;
21 $wgExternalBlobCache = array();
24 * DB accessable external objects
25 * @ingroup ExternalStorage
27 class ExternalStoreDB
{
29 function __construct( $params = array() ) {
30 $this->mParams
= $params;
34 function &getLoadBalancer( $cluster ) {
35 $wiki = isset($this->mParams
['wiki']) ?
$this->mParams
['wiki'] : false;
37 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
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 );
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 );
55 function getTable( &$db ) {
56 $table = $db->getLBInfo( 'blobs table' );
57 if ( is_null( $table ) ) {
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.
67 function fetchFromURL( $url ) {
68 $path = explode( '/', $url );
71 if ( isset( $path[4] ) ) {
77 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
79 if ( $itemID !== false && $ret !== false ) {
80 return $ret->getItem( $itemID );
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.
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];
103 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
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" );
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" );
116 if( $itemID !== false && $ret !== false ) {
117 // Unserialise object; caller extracts item
118 $ret = unserialize( $ret );
121 $wgExternalBlobCache = array( $cacheID => &$ret );
126 * Insert a data item into a given cluster
128 * @param $cluster String: the cluster name
129 * @param $data String: the data item
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 ),
138 $id = $dbw->insertId();
140 throw new MWException( __METHOD__
.': no insert ID' );
142 if ( $dbw->getFlag( DBO_TRX
) ) {
143 $dbw->immediateCommit();
145 return "DB://$cluster/$id";