HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/uhashring/__pycache__/ring.cpython-37.pyc
B

��f�+�@s6ddlmZddlmZddlmZGdd�d�ZdS)�)�bisect)�
KetamaRing)�MetaRingc@s8eZdZdZgfdd�Zdd�Zdd�ZeZdd	�ZeZ	d
difdd
�Z
e
Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd@d)d*�Zd+d,�ZdAd.d/�Zd0d1�Zed2d3��ZeZed4d5��Z ed6d7��Z!e!Z"ed8d9��Z#ed:d;��Z$ed<d=��Z%ed>d?��Z&d-S)B�HashRingz$Implement a consistent hashing ring.cKs�|�dd�}|�dd�}|�dd�}|dkrXdd�|��D�}|dkrJd}tf|�|_n|dkrdd	}t|�|_||_|jj|_|r�t|d
�s�td��||_	|�
|�r�|j�|jj���dS)a�Create a new HashRing given the implementation.

        :param nodes: nodes used to create the continuum (see doc for format).
        :param hash_fn: use this callable function to hash keys, can be set to
                        'ketama' to use the ketama compatible implementation.
        :param vnodes: default number of vnodes per node.
        :param weight_fn: use this function to calculate the node's weight.
        �hash_fnN�vnodes�	weight_fnZketamacSsi|]\}}|dkr||�qS))Zreplicas�)�.0�k�vr	r	�?/opt/alt/python37/lib/python3.7/site-packages/uhashring/ring.py�
<dictcomp>sz%HashRing.__init__.<locals>.<dictcomp>�(��__call__z'weight_fn should be a callable function)
�get�itemsr�runtimer�_default_vnodes�hashi�hasattr�	TypeError�
_weight_fn�_configure_nodes�_create_ring�_nodes)�self�nodes�kwargsrrrZketama_argsr	r	r
�__init__
s$	


zHashRing.__init__c
Cs8t|t�r|g}n t|ttf�s2td�t|����d}x�|D]�}|d|d|jdd�}|jj	�
|i�}|}|spd}t|t�r�||}t|t�r�||d<n^t|t�r�xR|��D]2\}}	||kr�|	||<|dkr�|�
|�|	kr�d}q�Wntd	�t|����|j
�r|j
f|�|d<|�
d�|dk�r$d}||jj	|<q<W|S)
zzParse and set up the given nodes.

        :param nodes: nodes used to create the continuum (see doc for format).
        z6nodes configuration should be a list or a dict, got {}FN�)�hostname�instance�nodename�portr�weightTr&)r$rr&z5node configuration should be a dict or an int, got {})�
isinstance�str�dict�list�
ValueError�format�typerrrr�intrr)
rrZconf_changed�node�confZcurrent_confr$Z	node_confrrr	r	r
r+sJ






zHashRing._configure_nodescCs|j�|�dS)zHRemove the given node.

        :param nodename: the node name.
        N)rZ_remove_node)rr$r	r	r
