updated
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 *
6 * DB accessable external objects
7 *
8 */
9 require_once( 'LoadBalancer.php' );
10
11
12 /** @package MediaWiki */
13
14 /**
15 * External database storage will use one (or more) separate connection pools
16 * from what the main wiki uses. If we load many revisions, such as when doing
17 * bulk backups or maintenance, we want to keep them around over the lifetime
18 * of the script.
19 *
20 * Associative array of LoadBalancer objects, indexed by cluster name.
21 */
22 global $wgExternalLoadBalancers;
23 $wgExternalLoadBalancers = array();
24
25 /**
26 * One-step cache variable to hold base blobs; operations that
27 * pull multiple revisions may often pull multiple times from
28 * the same blob. By keeping the last-used one open, we avoid
29 * redundant unserialization and decompression overhead.
30 */
31 global $wgExternalBlobCache;
32 $wgExternalBlobCache = array();
33
34 class ExternalStoreDB {
35 /**
36 * Fetch data from given URL
37 * @param string $url An url
38 */
39
40 function &getLoadBalancer( $cluster ) {
41 global $wgExternalServers, $wgExternalLoadBalancers;
42 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
43 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
44 }
45 return $wgExternalLoadBalancers[$cluster];
46 }
47
48 function &getSlave( $cluster ) {
49 $lb =& $this->getLoadBalancer( $cluster );
50 return $lb->getConnection( DB_SLAVE );
51 }
52
53 function &getMaster( $cluster ) {
54 $lb =& $this->getLoadBalancer( $cluster );
55 return $lb->getConnection( DB_MASTER );
56 }
57
58 function fetchFromURL($url) {
59 global $wgExternalServers;
60 #
61 # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage
62 #
63 $path = explode( '/', $url );
64 $cluster = $path[2];
65 $id = $path[3];
66 if ( isset( $path[4] ) ) {
67 $itemID = $path[4];
68 } else {
69 $itemID = false;
70 }
71
72 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
73
74 if ( $itemID !== false ) {
75 return $ret->getItem( $itemID );
76 }
77 return $ret;
78 }
79
80 /**
81 * Fetch a blob item out of the database; a cache of the last-loaded
82 * blob will be kept so that multiple loads out of a multi-item blob
83 * can avoid redundant database access and decompression.
84 * @return mixed
85 * @access private
86 */
87 function &fetchBlob( $cluster, $id, $itemID ) {
88 global $wgExternalBlobCache;
89 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
90 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
91 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
92 return $wgExternalBlobCache[$cacheID];
93 }
94
95 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
96
97 $dbr =& $this->getSlave( $cluster );
98 $ret = $dbr->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) );
99 if( $itemID !== false ) {
100 // Unserialise object; caller extracts item
101 $ret = unserialize( $ret );
102 }
103
104 $wgExternalBlobCache = array( $cacheID => &$ret );
105 return $ret;
106 }
107
108 /**
109 * Insert a data item into a given cluster
110 *
111 * @param string $cluster The cluster name
112 * @param string $data The data item
113 * @return string URL
114 */
115 function store( $cluster, $data ) {
116 global $wgExternalServers;
117 $fname = 'ExternalStoreDB::store';
118
119 $dbw =& $this->getMaster( $cluster );
120
121 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
122 $dbw->insert( 'blobs', array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
123 return "DB://$cluster/" . $dbw->insertId();
124 }
125 }
126 ?>