PredisStrategy.php 14 KB

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