U
    Lk7g0                     @   s\   d dl mZ d dlmZmZmZmZmZmZ dZ	dZ
dZG dd deZG dd	 d	eZd
S )    )OperationError)CASCADEDENY
DO_NOTHINGNULLIFYPULLBaseQuerySet)QuerySetQuerySetNoCacher   r   r   r   r      d   c                       s^   e Zd ZdZdZdZdZdd Zdd Zdd	 Z	d
d Z
dd Zd fdd	Zdd Z  ZS )r	   zThe default queryset, that builds queries and handles a set of results
    returned from a query.

    Wraps a MongoDB cursor, providing :class:`~mongoengine.Document` objects as
    the results.
    TNc                 C   s   d| _ | jr|  S t| jS )zIteration utilises a results cache which iterates the cursor
        in batches of ``ITER_CHUNK_SIZE``.

        If ``self._has_more`` the cursor hasn't been exhausted so cache then
        batch. Otherwise iterate the result_cache.
        T)_iter	_has_more_iter_resultsiter_result_cacheself r   A/tmp/pip-unpacked-wheel-n1etwkgt/mongoengine/queryset/queryset.py__iter__&   s    zQuerySet.__iter__c                 C   s4   | j dk	r| j S | jr"t|   t| j| _ | j S )zSince __len__ is called quite frequently (for example, as part of
        list(qs)), we populate the result cache and cache the length.
        N)_lenr   listr   lenr   r   r   r   r   __len__5   s    
zQuerySet.__len__c                 C   s@   | j r
dS |   | jdtd  }t|tkr8d|d< t|S )z/Provide a string representation of the QuerySet.. queryset mid-iteration ..N   $...(remaining elements truncated)...)r   _populate_cacher   REPR_OUTPUT_SIZEr   repr)r   datar   r   r   __repr__D   s    zQuerySet.__repr__c                 c   s^   | j dkrg | _ d}|t| j k r8| j | V  |d7 }q| jsBdS t| j |kr|   qdS )zA generator for iterating over the result cache.

        Also populates the cache if there are more possible results to
        yield. Raises StopIteration when there are no more results.
        Nr   r   )r   r   r   r   )r   posr   r   r   r   O   s    


zQuerySet._iter_resultsc                 C   s\   | j dkrg | _ | jsdS z"ttD ]}| j t|  q$W n tk
rV   d| _Y nX dS )z{
        Populates the result cache with ``ITER_CHUNK_SIZE`` more entries
        (until the cursor is exhausted).
        NF)r   r   rangeITER_CHUNK_SIZEappendnextStopIteration)r   _r   r   r   r   o   s    
zQuerySet._populate_cacheFc                    s2   |dkrt  |S | jdkr,t  || _| jS )zCount the selected elements in the query.

        :param with_limit_and_skip (optional): take any :meth:`limit` or
            :meth:`skip` that has been applied to this cursor into account when
            getting the count
        FN)supercountr   )r   Zwith_limit_and_skip	__class__r   r   r,      s
    
zQuerySet.countc                 C   s&   | j dk	rtd| t| j| jS )z!Convert to a non-caching querysetNzQuerySet already cached)r   r   _clone_intor
   	_document_collectionr   r   r   r   no_cache   s    
zQuerySet.no_cache)F)__name__
__module____qualname____doc__r   r   r   r   r   r#   r   r   r,   r2   __classcell__r   r   r-   r   r	      s    r	   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r
   zA non caching QuerySetc                 C   s   |  t| j| jS )zConvert to a caching queryset)r/   r	   r0   r1   r   r   r   r   cache   s    zQuerySetNoCache.cachec              	   C   sr   | j r
dS g }ttd D ]2}z|t|  W q tk
rJ   Y  qNY qX qt|tkrbd|d< |   t|S )z2Provides the string representation of the QuerySetr   r   r   r   )	r   r%   r    r'   r(   r)   r   rewindr!   )r   r"   r*   r   r   r   r#      s    zQuerySetNoCache.__repr__c                 C   s   | }|j r|  }|  |S )N)r   cloner9   )r   Zquerysetr   r   r   r      s
    zQuerySetNoCache.__iter__N)r3   r4   r5   r6   r8   r#   r   r   r   r   r   r
      s   r
   N)Zmongoengine.errorsr   Zmongoengine.queryset.baser   r   r   r   r   r   __all__r    r&   r	   r
   r   r   r   r   <module>   s    	 