CommandHashStrategy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. * Extracts the key from the first argument of a command instance.
  147. *
  148. * @param CommandInterface $command Command instance.
  149. * @return string
  150. */
  151. protected function getKeyFromFirstArgument(CommandInterface $command)
  152. {
  153. return $command->getArgument(0);
  154. }
  155. /**
  156. * Extracts the key from a command with multiple keys only when all keys
  157. * in the arguments array produce the same hash.
  158. *
  159. * @param CommandInterface $command Command instance.
  160. * @return string
  161. */
  162. protected function getKeyFromAllArguments(CommandInterface $command)
  163. {
  164. $arguments = $command->getArguments();
  165. if ($this->checkSameHashForKeys($arguments)) {
  166. return $arguments[0];
  167. }
  168. }
  169. /**
  170. * Extracts the key from a command with multiple keys only when all keys
  171. * in the arguments array produce the same hash.
  172. *
  173. * @param CommandInterface $command Command instance.
  174. * @return string
  175. */
  176. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  177. {
  178. $arguments = $command->getArguments();
  179. $keys = array();
  180. for ($i = 0; $i < count($arguments); $i += 2) {
  181. $keys[] = $arguments[$i];
  182. }
  183. if ($this->checkSameHashForKeys($keys)) {
  184. return $arguments[0];
  185. }
  186. }
  187. /**
  188. * Extracts the key from BLPOP and BRPOP commands.
  189. *
  190. * @param CommandInterface $command Command instance.
  191. * @return string
  192. */
  193. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  194. {
  195. $arguments = $command->getArguments();
  196. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  197. return $arguments[0];
  198. }
  199. }
  200. /**
  201. * Extracts the key from BITOP command.
  202. *
  203. * @param CommandInterface $command Command instance.
  204. * @return string
  205. */
  206. protected function getKeyFromBitOp(CommandInterface $command)
  207. {
  208. $arguments = $command->getArguments();
  209. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  210. return $arguments[1];
  211. }
  212. }
  213. /**
  214. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  215. *
  216. * @param CommandInterface $command Command instance.
  217. * @return string
  218. */
  219. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  220. {
  221. $arguments = $command->getArguments();
  222. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  223. if ($this->checkSameHashForKeys($keys)) {
  224. return $arguments[0];
  225. }
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function getHash(HashGeneratorInterface $hasher, CommandInterface $command)
  231. {
  232. if (isset($this->commands[$cmdID = $command->getId()])) {
  233. if ($key = call_user_func($this->commands[$cmdID], $command)) {
  234. return $this->getKeyHash($hasher, $key);
  235. }
  236. }
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getKeyHash(HashGeneratorInterface $hasher, $key)
  242. {
  243. $key = $this->extractKeyTag($key);
  244. $hash = $hasher->hash($key);
  245. return $hash;
  246. }
  247. /**
  248. * Checks if the specified array of keys will generate the same hash.
  249. *
  250. * @param array $keys Array of keys.
  251. * @return Boolean
  252. */
  253. protected function checkSameHashForKeys(Array $keys)
  254. {
  255. if (($count = count($keys)) === 0) {
  256. return false;
  257. }
  258. $currentKey = $this->extractKeyTag($keys[0]);
  259. for ($i = 1; $i < $count; $i++) {
  260. $nextKey = $this->extractKeyTag($keys[$i]);
  261. if ($currentKey !== $nextKey) {
  262. return false;
  263. }
  264. $currentKey = $nextKey;
  265. }
  266. return true;
  267. }
  268. /**
  269. * Returns only the hashable part of a key (delimited by "{...}"), or the
  270. * whole key if a key tag is not found in the string.
  271. *
  272. * @param string $key A key.
  273. * @return string
  274. */
  275. protected function extractKeyTag($key)
  276. {
  277. $start = strpos($key, '{');
  278. if ($start !== false) {
  279. $end = strpos($key, '}', $start);
  280. if ($end !== false) {
  281. $key = substr($key, ++$start, $end - $start);
  282. }
  283. }
  284. return $key;
  285. }
  286. }