
    \j                     x    d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ d Zd Z G d	 d
ee	      Zy)zQ
This submodule contains support code for writing feature store implementations.
    )DictMappingAny)ExpiringDict)DiagnosticDescriptionFeatureStoreFeatureStoreCore)VersionedDataKind)CacheConfigc                 H    t        |t              r|S | j                  |      S N)
isinstancedictencode)kinditems     H/root/env/lib/python3.12/site-packages/ldclient/feature_store_helpers.py_ensure_encodedr      s    dD)4@t{{4/@@    c                 4    | d uxr | j                  d      du S )NdeletedT)get)r   s    r   _is_deletedr      s     t; 3t ;;r   c                       e Zd ZdZdZdedefdZdee	ee
eeef   f   f   fdZd fd	Zd
 fdZd Zd Zedefd       Zd Zed        Zed        Zed        Zy)CachingStoreWrappera  A partial implementation of :class:`ldclient.interfaces.FeatureStore`.

    This class delegates the basic functionality to an implementation of
    :class:`ldclient.interfaces.FeatureStoreCore` - while adding optional caching behavior and other logic
    that would otherwise be repeated in every feature store implementation. This makes it easier to create
    new database integrations by implementing only the database-specific logic. 
    z$initedcorecache_configc                     || _         |j                  r.t        |j                  |j                        | _        d| _        yd| _        d| _        y)zConstructs an instance by wrapping a core implementation object.

        :param core: the implementation object
        :param cache_config: the caching parameters
        )max_lenmax_age_secondsNF)_coreenabledr   capacity
expiration_cache_inited)selfr   r   s      r   __init__zCachingStoreWrapper.__init__   sH     
&|/D/DVbVmVmnDK  DKr   all_encoded_datac                    | j                   j                  |       | j                  | j                  j                          |j	                         D ]  \  }}i }|j	                         D ]G  \  }}|j                  |      }|g| j                  | j                  ||      <   t        |      rC|||<   I || j                  | j                  |      <    d| _	        y)	
        NT)
r!   init_internalr%   clearitemsdecode_item_cache_keyr   _all_cache_keyr&   )r'   r)   r   r.   decoded_itemskeyr   decoded_items           r   initzCachingStoreWrapper.init)   s     	

  !12;;"KK/557 Ge "!& :IC#';;t#4LDP>DKK 4 4T3 ?@&|4-9c*	:
 :GD//56G r   c                     | S r    xs    r   <lambda>zCachingStoreWrapper.<lambda>9   s     r   c                    | j                   M| j                  ||      }| j                   j                  |      }||d   } |t        |      rd      S |      S | j                  j                  ||      }|dn|j                  |      }| j                   |g| j                   <    |t        |      rd      S |      S )r+   Nr   )r%   r0   r   r   r!   get_internalr/   )r'   r   r3   callback	cache_keycached_itemr   encoded_items           r   r   zCachingStoreWrapper.get9   s     ;;",,T37I++//)4K&"1~D(9DDtDDzz..tS9#+t\1J;;"&*VDKK	"D 1<<t<<r   c                     | S r   r7   r8   s    r   r:   zCachingStoreWrapper.<lambda>I   s    1 r   c                    | j                   6| j                  |      }| j                   j                  |      }| ||      S | j                  j	                  |      }i }|,|j                         D ]  \  }}|j                  |      ||<    | j                  |      }	| j                   |	| j                   <    ||	      S )r+   )r%   r1   r   r!   get_all_internalr.   r/   _items_if_not_deleted)
r'   r   r=   r>   cached_itemsencoded_items	all_itemsr3   r   r.   s
             r   allzCachingStoreWrapper.allI   s     ;;"++D1I;;??95L'--

33D9	$*002 3	T!%T!2	#3**95;;"%*DKK	"r   c                 4    ||dd}| j                  ||       y)r+   T)r3   versionr   N)upsert)r'   r   r3   rJ   deleted_items        r   deletezCachingStoreWrapper.delete[   s     !$DJD,'r   c                 D   t        ||      }| j                  j                  ||      }|j                  |      }| j                  [|g| j                  | j                  ||j                  d            <   | j                  j                  | j                  |      d       yy)r+   Nr3   )	r   r!   upsert_internalr/   r%   r0   r   popr1   )r'   r   r@   	new_statenew_decoded_items        r   rK   zCachingStoreWrapper.upserta   s     't\:JJ..t\B	;;y1;;"TdSeDKK,,T3C3G3G3NOPKKOOD//5t< #r   returnc                 h   | j                   ry| j                  $t        | j                  j	                               }nk| j                  j                  t        j                        }|@t        | j                  j	                               }|| j                  t        j                  <   |rd| _         |S )r+   T)r&   r%   boolr!   initialized_internalr   r   __INITED_CACHE_KEY__)r'   results     r   initializedzCachingStoreWrapper.initializedk   s     <<;;$**99;<F[[__%8%M%MNF~djj==?@HN/DDEDLr   c                 z    t        t        | j                  dd             r| j                  j                  |      S y)Ndescribe_configurationcustom)callablegetattrr!   r[   )r'   configs     r   r[   z*CachingStoreWrapper.describe_configuration|   s0    GDJJ(@$GH::44V<<r   c                 :    dj                  | j                  |      S )Nz{0}:{1})format	namespace)r   r3   s     r   r0   z#CachingStoreWrapper._item_cache_key   s    44r   c                     | j                   S r   )rb   )r   s    r   r1   z"CachingStoreWrapper._all_cache_key   s    ~~r   c                 n    i }| 0| j                         D ]  \  }}|j                  dd      r|||<    |S )Nr   F)r.   r   )r.   resultsr3   r   s       r   rD   z)CachingStoreWrapper._items_if_not_deleted   sD    "[[] (	Txx	51#'GCL( r   N)__name__
__module____qualname____doc__rW   r	   r   r(   r   r
   strr   r   r5   r   rH   rM   rK   propertyrU   rY   r[   staticmethodr0   r1   rD   r7   r   r   r   r      s     %- [ W->TRUWZRZ^H[@\-\%]   '2 =  "- $(= T   
 5 5    r   r   N)ri   typingr   r   r   expiringdictr   ldclient.interfacesr   r   r	   ldclient.versioned_data_kindr
   ldclient.feature_storer   r   r   r   r7   r   r   <module>rr      s=    & % % U U : .A<~/ ~r   