* Introduced LBFactory -- an abstract class for configuring database load balancers...
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2 /**
3 *
4 *
5 * DB accessable external objects
6 *
7 */
8
9
10
11 /**
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
15 * of the script.
16 *
17 * Associative array of LoadBalancer objects, indexed by cluster name.
18 */
19 global $wgExternalLoadBalancers;
20 $wgExternalLoadBalancers = array();
21
22 /**
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.
27 */
28 global $wgExternalBlobCache;
29 $wgExternalBlobCache = array();
30
31 class ExternalStoreDB {
32
33 /** @todo Document.*/
34 function &getLoadBalancer( $cluster ) {
35 return wfGetLBFactory()->getExternalLB( $cluster );
36 }
37
38 /** @todo Document.*/
39 function &getSlave( $cluster ) {
40 $lb =& $this->getLoadBalancer( $cluster );
41 return $lb->getConnection( DB_SLAVE );
42 }
43
44 /** @todo Document.*/
45 function &getMaster( $cluster ) {
46 $lb =& $this->getLoadBalancer( $cluster );
47 return $lb->getConnection( DB_MASTER );
48 }
49
50 /** @todo Document.*/
51 function getTable( &$db ) {
52 $table = $db->getLBInfo( 'blobs table' );
53 if ( is_null( $table ) ) {
54 $table = 'blobs';
55 }
56 return $table;
57 }
58
59 /**
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.
62 */
63 function fetchFromURL($url) {
64 $path = explode( '/', $url );
65 $cluster = $path[2];
66 $id = $path[3];
67 if ( isset( $path[4] ) ) {
68 $itemID = $path[4];
69 } else {
70 $itemID = false;
71 }
72
73 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
74
75 if ( $itemID !== false && $ret !== false ) {
76 return $ret->getItem( $itemID );
77 }
78 return $ret;
79 }
80
81 /**
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.
85 * @param $cluster
86 * @param $id
87 * @param $itemID
88 * @return mixed
89 * @private
90 */
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];
97 }
98
99 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
100
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" );
105 // Try the master
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" );
110 }
111 }
112 if( $itemID !== false && $ret !== false ) {
113 // Unserialise object; caller extracts item
114 $ret = unserialize( $ret );
115 }
116
117 $wgExternalBlobCache = array( $cacheID => &$ret );
118 return $ret;
119 }
120
121 /**
122 * Insert a data item into a given cluster
123 *
124 * @param $cluster String: the cluster name
125 * @param $data String: the data item
126 * @return string URL
127 */
128 function store( $cluster, $data ) {
129 $fname = 'ExternalStoreDB::store';
130
131 $dbw =& $this->getMaster( $cluster );
132
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();
138 }
139 return "DB://$cluster/$id";
140 }
141 }
142