PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/predis/predis/lib/Predis/Cluster/PredisClusterHashStrategy.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 398 lines | 240 code | 48 blank | 110 comment | 20 complexity | 820998e59854b734ddca19c8c4d8d376 MD5 | raw file
  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\ScriptedCommand;
  14. /**
  15. * Default class used by Predis for client-side sharding to calculate
  16. * hashes out of keys of supported commands.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class PredisClusterHashStrategy implements CommandHashStrategyInterface
  21. {
  22. protected $commands;
  23. protected $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. $getKeyFromFirstArgument = array($this, 'getKeyFromFirstArgument');
  40. $getKeyFromAllArguments = array($this, 'getKeyFromAllArguments');
  41. return array(
  42. /* commands operating on the key space */
  43. 'EXISTS' => $getKeyFromFirstArgument,
  44. 'DEL' => $getKeyFromAllArguments,
  45. 'TYPE' => $getKeyFromFirstArgument,
  46. 'EXPIRE' => $getKeyFromFirstArgument,
  47. 'EXPIREAT' => $getKeyFromFirstArgument,
  48. 'PERSIST' => $getKeyFromFirstArgument,
  49. 'PEXPIRE' => $getKeyFromFirstArgument,
  50. 'PEXPIREAT' => $getKeyFromFirstArgument,
  51. 'TTL' => $getKeyFromFirstArgument,
  52. 'PTTL' => $getKeyFromFirstArgument,
  53. 'SORT' => $getKeyFromFirstArgument, // TODO
  54. 'DUMP' => $getKeyFromFirstArgument,
  55. 'RESTORE' => $getKeyFromFirstArgument,
  56. /* commands operating on string values */
  57. 'APPEND' => $getKeyFromFirstArgument,
  58. 'DECR' => $getKeyFromFirstArgument,
  59. 'DECRBY' => $getKeyFromFirstArgument,
  60. 'GET' => $getKeyFromFirstArgument,
  61. 'GETBIT' => $getKeyFromFirstArgument,
  62. 'MGET' => $getKeyFromAllArguments,
  63. 'SET' => $getKeyFromFirstArgument,
  64. 'GETRANGE' => $getKeyFromFirstArgument,
  65. 'GETSET' => $getKeyFromFirstArgument,
  66. 'INCR' => $getKeyFromFirstArgument,
  67. 'INCRBY' => $getKeyFromFirstArgument,
  68. 'INCRBYFLOAT' => $getKeyFromFirstArgument,
  69. 'SETBIT' => $getKeyFromFirstArgument,
  70. 'SETEX' => $getKeyFromFirstArgument,
  71. 'MSET' => array($this, 'getKeyFromInterleavedArguments'),
  72. 'MSETNX' => array($this, 'getKeyFromInterleavedArguments'),
  73. 'SETNX' => $getKeyFromFirstArgument,
  74. 'SETRANGE' => $getKeyFromFirstArgument,
  75. 'STRLEN' => $getKeyFromFirstArgument,
  76. 'SUBSTR' => $getKeyFromFirstArgument,
  77. 'BITOP' => array($this, 'getKeyFromBitOp'),
  78. 'BITCOUNT' => $getKeyFromFirstArgument,
  79. /* commands operating on lists */
  80. 'LINSERT' => $getKeyFromFirstArgument,
  81. 'LINDEX' => $getKeyFromFirstArgument,
  82. 'LLEN' => $getKeyFromFirstArgument,
  83. 'LPOP' => $getKeyFromFirstArgument,
  84. 'RPOP' => $getKeyFromFirstArgument,
  85. 'RPOPLPUSH' => $getKeyFromAllArguments,
  86. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  87. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  88. 'BRPOPLPUSH' => array($this, 'getKeyFromBlockingListCommands'),
  89. 'LPUSH' => $getKeyFromFirstArgument,
  90. 'LPUSHX' => $getKeyFromFirstArgument,
  91. 'RPUSH' => $getKeyFromFirstArgument,
  92. 'RPUSHX' => $getKeyFromFirstArgument,
  93. 'LRANGE' => $getKeyFromFirstArgument,
  94. 'LREM' => $getKeyFromFirstArgument,
  95. 'LSET' => $getKeyFromFirstArgument,
  96. 'LTRIM' => $getKeyFromFirstArgument,
  97. /* commands operating on sets */
  98. 'SADD' => $getKeyFromFirstArgument,
  99. 'SCARD' => $getKeyFromFirstArgument,
  100. 'SDIFF' => $getKeyFromAllArguments,
  101. 'SDIFFSTORE' => $getKeyFromAllArguments,
  102. 'SINTER' => $getKeyFromAllArguments,
  103. 'SINTERSTORE' => $getKeyFromAllArguments,
  104. 'SUNION' => $getKeyFromAllArguments,
  105. 'SUNIONSTORE' => $getKeyFromAllArguments,
  106. 'SISMEMBER' => $getKeyFromFirstArgument,
  107. 'SMEMBERS' => $getKeyFromFirstArgument,
  108. 'SSCAN' => $getKeyFromFirstArgument,
  109. 'SPOP' => $getKeyFromFirstArgument,
  110. 'SRANDMEMBER' => $getKeyFromFirstArgument,
  111. 'SREM' => $getKeyFromFirstArgument,
  112. /* commands operating on sorted sets */
  113. 'ZADD' => $getKeyFromFirstArgument,
  114. 'ZCARD' => $getKeyFromFirstArgument,
  115. 'ZCOUNT' => $getKeyFromFirstArgument,
  116. 'ZINCRBY' => $getKeyFromFirstArgument,
  117. 'ZINTERSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  118. 'ZRANGE' => $getKeyFromFirstArgument,
  119. 'ZRANGEBYSCORE' => $getKeyFromFirstArgument,
  120. 'ZRANK' => $getKeyFromFirstArgument,
  121. 'ZREM' => $getKeyFromFirstArgument,
  122. 'ZREMRANGEBYRANK' => $getKeyFromFirstArgument,
  123. 'ZREMRANGEBYSCORE' => $getKeyFromFirstArgument,
  124. 'ZREVRANGE' => $getKeyFromFirstArgument,
  125. 'ZREVRANGEBYSCORE' => $getKeyFromFirstArgument,
  126. 'ZREVRANK' => $getKeyFromFirstArgument,
  127. 'ZSCORE' => $getKeyFromFirstArgument,
  128. 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  129. 'ZSCAN' => $getKeyFromFirstArgument,
  130. 'ZLEXCOUNT' => $getKeyFromFirstArgument,
  131. 'ZRANGEBYLEX' => $getKeyFromFirstArgument,
  132. 'ZREMRANGEBYLEX' => $getKeyFromFirstArgument,
  133. /* commands operating on hashes */
  134. 'HDEL' => $getKeyFromFirstArgument,
  135. 'HEXISTS' => $getKeyFromFirstArgument,
  136. 'HGET' => $getKeyFromFirstArgument,
  137. 'HGETALL' => $getKeyFromFirstArgument,
  138. 'HMGET' => $getKeyFromFirstArgument,
  139. 'HMSET' => $getKeyFromFirstArgument,
  140. 'HINCRBY' => $getKeyFromFirstArgument,
  141. 'HINCRBYFLOAT' => $getKeyFromFirstArgument,
  142. 'HKEYS' => $getKeyFromFirstArgument,
  143. 'HLEN' => $getKeyFromFirstArgument,
  144. 'HSET' => $getKeyFromFirstArgument,
  145. 'HSETNX' => $getKeyFromFirstArgument,
  146. 'HVALS' => $getKeyFromFirstArgument,
  147. 'HSCAN' => $getKeyFromFirstArgument,
  148. /* commands operating on HyperLogLog */
  149. 'PFADD' => $getKeyFromFirstArgument,
  150. 'PFCOUNT' => $getKeyFromAllArguments,
  151. 'PFMERGE' => $getKeyFromAllArguments,
  152. /* scripting */
  153. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  154. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  155. );
  156. }
  157. /**
  158. * Returns the list of IDs for the supported commands.
  159. *
  160. * @return array
  161. */
  162. public function getSupportedCommands()
  163. {
  164. return array_keys($this->commands);
  165. }
  166. /**
  167. * Sets an handler for the specified command ID.
  168. *
  169. * The signature of the callback must have a single parameter
  170. * of type Predis\Command\CommandInterface.
  171. *
  172. * When the callback argument is omitted or NULL, the previously
  173. * associated handler for the specified command ID is removed.
  174. *
  175. * @param string $commandId The ID of the command to be handled.
  176. * @param mixed $callback A valid callable object or NULL.
  177. */
  178. public function setCommandHandler($commandId, $callback = null)
  179. {
  180. $commandId = strtoupper($commandId);
  181. if (!isset($callback)) {
  182. unset($this->commands[$commandId]);
  183. return;
  184. }
  185. if (!is_callable($callback)) {
  186. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  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
  202. * in 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
  216. * in 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 ScriptedCommand) {
  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)) {
  345. $key = substr($key, ++$start, $end - $start);
  346. }
  347. }
  348. return $key;
  349. }
  350. }