Index: branches/REL1_5/phase3/includes/ExternalStoreDB.php |
— | — | @@ -10,31 +10,74 @@ |
11 | 11 | |
12 | 12 | |
13 | 13 | /** @package MediaWiki */ |
| 14 | + |
14 | 15 | class ExternalStoreDB { |
| 16 | + var $loadBalancers = array(); |
| 17 | + |
15 | 18 | /** |
16 | 19 | * Fetch data from given URL |
17 | 20 | * @param string $url An url |
18 | 21 | */ |
| 22 | + |
| 23 | + function &getLoadBalancer( $cluster ) { |
| 24 | + global $wgExternalServers; |
| 25 | + if ( !array_key_exists( $cluster, $this->loadBalancers ) ) { |
| 26 | + $this->loadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] ); |
| 27 | + } |
| 28 | + return $this->loadBalancers[$cluster]; |
| 29 | + } |
| 30 | + |
| 31 | + function &getSlave( $cluster ) { |
| 32 | + $lb =& $this->getLoadBalancer( $cluster ); |
| 33 | + return $lb->getConnection( DB_SLAVE ); |
| 34 | + } |
| 35 | + |
| 36 | + function &getMaster( $cluster ) { |
| 37 | + $lb =& $this->getLoadBalancer( $cluster ); |
| 38 | + return $lb->getConnection( DB_MASTER ); |
| 39 | + } |
| 40 | + |
19 | 41 | function fetchFromURL($url) { |
20 | 42 | global $wgExternalServers; |
21 | 43 | # |
22 | | - # URLs have the form DB://cluster/id, e.g. |
23 | | - # DB://cluster1/3298247 |
| 44 | + # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage |
24 | 45 | # |
25 | 46 | $path = explode( '/', $url ); |
26 | 47 | $cluster = $path[2]; |
27 | 48 | $id = $path[3]; |
| 49 | + if ( isset( $path[4] ) ) { |
| 50 | + $itemID = $path[4]; |
| 51 | + } else { |
| 52 | + $itemID = false; |
| 53 | + } |
28 | 54 | |
29 | | - $lb = LoadBalancer::NewFromParams( $wgExternalServers[$cluster] ); |
30 | | - $db = $lb->getConnection( DB_SLAVE ); |
| 55 | + $dbr =& $this->getSlave( $cluster ); |
| 56 | + $ret = $dbr->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) ); |
31 | 57 | |
32 | | - $ret = $db->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) ); |
33 | | - |
| 58 | + if ( $itemID !== false ) { |
| 59 | + # Unserialise object and get item |
| 60 | + $obj = unserialize( $ret ); |
| 61 | + $ret = $obj->getItem( $itemID ); |
| 62 | + } |
34 | 63 | return $ret; |
35 | 64 | } |
36 | 65 | |
37 | | - /* @fixme XXX: may require other methods, for store, delete, |
38 | | - * whatever, for initial ext storage |
| 66 | + /** |
| 67 | + * Insert a data item into a given cluster |
| 68 | + * |
| 69 | + * @param string $cluster The cluster name |
| 70 | + * @param string $data The data item |
| 71 | + * @return string URL |
39 | 72 | */ |
| 73 | + function store( $cluster, $data ) { |
| 74 | + global $wgExternalServers; |
| 75 | + $fname = 'ExternalStoreDB::store'; |
| 76 | + |
| 77 | + $dbw =& $this->getMaster( $cluster ); |
| 78 | + |
| 79 | + $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' ); |
| 80 | + $dbw->insert( 'blobs', array( 'blob_id' => $id, 'blob_text' => $data ), $fname ); |
| 81 | + return "DB://$cluster/" . $dbw->insertId(); |
| 82 | + } |
40 | 83 | } |
41 | 84 | ?> |