PredisStrategy.php 14 KB

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