U
    d$                     @  s&  d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ erdd
lmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddl"m#Z# ddl$m%Z%m&Z& G dd dZ'G dd de'Z(G dd de(Z)G dd de'Z*dS )z;Perform aggregation operations on a collection or database.    )annotations)CallableMappingMutableMapping)TYPE_CHECKINGAnyOptionalUnion)SON)common)validate_collation_or_none)ConfigurationError)ReadPreference_AggWritePref)ClientSession)
Collection)CommandCursor)Database)
Connection)_ServerMode)Server)_DocumentType	_Pipelinec                   @  s   e Zd ZdZd+ddddddd	d
ddd
ddZeddddZeddddZdddddZeddddZ	dd d!d"d#Z
dd$d%d&d'd(d)d*ZdS ),_AggregationCommandzThe internal abstract base class for aggregation cursors.

    Should not be called directly by application developers. Use
    :meth:`pymongo.collection.Collection.aggregate`, or
    :meth:`pymongo.database.Database.aggregate` instead.
    NzUnion[Database, Collection]ztype[CommandCursor]r   zMutableMapping[str, Any]boolzOptional[Mapping[str, Any]]z"Optional[MutableMapping[str, Any]]z9Optional[Callable[[Mapping[str, Any], Connection], None]]r   None)
targetcursor_classpipelineoptionsexplicit_sessionletuser_fieldsresult_processorcommentreturnc
           
      C  s  d|krt d|| _td|}|| _d| _|rPd|d ksJd|d krPd| _td	| |d k	rxtd
| ||d
< |	d k	r|	|d< || _td| j	dd | _
| jdi  | j
d k	r| js| j
| jd d< || _|| _|| _|| _t|	dd | _|	dd | _d | _d S )NexplainzBThe explain option is not supported. Use Database.command instead.r   Fz$outz$mergeTr   r!   r$   	batchSizecursor	collationZmaxAwaitTimeMS)r   _targetr   Zvalidate_list	_pipeline_performs_writeZvalidate_is_mapping_optionsZ%validate_non_negative_integer_or_nonepop_batch_size
setdefault_cursor_class_explicit_session_user_fields_result_processorr   
_collation_max_await_time_ms_write_preference)
selfr   r   r   r   r    r!   r"   r#   r$    r:   7/tmp/pip-unpacked-wheel-oblwsawz/pymongo/aggregation.py__init__.   s>     z_AggregationCommand.__init__zUnion[str, int]r%   c                 C  s   t dS )z.The argument to pass to the aggregate command.NNotImplementedErrorr9   r:   r:   r;   _aggregation_targetg   s    z'_AggregationCommand._aggregation_targetstrc                 C  s   t dS )z4The namespace in which the aggregate command is run.Nr>   r@   r:   r:   r;   _cursor_namespacel   s    z%_AggregationCommand._cursor_namespaceMapping[str, Any]r   )
cursor_docr%   c                 C  s   t dS )5The Collection used for the aggregate command cursor.Nr>   )r9   rE   r:   r:   r;   _cursor_collectionq   s    z&_AggregationCommand._cursor_collectionr   c                 C  s   t dS )z:The database against which the aggregation command is run.Nr>   r@   r:   r:   r;   	_databaseu   s    z_AggregationCommand._databasezOptional[ClientSession]z!Union[_AggWritePref, _ServerMode])sessionr%   c                 C  s:   | j r| j S | j|}| jr6|tjkr6t| | _ }|S N)r8   r+   Z_read_preference_forr-   r   ZPRIMARYr   )r9   rI   Zprefr:   r:   r;   get_read_preferencez   s    z'_AggregationCommand.get_read_preferencer   r   r   zCommandCursor[_DocumentType])rI   serverconnread_preferencer%   c                 C  s  t d| jfd| jfg}|| j d|krF| jr<|jdkrF| jj}nd }d|krf| jrf| j	|}nd }|j
| jj||| jjd||| j|| jj| jd}| jr| || d|kr|d }	nd	|d
g | jd}	| j| |	|	|j| jpd	| j|| j| jdd}
|
| |
S )NZ	aggregater   ZreadConcern   ZwriteConcernT)Zparse_write_concern_errorread_concernwrite_concernr*   rI   clientr"   r)   r   result)idZ
firstBatchnsr$   )Z
batch_sizeZmax_await_time_msrI   r    r$   )r
   rA   r,   updater.   r-   Zmax_wire_versionr+   rP   Z_write_concern_forcommandrH   nameZcodec_optionsr6   rR   r4   r5   getrC   r2   rG   addressr0   r7   r3   Z_maybe_pin_connection)r9   rI   rL   rM   rN   cmdrP   rQ   rS   r)   Z
