
    @&                         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dlmZ ddl	Z	 G d d	e
      Z e	j                  ej                         G d
 dej                               Zy)zA persistent cache metadata table implementation layer.

Used by persistent cache implementations that maintain a metadata table to keep
track of cache tables.
    )absolute_import)division)unicode_literalsN)
exceptions)persistent_cache_basec                   0    e Zd ZdZdZd Ze	 	 dd       Zy)Metadataa@  Metadata table row container.

  This object encapsulates the persistent metadata table row layout.

  Attributes:
    name: The table name.
    columns: The number of columns in the table. Must be >= 1.
    keys: The number of columns, starting from 0, that form the primary
      row key. Must be 1 <= keys <= columns. The primary key is used to
      differentiate rows in the AddRows and DeleteRows methods.
    timeout: A float number of seconds. Tables older than (modified+timeout)
      are invalid. 0 means no timeout.
    modified: Table modify Now() (time.time()) value. 0 for expired tables.
    restricted: True if Table is restricted.
    version: A caller defined version string that must match the version string
      stored when the persistent object was created. '' for all but the
      metadata table itself.
     c                 t    |\  | _         | _        | _        | _        | _        }| _        t        |      | _        y)z+Constructs a metadata container from a row.N)namecolumnskeystimeoutmodifiedversionbool
restricted)selfrowr   s      /lib/googlecloudsdk/core/cache/metadata_table.py__init__zMetadata.__init__8   s3     "%TYdit}:&DO    Nc                 .    |t        |      }|||||||fS )z:Constructs and returns a metadata table row from the args.)int)clsr   r   r   r   r   r   r   s           r   RowzMetadata.Row>   s(     z?j'4(JHHr   )NNNNNNN)__name__
__module____qualname____doc__COLUMNSr   classmethodr    r   r   r	   r	   "   s/    & '' ;?26I Ir   r	   c                        e Zd ZdZd fd	Zej                  d        Zej                  d        Zej                  dd       Z	d Z
d Z	 	 ddZd	 Zdd
Z xZS )CacheUsingMetadataTablea
  A persistent cache metadata table implementation layer.

  Attributes:
    _metadata: A table containing a row for each table.
    _table_class: The cache Table class.
    _restricted: The set of restricted table names.
    _tables: The map of open table objects.
  c                 l    t         t        |   ||||       d | _        || _        d | _        d | _        y )N)creater   r   )superr%   r   	_metadata_table_class_restricted_tables)r   tabler   r'   r   r   	__class__s         r   r   z CacheUsingMetadataTable.__init__R   s?    	
!41VWg 2 ?DNDDDLr   c                      y Nr#   r   s    r   DeletezCacheUsingMetadataTable.DeleteZ       r   c                      y r0   r#   r1   s    r   CommitzCacheUsingMetadataTable.Commit^   r3   r   c                      y r0   r#   )r   commits     r   ClosezCacheUsingMetadataTable.Closeb   r3   r   c                      y)aZ  Implementation layer _CreateTable.

    The cache implementation object can override this method to do
    implementation specific table initialization.

    Args:
      name: The table name.
      columns: The number of columns in each row.
      keys: The number of columns, left to right, that are primary keys. 0 for
        all columns.
    Nr#   )r   r   r   r   s       r   _ImplementationCreateTablez2CacheUsingMetadataTable._ImplementationCreateTablef   s     	r   c                    |d}|dk  r0t        j                  dj                  | j                  ||            ||}|dk  s||kD  r1t        j                  dj                  | j                  |||            || j
                  }| j                  |||       | j                  | ||||d|      }| j                  rd}n|| _        |j                          |j                  t        j                  |            }|r|d   nd}	|	rt        |	      }
| j                  |
j                  xs d| _        ns| j                  |
j                  k7  rZt        j                  d	j                  | j                  |
j                  | j                        |
j                  | j                        | j
                  |
