/vendor/predis/predis/lib/Predis/Cluster/RedisClusterHashStrategy.php

https://bitbucket.org/helfreire/tccsite · PHP · 291 lines · 174 code · 32 blank · 85 comment · 11 complexity · aa3e4dd4c15a1074d35923ba64c8c1c5 MD5 · raw file

  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\CRC16HashGenerator;
  12. use Predis\Command\CommandInterface;
  13. use Predis\Command\ScriptedCommand;
  14. /**
  15. * Default class used by Predis to calculate hashes out of keys of
  16. * commands supported by redis-cluster.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class RedisClusterHashStrategy implements CommandHashStrategyInterface
  21. {
  22. private $commands;
  23. private $hashGenerator;
  24. /**
  25. *
  26. */
  27. public function __construct()
  28. {
  29. $this->commands = $this->getDefaultCommands();
  30. $this->hashGenerator = new CRC16HashGenerator();
  31. }
  32. /**
  33. * Returns the default map of supported commands with their handlers.
  34. *
  35. * @return array
  36. */
  37. protected function getDefaultCommands()
  38. {
  39. $keyIsFirstArgument = array($this, 'getKeyFromFirstArgument');
  40. return array(
  41. /* commands operating on the key space */
  42. 'EXISTS' => $keyIsFirstArgument,
  43. 'DEL' => array($this, 'getKeyFromAllArguments'),
  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' => array($this, 'getKeyFromAllArguments'),
  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. 'BITCOUNT' => $keyIsFirstArgument,
  74. /* commands operating on lists */
  75. 'LINSERT' => $keyIsFirstArgument,
  76. 'LINDEX' => $keyIsFirstArgument,
  77. 'LLEN' => $keyIsFirstArgument,
  78. 'LPOP' => $keyIsFirstArgument,
  79. 'RPOP' => $keyIsFirstArgument,
  80. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  81. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  82. 'LPUSH' => $keyIsFirstArgument,
  83. 'LPUSHX' => $keyIsFirstArgument,
  84. 'RPUSH' => $keyIsFirstArgument,
  85. 'RPUSHX' => $keyIsFirstArgument,
  86. 'LRANGE' => $keyIsFirstArgument,
  87. 'LREM' => $keyIsFirstArgument,
  88. 'LSET' => $keyIsFirstArgument,
  89. 'LTRIM' => $keyIsFirstArgument,
  90. /* commands operating on sets */
  91. 'SADD' => $keyIsFirstArgument,
  92. 'SCARD' => $keyIsFirstArgument,
  93. 'SISMEMBER' => $keyIsFirstArgument,
  94. 'SMEMBERS' => $keyIsFirstArgument,
  95. 'SSCAN' => $keyIsFirstArgument,
  96. 'SPOP' => $keyIsFirstArgument,
  97. 'SRANDMEMBER' => $keyIsFirstArgument,
  98. 'SREM' => $keyIsFirstArgument,
  99. /* commands operating on sorted sets */
  100. 'ZADD' => $keyIsFirstArgument,
  101. 'ZCARD' => $keyIsFirstArgument,
  102. 'ZCOUNT' => $keyIsFirstArgument,
  103. 'ZINCRBY' => $keyIsFirstArgument,
  104. 'ZRANGE' => $keyIsFirstArgument,
  105. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  106. 'ZRANK' => $keyIsFirstArgument,
  107. 'ZREM' => $keyIsFirstArgument,
  108. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  109. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  110. 'ZREVRANGE' => $keyIsFirstArgument,
  111. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  112. 'ZREVRANK' => $keyIsFirstArgument,
  113. 'ZSCORE' => $keyIsFirstArgument,
  114. 'ZSCAN' => $keyIsFirstArgument,
  115. /* commands operating on hashes */
  116. 'HDEL' => $keyIsFirstArgument,
  117. 'HEXISTS' => $keyIsFirstArgument,
  118. 'HGET' => $keyIsFirstArgument,
  119. 'HGETALL' => $keyIsFirstArgument,
  120. 'HMGET' => $keyIsFirstArgument,
  121. 'HMSET' => $keyIsFirstArgument,
  122. 'HINCRBY' => $keyIsFirstArgument,
  123. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  124. 'HKEYS' => $keyIsFirstArgument,
  125. 'HLEN' => $keyIsFirstArgument,
  126. 'HSET' => $keyIsFirstArgument,
  127. 'HSETNX' => $keyIsFirstArgument,
  128. 'HVALS' => $keyIsFirstArgument,
  129. 'HSCAN' => $keyIsFirstArgument,
  130. /* scripting */
  131. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  132. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  133. );
  134. }
  135. /**
  136. * Returns the list of IDs for the supported commands.
  137. *
  138. * @return array
  139. */
  140. public function getSupportedCommands()
  141. {
  142. return array_keys($this->commands);
  143. }
  144. /**
  145. * Sets an handler for the specified command ID.
  146. *
  147. * The signature of the callback must have a single parameter
  148. * of type Predis\Command\CommandInterface.
  149. *
  150. * When the callback argument is omitted or NULL, the previously
  151. * associated handler for the specified command ID is removed.
  152. *
  153. * @param string $commandId The ID of the command to be handled.
  154. * @param mixed $callback A valid callable object or NULL.
  155. */
  156. public function setCommandHandler($commandId, $callback = null)
  157. {
  158. $commandId = strtoupper($commandId);
  159. if (!isset($callback)) {
  160. unset($this->commands[$commandId]);
  161. return;
  162. }
  163. if (!is_callable($callback)) {
  164. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  165. }
  166. $this->commands[$commandId] = $callback;
  167. }
  168. /**
  169. * Extracts the key from the first argument of a command instance.
  170. *
  171. * @param CommandInterface $command Command instance.
  172. * @return string
  173. */
  174. protected function getKeyFromFirstArgument(CommandInterface $command)
  175. {
  176. return $command->getArgument(0);
  177. }
  178. /**
  179. * Extracts the key from a command that can accept multiple keys ensuring
  180. * that only one key is actually specified to comply with redis-cluster.
  181. *
  182. * @param CommandInterface $command Command instance.
  183. * @return string
  184. */
  185. protected function getKeyFromAllArguments(CommandInterface $command)
  186. {
  187. $arguments = $command->getArguments();
  188. if (count($arguments) === 1) {
  189. return $arguments[0];
  190. }
  191. }
  192. /**
  193. * Extracts the key from a command that can accept multiple keys ensuring
  194. * that only one key is actually specified to comply with redis-cluster.
  195. *
  196. * @param CommandInterface $command Command instance.
  197. * @return string
  198. */
  199. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  200. {
  201. $arguments = $command->getArguments();
  202. if (count($arguments) === 2) {
  203. return $arguments[0];
  204. }
  205. }
  206. /**
  207. * Extracts the key from BLPOP and BRPOP commands ensuring that only one key
  208. * is actually specified to comply with redis-cluster.
  209. *
  210. * @param CommandInterface $command Command instance.
  211. * @return string
  212. */
  213. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  214. {
  215. $arguments = $command->getArguments();
  216. if (count($arguments) === 2) {
  217. return $arguments[0];
  218. }
  219. }
  220. /**
  221. * Extracts the key from EVAL and EVALSHA commands.
  222. *
  223. * @param CommandInterface $command Command instance.
  224. * @return string
  225. */
  226. protected function getKeyFromScriptingCommands(CommandInterface $command)
  227. {
  228. if ($command instanceof ScriptedCommand) {
  229. $keys = $command->getKeys();
  230. } else {
  231. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  232. }
  233. if (count($keys) === 1) {
  234. return $keys[0];
  235. }
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getHash(CommandInterface $command)
  241. {
  242. $hash = $command->getHash();
  243. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  244. $key = call_user_func($this->commands[$cmdID], $command);
  245. if (isset($key)) {
  246. $hash = $this->hashGenerator->hash($key);
  247. $command->setHash($hash);
  248. }
  249. }
  250. return $hash;
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. public function getKeyHash($key)
  256. {
  257. return $this->hashGenerator->hash($key);
  258. }
  259. }