5 * DB accessable external objects
12 * External database storage will use one (or more) separate connection pools
13 * from what the main wiki uses. If we load many revisions, such as when doing
14 * bulk backups or maintenance, we want to keep them around over the lifetime
17 * Associative array of LoadBalancer objects, indexed by cluster name.
19 global $wgExternalLoadBalancers;
20 $wgExternalLoadBalancers = array();
23 * One-step cache variable to hold base blobs; operations that
24 * pull multiple revisions may often pull multiple times from
25 * the same blob. By keeping the last-used one open, we avoid
26 * redundant unserialization and decompression overhead.
28 global $wgExternalBlobCache;
29 $wgExternalBlobCache = array();
31 class ExternalStoreDB
{
34 function &getLoadBalancer( $cluster ) {
35 return wfGetLBFactory()->getExternalLB( $cluster );
39 function &getSlave( $cluster ) {
40 $lb =& $this->getLoadBalancer( $cluster );
41 return $lb->getConnection( DB_SLAVE
);
45 function &getMaster( $cluster ) {
46 $lb =& $this->getLoadBalancer( $cluster );
47 return $lb->getConnection( DB_MASTER
);
51 function getTable( &$db ) {
52 $table = $db->getLBInfo( 'blobs table' );
53 if ( is_null( $table ) ) {
60 * Fetch data from given URL
61 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
63 function fetchFromURL($url) {
64 $path = explode( '/', $url );
67 if ( isset( $path[4] ) ) {
73 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
75 if ( $itemID !== false && $ret !== false ) {
76 return $ret->getItem( $itemID );
82 * Fetch a blob item out of the database; a cache of the last-loaded
83 * blob will be kept so that multiple loads out of a multi-item blob
84 * can avoid redundant database access and decompression.
91 function &fetchBlob( $cluster, $id, $itemID ) {
92 global $wgExternalBlobCache;
93 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
94 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
95 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
96 return $wgExternalBlobCache[$cacheID];
99 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
101 $dbr =& $this->getSlave( $cluster );
102 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
103 if ( $ret === false ) {
104 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
106 $dbw =& $this->getMaster( $cluster );
107 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
108 if( $ret === false) {
109 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
112 if( $itemID !== false && $ret !== false ) {
113 // Unserialise object; caller extracts item
114 $ret = unserialize( $ret );
117 $wgExternalBlobCache = array( $cacheID => &$ret );
122 * Insert a data item into a given cluster
124 * @param $cluster String: the cluster name
125 * @param $data String: the data item
128 function store( $cluster, $data ) {
129 $fname = 'ExternalStoreDB::store';
131 $dbw =& $this->getMaster( $cluster );
133 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
134 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
135 $id = $dbw->insertId();
136 if ( $dbw->getFlag( DBO_TRX
) ) {
137 $dbw->immediateCommit();
139 return "DB://$cluster/$id";