PoolingShardManager.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Sharding;
  20. use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
  21. /**
  22. * Shard Manager for the Connection Pooling Shard Strategy
  23. *
  24. * @author Benjamin Eberlei <kontakt@beberlei.de>
  25. */
  26. class PoolingShardManager implements ShardManager
  27. {
  28. private $conn;
  29. private $choser;
  30. private $currentDistributionValue;
  31. public function __construct(PoolingShardConnection $conn)
  32. {
  33. $params = $conn->getParams();
  34. $this->conn = $conn;
  35. $this->choser = $params['shardChoser'];
  36. }
  37. public function selectGlobal()
  38. {
  39. $this->conn->connect(0);
  40. $this->currentDistributionValue = null;
  41. }
  42. public function selectShard($distributionValue)
  43. {
  44. $shardId = $this->choser->pickShard($distributionValue, $this->conn);
  45. $this->conn->connect($shardId);
  46. $this->currentDistributionValue = $distributionValue;
  47. }
  48. public function getCurrentDistributionValue()
  49. {
  50. return $this->currentDistributionValue;
  51. }
  52. public function getShards()
  53. {
  54. $params = $this->conn->getParams();
  55. $shards = array();
  56. foreach ($params['shards'] as $shard) {
  57. $shards[] = array('id' => $shard['id']);
  58. }
  59. return $shards;
  60. }
  61. public function queryAll($sql, array $params, array $types)
  62. {
  63. $shards = $this->getShards();
  64. if (!$shards) {
  65. throw new \RuntimeException("No shards found.");
  66. }
  67. $result = array();
  68. $oldDistribution = $this->getCurrentDistributionValue();
  69. foreach ($shards as $shard) {
  70. $this->selectShard($shard['id']);
  71. foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
  72. $result[] = $row;
  73. }
  74. }
  75. if ($oldDistribution === null) {
  76. $this->selectGlobal();
  77. } else {
  78. $this->selectShard($oldDistribution);
  79. }
  80. return $result;
  81. }
  82. }