
    "                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	m
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  G d dej$                        Z G d dej(                        Zy)zlA persistent cache implementation using files.

See the persistent_cache module for a detailed description.
    )absolute_import)division)unicode_literalsN)
exceptions)metadata_table)persistent_cache_base)files)rangec                   \     e Zd ZdZ	 	 d fd	Zd Zd Zd Zd Zd Z	d Z
dd	Zdd
Z xZS )_TableaC  A persistent cache table.

  Attributes:
    name: The table name.
    deleted: Table was deleted if True.
    restricted: Table is restricted if True.
    modified: Table modify timestamp.
    timeout: Tables older than timeout are invalid.
    _cache: The parent cache object.
    _rows: The list of rows in the table.
  c           	         d | _         t        t        |   |||||||       |r%| j                  j
                  j                  |       d| _        	 t        j                  t        j                  j                  | j                  j                  | j                  |                  }|r2t%        j&                  |      D 	cg c]  }	t)        |	       c}	| _         ng | _         | j                  j*                  r| | j                  j,                  |<   y y # t        j                  $ r d }d| _        Y t        j"                  $ r  w xY wc c}	w )N)columnskeystimeoutmodified
restrictedFT)_rowssuperr   __init___cache_restrictedadddeletedr	   ReadFileContentsospathjoinname
EncodeNameMissingFileErrorchangedErrorjsonloadstuple	_metadata_tables)selfcacher   r   r   r   r   r   contentsr	__class__s             +lib/googlecloudsdk/core/cache/file_cache.pyr   z_Table.__init__4   s   DJ	&$ gD)08,6 ! 8 
kk!!$'DL''
'',,t{{'')>
?Ah &*jj&:;&:E!H&:;djdj{{"&dkk$  !! hdl;;  <s   AD E	E2Ec                     | j                          | j                          | j                  j                  j                  | j                  fg       d| _        y)zDeletes the table.TN)
Invalidate
DeleteRowsr   r&   r   r   r(   s    r-   Deletez_Table.DeleteN   s=    OOOOKK$$tyyl^4DL    c                    | j                   rd| _         t        j                  j                  | j                  j
                  | j                  | j
                              }| j                  rpd| _        | j                  j                  j                  | j
                  fg       | j                  j                  | j
                  = 	 t        j                  |       y| j                  j                  j                  t         j"                  j%                  | j
                  | j&                  | j(                  | j*                  | j,                  | j.                  | j                  j0                        g       t3        j4                  |t7        j8                  | j:                               yy# t        $ r(}|j                  t        j                  k7  r Y d}~yd}~ww xY w)z5Commits changed/deleted table data to the table file.FN)r   r   r   r   r   r   version)r!   r   r   r   r   r   r   r   r&   r0   r'   removeOSErrorerrnoENOENTAddRowsr   MetadataRowr   r   r   r   r   r5   r	   WriteFileContentsr#   dumpsr   )r(   r   es      r-   _Commitz_Table._CommitV   sL   ||dlWW\\$++**DOODII,FGd	((499,8KK		*	
))D/ 	%%$$((YYYY??++ ) - .	/ 	djj&<=1   	WW$ %	s   F- -	G6GGc                 @    |d| j                    |d| j                    k(  S )z/Returns True if rows a and b have the same key.Nr   )r(   abs      r-   	_RowEqualz_Table._RowEqualr   s!    Zdii=AjtyyM))r3   c                    |rt        t        |            D ]n  }||   	t        ||   t        j                        r<t        ||   t        j                        rt        j
                  ||   ||         r` y||   ||   k7  sn y y)z)Returns True if row_template matches row.FT)r
   len
isinstancesixstring_typesfnmatch)r(   row_templaterowis       r-   	_RowMatchz_Table._RowMatchv   s|    S&'!?&a#*:*:;Q!1!12??3q6<?;A#a&( ( r3   c                 :    |D ]  }| j                  ||      s y y)z:Returns True if any template in row_templates matches row.TF)rO   )r(   row_templatesrM   rL   s       r-   _AnyRowMatchz_Table._AnyRowMatch   s"    %	c	* & r3   c                      j                  |       d _        t         j                  t	        |      z    fd      }g  _        d}|t        |      k  r|t        |      dz
  k  rM j                  ||   ||dz            r2|dz  }|t        |      dz
  k  r j                  ||   ||dz            r2 j                  j                  ||          |dz  }|t        |      k  ryy)z#Adds each row in rows to the table.Tc                 "    | d j                    S NrB   )xr(   s    r-   <lambda>z _Table.AddRows.<locals>.<lambda>   s    :DIIr3   )keyr      N)
_CheckRowsr!   sortedr   listrG   rE   append)r(   rowsrN   s   `  r-   r:   z_Table.AddRows   s    OODDL$**tDz)/FGDDJ	A
c$i- D	A$..a$q1u+"F	Q D	A$..a$q1u+"F
jjQ 1fa c$i-r3   c                     d| _         |rP| j                  |       g }| j                  D ]&  }| j                  ||      r|j	                  |       ( || _        yg | _        y)z@Deletes each row in the table matching any of the row_templates.TN)r!   _CheckRowTemplatesr   rR   r]   )r(   rQ   keeprM   s       r-   r0   z_Table.DeleteRows   sY    DL
m,d#  4
++c
  djdjr3   c                 \   || j                  |g       |s[| j                  sO| j                  sCt        j                  dj                  | j                  j                  | j                              g }| j                  D ])  }|s| j                  ||      s|j                  |       + |S )z?Returns the list of rows that match row_template, None for all.z"[{}] cache table [{}] has expired.)r`   r   r   r   CacheTableExpiredformatr   r   r   rO   r]   )r(   rL   ignore_expirationmatchedrM   s        r-   Selectz_Table.Select   s    
