PredisClusterHashStrategy.php 12 KB

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