PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/foody.blogtamsudev.vn/vendor/predis/predis/src/Cluster/ClusterStrategy.php

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