|n-T__T]]((
.
5
5kk		+, , Gzz	c2s  Nr3   )rY   rY   r   r   FrU   )NF)__name__
__module____qualname____doc__r   r2   r@   rE   rO   rR   r:   r0   rg   __classcell__r,   s   @r-   r   r   '   s?    
 JK'4>8*r3   r   c                   8     e Zd ZdZd fd	Zd Zd ZddZ xZS )Cachea  A persistent cache object.

  Attributes:
    name: The db path name. Created/removed by this object. May be a file or
      directory. In this implementation its a file.
    timeout: The default table timeout.
    version: A caller defined version string that must match the version string
      stored when the persistent object was created.
    _lock: The cache lock object. None if no files have been committed yet.
    _lock_path: The cache lock meta file.
    _metadata: The metadata restricted _Table.
    _persistent: True if the persistent object has been committed at least once.
    _restricted: The set of restricted table names.
    _start: The cache instance start time.
    _tables: The map of open table objects.
  c                    t         t        |   t        ||||       d}t	        |g      | _        i | _        d | _        t        j                         | _
        t        j                  j                  | j                  |      | _        d | _        d| _        t        j                  j%                  | j                        s0|st'        j(                  dj+                  | j                              t        j                  j%                  | j                        s.t'        j,                  dj+                  | j                              d| _        t/        j0                  | j                  d      | _        | j                   j3                          	 | j5                          y # t&        j6                  $ r | j9                  d	        w xY w)
N)creater   r5   __lock__Fz Persistent cache [{}] not found.z[{}] is not a persistent cache.T   timeout_secscommit)r   ro   r   r   setr   r'   r&   r   Now_startr   r   r   r   
_lock_path_lock_persistentexistsr   CacheNotFoundrd   CacheInvalidr	   FileLockLockInitializeMetadatar"   Close)r(   r   rq   r   r5   	lock_namer,   s         r-   r   zCache.__init__   sZ   	%VWg   GII;'DDLDN'++-DKggll499i8DODJD77>>$))$&&.55dii@B 	BWW^^DOO,##
+
2
2499
=? ? d>>$//Bdj
jjoo
 
jjjs   F 'Gc                     | j                  d       | j                  r't        j                  | j                         d| _        yy)z)Permanently deletes the persistent cache.Frv   N)r   r}   r	   RmTreer   r1   s    r-   r2   zCache.Delete   s6    JJeJll499d r3   c                    | j                   sgt        j                  | j                  d       d| _        t        j                  | j                  d      | _         | j                   j                          t        | j                  j                         D cg c]  }|j                  s| c}      D ]  }|j                           | j                  j                  r| j                  j                          yyc c}w )z(Commits all operations up to this point.i  Trs   rt   N)r|   r   mkdirr   r}   r	   r   r{   r   r\   r'   valuesr!   r@   r&   )r(   rV   tables      r-   CommitzCache.Commit   s    ::hhtyy% d>>$//Bdj
jjoo$,,"5"5"7E"7Q199q"7EFmmo G~~
nn  Fs   C9'C9c                     |r| j                          | j                  r!| j                  j                          d| _        d| _        d| _        y)z~Closes the cache, optionally committing any changes.

    Args:
      commit: Commits any changes before closing if True.
    N)r   r|   Unlockr&   r'   )r(   rw   s     r-   r   zCache.Close   s<     
kkmzz
jjdjDNDLr3   )TNN)T)	rh   ri   rj   rk   r   r2   r   r   rl   rm   s   @r-   ro   ro      s    "<r3   ro   )rk   
__future__r   r   r   r8   rK   r#   r   googlecloudsdk.core.cacher   r   r   googlecloudsdk.core.utilr	   rI   	six.movesr
   Tabler   CacheUsingMetadataTablero    r3   r-   <module>r      s]    
 '  '    	 0 4 ; * 
 K"(( K\PN22 Pr3   