PredisClusterHashStrategy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Cluster;
  11. use Predis\Cluster\Hash\HashGeneratorInterface;
  12. use Predis\Command\CommandInterface;
  13. /**
  14. * Default class used by Predis for client-side sharding to calculate
  15. * hashes out of keys of supported commands.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class PredisClusterHashStrategy implements CommandHashStrategyInterface
  20. {
  21. private $commands;
  22. private $hashGenerator;
  23. /**
  24. * @param HashGeneratorInterface $hashGenerator Hash generator instance.
  25. */
  26. public function __construct(HashGeneratorInterface $hashGenerator)
  27. {
  28. $this->commands = $this->getDefaultCommands();
  29. $this->hashGenerator = $hashGenerator;
  30. }
  31. /**
  32. * Returns the default map of supported commands with their handlers.
  33. *
  34. * @return array
  35. */
  36. protected function getDefaultCommands()
  37. {
  38. $keyIsFirstArgument = array($this, 'getKeyFromFirstArgument');
  39. $keysAreAllArguments = array($this, 'getKeyFromAllArguments');
  40. return array(
  41. /* commands operating on the key space */
  42. 'EXISTS' => $keyIsFirstArgument,
  43. 'DEL' => $keysAreAllArguments,
  44. 'TYPE' => $keyIsFirstArgument,
  45. 'EXPIRE' => $keyIsFirstArgument,
  46. 'EXPIREAT' => $keyIsFirstArgument,
  47. 'PERSIST' => $keyIsFirstArgument,
  48. 'PEXPIRE' => $keyIsFirstArgument,
  49. 'PEXPIREAT' => $keyIsFirstArgument,
  50. 'TTL' => $keyIsFirstArgument,
  51. 'PTTL' => $keyIsFirstArgument,
  52. 'SORT' => $keyIsFirstArgument, // TODO
  53. /* commands operating on string values */
  54. 'APPEND' => $keyIsFirstArgument,
  55. 'DECR' => $keyIsFirstArgument,
  56. 'DECRBY' => $keyIsFirstArgument,
  57. 'GET' => $keyIsFirstArgument,
  58. 'GETBIT' => $keyIsFirstArgument,
  59. 'MGET' => $keysAreAllArguments,
  60. 'SET' => $keyIsFirstArgument,
  61. 'GETRANGE' => $keyIsFirstArgument,
  62. 'GETSET' => $keyIsFirstArgument,
  63. 'INCR' => $keyIsFirstArgument,
  64. 'INCRBY' => $keyIsFirstArgument,
  65. 'SETBIT' => $keyIsFirstArgument,
  66. 'SETEX' => $keyIsFirstArgument,
  67. 'MSET' => array($this, 'getKeyFromInterleavedArguments'),
  68. 'MSETNX' => array($this, 'getKeyFromInterleavedArguments'),
  69. 'SETNX' => $keyIsFirstArgument,
  70. 'SETRANGE' => $keyIsFirstArgument,
  71. 'STRLEN' => $keyIsFirstArgument,
  72. 'SUBSTR' => $keyIsFirstArgument,
  73. 'BITOP' => array($this, 'getKeyFromBitOp'),
  74. 'BITCOUNT' => $keyIsFirstArgument,
  75. /* commands operating on lists */
  76. 'LINSERT' => $keyIsFirstArgument,
  77. 'LINDEX' => $keyIsFirstArgument,
  78. 'LLEN' => $keyIsFirstArgument,
  79. 'LPOP' => $keyIsFirstArgument,
  80. 'RPOP' => $keyIsFirstArgument,
  81. 'RPOPLPUSH' => $keysAreAllArguments,
  82. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  83. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  84. 'BRPOPLPUSH' => array($this, 'getKeyFromBlockingListCommands'),
  85. 'LPUSH' => $keyIsFirstArgument,
  86. 'LPUSHX' => $keyIsFirstArgument,
  87. 'RPUSH' => $keyIsFirstArgument,
  88. 'RPUSHX' => $keyIsFirstArgument,
  89. 'LRANGE' => $keyIsFirstArgument,
  90. 'LREM' => $keyIsFirstArgument,
  91. 'LSET' => $keyIsFirstArgument,
  92. 'LTRIM' => $keyIsFirstArgument,
  93. /* commands operating on sets */
  94. 'SADD' => $keyIsFirstArgument,
  95. 'SCARD' => $keyIsFirstArgument,
  96. 'SDIFF' => $keysAreAllArguments,
  97. 'SDIFFSTORE' => $keysAreAllArguments,
  98. 'SINTER' => $keysAreAllArguments,
  99. 'SINTERSTORE' => $keysAreAllArguments,
  100. 'SUNION' => $keysAreAllArguments,
  101. 'SUNIONSTORE' => $keysAreAllArguments,
  102. 'SISMEMBER' => $keyIsFirstArgument,
  103. 'SMEMBERS' => $keyIsFirstArgument,
  104. 'SPOP' => $keyIsFirstArgument,
  105. 'SRANDMEMBER' => $keyIsFirstArgument,
  106. 'SREM' => $keyIsFirstArgument,
  107. /* commands operating on sorted sets */
  108. 'ZADD' => $keyIsFirstArgument,
  109. 'ZCARD' => $keyIsFirstArgument,
  110. 'ZCOUNT' => $keyIsFirstArgument,
  111. 'ZINCRBY' => $keyIsFirstArgument,
  112. 'ZINTERSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  113. 'ZRANGE' => $keyIsFirstArgument,
  114. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  115. 'ZRANK' => $keyIsFirstArgument,
  116. 'ZREM' => $keyIsFirstArgument,
  117. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  118. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  119. 'ZREVRANGE' => $keyIsFirstArgument,
  120. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  121. 'ZREVRANK' => $keyIsFirstArgument,
  122. 'ZSCORE' => $keyIsFirstArgument,
  123. 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  124. /* commands operating on hashes */
  125. 'HDEL' => $keyIsFirstArgument,
  126. 'HEXISTS' => $keyIsFirstArgument,
  127. 'HGET' => $keyIsFirstArgument,
  128. 'HGETALL' => $keyIsFirstArgument,
  129. 'HMGET' => $keyIsFirstArgument,
  130. 'HINCRBY' => $keyIsFirstArgument,
  131. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  132. 'HKEYS' => $keyIsFirstArgument,
  133. 'HLEN' => $keyIsFirstArgument,
  134. 'HSET' => $keyIsFirstArgument,
  135. 'HSETNX' => $keyIsFirstArgument,
  136. 'HVALS' => $keyIsFirstArgument,
  137. );
  138. }
  139. /**
  140. * Returns the list of IDs for the supported commands.
  141. *
  142. * @return array
  143. */
  144. public function getSupportedCommands()
  145. {
  146. return array_keys($this->commands);
  147. }
  148. /**
  149. * Sets an handler for the specified command ID.
  150. *
  151. * The signature of the callback must have a single parameter
  152. * of type Predis\Command\CommandInterface.
  153. *
  154. * When the callback argument is omitted or NULL, the previously
  155. * associated handler for the specified command ID is removed.
  156. *
  157. * @param string $commandId The ID of the command to be handled.
  158. * @param mixed $callback A valid callable object or NULL.
  159. */
  160. public function setCommandHandler($commandId, $callback = null)
  161. {
  162. $commandId = strtoupper($commandId);
  163. if (!isset($callback)) {
  164. unset($this->commands[$commandId]);
  165. return;
  166. }
  167. if (!is_callable($callback)) {
  168. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  169. }
  170. $this->commands[$commandId] = $callback;
  171. }
  172. /**
  173. * Extracts the key from the first argument of a command instance.
  174. *
  175. * @param CommandInterface $command Command instance.
  176. * @return string
  177. */
  178. protected function getKeyFromFirstArgument(CommandInterface $command)
  179. {
  180. return $command->getArgument(0);
  181. }
  182. /**
  183. * Extracts the key from a command with multiple keys only when all keys
  184. * in the arguments array produce the same hash.
  185. *
  186. * @param CommandInterface $command Command instance.
  187. * @return string
  188. */
  189. protected function getKeyFromAllArguments(CommandInterface $command)
  190. {
  191. $arguments = $command->getArguments();
  192. if ($this->checkSameHashForKeys($arguments)) {
  193. return $arguments[0];
  194. }
  195. }
  196. /**
  197. * Extracts the key from a command with multiple keys only when all keys
  198. * in the arguments array produce the same hash.
  199. *
  200. * @param CommandInterface $command Command instance.
  201. * @return string
  202. */
  203. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  204. {
  205. $arguments = $command->getArguments();
  206. $keys = array();
  207. for ($i = 0; $i < count($arguments); $i += 2) {
  208. $keys[] = $arguments[$i];
  209. }
  210. if ($this->checkSameHashForKeys($keys)) {
  211. return $arguments[0];
  212. }
  213. }
  214. /**
  215. * Extracts the key from BLPOP and BRPOP commands.
  216. *
  217. * @param CommandInterface $command Command instance.
  218. * @return string
  219. */
  220. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  221. {
  222. $arguments = $command->getArguments();
  223. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  224. return $arguments[0];
  225. }
  226. }
  227. /**
  228. * Extracts the key from BITOP command.
  229. *
  230. * @param CommandInterface $command Command instance.
  231. * @return string
  232. */
  233. protected function getKeyFromBitOp(CommandInterface $command)
  234. {
  235. $arguments = $command->getArguments();
  236. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  237. return $arguments[1];
  238. }
  239. }
  240. /**
  241. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  242. *
  243. * @param CommandInterface $command Command instance.
  244. * @return string
  245. */
  246. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  247. {
  248. $arguments = $command->getArguments();
  249. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  250. if ($this->checkSameHashForKeys($keys)) {
  251. return $arguments[0];
  252. }
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function getHash(CommandInterface $command)
  258. {
  259. $hash = $command->getHash();
  260. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  261. if ($key = call_user_func($this->commands[$cmdID], $command)) {
  262. $hash = $this->getKeyHash($key);
  263. $command->setHash($hash);
  264. }
  265. }
  266. return $hash;
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function getKeyHash($key)
  272. {
  273. $key = $this->extractKeyTag($key);
  274. $hash = $this->hashGenerator->hash($key);
  275. return $hash;
  276. }
  277. /**
  278. * Checks if the specified array of keys will generate the same hash.
  279. *
  280. * @param array $keys Array of keys.
  281. * @return Boolean
  282. */
  283. protected function checkSameHashForKeys(Array $keys)
  284. {
  285. if (($count = count($keys)) === 0) {
  286. return false;
  287. }
  288. $currentKey = $this->extractKeyTag($keys[0]);
  289. for ($i = 1; $i < $count; $i++) {
  290. $nextKey = $this->extractKeyTag($keys[$i]);
  291. if ($currentKey !== $nextKey) {
  292. return false;
  293. }
  294. $currentKey = $nextKey;
  295. }
  296. return true;
  297. }
  298. /**
  299. * Returns only the hashable part of a key (delimited by "{...}"), or the
  300. * whole key if a key tag is not found in the string.
  301. *
  302. * @param string $key A key.
  303. * @return string
  304. */
  305. protected function extractKeyTag($key)
  306. {
  307. if (false !== $start = strpos($key, '{')) {
  308. if (false !== $end = strpos($key, '}', $start)) {
  309. $key = substr($key, ++$start, $end - $start);
  310. }
  311. }
  312. return $key;
  313. }
  314. }