support for two-part external storage URLs referring to parts of history blobs
authorTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Aug 2005 07:18:34 +0000 (07:18 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Aug 2005 07:18:34 +0000 (07:18 +0000)
includes/ExternalStoreDB.php
includes/HistoryBlob.php

index 00f8c48..155e428 100644 (file)
@@ -10,31 +10,79 @@ require_once( 'LoadBalancer.php' );
 
 
 /** @package MediaWiki */
+
 class ExternalStoreDB {
+       var $loadBalancers = array();
+       
        /**
         * Fetch data from given URL
         * @param string $url An url
         */
+
+       function &getLoadBalancer( $cluster ) {
+               global $wgExternalServers;
+               if ( !array_key_exists( $cluster, $this->loadBalancers ) ) {
+                       $this->loadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
+               }
+               return $this->loadBalancers[$cluster];
+       }
+       
+       function &getSlave( $cluster ) {
+               $lb =& $this->getLoadBalancer( $cluster );
+               return $lb->getConnection( DB_SLAVE );
+       }
+
+       function &getMaster( $cluster ) {
+               $lb =& $this->getLoadBalancer( $cluster );
+               return $lb->getConnection( DB_MASTER );
+       }               
+               
+       function ExternalStoreDB() {
+               $this->mLoadBalancer = LoadBalancer::NewFromParams( $wgExternalServers[$cluster] );
+       }
+
        function fetchFromURL($url) {
                global $wgExternalServers;
                #
-               # URLs have the form DB://cluster/id, e.g.
-               # DB://cluster1/3298247
+               # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage
                #
                $path = explode( '/', $url );
                $cluster  = $path[2];
                $id       = $path[3];
+               if ( isset( $path[4] ) ) {
+                       $itemID = $path[4];
+               } else {
+                       $itemID = false;
+               }
 
-               $lb = LoadBalancer::NewFromParams( $wgExternalServers[$cluster] );
-               $db = $lb->getConnection( DB_SLAVE );
+               $dbr =& $this->getSlave( $cluster );
+               $ret = $dbr->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) ); 
 
-               $ret = $db->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) ); 
+               if ( $itemID !== false ) {
+                       # Unserialise object and get item
+                       $obj = unserialize( $ret );
+                       $ret = $obj->getItem( $itemID );
+               }
                
                return $ret;
        }
 
-       /* @fixme XXX: may require other methods, for store, delete, 
-        * whatever, for initial ext storage  
+       /**
+        * Insert a data item into a given cluster
+        *
+        * @param string $cluster The cluster name
+        * @param string $data The data item
+        * @return string URL
         */
+       function store( $cluster, $data ) {
+               global $wgExternalServers;
+               $fname = 'ExternalStoreDB::store';
+
+               $dbw =& $this->getMaster( $cluster );
+
+               $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
+               $dbw->insert( 'blobs', array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
+               return "DB://$cluster/" . $dbw->insertId();
+       }
 }
 ?>
index f7fa77a..b407a83 100644 (file)
@@ -210,14 +210,14 @@ class HistoryBlobStub {
                        }
                        $flags = explode( ',', $row->old_flags );
                        if( in_array( 'external', $flags ) ) {
-                               $url=$row->old_text;
-                               @list($proto,$path)=explode('://',$url,2);
-                               if ($path=="") {
-                                       wfProfileOut( $fname );
-                                       return false;
-                               }
-                               require_once('ExternalStore.php');
-                               $row->old_text=ExternalStore::fetchFromUrl($url);
+                               $url=$row->old_text;
+                               @list($proto,$path)=explode('://',$url,2);
+                               if ($path=="") {
+                                       wfProfileOut( $fname );
+                                       return false;
+                               }
+                               require_once('ExternalStore.php');
+                               $row->old_text=ExternalStore::fetchFromUrl($url);
 
                        }
                        if( !in_array( 'object', $flags ) ) {