ExternalStoreDB.php 3.8 KB

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