j
                  | _        d|_        | j                  }| j                  j!                  t        j                  |j                  |j"                  |j$                  |j
                  |j                  |j&                  |
      g       |S )a  Creates and returns a table object for name.

    NOTE: This code is conditioned on self._metadata. If self._metadata is None
    then we are initializing/updating the metadata table. The table name is
    relaxed, in particular '_' is allowed in the table name. This avoids user
    table name conflicts. Finally, self._metadata is set and the metadata
    table row is updated to reflect any changes in the default timeout.

    Args:
      name: The table name.
      restricted: Return a restricted table object.
      columns: The number of columns in each row.
      keys: The number of columns, left to right, that are primary keys. 0 for
        all columns.
      timeout: The number of seconds after last modification when the table
        becomes invalid. 0 for no timeout.

    Raises:
      CacheTableNameInvalid: If name is invalid.

    Returns:
      A table object for name.
    N   z/[{}] table [{}] column count [{}] must be >= 1.z>[{}] table [{}] primary key count [{}] must be >= 1 and <= {}.r   r   r   r   r   r   r   r    z,[{}] cache version [{}] does not match [{}].)r   r   r   r   r   r   r   )r   CacheTableColumnsInvalidformatr   CacheTableKeysInvalidr   r:   r*   r)   ValidateSelectr	   r   r   CacheVersionMismatchr   AddRowsr   r   r   )r   r   r   r   r   r   r-   r   rowsr   metadatas              r   _CreateTablez$CacheUsingMetadataTable._CreateTablet   s   0 g{//
;
B
Biiw() ) |dax4'>,,
J6$))T413 3 g##D'48d#'&-#'&-'()3  5E ~~g dnnn\\(,,D,12dDGc	C=<<!))/R$,\\X---//<CC))X--t||=. . <<!))$,engNNHLLZZZZ## )    Lr   c           	      <   || j                   v r/t        j                  dj                  | j                  |            | j
                  j                  |d      }|r|j                  s|J||j                  k7  r;t        j                  dj                  | j                  |||j                              |J||j                  k7  r;t        j                  dj                  | j                  |||j                              |S |s/t        j                  dj                  | j                  |            | j                  r| j                  j                  t        j!                  |            }|r|d   nd}	|	r_t        |	      }
| j#                  | |
j                  |
j                  |
j                  |
j$                  |
j&                  |
j(                        S |s/t        j                  dj                  | j                  |            | j+                  |||||      S )	a  Returns the Table object for existing table name.

    Args:
      name: The table name.
      create: If True creates the table if it does not exist.
      restricted: Return a restricted table object.
      columns: The number of columns in each row.
      keys: The number of columns, left to right, that are primary keys. 0 for
        all columns.
      timeout: The number of seconds after last modification when the table
        becomes invalid. 0 for no timeout. If None then the default cache
        timeout is assumed.

    Raises:
      CacheTableNameInvalid: name is not a valid table name.
      CacheTableNotFound: If the table does not exist.

    Returns:
      A Table object for name.
    z$[{}] cache table [{}] is restricted.Nz>[{}] cache table [{}] columns [{}] does not match existing {}.z;[{}] cache table [{}] keys [{}] does not match existing {}.z [{}] cache table [{}] not found.r>   r   r=   )r+   r   CacheTableRestrictedrA   r   r,   getdeletedr   r@   r   rB   CacheTableNotFoundr)   rD   r	   r   r*   r   r   r   rI   )r   r   r'   r   r   r   r   r-   rG   r   rH   s              r   TablezCacheUsingMetadataTable.Table   s   , t++
0
7
7		4
HJ JLLT4(E]]7emm#;33Nvdiiw>@ @ 

 200KvdiitUZZ8: : ++.55diiFH 	H~~^^""8<<T<#:;dDGc	C=  &.mm)1)9)9&.mm)1)9)9*2*;*;,4,?,? ! A 	A ))
,
3
3DIIt
DF FT:wgFFr   c                 L    | j                  ddt        j                  dd       y)z2Initializes the metadata table and self._metadata.__metadata__Tr<   r   )r   r   r   r   N)rO   r	   r!   r1   s    r   InitializeMetadataz*CacheUsingMetadataTable.InitializeMetadata   s$    JJ~$8H8Hq  "r   c                     | j                   j                  t        j                  |d            }|D cg c]  }t        |      j                   c}S c c}w )a  Returns the list of unrestricted table names matching name.

    Args:
      name: The table name pattern. None for all unrestricted tables. May
        contain the * and ? pattern match characters.

    Returns:
      The list of unrestricted table names matching name.
    F)r   r   )r)   rD   r	   r   r   )r   r   rG   r   s       r   rD   zCacheUsingMetadataTable.Select   sG     >>  4E!JKD*./$3HSM$///s   A)Tr   N)T)TFNNNr0   )r   r   r   r    r   abcabstractmethodr2   r5   r8   r:   rI   rO   rR   rD   __classcell__)r.   s   @r   r%   r%   G   s     	 	 	 	 	 		KZ KO7Gr"
0r   r%   )r    
__future__r   r   r   rT   googlecloudsdk.core.cacher   r   sixobjectr	   add_metaclassABCMetaCacher%   r#   r   r   <module>r^      sf     '  ' 
 0 ; 
"Iv "IJ 3;;B0399 B0  B0r   