CommandHashStrategy.php 12 KB

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