
                         p    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ dZdZ	 G d	 d
      Z
 G d d      Zy)zImplements a buffer for tasks used in task_graph_executor.

See go/parallel-processing-in-gcloud-storage for more information.
    )absolute_import)division)unicode_literalsN)queuezBuffer Contents:
zTask Buffer is empty.c                       e Zd ZdZd Zd Zy)_PriorityWrappera  Wraps a buffered task and tracks priority information.

  Attributes:
    task (Union[task.Task, str]): A buffered item. Expected to be a task or a
      string (to handle shutdowns) when used by task_graph_executor.
    priority (int): The priority of this task. A task with a lower value will be
      executed before a task with a higher value, since queue.PriorityQueue uses
      a min-heap.
  c                      || _         || _        y N)taskpriority)selfr   r   s      ;lib/googlecloudsdk/command_lib/storage/tasks/task_buffer.py__init__z_PriorityWrapper.__init__,   s    DIDM    c                 4    | j                   |j                   k  S r
   )r   )r   others     r   __lt__z_PriorityWrapper.__lt__0   s    ==5>>))r   N)__name__
__module____qualname____doc__r   r    r   r   r   r   !   s    *r   r   c                   6    e Zd ZdZd Zd Zd	dZdefdZd Z	y)

TaskBuffera'  Stores and prioritizes tasks.

  The current implementation uses a queue.PriorityQueue under the hood, since
  in experiments we found that the heap it maintains did not add too much
  overhead. If it does end up being a bottleneck, the same API can be
  implemented with a collections.deque.
  c                 6    t        j                         | _        y r
   )r   PriorityQueue_queuer   s    r   r   zTaskBuffer.__init__=   s    %%'DKr   c                 J    | j                   j                         j                  S )a6  Removes and returns an item from the buffer.

    Calls to `get` block if there are no elements in the queue, and return
    prioritized items before non-prioritized items.

    Returns:
      A buffered item. Expected to be a task or a string (to handle shutdowns)
      when used by task_graph_executor.
    )r   getr   r   s    r   r    zTaskBuffer.get@   s     ;;??!!!r   c                 ^    |rdnd}t        ||      }| j                  j                  |       y)aH  Adds an item to the buffer.

    Args:
      task (Union[task.Task, str]): A buffered item. Expected to be a task or a
        string (to handle shutdowns) when used by task_graph_executor.
      prioritize (bool): Tasks added with prioritize=True will be returned by
        `get` before tasks added with prioritize=False.
    r      N)r   r   put)r   r   
prioritizer   prioritized_items        r   r#   zTaskBuffer.putL   s+     qAH'h7KKOO$%r   returnc                 6    | j                   j                         S )z*Returns the number of items in the buffer.)r   qsizer   s    r   sizezTaskBuffer.sizeY   s    ;;r   c                 *   | j                         dk(  rt        S t        g}t        j                  | j
                  j                        }|r8|j                  d      }|j                  t        |j                               |r8dj                  |      S )z.Returns a string representation of the buffer.r   
)r)   BUFFER_EMPTY_MESSAGEBUFFER_HEADERcopydeepcopyr   r   popappendstrr   join)r   output_lines
temp_queuepriority_wrappers       r   __str__zTaskBuffer.__str__]   sy    yy{a!! "?Lt{{001J
#*#.3345  99\""r   N)F)
r   r   r   r   r   r    r#   intr)   r7   r   r   r   r   r   4   s'    (
"&C #r   r   )r   
__future__r   r   r   r.   	six.movesr   r-   r,   r   r   r   r   r   <module>r;      s<   
 '  '   %. * *&5# 5#r   