cmd_cursorr:   r:   r;   
get_cursor   sZ    





z_AggregationCommand.get_cursor)NNNN)__name__
__module____qualname____doc__r<   propertyrA   rC   rG   rH   rK   r\   r:   r:   r:   r;   r   &   s       "9r   c                   @  s\   e Zd ZU ded< eddddZeddddZd	dd
ddZeddddZdS )_CollectionAggregationCommandr   r+   rB   r=   c                 C  s   | j jS rJ   r+   rX   r@   r:   r:   r;   rA      s    z1_CollectionAggregationCommand._aggregation_targetc                 C  s   | j jS rJ   )r+   Z	full_namer@   r:   r:   r;   rC      s    z/_CollectionAggregationCommand._cursor_namespacerD   r)   r%   c                 C  s   | j S )rF   r+   )r9   r)   r:   r:   r;   rG      s    z0_CollectionAggregationCommand._cursor_collectionr   c                 C  s   | j jS rJ   )r+   Zdatabaser@   r:   r:   r;   rH      s    z'_CollectionAggregationCommand._databaseN)	r]   r^   r_   __annotations__ra   rA   rC   rG   rH   r:   r:   r:   r;   rb      s   
rb   c                      s&   e Zd Zdddd fddZ  ZS ) _CollectionRawAggregationCommandr   r   )argskwargsr%   c                   s&   t  j|| | js"d| jd d< d S )Nr   r)   r(   )superr<   r-   r.   )r9   rh   ri   	__class__r:   r;   r<      s    z)_CollectionRawAggregationCommand.__init__)r]   r^   r_   r<   __classcell__r:   r:   rk   r;   rg      s   rg   c                   @  s\   e Zd ZU ded< eddddZedddd	Zeddd
dZdddddZdS )_DatabaseAggregationCommandr   r+   intr=   c                 C  s   dS )N   r:   r@   r:   r:   r;   rA      s    z/_DatabaseAggregationCommand._aggregation_targetrB   c                 C  s   | j j dS )Nz.$cmd.aggregaterc   r@   r:   r:   r;   rC      s    z-_DatabaseAggregationCommand._cursor_namespacec                 C  s   | j S rJ   re   r@   r:   r:   r;   rH      s    z%_DatabaseAggregationCommand._databaserD   r   rd   c                 C  s$   | d| jdd\}}| j| S )rF   rU   .rp   )rY   rC   splitrH   )r9   r)   _Zcollnamer:   r:   r;   rG      s    z._DatabaseAggregationCommand._cursor_collectionN)	r]   r^   r_   rf   ra   rA   rC   rH   rG   r:   r:   r:   r;   rn      s   
rn   N)+r`   
__future__r   collections.abcr   r   r   typingr   r   r   r	   Zbson.sonr
   Zpymongor   Zpymongo.collationr   Zpymongo.errorsr   Zpymongo.read_preferencesr   r   Zpymongo.client_sessionr   Zpymongo.collectionr   Zpymongo.command_cursorr   Zpymongo.databaser   Zpymongo.poolr   r   Zpymongo.serverr   Zpymongo.typingsr   r   r   rb   rg   rn   r:   r:   r:   r;   <module>   s,    *	