
    @&                         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SKJr  SSK	r	 " S S	\
5      r\	R                  " \R                  5       " S
 S\R                  5      5       rg)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                   :    \ rS rSrSrSrS r\  SS j5       rSr	g)	Metadata"   a"  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                 r    Uu  U l         U l        U l        U l        U l        o l        [        U5      U l        g)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__Metadata.__init__8   s1     "%TYdit}:&DO    Nc                 *    Ub  [        U5      nXX4XVU4$ )z:Constructs and returns a metadata table row from the args.)int)clsr   r   r   r   r   r   r   s           r   RowMetadata.Row>   s"     z?j4(HHr   )r   r   r   r   r   r   r   )NNNNNNN)
__name__
__module____qualname____firstlineno____doc__COLUMNSr   classmethodr   __static_attributes__ r   r   r	   r	   "   s/    & '' ;?26I Ir   r	   c                      ^  \ rS rSrSrSU 4S jjr\R                  S 5       r\R                  S 5       r	\R                  SS j5       r
S rS r  SS	 jrS
 rSS jrSrU =r$ )CacheUsingMetadataTableG   zA 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                 `   > [         [        U ]  X#XES9  S U l        Xl        S U l        S U l        g )N)creater   r   )superr*   r   	_metadata_table_class_restricted_tables)r   tabler   r-   r   r   	__class__s         r   r    CacheUsingMetadataTable.__init__R   s9    	
!41W 2 ?DNDDLr   c                     g Nr(   r   s    r   DeleteCacheUsingMetadataTable.DeleteZ       r   c                     g r7   r(   r8   s    r   CommitCacheUsingMetadataTable.Commit^   r;   r   c                     g r7   r(   )r   commits     r   CloseCacheUsingMetadataTable.Closeb   r;   r   c                     g)a:  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   _ImplementationCreateTable2CacheUsingMetadataTable._ImplementationCreateTablef   s     	r   c                    Uc  SnUS:  a0  [         R                  " SR                  U R                  X5      5      eUc  UnUS:  d  XC:  a1  [         R                  " SR                  U R                  XU5      5      eUc  U R
                  nU R                  XU5        U R                  U UUUUSUS9nU R                  (       a  SnGO&X`l        UR                  5         UR                  [        R                  US95      nU(       a  US   OSn	U	(       a  [        U	5      n
U R                  c  U
R                  =(       d    SU l        OuU R                  U
R                  :w  a[  [         R                  " S	R                  U R                  U
R                  U R                  5      U
R                  U R                  5      eU R
                  c  U
R
                  U l        SUl        U R                  nU R                  R!                  [        R                  UR                  UR"                  UR$                  UR
                  UR                  UR&                  US
9/5        U$ )a9  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   rD   r0   r/   ValidateSelectr	   r   r   CacheVersionMismatchr   AddRowsr   r   r   )r   r   r   r   r   r   r3   r   rowsr   metadatas              r   _CreateTable$CacheUsingMetadataTable._CreateTablet   s   0 g{//
;
B
Bii() ) |dax4>,,
J6$))T13 3 g##D48d#'&-#'&-'()3  5E ~~g nnn\\(,,D,12dDGc	C=<<!))/R$,\\X---//<CC))X--t||=. . <<!))$,engNNHLLZZZZ## )    Lr   c           
      ~   XR                   ;   a0  [        R                  " SR                  U R                  U5      5      eU R
                  R                  US5      nU(       a  UR                  (       d  UbJ  XGR                  :w  a;  [        R                  " SR                  U R                  XUR                  5      5      eUbJ  XWR                  :w  a;  [        R                  " SR                  U R                  XUR                  5      5      eU$ U(       d0  [        R                  " SR                  U R                  U5      5      eU R                  (       a  U R                  R                  [        R!                  US95      nU(       a  US   OSn	U	(       a\  [        U	5      n
U R#                  U U
R                  U
R                  U
R                  U
R$                  U
R&                  U
R(                  S9$ U(       d0  [        R                  " SR                  U R                  U5      5      eU R+                  XXEU5      $ )	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.rI   r   rH   )r1   r   CacheTableRestrictedrL   r   r2   getdeletedr   rK   r   rM   CacheTableNotFoundr/   rO   r	   r   r0   r   r   r   rT   )r   r   r-   r   r   r   r   r3   rR   r   rS   s              r   TableCacheUsingMetadataTable.Table   s   , ++
0
7
7		4
HJ JLLT4(E]]7mm#;33Nvdii>@ @ 

 200KvdiiUZZ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TwgFFr   c                 F    U R                  SS[        R                  SSS9  g)z2Initializes the metadata table and self._metadata.__metadata__TrG   r   )r   r   r   r   N)r[   r	   r%   r8   s    r   InitializeMetadata*CacheUsingMetadataTable.InitializeMetadata   s$    JJ~$8H8Hq  "r   c                     U R                   R                  [        R                  USS95      nU Vs/ s H  n[        U5      R                  PM     sn$ s  snf )zReturns 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/   rO   r	   r   r   )r   r   rR   r   s       r   rO   CacheUsingMetadataTable.Select   sG     >>  4E!JKD*./$3HSM$///s   A)r/   r1   r0   r2   r   r   )Tr   N)T)TFNNNr7   )r    r!   r"   r#   r$   r   abcabstractmethodr9   r=   rA   rD   rT   r[   r_   rO   r'   __classcell__)r4   s   @r   r*   r*   G   s     	 	 	 	 	 		KZ KO7Gr"
0 0r   r*   )r$   
__future__r   r   r   rc   googlecloudsdk.core.cacher   r   sixobjectr	   add_metaclassABCMetaCacher*   r(   r   r   <module>rm      sd     '  ' 
 0 ; 
"Iv "IJ 3;;B0399 B0  B0r   