
    "                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	J
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\R$                  5      r " S S\R(                  5      rg)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                   l   ^  \ rS rSrSr  SU 4S jjrS rS rS rS r	S r
S	 rSS
 jrSS jrSrU =r$ )_Table'   a1  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           
        > S U l         [        [        U ]  XX4XVUS9  U(       a%  U R                  R
                  R                  U5        SU l         [        R                  " [        R                  R                  U R                  R                  U R                  U5      5      5      nU(       a6  [$        R&                  " U5       V	s/ s H  n	[)        U	5      PM     sn	U l         O/ U l         U R                  R*                  (       a  X R                  R,                  U'   g g ! [        R                   a    S nSU l         N[        R"                   a    e f = fs  sn	f )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   _Table.__init__4   s   DJ	&$ g)0,6 ! 8 
kk!!$'DL''
'',,t{{'')>
?Ah &*jj&:;&:E!H&:;djdj{{"&kk$  !! hdl;;  <s   AD! E!E Ec                     U R                  5         U R                  5         U R                  R                  R                  U R                  4/5        SU l        g)zDeletes the table.TN)
Invalidate
DeleteRowsr   r'   r   r   r)   s    r.   Delete_Table.DeleteN   s=    OOOOKK$$tyyl^4DL    c                    U R                   (       Ga  SU l         [        R                  R                  U R                  R
                  U R                  U R
                  5      5      nU R                  (       aq  SU l        U R                  R                  R                  U R
                  4/5        U R                  R                  U R
                  	  [        R                  " U5        gU R                  R                  R                  [         R"                  R%                  U R
                  U R&                  U R(                  U R*                  U R,                  U R.                  U R                  R0                  S9/5        [2        R4                  " U[6        R8                  " U R:                  5      5        gg! [         a)  nUR                  [        R                  :w  a  e  SnAgSnAff = f)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'   r2   r(   removeOSErrorerrnoENOENTAddRowsr   MetadataRowr   r   r   r   r   r8   r	   WriteFileContentsr$   dumpsr   )r)   r   es      r.   _Commit_Table._CommitV   sN   |||dlWW\\$++**DOODII,FGd	((499,8KK		*	
))D/ 	%%$$((YYYY??++ ) - .	/ 	djj&<=1   	WW$ %	s   F7 7
G*G%%G*c                 @    USU R                    USU R                    :H  $ )z/Returns True if rows a and b have the same key.Nr   )r)   abs      r.   	_RowEqual_Table._RowEqualr   s!    Zdii=AjtyyM))r6   c                 :   U(       a  [        [        U5      5       H|  nX   c  M
  [        X   [        R                  5      (       aE  [        X#   [        R                  5      (       a$  [
        R
                  " X#   X   5      (       d    gMp  X   X#   :w  d  M|    g   g)z)Returns True if row_template matches row.FT)r
   len
isinstancesixstring_typesfnmatch)r)   row_templaterowis       r.   	_RowMatch_Table._RowMatchv   s|    S&'!?&#*:*:;;!1!122??36<?;; <#&( ( r6   c                 F    U H  nU R                  X25      (       d  M    g   g)z:Returns True if any template in row_templates matches row.TF)rT   )r)   row_templatesrR   rQ   s       r.   _AnyRowMatch_Table._AnyRowMatch   s#    %		*	* & r6   c                   ^  T R                  U5        ST l        [        T R                  [	        U5      -   U 4S jS9n/ T l        SnU[        U5      :  a  U[        U5      S-
  :  aU  T R                  X   XS-      5      (       a7  US-  nU[        U5      S-
  :  a   T R                  X   XS-      5      (       a  M7  T R                  R                  X   5        US-  nU[        U5      :  a  M  gg)z#Adds each row in rows to the table.Tc                 "   > U S TR                    $ NrF   )xr)   s    r.   <lambda> _Table.AddRows.<locals>.<lambda>   s    :DIIr6   )keyr      N)
_CheckRowsr"   sortedr   listrL   rI   append)r)   rowsrS   s   `  r.   r=   _Table.AddRows   s    OODDL$**tDz)/FGDDJ	A
c$i- D	A$..$1u+"F"F	Q D	A$..$1u+"F"F
jj 1fa c$i-r6   c                     SU l         U(       aV  U R                  U5        / nU R                   H,  nU R                  X5      (       a  M  UR	                  U5        M.     X l        g/ U l        g)z@Deletes each row in the table matching any of the row_templates.TN)r"   _CheckRowTemplatesr   rX   re   )r)   rW   keeprR   s       r.   r2   _Table.DeleteRows   sX    DL
m,d#  44
++c
  jdjr6   c                    Ub  U R                  U/5        U(       df  U R                  (       dU  U R                  (       dD  [        R                  " SR                  U R                  R                  U R                  5      5      e/ nU R                   H5  nU(       d  M  U R                  X5      (       d  M$  UR                  U5        M7     U$ )z?Returns the list of rows that match row_template, None for all.z"[{}] cache table [{}] has expired.)ri   r   r   r   CacheTableExpiredformatr   r   r   rT   re   )r)   rQ   ignore_expirationmatchedrR   s        r.   Select_Table.Select   s    
|n-T__T]]((
.
5
5kk		+, , Gzz	22s  Nr6   )r   r"   r   )ra   ra   r   r   Fr\   )NF)__name__
__module____qualname____firstlineno____doc__r   r4   rC   rI   rT   rX   r=   r2   rq   __static_attributes____classcell__r-   s   @r.   r   r   '   sD    
 JK'4>8* r6   r   c                   F   ^  \ rS rSrSrSU 4S jjrS rS rS	S jrSr	U =r
$ )
Cache   a  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                   > [         [        U ]  [        XX4S9  Sn[	        U/5      U l        0 U l        S U l        [        R                  " 5       U l
        [        R                  R                  U R                  U5      U l        S U l        SU l        [        R                  R%                  U R                  5      (       d7  U(       d/  [&        R(                  " SR+                  U R                  5      5      eO[        R                  R%                  U R                  5      (       d/  [&        R,                  " SR+                  U R                  5      5      eSU l        [.        R0                  " U R                  SS9U l        U R                   R3                  5          U R5                  5         g ! [&        R6                   a    U R9                  SS	9  e f = f)
N)creater   r8   __lock__Fz Persistent cache [{}] not found.z[{}] is not a persistent cache.T   timeout_secscommit)r   r|   r   r   setr   r(   r'   r   Now_startr   r   r   r   
_lock_path_lock_persistentexistsr   CacheNotFoundrn   CacheInvalidr	   FileLockLockInitializeMetadatar#   Close)r)   r   r   r   r8   	lock_namer-   s         r.   r   Cache.__init__   sc   	%W   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                     U R                  SS9  U R                  (       a(  [        R                  " U R                  5        SU l        gg)z)Permanently deletes the persistent cache.Fr   N)r   r   r	   RmTreer   r3   s    r.   r4   Cache.Delete   s6    JJeJll499d r6   c                 "   U R                   (       df  [        R                  " U R                  S5        SU l        [
        R                  " U R                  SS9U l         U R                   R                  5         [        U R                  R                  5        Vs/ s H  oR                  (       d  M  UPM     sn5       H  nUR                  5         M     U R                  R                  (       a  U R                  R                  5         ggs  snf )z(Commits all operations up to this point.i  Tr   r   N)r   r   mkdirr   r   r	   r   r   r   rd   r(   valuesr"   rC   r'   )r)   r]   tables      r.   CommitCache.Commit   s    ::hhtyy% d>>$//Bdj
jjoo$,,"5"5"7E"7Q99q"7EFmmo G~~
nn  Fs   D1Dc                     U(       a  U R                  5         U R                  (       a!  U R                  R                  5         SU l        SU l        SU l        g)zrCloses the cache, optionally committing any changes.

Args:
  commit: Commits any changes before closing if True.
N)r   r   Unlockr'   r(   )r)   r   s     r.   r   Cache.Close   s<     
kkmzz
jjdjDNDLr6   )r   r   r'   r   r   r   r(   )TNN)T)rs   rt   ru   rv   rw   r   r4   r   r   rx   ry   rz   s   @r.   r|   r|      s!    "< r6   r|   )rw   
__future__r   r   r   r;   rP   r$   r   googlecloudsdk.core.cacher   r   r   googlecloudsdk.core.utilr	   rN   	six.movesr
   Tabler   CacheUsingMetadataTabler|    r6   r.   <module>r      s]    
 '  '    	 0 4 ; * 
 K"(( K\PN22 Pr6   