PredisStrategy.php 15 KB

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