CommandHashStrategy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /* commands operating on lists */
  71. 'LINSERT' => $keyIsFirstArgument,
  72. 'LINDEX' => $keyIsFirstArgument,
  73. 'LLEN' => $keyIsFirstArgument,
  74. 'LPOP' => $keyIsFirstArgument,
  75. 'RPOP' => $keyIsFirstArgument,
  76. 'RPOPLPUSH' => array($this, 'getKeyFromAllArguments'),
  77. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  78. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  79. 'BRPOPLPUSH' => array($this, 'getKeyFromBlockingListCommands'),
  80. 'LPUSH' => $keyIsFirstArgument,
  81. 'LPUSHX' => $keyIsFirstArgument,
  82. 'RPUSH' => $keyIsFirstArgument,
  83. 'RPUSHX' => $keyIsFirstArgument,
  84. 'LRANGE' => $keyIsFirstArgument,
  85. 'LREM' => $keyIsFirstArgument,
  86. 'LSET' => $keyIsFirstArgument,
  87. 'LTRIM' => $keyIsFirstArgument,
  88. /* commands operating on sets */
  89. 'SADD' => $keyIsFirstArgument,
  90. 'SCARD' => $keyIsFirstArgument,
  91. 'SDIFF' => array($this, 'getKeyFromAllArguments'),
  92. 'SDIFFSTORE' => array($this, 'getKeyFromAllArguments'),
  93. 'SINTER' => array($this, 'getKeyFromAllArguments'),
  94. 'SINTERSTORE' => array($this, 'getKeyFromAllArguments'),
  95. 'SUNION' => array($this, 'getKeyFromAllArguments'),
  96. 'SUNIONSTORE' => array($this, 'getKeyFromAllArguments'),
  97. 'SISMEMBER' => $keyIsFirstArgument,
  98. 'SMEMBERS' => $keyIsFirstArgument,
  99. 'SPOP' => $keyIsFirstArgument,
  100. 'SRANDMEMBER' => $keyIsFirstArgument,
  101. 'SREM' => $keyIsFirstArgument,
  102. /* commands operating on sorted sets */
  103. 'ZADD' => $keyIsFirstArgument,
  104. 'ZCARD' => $keyIsFirstArgument,
  105. 'ZCOUNT' => $keyIsFirstArgument,
  106. 'ZINCRBY' => $keyIsFirstArgument,
  107. 'ZINTERSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  108. 'ZRANGE' => $keyIsFirstArgument,
  109. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  110. 'ZRANK' => $keyIsFirstArgument,
  111. 'ZREM' => $keyIsFirstArgument,
  112. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  113. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  114. 'ZREVRANGE' => $keyIsFirstArgument,
  115. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  116. 'ZREVRANK' => $keyIsFirstArgument,
  117. 'ZSCORE' => $keyIsFirstArgument,
  118. 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  119. /* commands operating on hashes */
  120. 'HDEL' => $keyIsFirstArgument,
  121. 'HEXISTS' => $keyIsFirstArgument,
  122. 'HGET' => $keyIsFirstArgument,
  123. 'HGETALL' => $keyIsFirstArgument,
  124. 'HMGET' => $keyIsFirstArgument,
  125. 'HINCRBY' => $keyIsFirstArgument,
  126. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  127. 'HKEYS' => $keyIsFirstArgument,
  128. 'HLEN' => $keyIsFirstArgument,
  129. 'HSET' => $keyIsFirstArgument,
  130. 'HSETNX' => $keyIsFirstArgument,
  131. 'HVALS' => $keyIsFirstArgument,
  132. );
  133. }
  134. /**
  135. * Returns the list of IDs for the supported commands.
  136. *
  137. * @return array
  138. */
  139. public function getSupportedCommands()
  140. {
  141. return array_keys($this->commands);
  142. }
  143. /**
  144. * Extracts the key from the first argument of a command instance.
  145. *
  146. * @param CommandInterface $command Command instance.
  147. * @return string
  148. */
  149. protected function getKeyFromFirstArgument(CommandInterface $command)
  150. {
  151. return $command->getArgument(0);
  152. }
  153. /**
  154. * Extracts the key from a command with multiple keys only when all keys
  155. * in the arguments array produce the same hash.
  156. *
  157. * @param CommandInterface $command Command instance.
  158. * @return string
  159. */
  160. protected function getKeyFromAllArguments(CommandInterface $command)
  161. {
  162. $arguments = $command->getArguments();
  163. if ($this->checkSameHashForKeys($arguments)) {
  164. return $arguments[0];
  165. }
  166. }
  167. /**
  168. * Extracts the key from a command with multiple keys only when all keys
  169. * in the arguments array produce the same hash.
  170. *
  171. * @param CommandInterface $command Command instance.
  172. * @return string
  173. */
  174. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  175. {
  176. $arguments = $command->getArguments();
  177. $keys = array();
  178. for ($i = 0; $i < count($arguments); $i += 2) {
  179. $keys[] = $arguments[$i];
  180. }
  181. if ($this->checkSameHashForKeys($keys)) {
  182. return $arguments[0];
  183. }
  184. }
  185. /**
  186. * Extracts the key from BLPOP and BRPOP commands.
  187. *
  188. * @param CommandInterface $command Command instance.
  189. * @return string
  190. */
  191. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  192. {
  193. $arguments = $command->getArguments();
  194. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  195. return $arguments[0];
  196. }
  197. }
  198. /**
  199. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  200. *
  201. * @param CommandInterface $command Command instance.
  202. * @return string
  203. */
  204. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  205. {
  206. $arguments = $command->getArguments();
  207. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  208. if ($this->checkSameHashForKeys($keys)) {
  209. return $arguments[0];
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function getHash(HashGeneratorInterface $hasher, CommandInterface $command)
  216. {
  217. if (isset($this->commands[$cmdID = $command->getId()])) {
  218. if ($key = call_user_func($this->commands[$cmdID], $command)) {
  219. return $this->getKeyHash($hasher, $key);
  220. }
  221. }
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getKeyHash(HashGeneratorInterface $hasher, $key)
  227. {
  228. $key = $this->extractKeyTag($key);
  229. $hash = $hasher->hash($key);
  230. return $hash;
  231. }
  232. /**
  233. * Checks if the specified array of keys will generate the same hash.
  234. *
  235. * @param array $keys Array of keys.
  236. * @return Boolean
  237. */
  238. protected function checkSameHashForKeys(Array $keys)
  239. {
  240. if (($count = count($keys)) === 0) {
  241. return false;
  242. }
  243. $currentKey = $this->extractKeyTag($keys[0]);
  244. for ($i = 1; $i < $count; $i++) {
  245. $nextKey = $this->extractKeyTag($keys[$i]);
  246. if ($currentKey !== $nextKey) {
  247. return false;
  248. }
  249. $currentKey = $nextKey;
  250. }
  251. return true;
  252. }
  253. /**
  254. * Returns only the hashable part of a key (delimited by "{...}"), or the
  255. * whole key if a key tag is not found in the string.
  256. *
  257. * @param string $key A key.
  258. * @return string
  259. */
  260. protected function extractKeyTag($key)
  261. {
  262. $start = strpos($key, '{');
  263. if ($start !== false) {
  264. $end = strpos($key, '}', $start);
  265. if ($end !== false) {
  266. $key = substr($key, ++$start, $end - $start);
  267. }
  268. }
  269. return $key;
  270. }
  271. }