4 * DB accessable external objects
5 * @ingroup ExternalStorage
7 class ExternalStoreDB
{
9 function __construct( $params = array() ) {
10 $this->mParams
= $params;
14 * Get a LoadBalancer for the specified cluster
16 * @param $cluster String: cluster name
17 * @return LoadBalancer object
19 function &getLoadBalancer( $cluster ) {
20 $wiki = isset($this->mParams
['wiki']) ?
$this->mParams
['wiki'] : false;
22 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
26 * Get a slave database connection for the specified cluster
28 * @param $cluster String: cluster name
29 * @return DatabaseBase object
31 function &getSlave( $cluster ) {
32 global $wgDefaultExternalStore;
34 $wiki = isset($this->mParams
['wiki']) ?
$this->mParams
['wiki'] : false;
35 $lb =& $this->getLoadBalancer( $cluster );
37 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
38 wfDebug( "read only external store" );
39 $lb->allowLagged(true);
41 wfDebug( "writable external store" );
44 return $lb->getConnection( DB_SLAVE
, array(), $wiki );
48 * Get a master database connection for the specified cluster
50 * @param $cluster String: cluster name
51 * @return DatabaseBase object
53 function &getMaster( $cluster ) {
54 $wiki = isset($this->mParams
['wiki']) ?
$this->mParams
['wiki'] : false;
55 $lb =& $this->getLoadBalancer( $cluster );
56 return $lb->getConnection( DB_MASTER
, array(), $wiki );
60 * Get the 'blobs' table name for this database
62 * @param $db DatabaseBase
63 * @return String: table name ('blobs' by default)
65 function getTable( &$db ) {
66 $table = $db->getLBInfo( 'blobs table' );
67 if ( is_null( $table ) ) {
74 * Fetch data from given URL
75 * @param $url String: an url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
78 function fetchFromURL( $url ) {
79 $path = explode( '/', $url );
82 if ( isset( $path[4] ) ) {
88 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
90 if ( $itemID !== false && $ret !== false ) {
91 return $ret->getItem( $itemID );
97 * Fetch a blob item out of the database; a cache of the last-loaded
98 * blob will be kept so that multiple loads out of a multi-item blob
99 * can avoid redundant database access and decompression.
106 function &fetchBlob( $cluster, $id, $itemID ) {
108 * One-step cache variable to hold base blobs; operations that
109 * pull multiple revisions may often pull multiple times from
110 * the same blob. By keeping the last-used one open, we avoid
111 * redundant unserialization and decompression overhead.
113 static $externalBlobCache = array();
115 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
116 if( isset( $externalBlobCache[$cacheID] ) ) {
117 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
118 return $externalBlobCache[$cacheID];
121 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
123 $dbr =& $this->getSlave( $cluster );
124 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ), __METHOD__
);
125 if ( $ret === false ) {
126 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
128 $dbw =& $this->getMaster( $cluster );
129 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ), __METHOD__
);
130 if( $ret === false) {
131 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
134 if( $itemID !== false && $ret !== false ) {
135 // Unserialise object; caller extracts item
136 $ret = unserialize( $ret );
139 $externalBlobCache = array( $cacheID => &$ret );
144 * Insert a data item into a given cluster
146 * @param $cluster String: the cluster name
147 * @param $data String: the data item
150 function store( $cluster, $data ) {
151 $dbw = $this->getMaster( $cluster );
152 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
153 $dbw->insert( $this->getTable( $dbw ),
154 array( 'blob_id' => $id, 'blob_text' => $data ),
156 $id = $dbw->insertId();
158 throw new MWException( __METHOD__
.': no insert ID' );
160 if ( $dbw->getFlag( DBO_TRX
) ) {
161 $dbw->commit( __METHOD__
);
163 return "DB://$cluster/$id";