o
    @&                     @   s   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	 d	e
Ze	ejG d
d dejZdS )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                   @   s.   e Zd ZdZdZdd Ze		dddZdS )	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                 C   s,   |\| _ | _| _| _| _}| _t|| _dS )z+Constructs a metadata container from a row.N)namecolumnskeystimeoutmodifiedversionbool
restricted)selfrowr    r   E/tmp/google-cloud-sdk/lib/googlecloudsdk/core/cache/metadata_table.py__init__8   s   zMetadata.__init__Nc                 C   s"   |durt |}|||||||fS )z:Constructs and returns a metadata table row from the args.N)int)clsr	   r
   r   r   r   r   r   r   r   r   Row>   s   zMetadata.Row)NNNNNNN)__name__
__module____qualname____doc__COLUMNSr   classmethodr   r   r   r   r   r   "   s    r   c                       s~   e Zd ZdZd fdd	Zejdd Zejd	d
 ZejdddZ	dd Z
dd Z		dddZdd ZdddZ  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.
  Tr   Nc                    s4   t t| j||||d d | _|| _d | _d | _d S )N)creater   r   )superr   r   	_metadata_table_class_restricted_tables)r   tabler	   r    r   r   	__class__r   r   r   R   s   

z CacheUsingMetadataTable.__init__c                 C      d S Nr   r   r   r   r   DeleteZ      zCacheUsingMetadataTable.Deletec                 C   r)   r*   r   r+   r   r   r   Commit^   r-   zCacheUsingMetadataTable.Commitc                 C   r)   r*   r   )r   commitr   r   r   Closeb   r-   zCacheUsingMetadataTable.Closec                 C   s   dS )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   r   r   r   _ImplementationCreateTablef   s   z2CacheUsingMetadataTable._ImplementationCreateTablec                 C   sp  |du rd}|dk rt d| j|||du r|}|dk s#||kr/t d| j||||du r6| j}| ||| | j| ||||d|d}| jrOd}nP|| _|	  |
tj|d}|re|d nd}	|	rt|	}
| jdu ry|
jpvd| _n| j|
jkrt d	| j|
j| j|
j| j| jdu r|
j| _d|_| j}| jtj|j|j|j|j|j|j|d
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   r1   r#   r"   ValidateSelectr   r   r   CacheVersionMismatchr   AddRowsr
   r   r   )r   r	   r   r
   r   r   r&   r   rowsr   metadatar   r   r   _CreateTablet   sr   



z$CacheUsingMetadataTable._CreateTableFc              	   C   s.  || j v rtd| j|| j|d}|rU|jsI|dur1||jkr1t	d| j|||j|durG||j
krGtd| j|||j
|S |sUtd| j|| jr| jtj|d}|rh|d nd}	|	rt|	}
| j| |
j|
j|
j
|
j|
j|
jdS |std| 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.r4   r   r3   )r$   r   CacheTableRestrictedr7   r	   r%   getdeletedr
   r6   r   r8   CacheTableNotFoundr"   r:   r   r   r#   r   r   r   r?   )r   r	   r    r   r
   r   r   r&   r=   r   r>   r   r   r   Table   sP   
zCacheUsingMetadataTable.Tablec                 C   s   | j ddtjddd dS )z2Initializes the metadata table and self._metadata.__metadata__Tr2   r   )r   r
   r   r   N)rD   r   r   r+   r   r   r   InitializeMetadata   s   
z*CacheUsingMetadataTable.InitializeMetadatac                 C   s$   | j tj|dd}dd |D S )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   c                 S   s   g | ]}t |jqS r   )r   r	   ).0r   r   r   r   
<listcomp>
  s    z2CacheUsingMetadataTable.Select.<locals>.<listcomp>)r"   r:   r   r   )r   r	   r=   r   r   r   r:      s   
zCacheUsingMetadataTable.Select)Tr   N)T)TFNNNr*   )r   r   r   r   r   abcabstractmethodr,   r.   r0   r1   r?   rD   rF   r:   __classcell__r   r   r'   r   r   G   s     	

M
9r   )r   
__future__r   r   r   rI   googlecloudsdk.core.cacher   r   sixobjectr   add_metaclassABCMetaCacher   r   r   r   r   <module>   s   
%