B
    è?F[Ê  ã               @   s$   G d d„ de ƒZG dd„ deƒZdS )c                   sZ   e Zd ZdZd‡ fdd„	Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	e	Z
dd„ Zdd„ Z‡  ZS )Ú	ResultSeta¼  
    A class used to lazily handle page-to-page navigation through a set of
    results.

    It presents a transparent iterator interface, so that all the user has
    to do is use it in a typical ``for`` loop (or list comprehension, etc.)
    to fetch results, even if they weren't present in the current page of
    results.

    This is used by the ``Table.query`` & ``Table.scan`` methods.

    Example::

        >>> users = Table('users')
        >>> results = ResultSet()
        >>> results.to_call(users.query, username__gte='johndoe')
        # Now iterate. When it runs out of results, it'll fetch the next page.
        >>> for res in results:
        ...     print res['username']

    Nc                sN   t t| ƒ ¡  d | _g | _i | _g | _d| _d| _d | _	d| _
|| _d | _d S )NéÿÿÿÿTé    )Úsuperr   Ú__init__Úthe_callableÚ	call_argsÚcall_kwargsÚ_resultsÚ_offsetÚ_results_leftÚ_last_key_seenÚ_fetchesÚ_max_page_sizeÚ_limit)ÚselfZmax_page_size)Ú	__class__© ú5lib/python3.7/site-packages/boto/dynamodb2/results.pyr      s    zResultSet.__init__c             C   s   dS )NZexclusive_start_keyr   )r   r   r   r   Ú	first_key$   s    zResultSet.first_keyc             C   s   g | _ d| _dS )zº
        Resets the internal state of the ``ResultSet``.

        This prevents results from being cached long-term & consuming
        excess memory.

        Largely internal.
        r   N)r	   r
   )r   r   r   r   Ú_reset(   s    	zResultSet._resetc             C   s   | S )Nr   )r   r   r   r   Ú__iter__4   s    zResultSet.__iter__c             C   s¢   |  j d7  _ | j t| jƒkrT| jdkr.tƒ ‚|  ¡  xt| jƒsR| jrR|  ¡  q8W | j t| jƒk r˜| jd k	rŒ|  jd8  _| jdk rŒtƒ ‚| j| j  S tƒ ‚d S )Né   Fr   )r
   Úlenr	   r   ÚStopIterationÚ
fetch_morer   )r   r   r   r   Ú__next__7   s    


zResultSet.__next__c             O   sN   t |ƒstdƒ‚| dd¡| _| jdk	r8| jdk r8d| _|| _|| _|| _dS )aê  
        Sets up the callable & any arguments to run it with.

        This is stored for subsequent calls so that those queries can be
        run without requiring user intervention.

        Example::

            # Just an example callable.
            >>> def squares_to(y):
            ...     for x in range(1, y):
            ...         yield x**2
            >>> rs = ResultSet()
            # Set up what to call & arguments.
            >>> rs.to_call(squares_to, y=3)

        z3You must supply an object or function to be called.ÚlimitNr   )ÚcallableÚ
ValueErrorÚpopr   r   r   r   )r   r   ÚargsÚkwargsr   r   r   Úto_callS   s    zResultSet.to_callc             C   s   |   ¡  | jdd… }| j ¡ }| jdk	r6| j|| j< | jrV| jrV| j| jkrV| j| _| jdk	rl| j|d< n| jdk	r€| j|d< | j||Ž}|  j	d7  _	| 
dg ¡}| 
dd¡| _t|ƒrÌ| j |d ¡ | jdk	r
| jdkr
| j}|t|d ƒ8 }|dkr
d| _| jdkrd| _dS )z¯
        When the iterator runs out of results, this method is run to re-execute
        the callable (& arguments) to fetch the next page.

        Largely internal.
        Nr   r   ÚresultsZlast_keyr   F)r   r   r   Úcopyr   r   r   r   r   r   Úgetr   r	   Úextendr   )r   r    r!   r#   Znew_resultsr   r   r   r   r   v   s0    





zResultSet.fetch_more)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   Únextr"   r   Ú__classcell__r   r   )r   r   r      s   #r   c                   s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚBatchGetResultSetc                s2   |  dg ¡| _|  dd¡| _tt| ƒj||Ž d S )NÚkeysZmax_batch_getéd   )r   Ú
_keys_leftÚ_max_batch_getr   r.   r   )r   r    r!   )r   r   r   r   ¨   s    zBatchGetResultSet.__init__c             C   sô   |   ¡  | jd d … }| j ¡ }| jd | j… |d< | j| jd … | _t| jƒdkrZd| _| j||Ž}t| 	dg ¡ƒszd S | j
 |d ¡ x*t| 	dg ¡ƒD ]\}}| j ||¡ qœW t| jƒdkrÊd| _| j 	d¡rð| jd  t|d ƒ8  < d S )Nr/   r   Fr#   Zunprocessed_keysTr   )r   r   r   r$   r1   r2   r   r   r   r%   r	   r&   Ú	enumerateÚinsert)r   r    r!   r#   ÚoffsetZkey_datar   r   r   r   ­   s"    
zBatchGetResultSet.fetch_more)r'   r(   r)   r   r   r-   r   r   )r   r   r.   §   s   r.   N)Úobjectr   r.   r   r   r   r   Ú<module>   s    '