PredisStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* scripting */
  147. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  148. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  149. );
  150. }
  151. /**
  152. * Returns the list of IDs for the supported commands.
  153. *
  154. * @return array
  155. */
  156. public function getSupportedCommands()
  157. {
  158. return array_keys($this->commands);
  159. }
  160. /**
  161. * Sets an handler for the specified command ID.
  162. *
  163. * The signature of the callback must have a single parameter of type
  164. * Predis\Command\CommandInterface.
  165. *
  166. * When the callback argument is omitted or NULL, the previously associated
  167. * handler for the specified command ID is removed.
  168. *
  169. * @param string $commandID Command ID.
  170. * @param mixed $callback A valid callable object, or NULL to unset the handler.
  171. */
  172. public function setCommandHandler($commandID, $callback = null)
  173. {
  174. $commandID = strtoupper($commandID);
  175. if (!isset($callback)) {
  176. unset($this->commands[$commandID]);
  177. return;
  178. }
  179. if (!is_callable($callback)) {
  180. throw new InvalidArgumentException(
  181. "The argument must be a callable object or NULL."
  182. );
  183. }
  184. $this->commands[$commandID] = $callback;
  185. }
  186. /**
  187. * Extracts the key from the first argument of a command instance.
  188. *
  189. * @param CommandInterface $command Command instance.
  190. * @return string
  191. */
  192. protected function getKeyFromFirstArgument(CommandInterface $command)
  193. {
  194. return $command->getArgument(0);
  195. }
  196. /**
  197. * Extracts the key from a command with multiple keys only when all keys in
  198. * the arguments array produce the same hash.
  199. *
  200. * @param CommandInterface $command Command instance.
  201. * @return string
  202. */
  203. protected function getKeyFromAllArguments(CommandInterface $command)
  204. {
  205. $arguments = $command->getArguments();
  206. if ($this->checkSameHashForKeys($arguments)) {
  207. return $arguments[0];
  208. }
  209. }
  210. /**
  211. * Extracts the key from a command with multiple keys only when all keys in
  212. * the arguments array produce the same hash.
  213. *
  214. * @param CommandInterface $command Command instance.
  215. * @return string
  216. */
  217. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  218. {
  219. $arguments = $command->getArguments();
  220. $keys = array();
  221. for ($i = 0; $i < count($arguments); $i += 2) {
  222. $keys[] = $arguments[$i];
  223. }
  224. if ($this->checkSameHashForKeys($keys)) {
  225. return $arguments[0];
  226. }
  227. }
  228. /**
  229. * Extracts the key from BLPOP and BRPOP commands.
  230. *
  231. * @param CommandInterface $command Command instance.
  232. * @return string
  233. */
  234. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  235. {
  236. $arguments = $command->getArguments();
  237. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  238. return $arguments[0];
  239. }
  240. }
  241. /**
  242. * Extracts the key from BITOP command.
  243. *
  244. * @param CommandInterface $command Command instance.
  245. * @return string
  246. */
  247. protected function getKeyFromBitOp(CommandInterface $command)
  248. {
  249. $arguments = $command->getArguments();
  250. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  251. return $arguments[1];
  252. }
  253. }
  254. /**
  255. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  256. *
  257. * @param CommandInterface $command Command instance.
  258. * @return string
  259. */
  260. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  261. {
  262. $arguments = $command->getArguments();
  263. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  264. if ($this->checkSameHashForKeys($keys)) {
  265. return $arguments[0];
  266. }
  267. }
  268. /**
  269. * Extracts the key from EVAL and EVALSHA commands.
  270. *
  271. * @param CommandInterface $command Command instance.
  272. * @return string
  273. */
  274. protected function getKeyFromScriptingCommands(CommandInterface $command)
  275. {
  276. if ($command instanceof ScriptCommand) {
  277. $keys = $command->getKeys();
  278. } else {
  279. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  280. }
  281. if ($keys && $this->checkSameHashForKeys($keys)) {
  282. return $keys[0];
  283. }
  284. }
  285. /**
  286. * {@inheritdoc}
  287. */
  288. public function getHash(CommandInterface $command)
  289. {
  290. $hash = $command->getHash();
  291. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  292. $key = call_user_func($this->commands[$cmdID], $command);
  293. if (isset($key)) {
  294. $hash = $this->getKeyHash($key);
  295. $command->setHash($hash);
  296. }
  297. }
  298. return $hash;
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function getKeyHash($key)
  304. {
  305. $key = $this->extractKeyTag($key);
  306. $hash = $this->hashGenerator->hash($key);
  307. return $hash;
  308. }
  309. /**
  310. * Checks if the specified array of keys will generate the same hash.
  311. *
  312. * @param array $keys Array of keys.
  313. * @return bool
  314. */
  315. protected function checkSameHashForKeys(array $keys)
  316. {
  317. if (!$count = count($keys)) {
  318. return false;
  319. }
  320. $currentKey = $this->extractKeyTag($keys[0]);
  321. for ($i = 1; $i < $count; $i++) {
  322. $nextKey = $this->extractKeyTag($keys[$i]);
  323. if ($currentKey !== $nextKey) {
  324. return false;
  325. }
  326. $currentKey = $nextKey;
  327. }
  328. return true;
  329. }
  330. /**
  331. * Returns only the hashable part of a key (delimited by "{...}"), or the
  332. * whole key if a key tag is not found in the string.
  333. *
  334. * @param string $key A key.
  335. * @return string
  336. */
  337. protected function extractKeyTag($key)
  338. {
  339. if (false !== $start = strpos($key, '{')) {
  340. if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) {
  341. $key = substr($key, $start, $end - $start);
  342. }
  343. }
  344. return $key;
  345. }
  346. }