b t ddlZddlmZmZmZmZmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZmZddlmZmZmZmZmZmZddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)dd l*m+Z+erdd l,m-Z-e.gd Z/Gd deZ0Gdde0Z1Gdde$Z2Gdde2e Z3Gdde"Z4Gdde4eZ5Gdde0e2ee%e4e'e#e+ Z6Gdde1e3ee5e!eZ7y)N) TYPE_CHECKINGAny AsyncIteratorDictIterableIteratorListMappingNoReturnOptionalUnion)Literal)key_slot)RedisClusterException RedisError)AnyKeyTClusterCommandsProtocol EncodableTKeysTKeyTPatternT) ACLCommandsAsyncACLCommandsAsyncDataAccessCommandsAsyncFunctionCommandsAsyncManagementCommandsAsyncScriptCommandsDataAccessCommandsFunctionCommandsManagementCommandsPubSubCommands ResponseTScriptCommands) list_or_args)RedisModuleCommands) TargetNodesT)'BITCOUNTBITPOSEXISTSGEODISTGEOHASHGEOPOS GEORADIUSGEORADIUSBYMEMBERGETGETBITGETRANGEHEXISTSHGETHGETALLHKEYSHLENHMGETHSTRLENHVALSKEYSLINDEXLLENLRANGEMGETPTTL RANDOMKEYSCARDSDIFFSINTER SISMEMBERSMEMBERS SRANDMEMBERSTRLENSUNIONTTLZCARDZCOUNTZRANGEZSCOREc XeZdZdZdeedeeeeffdZ de e e fdeeee ffdZ dede eee fdeefd Zdeede eee fd eedeefd Zded edeeefd Zde e e fdeefdZdededefdZdedefdZdedefdZdedefdZdedefdZy)ClusterMultiKeyCommandsC A class containing commands that handle more than one key keysreturnci}|D]G}t|jj|}|j|gj |I|S)z@Split keys into a dictionary that maps a slot to a list of keys.)rencoderencode setdefaultappend)selfrR slots_to_keyskeyslots 8/usr/lib/python3/dist-packages/redis/commands/cluster.py_partition_keys_by_slotz/ClusterMultiKeyCommands._partition_keys_by_slotesT  ;CDLL//45D  $ $T2 . 5 5c : ;mappingci}|jD]J}t|jj|d}|j |gj |L|S)zBSplit pairs into a dictionary that maps a slot to a list of pairs.r)itemsrrUrVrWextend)rYr`slots_to_pairspairr\s r]_partition_pairs_by_slotz0ClusterMultiKeyCommands._partition_pairs_by_slotos^ MMO =DDLL//Q89D  % %dB / 6 6t < =r_command slots_to_argsc |jxr|tv}|j}|jDcgc]6\}}|j|g|d|j j ||gi8c}}|jScc}}w)N target_nodes)read_from_replicas READ_COMMANDSpipelinerbexecute_command nodes_managerget_node_from_slotexecute)rYrgrhrkpiper\ slot_argss r]_execute_pipeline_by_slotz1ClusterMultiKeyCommands._execute_pipeline_by_slot{s"44QM9Q}}$1#6#6#8  i !D   &&99$@RS  ||~ s;B  responsesc t|j|Dcic]\}}t||D]\}}|| }}}}}|D cgc]} ||  c} Scc}}}}wcc} wN)zipvalues) rYrRrhru slot_valuesresponsekvresultsr[s r]_reorder_keys_by_commandz0ClusterMultiKeyCommands._reorder_keys_by_commands*-]-A-A-CY)O  % XK2 1 qD   )-- --  .s !A A!argsct||}|j|}|jd|}|j|||S)A Splits the keys into different slots and then calls MGET for the keys of every slot. This operation will not be atomic if keys belong to more than one slot. Returns a list of values ordered identically to ``keys`` For more information see https://redis.io/commands/mget r?r%r^rtrrYrRrrZress r]mget_nonatomicz&ClusterMultiKeyCommands.mget_nonatomicsMD$'44T: ,,V]C,,T=#FFr_cH|j|}|jd|S) Sets key/values based on a mapping. Mapping is a dictionary of key/value pairs. Both keys and values should be strings or types that can be cast to a string via str(). Splits the keys into different slots and then calls MSET for the keys of every slot. This operation will not be atomic if keys belong to more than one slot. For more information see https://redis.io/commands/mset MSETrfrtrYr`rds r]mset_nonatomicz&ClusterMultiKeyCommands.mset_nonatomics)66w?--fnEEr_cZ|j|}t|j||S)v Runs the given command once for the keys of each slot. Returns the sum of the return values. r^sumrtrYrgrRrZs r]_split_command_across_slotsz3ClusterMultiKeyCommands._split_command_across_slotss.44T: 411'=IJJr_c(|jdg|S)z Returns the number of ``names`` that exist in the whole cluster. The keys are first split up into slots and then an EXISTS command is sent for every slot For more information see https://redis.io/commands/exists r*rrYrRs r]existszClusterMultiKeyCommands.existss0t//@4@@r_c(|jdg|S)a< Deletes the given keys in the cluster. The keys are first split up into slots and then an DEL command is sent for every slot Non-existant keys are ignored. Returns the number of keys that were deleted. For more information see https://redis.io/commands/del DELrrs r]deletezClusterMultiKeyCommands.deletes0t//===r_c(|jdg|S)aa Updates the last access time of given keys across the cluster. The keys are first split up into slots and then an TOUCH command is sent for every slot Non-existant keys are ignored. Returns the number of keys that were touched. For more information see https://redis.io/commands/touch TOUCHrrs r]touchzClusterMultiKeyCommands.touchs0t//?$??r_c(|jdg|S)aM Remove the specified keys in a different thread. The keys are first split up into slots and then an TOUCH command is sent for every slot Non-existant keys are ignored. Returns the number of keys that were unlinked. For more information see https://redis.io/commands/unlink UNLINKrrs r]unlinkzClusterMultiKeyCommands.unlinks0t//@4@@r_N)__name__ __module__ __qualname____doc__rrrintr r^r rrrfstrrrtrrr rboolrrr#rrrrr_r]rPrP`sHTNtCdO?T w 23  c4 ## $ +238L3L+M c" .tn .sHZ$889 .C= . c .G5GG$x}:MG.Fggz.A&BFtDzF& K3 Kt K KADAYA >D >Y > @4 @I @ AD AY Ar_rPcdeZdZdZdededeeefdZ de e e fdee fdZdededefd Zy ) AsyncClusterMultiKeyCommandsrQrRrrScKt||}|j|}|jd|d{}|j|||S7w)rr?Nrrs r]rz+AsyncClusterMultiKeyCommands.mget_nonatomic sZD$'44T: 226=II,,T=#FFJs3AA Ar`cdK|j|}|jd|d{S7w)rrNrrs r]rz+AsyncClusterMultiKeyCommands.mset_nonatomic s366w?33FNKKKKs '0.0rgcvK|j|}t|j||d{S7w)rNrrs r]rz8AsyncClusterMultiKeyCommands._split_command_across_slots3s844T: 77OOPPOs ,97 9N)rrrrrrr r rrr rrrrrrrrr_r]rrsqGGtGXc]@SG.LGGZ4G,HLTRVZL& Q QT Qc Qr_rc NeZdZdZdefdZdefdZdefdZdddefdZ ddd e defd Z ddd e defd Z d e defd ZdedefdZd e deefdZd e defdZ d/dddeedefdZd/deddefdZdedefdZ d/dede deddefdZdefdZdddedefdZ d0dededdefdZ d/deddefd Zd!e d"e defd#Z d/d$e deddefd%Zddded e d&edef d'Z d e defd(Z! d/dededdefd)Z"d/deddefd*Z#d/d+Z$dddefd,Z%d/deddefd-Z&d/deddefd.Z'y)1ClusterManagementCommands A class for Redis Cluster management commands The class inherits from Redis's core ManagementCommands class and do the required adjustments to work with cluster mode rSctd)z Make the server a replica of another instance, or promote it as master. For more information see https://redis.io/commands/slaveof z(SLAVEOF is not supported in cluster moderrYrkwargss r]slaveofz!ClusterManagementCommands.slaveofHs $$NOOr_ctd)z Make the server a replica of another instance, or promote it as master. For more information see https://redis.io/commands/replicaof z*REPLICAOF is not supported in cluster moderrs r] replicaofz#ClusterManagementCommands.replicaofPs $$PQQr_ctd)zo Swaps two Redis databases. For more information see https://redis.io/commands/swapdb z'SWAPDB is not supported in cluster moderrs r]swapdbz ClusterManagementCommands.swapdbXs $$MNNr_ target_noder'c(|jd|S)z Returns the node's id. :target_node: 'ClusterNode' The node to execute the command on For more information check https://redis.io/commands/cluster-myid/ z CLUSTER MYIDrjrnrYrs r] cluster_myidz&ClusterManagementCommands.cluster_myid`s##N#MMr_slotsc.|jdg|d|iS)z Assign new hash slots to receiving node. Sends to specified node. :target_node: 'ClusterNode' The node to execute the command on For more information see https://redis.io/commands/cluster-addslots zCLUSTER ADDSLOTSrjrrYrrs r]cluster_addslotsz*ClusterManagementCommands.cluster_addslotsks,$t##  !& 5@  r_c.|jdg|d|iS)a Similar to the CLUSTER ADDSLOTS command. The difference between the two commands is that ADDSLOTS takes a list of slots to assign to the node, while ADDSLOTSRANGE takes a list of slot ranges (specified by start and end slots) to assign to the node. :target_node: 'ClusterNode' The node to execute the command on For more information see https://redis.io/commands/cluster-addslotsrange zCLUSTER ADDSLOTSRANGErjrrs r]cluster_addslotsrangez/ClusterManagementCommands.cluster_addslotsrangezs,$t## # &+ :E  r_slot_idc&|jd|S)z Return the number of local keys in the specified hash slot Send to node based on specified slot_id For more information see https://redis.io/commands/cluster-countkeysinslot zCLUSTER COUNTKEYSINSLOTrrYrs r]cluster_countkeysinslotz1ClusterManagementCommands.cluster_countkeysinslots##$=wGGr_node_idc&|jd|S)z Return the number of failure reports active for a given node Sends to a random node For more information see https://redis.io/commands/cluster-count-failure-reports zCLUSTER COUNT-FAILURE-REPORTSr)rYrs r]cluster_count_failure_reportz6ClusterManagementCommands.cluster_count_failure_reports##$CWMMr_cL|Dcgc]}|jd|c}Scc}w) Set hash slots as unbound in the cluster. It determines by it self what node the slot is in and sends it there Returns a list of the results for each processed slot. For more information see https://redis.io/commands/cluster-delslots CLUSTER DELSLOTSr)rYrr\s r]cluster_delslotsz*ClusterManagementCommands.cluster_delslotss(LQQ4$$%7>QQQs!c(|jdg|S)aQ Similar to the CLUSTER DELSLOTS command. The difference is that CLUSTER DELSLOTS takes a list of hash slots to remove from the node, while CLUSTER DELSLOTSRANGE takes a list of slot ranges to remove from the node. For more information see https://redis.io/commands/cluster-delslotsrange zCLUSTER DELSLOTSRANGErrYrs r]cluster_delslotsrangez/ClusterManagementCommands.cluster_delslotsranges$t##$;DeDDr_Noptionc|r4|jdvrtd||jd||S|jd|S)a  Forces a slave to perform a manual failover of its master Sends to specified node :target_node: 'ClusterNode' The node to execute the command on For more information see https://redis.io/commands/cluster-failover )FORCETAKEOVERz-Invalid option for CLUSTER FAILOVER command: zCLUSTER FAILOVERr)upperrrn)rYrrs r]cluster_failoverz*ClusterManagementCommands.cluster_failoversg ||~%:: CF8L++&[,''(:'U Ur_rjc(|jd|S)z Provides info about Redis Cluster node state. The command will be sent to a random node in the cluster if no target node is specified. For more information see https://redis.io/commands/cluster-info z CLUSTER INFOrrrYrjs r] cluster_infoz&ClusterManagementCommands.cluster_infos##N#NNr_r[c&|jd|S)z Returns the hash slot of the specified key Sends to random node in the cluster For more information see https://redis.io/commands/cluster-keyslot zCLUSTER KEYSLOTr)rYr[s r]cluster_keyslotz)ClusterManagementCommands.cluster_keyslots##$5s;;r_hostportc,|jd|||S)z Force a node cluster to handshake with another node. Sends to specified node. For more information see https://redis.io/commands/cluster-meet z CLUSTER MEETrr)rYrrrjs r] cluster_meetz&ClusterManagementCommands.cluster_meets%## D$\$  r_c$|jdS)z Get Cluster config for the node. Sends to random node in the cluster For more information see https://redis.io/commands/cluster-nodes z CLUSTER NODESr)rYs r] cluster_nodesz'ClusterManagementCommands.cluster_nodess##O44r_c*|jd||S)z Reconfigure a node as a slave of the specified master node For more information see https://redis.io/commands/cluster-replicate zCLUSTER REPLICATErr)rYrjrs r]cluster_replicatez+ClusterManagementCommands.cluster_replicates### |$  r_softc>|jd|rd|Sd|S)z Reset a Redis Cluster node If 'soft' is True then it will send 'SOFT' argument If 'soft' is False then it will send 'HARD' argument For more information see https://redis.io/commands/cluster-reset z CLUSTER RESETsSOFTsHARDrr)rYrrjs r] cluster_resetz'ClusterManagementCommands.cluster_resets9## W $  29 $  r_c(|jd|S)z Forces the node to save cluster state on disk For more information see https://redis.io/commands/cluster-saveconfig zCLUSTER SAVECONFIGrrrs r]cluster_save_configz-ClusterManagementCommands.cluster_save_configs##$8|#TTr_r\num_keysc(|jd||S)z Returns the number of keys in the specified cluster slot For more information see https://redis.io/commands/cluster-getkeysinslot zCLUSTER GETKEYSINSLOTr)rYr\rs r]cluster_get_keys_in_slotz2ClusterManagementCommands.cluster_get_keys_in_slots ##$;T8LLr_epochc*|jd||S)z Set the configuration epoch in a new node For more information see https://redis.io/commands/cluster-set-config-epoch zCLUSTER SET-CONFIG-EPOCHrr)rYrrjs r]cluster_set_config_epochz2ClusterManagementCommands.cluster_set_config_epoch"s### &L$  r_statec|jdvr|jd||||S|jdk(r tdtd|)z Bind an hash slot to a specific node :target_node: 'ClusterNode' The node to execute the command on For more information see https://redis.io/commands/cluster-setslot ) IMPORTINGNODE MIGRATINGCLUSTER SETSLOTrSTABLEz4For "stable" state please use cluster_setslot_stablezInvalid slot state: )rrnr)rYrrrrs r]cluster_setslotz)ClusterManagementCommands.cluster_setslot.se ;;=> >''!7E7( [[]h &VW W3E7;< '+D ###J\#JJr_c6d|_|jd|S)z Disables read queries. The command will be sent to the default cluster node if target_nodes is not specified. For more information see https://redis.io/commands/readwrite F READWRITErrrs r] readwritez#ClusterManagementCommands.readwrites"#(##Kl#KKr_rw)TN)(rrrrr rrrr#rrrrrrrrr rrrr rrrrrrrrrrrrrrrrrr rr_r]rr@sP(PRHROO N N9 N  )  3=     ) 3=  $HsHyHNCNIN Rz Rd4j R EJ E9 EDHV)V3;C=V V0O.)AOYO<3<9<NR    "  2:>2J    5y5  *  58    KO    /7/G    8<U$^4U UMSMCMIMDH    (0(@    =)=47=BE=NQ= =(JcJiJFJ    *2>*B    P(>*BPiPQ O OI O KXn%= K K Lh~&> L) Lr_rc&eZdZdZdedeefdZy)AsyncClusterManagementCommandsrrrScVKtjfd|Dd{S7w)rc3hK|])}tjjd|+yw)rN)asyncio ensure_futurern).0r\rYs r] zBAsyncClusterManagementCommands.cluster_delslots..s2%%d&:&:;Mt&TUs/2N)rgatherrs` r]rz/AsyncClusterManagementCommands.cluster_delslotss1^^!    s )')N)rrrrrr rrrr_r]r r s Z DJ r_r ceZdZdZ ddeddededeededfd ed ed ee d ed e ffd Z ddee dee dee d efdZxZS)ClusterDataAccessCommands A class for Redis Cluster Data Access Commands The class inherits from Redis's core DataAccessCommand class and do the required adjustments to work with cluster mode stringsalgoLCSvalue1value2specific_argumentrRlenidx minmatchlen withmatchlenrSc | jdd} |dk(r| d} | jd| it | ||||||||fi| S)a Implements complex algorithms that operate on strings. Right now the only algorithm implemented is the LCS algorithm (longest common substring). However new algorithms could be implemented in the future. ``algo`` Right now must be LCS ``value1`` and ``value2`` Can be two strings or two keys ``specific_argument`` Specifying if the arguments to the algorithm will be keys or strings. strings is the default. ``len`` Returns just the len of the match. ``idx`` Returns the match positions in each string. ``minmatchlen`` Restrict the list of matches to the ones of a given minimal length. Can be provided only when ``idx`` set to True. ``withmatchlen`` Returns the matches with the len of the match. Can be provided only when ``idx`` set to True. For more information see https://redis.io/commands/stralgo rjNrz default-node)popupdatesuperstralgo) rYrrrrrrr r!rrj __class__s r]r&z!ClusterDataAccessCommands.stralgosk>zz.$7  )l.B)L ~|45w            r_matchcount_typec +BK|jd|||d|\}}|Ed{|jDcic]\}}|dk7s ||}}}|r|jDcic]}||j|} }|j dd|ru|jD]5\}}|jd||||| |d|\} }|Ed{| |||<7|jDcic]\}}|dk7s ||}}}|rtyyy7cc}}wcc}w7Jcc}}wwN)r(r)r*r) node_namerj)cursorr(r)r*rjrscanrbrRget_noder#) rYr(r)r*rcursorsdatanamer.nodescurs r] scan_iterz#ClusterDataAccessCommands.scan_itersN" RU%R6R 4;MMOSLD&vQR{4<SS EL\\^TTT4==4=88TET JJ~t ,$+MMO .LD& ) !%###%*4[ ! ! !IC $OO$'IGDM .6=]]_%1T6RS D&L  SU$s]DD D D D  D#D=AD D !D. D<DDD DD)rFFNFNNN)rrrrrrr rr rr#r&rrrr7 __classcell__)r's@r]rrsIR%)"- en- -  - !!3WV_!DE -  - - c]- -  - b%)## !!!}!} !  !r_rc BeZdZdZ ddeedeedeedefdZ y) AsyncClusterDataAccessCommandsrNr(r)r*rSc lK|jd|||d|d{\}}|D]}| |jD cic]\}} | dk7s || }}} |r|jDcic]}||j|} }|j dd|r|jD]?\}} |jd| |||| |d|d{\} }|D]}| | |||<A|jD cic]\}} | dk7s || }}} |r~yyy7cc} }wcc}w7Zcc} }wwr,r/) rYr(r)r*rr2r3valuer4r.r5r6s r]r7z(AsyncClusterDataAccessCommands.scan_itersw(diiXe5XQWXX  EK 5rNs    !>    "-2 (* ZaA5aAH9Q#:9QxLL 2LL^  6 6X 2Xv-6-`< "" r_