PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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