�
J�gn{ � � � d dl Z d dlmZmZmZmZmZmZmZm Z m
Z
mZmZm
Z
d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZ ddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, dd l-m.Z.m/Z/ erd d
l0m1Z1 e2g d�� � Z3 G d� d
e� � Z4 G d� de4� � Z5 G d� de'� � Z6 G d� de6e!� � Z7 G d� de$� � Z8 G d� de8e� � Z9 G d� de4e6ee)e8e*e%e&e(e/� � Z: G d� de5e7ee9e#ee e"e.� � Z;dS )� N)�
TYPE_CHECKING�Any�
AsyncIterator�Dict�Iterable�Iterator�List�Literal�Mapping�NoReturn�Optional�Union)�key_slot)�RedisClusterException�
RedisError)�AnyKeyT�ClusterCommandsProtocol�
EncodableT�KeysT�KeyT�PatternT� ResponseT� )�ACLCommands�AsyncACLCommands�AsyncDataAccessCommands�AsyncFunctionCommands�AsyncGearsCommands�AsyncManagementCommands�AsyncModuleCommands�AsyncScriptCommands�DataAccessCommands�FunctionCommands�
GearsCommands�ManagementCommands�ModuleCommands�PubSubCommands�ScriptCommands)�list_or_args)�AsyncRedisModuleCommands�RedisModuleCommands)�TargetNodesT))�BITCOUNT�BITPOS�EVAL_RO�
EVALSHA_RO�EXISTS�GEODIST�GEOHASH�GEOPOS� GEORADIUS�GEORADIUSBYMEMBER�GET�GETBIT�GETRANGE�HEXISTS�HGET�HGETALL�HKEYS�HLEN�HMGET�HSTRLEN�HVALS�KEYS�LINDEX�LLEN�LRANGE�MGET�PTTL� RANDOMKEY�SCARD�SDIFF�SINTER� SISMEMBER�SMEMBERS�SRANDMEMBER�STRLEN�SUNION�TTL�ZCARD�ZCOUNT�ZRANGE�ZSCOREc �� � e Zd ZdZdee deeee f fd�Z de
eef deeee f fd�Z
dede
eee f dee fd �Zdee de
eee f d
ee dee fd�Zdededeee fd
�Zde
eef dee fd�Zdededefd�Zdedefd�Zdedefd�Zdedefd�Zdedefd�ZdS )�ClusterMultiKeyCommands�C
A class containing commands that handle more than one key
�keys�returnc � � i }|D ]R}t | j � |� � � � }|� |g � � � |� � �S|S )z@Split keys into a dictionary that maps a slot to a list of keys.)r �encoder�encode�
setdefault�append)�selfrY �
slots_to_keys�key�slots �f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/redis/commands/cluster.py�_partition_keys_by_slotz/ClusterMultiKeyCommands._partition_keys_by_slotj sb � � �
�� ;� ;�C��D�L�/�/��4�4�5�5�D��$�$�T�2�.�.�5�5�c�:�:�:�:��� �mappingc �� � i }|� � � D ]X}t | j � |d � � � � }|� |g � � � |� � �Y|S )zBSplit pairs into a dictionary that maps a slot to a list of pairs.r )�itemsr r\ r] r^ �extend)r` rg �slots_to_pairs�pairrc s rd �_partition_pairs_by_slotz0ClusterMultiKeyCommands._partition_pairs_by_slott sn � �
���M�M�O�O� =� =�D��D�L�/�/��Q��8�8�9�9�D��%�%�d�B�/�/�6�6�t�<�<�<�<��rf �command�
slots_to_argsc �� � ���� � j o�t v �� � � � ����� fd�|� � � D � � �� � � S )Nc
�l �� g | ]0\ }} �j �g|�R d �j � |�� � gi���1S ��target_nodes��execute_command�
nodes_manager�get_node_from_slot��.0rc � slot_argsrn �pipe�read_from_replicasr` s ����rd �
<listcomp>zEClusterMultiKeyCommands._execute_pipeline_by_slot.<locals>.<listcomp>� �v �� �
�
�
� ��i�
!�D� ��
��
�
�
� �&�9�9�$�@R�S�S��
�
�
�
�
rf )r| �
READ_COMMANDS�pipelineri �execute�r` rn ro r{ r| s `` @@rd �_execute_pipeline_by_slotz1ClusterMultiKeyCommands._execute_pipeline_by_slot� s� ����� � "�4�Q��M�9Q���}�}����
�
�
�
�
�
�
� $1�#6�#6�#8�#8�
�
�
�
� �|�|�~�~�rf � responsesc �x �� d� t |� � � |� � D � � ��fd�|D � � S )Nc �F � i | ]\ }}t ||� � D ]\ }}||�� �S � )�zip)ry �slot_values�response�k�vs rd �
<dictcomp>zDClusterMultiKeyCommands._reorder_keys_by_command.<locals>.<dictcomp>� sS � �
�
�
�%��X��K��2�2�
�
� ��1�
�q�
�
�
�
rf c � �� g | ]
}�| ��S r� r� )ry rb �resultss �rd r} zDClusterMultiKeyCommands._reorder_keys_by_command.<locals>.<listcomp>� s �� �-�-�-�����-�-�-rf )r� �values)r` rY ro r� r� s @rd �_reorder_keys_by_commandz0ClusterMultiKeyCommands._reorder_keys_by_command� sS �� �
�
�),�]�-A�-A�-C�-C�Y�)O�)O�
�
�
��
.�-�-�-��-�-�-�-rf �argsc � � t ||� � }| � |� � }| � d|� � }| � |||� � 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
rF �r) re r� r� �r` rY r� ra �ress rd �mget_nonatomicz&ClusterMultiKeyCommands.mget_nonatomic� sY � � �D�$�'�'�� �4�4�T�:�:�
� �,�,�V�]�C�C�� �,�,�T�=�#�F�F�Frf c �X � | � |� � }| � d|� � 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
�MSET�rm r� �r` rg rk s rd �mset_nonatomicz&ClusterMultiKeyCommands.mset_nonatomic� s/ � � �6�6�w�?�?�� �-�-�f�n�E�E�Erf c �r � | � |� � }t | � ||� � � � S )�v
Runs the given command once for the keys
of each slot. Returns the sum of the return values.
�re �sumr� �r` rn rY ra s rd �_split_command_across_slotsz3ClusterMultiKeyCommands._split_command_across_slots� s7 � � �4�4�T�:�:�
� �4�1�1�'�=�I�I�J�J�Jrf c � � | j dg|�R � 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
r1 �r� �r` rY s rd �existszClusterMultiKeyCommands.exists� s � � 0�t�/��@�4�@�@�@�@rf c � � | j dg|�R � 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-existent keys are ignored.
Returns the number of keys that were deleted.
For more information see https://redis.io/commands/del
�DELr� r� s rd �deletezClusterMultiKeyCommands.delete� s � � 0�t�/��=��=�=�=�=rf c � � | j dg|�R � 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-existent keys are ignored.
Returns the number of keys that were touched.
For more information see https://redis.io/commands/touch
�TOUCHr� r� s rd �touchzClusterMultiKeyCommands.touch� s � � 0�t�/��?�$�?�?�?�?rf c � � | j dg|�R � 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-existent keys are ignored.
Returns the number of keys that were unlinked.
For more information see https://redis.io/commands/unlink
�UNLINKr� r� s rd �unlinkzClusterMultiKeyCommands.unlink� s � � 0�t�/��@�4�@�@�@�@rf N)�__name__�
__module__�__qualname__�__doc__r r r �intr re r r r rm �strr r� r� r r
r� �boolr� r� r r� r� r� r� r� rf rd rW rW e s] � � � � � �� ��H�T�N� �t�C��d��O�?T� � � � �
��w�
�2�3�
�
�c�4�
�#�#� $�
�
�
�
����+2�3���8L�3L�+M��
�c��� � � �".��t�n�.� �s�H�Z�$8�8�9�.� �C�=� .�
�c��.� .� .� .�G�5� G�� G�$�x��}�:M� G� G� G� G�.F�g�g�z�.A�&B� F�t�D�z� F� F� F� F�&
K�3�
K�t�
K��
K�
K�
K�
K�A�D� A�Y� A� A� A� A�>�D� >�Y� >� >� >� >�
@�4�
@�I�
@�
@�
@�
@�A�D� A�Y� A� A� A� A� A� Arf rW c � � e Zd ZdZdededeee fd�Z de
eef dee
fd�Zdededefd �Zded
e
eee f dee fd�ZdS )
�AsyncClusterMultiKeyCommandsrX rY r� rZ c � � K � t ||� � }| � |� � }| � d|� � � d{V ��}| � |||� � S )r� rF Nr� r� s rd r� z+AsyncClusterMultiKeyCommands.mget_nonatomic so � � � � �D�$�'�'�� �4�4�T�:�:�
� �2�2�6�=�I�I�I�I�I�I�I�I�� �,�,�T�=�#�F�F�Frf rg c � �h K � | � |� � }| � d|� � � d{V ��S )r� r� Nr� r� s rd r� z+AsyncClusterMultiKeyCommands.mset_nonatomic% sE � � � � �6�6�w�?�?�� �3�3�F�N�K�K�K�K�K�K�K�K�Krf rn c � � K � | � |� � }t | � ||� � � d{V ��� � S )r� Nr� r� s rd r� z8AsyncClusterMultiKeyCommands._split_command_across_slots8 sM � � � � �4�4�T�:�:�
� ��7�7���O�O�O�O�O�O�O�O�P�P�Prf ro c � � � ���K � � j r� � � � � d {V �� � j o�t v �� � � � ����� fd�|� � � D � � �� � � � d {V ��S )Nc
�l �� g | ]0\ }} �j �g|�R d �j � |�� � gi���1S rr rt rx s ����rd r} zJAsyncClusterMultiKeyCommands._execute_pipeline_by_slot.<locals>.<listcomp>K r~ rf )�_initialize�
initializer| r r� ri r� r� s `` @@rd r� z6AsyncClusterMultiKeyCommands._execute_pipeline_by_slotD s� ����� � � � �� $��/�/�#�#�#�#�#�#�#�#�#�!�4�Q��M�9Q���}�}����
�
�
�
�
�
�
� $1�#6�#6�#8�#8�
�
�
�
� �\�\�^�^�#�#�#�#�#�#�#rf N)r� r� r� r� r r r r
r r� r r r r� r� r� r� r� r r� r� rf rd r� r� s� � � � � � �� �G�� G�t� G��X�c�]�@S� G� G� G� G�.L�G�G�Z�4G�,H� L�T�RV�Z� L� L� L� L�&
Q��
Q�T�
Q�c�
Q�
Q�
Q�
Q�$��$�+2�3���8L�3L�+M�$�
�c��$� $� $� $� $� $rf r� c
�� � e Zd ZdZdefd�Zdefd�Zdefd�Zdddefd�Z ddd e
defd
�Zddd e
defd�Zde
defd
�Zdedefd�Zd e
dee fd�Zd e
defd�Z d4dddee defd�Zd4ded defd�Zdedefd�Z d4dede
ded defd�Zdefd�Zdddedefd�Z d5deded defd �Z d4ded defd!�Zd"e
d#e
defd$�Z d4d%e
ded defd&�Zdddede
d'edef
d(�Z de
defd)�Z! d4deded defd*�Z"d4ded defd+�Z#d4d,�Z$d4d-�Z%dddefd.�Z&d4ded ddfd/�Z'd4ded ddfd0�Z(d4ded defd1�Z)d4ded defd2�Z*defd3�Z+dS )6�ClusterManagementCommands��
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
rZ c � � t d� � �)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 mode�r �r` r� �kwargss rd �slaveofz!ClusterManagementCommands.slaveof` s � � $�$N�O�O�Orf c � � t d� � �)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 moder� r� s rd � replicaofz#ClusterManagementCommands.replicaofh s � � $�$P�Q�Q�Qrf c � � t d� � �)zo
Swaps two Redis databases.
For more information see https://redis.io/commands/swapdb
z'SWAPDB is not supported in cluster moder� r� s rd �swapdbz ClusterManagementCommands.swapdbp s � � $�$M�N�N�Nrf �target_noder, c �0 � | � d|�� � 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/
zCLUSTER MYIDrr �ru �r` r� s rd �cluster_myidz&ClusterManagementCommands.cluster_myidx s � � �#�#�N��#�M�M�Mrf �slotsc �$ � | j dg|�R d|i�S )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 ADDSLOTSrs r� �r` r� r� s rd �cluster_addslotsz*ClusterManagementCommands.cluster_addslots� s8 � � $�t�#��
�!&�
�
�
�5@�
�
�
rf c �$ � | j dg|�R d|i�S )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 ADDSLOTSRANGErs r� r� s rd �cluster_addslotsrangez/ClusterManagementCommands.cluster_addslotsrange� s8 � � $�t�#�#�
�&+�
�
�
�:E�
�
�
rf �slot_idc �. � | � d|� � 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 COUNTKEYSINSLOTr� �r` r� s rd �cluster_countkeysinslotz1ClusterManagementCommands.cluster_countkeysinslot� s � � �#�#�$=�w�G�G�Grf �node_idc �. � | � d|� � 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� )r` r� s rd �cluster_count_failure_reportz6ClusterManagementCommands.cluster_count_failure_report� s � � �#�#�$C�W�M�M�Mrf c � � � � fd�|D � � S )�
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
c �<