[7576] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | * |
---|
| 31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
| 32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 33 | * @link http://www.solarium-project.org/ |
---|
| 34 | * |
---|
| 35 | * @package Solarium |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Weighted random choice class |
---|
| 40 | * |
---|
| 41 | * For use in the loadbalancer plugin |
---|
| 42 | * |
---|
| 43 | * @package Solarium |
---|
| 44 | * @subpackage Plugin |
---|
| 45 | */ |
---|
| 46 | class Solarium_Plugin_Loadbalancer_WeightedRandomChoice |
---|
| 47 | { |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Total weight of all choices |
---|
| 51 | * |
---|
| 52 | * @var int |
---|
| 53 | */ |
---|
| 54 | protected $_totalWeight = 0; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Choices total lookup array |
---|
| 58 | * |
---|
| 59 | * @var array |
---|
| 60 | */ |
---|
| 61 | protected $_lookup = array(); |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Values lookup array |
---|
| 65 | * |
---|
| 66 | * @var array |
---|
| 67 | */ |
---|
| 68 | protected $_values = array(); |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Constructor |
---|
| 72 | * |
---|
| 73 | * @param array $choices |
---|
| 74 | */ |
---|
| 75 | public function __construct($choices) |
---|
| 76 | { |
---|
| 77 | $i = 0; |
---|
| 78 | foreach ($choices AS $key => $weight) { |
---|
| 79 | if ($weight <=0) throw new Solarium_Exception('Weight must be greater than zero'); |
---|
| 80 | |
---|
| 81 | $this->_totalWeight += $weight; |
---|
| 82 | $this->_lookup[$i] = $this->_totalWeight; |
---|
| 83 | $this->_values[$i] = $key; |
---|
| 84 | |
---|
| 85 | $i++; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Get a (weighted) random entry |
---|
| 91 | * |
---|
| 92 | * @param array $excludes Keys to exclude |
---|
| 93 | * @return string |
---|
| 94 | */ |
---|
| 95 | public function getRandom($excludes = array()) |
---|
| 96 | { |
---|
| 97 | if (count($excludes) == count($this->_values)) { |
---|
| 98 | throw new Solarium_Exception('No more server entries available'); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // continue until a non-excluded value is found |
---|
| 102 | // @todo optimize? |
---|
| 103 | $result = null; |
---|
| 104 | while (1) { |
---|
| 105 | $result = $this->_values[$this->_getKey()]; |
---|
| 106 | if(!in_array($result, $excludes)) break; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return $result; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Get a (weighted) random entry key |
---|
| 114 | * |
---|
| 115 | * @return int |
---|
| 116 | */ |
---|
| 117 | protected function _getKey() |
---|
| 118 | { |
---|
| 119 | $random = mt_rand(1, $this->_totalWeight); |
---|
| 120 | $high = count($this->_lookup)-1; |
---|
| 121 | $low = 0; |
---|
| 122 | |
---|
| 123 | while ( $low < $high ) { |
---|
| 124 | $probe = (int)(($high + $low) / 2); |
---|
| 125 | if ($this->_lookup[$probe] < $random) { |
---|
| 126 | $low = $probe + 1; |
---|
| 127 | } else if ($this->_lookup[$probe] > $random) { |
---|
| 128 | $high = $probe - 1; |
---|
| 129 | } else { |
---|
| 130 | return $probe; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if ($this->_lookup[$low] >= $random) { |
---|
| 135 | return $low; |
---|
| 136 | } else { |
---|
| 137 | return $low+1; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | } |
---|