�__delitem__`szHashRing.__delitem__cCs|�|d�S)zlReturns the instance of the node matching the hashed key.

        :param key: the key to look for.
        r#)�_get)r�keyr	r	r
�__getitem__iszHashRing.__getitem__r&r!cCs*|�||i�r&|j�||j|fg�dS)z�Add the given node with its associated configuration.

        :param nodename: the node name.
        :param conf: the node configuration.
        N)rrrr)rr$r0r	r	r
�__setitem__rszHashRing.__setitem__cCs0t|jj|�|��}|t|jj�kr(dS|SdS)a:Get the index of the given key in the sorted key list.

        We return the position with the nearest hash based on
        the provided key unless we reach the end of the continuum/ring
        in which case we return the 0 (beginning) index position.

        :param key: the key to hash and look for.
        rN)rr�_keysr�len)rr3�pr	r	r
�_get_pos}s	zHashRing._get_poscCs�|jjsdS|�|�}|dkr"|S|jj|jj|}|dkrN|jj||S|dkrb|jj|S|dkrn|S|dkr�|jj||fSdS)a�Generic getter magic method.

        The node with the nearest but not less hash value is returned.

        :param key: the key to look for.
        :param what: the information to look for in, allowed values:
            - instance (default): associated node instance
            - nodename: node name
            - pos: index of the given key in the ring
            - tuple: ketama compatible (pos, name) tuple
            - weight: node weight
        N�pos)r"r#r%r&r)r$�tuple)r�_ringr9r6r)rr3�whatr:r$r	r	r
r2�s

z
HashRing._getcCs|�|d�S)zhReturns the node object dict matching the hashed key.

        :param key: the key to look for.
        r))r2)rr3r	r	r
r�szHashRing.getcCsdd�|jj��D�S)z<Returns a list of the instances of all the configured nodes.cSs g|]}|�d�r|�d��qS)r#)r)r
�cr	r	r
�
<listcomp>�sz*HashRing.get_instances.<locals>.<listcomp>)rr�values)rr	r	r
�
get_instances�szHashRing.get_instancescCs
|�|�S)z�Alias of ketama hashi method, returns the hash of the given key.

        This method is present for hash_ring compatibility.

        :param key: the key to look for.
        )r)rr3r	r	r
�get_key�szHashRing.get_keycCs|�|d�S)zmReturns the node name of the node matching the hashed key.

        :param key: the key to look for.
        r$)r2)rr3r	r	r
�get_node�szHashRing.get_nodecCs|�|d�S)zlReturns the hostname of the node matching the hashed key.

        :param key: the key to look for.
        r")r2)rr3r	r	r
�get_node_hostname�szHashRing.get_node_hostnamecCs|�|d�S)zhReturns the port of the node matching the hashed key.

        :param key: the key to look for.
        r%)r2)rr3r	r	r
�
get_node_port�szHashRing.get_node_portcCs|�|d�S)zrReturns the index position of the node matching the hashed key.

        :param key: the key to look for.
        r:)r2)rr3r	r	r
�get_node_pos�szHashRing.get_node_poscCs|�|d�S)zjReturns the weight of the node matching the hashed key.

        :param key: the key to look for.
        r&)r2)rr3r	r	r
�get_node_weight�szHashRing.get_node_weightcCs|jj��S)z8Returns a list of the names of all the configured nodes.)rr�keys)rr	r	r
�	get_nodes�szHashRing.get_nodescs�fdd��jjD�S)z@Returns a ketama compatible list of (position, nodename) tuples.csg|]}|�jj|f�qSr	)rr<)r
r)rr	r
r?�sz'HashRing.get_points.<locals>.<listcomp>)rr6)rr	)rr
�
get_points�szHashRing.get_pointscCs|�|d�S)zjReturns a ketama compatible (position, nodename) tuple.

        :param key: the key to look for.
        r;)r2)rr3r	r	r
�
get_server�szHashRing.get_serverTccs6|jjsdVn"x |j||d�D]}|dVq WdS)aihash_ring compatibility implementation.

        Given a string key it returns the nodes as a generator that
        can hold the key.
        The generator iterates one time through the ring
        starting at the correct position.
        if `distinct` is set, then the nodes returned will be unique,
        i.e. no virtual copies will be returned.
        N)�uniquer$)rr<�range)rr3Zdistinctr/r	r	r
�
iterate_nodes�s
zHashRing.iterate_nodescCsZt|jj�}|r td|���ntd�x,|��D] }|\}}t|�d|�d��q2WdS)z,Prints a ketama compatible continuum report.zNumpoints in continuum: zContinuum emptyz (�)N)r7rr6�printrJ)rZ	numpointsr8Zpointr/r	r	r
�print_continuumszHashRing.print_continuumNccst�}|r|pt|jj�}ng}|�|�}x�|jj|d�D]P}|jj|}|rf||krZq<|�|�n
|�|�|jj|Vt|�|kr<Pq<Wxlt	|jj�D]\\}}||kr�|jj|}|r�||kr�q�|�|�n
|�|�|jj|Vt|�|kr�Pq�WdS)aReturns a generator of nodes' configuration available
        in the continuum/ring.

        :param key: the key to look for.
        :param size: limit the list to at most this number of nodes.
        :param unique: a node may only appear once in the list (default True).
        N)
�setr7rrr9r6r<�add�append�	enumerate)rr3�sizerLZ	all_nodesr:r$�ir	r	r
rMs4


zHashRing.rangecCs|j�|jj���dS)N)rrrr)rr	r	r
�
regenerate3szHashRing.regeneratecCs|jjS)N)rr)rr	r	r
r06sz
HashRing.confcCs|jjS)N)rZ
_distribution)rr	r	r
�distribution<szHashRing.distributioncCs|jjS)N)rr<)rr	r	r
�ring@sz
HashRing.ringcCst|jj�S)N)r7rr<)rr	r	r
rVFsz
HashRing.sizecCs|jjS)N)rr<)rr	r	r
r<JszHashRing._ringcCs|jjS)N)rr)rr	r	r
rNszHashRing._nodescCs|jjS)N)rr6)rr	r	r
r6RszHashRing._keys)T)NT)'�__name__�
__module__�__qualname__�__doc__r rr1�remove_noder4Zget_node_instancer5�add_noder9r2rrArBrCrDrErFrGrIrJrKrNrQrMrX�propertyr0rrYrZZ	continuumrVr<rr6r	r	r	r
rsF!5		

(rN)rZuhashring.ring_ketamarZuhashring.ring_metarrr	r	r	r
�<module>s