PredisStrategy.php 14 KB

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