
                         p    S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SrSr	 " S	 S
5      r
 " S S5      rg)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                   $    \ rS rSrSrS rS rSrg)_PriorityWrapper!   a  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                     Xl         X l        g N)taskpriority)selfr   r   s      ;lib/googlecloudsdk/command_lib/storage/tasks/task_buffer.py__init___PriorityWrapper.__init__,   s    IM    c                 4    U R                   UR                   :  $ r   )r   )r   others     r   __lt___PriorityWrapper.__lt__0   s    ==5>>))r   )r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__ r   r   r   r   !   s    *r   r   c                   B    \ rS rSrSrS rS rSS jrS\4S jr	S r
S	rg
)
TaskBuffer4   a  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                 8    [         R                  " 5       U l        g r   )r   PriorityQueue_queuer   s    r   r   TaskBuffer.__init__=   s    %%'DKr   c                 J    U R                   R                  5       R                  $ )a  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'   TaskBuffer.get@   s     ;;??!!!r   c                 f    U(       a  SOSn[        X5      nU R                  R                  U5        g)a0  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+   TaskBuffer.putL   s)     qAH'7KKOO$%r   returnc                 6    U R                   R                  5       $ )z*Returns the number of items in the buffer.)r#   qsizer$   s    r   sizeTaskBuffer.sizeY   s    ;;r   c                 D   U R                  5       S:X  a  [        $ [        /n[        R                  " U R
                  R                  5      nU(       a>  UR                  S5      nUR                  [        UR                  5      5        U(       a  M>  SR                  U5      $ )z.Returns a string representation of the buffer.r   
)r2   BUFFER_EMPTY_MESSAGEBUFFER_HEADERcopydeepcopyr#   r   popappendstrr   join)r   output_lines
temp_queuepriority_wrappers       r   __str__TaskBuffer.__str__]   s{    yy{a!! "?Lt{{001J
#*#.3345 * 99\""r   )r#   N)F)r   r   r   r   r   r   r'   r+   intr2   rA   r   r   r   r   r   r   4   s'    (
"&C #r   r   )r   
__future__r   r   r   r8   	six.movesr   r7   r6   r   r   r   r   r   <module>rF      s<   
 '  '   %. * *&5# 5#r   