
    HE^j                     :    d dl Z d dlmZ  G d d      Z e       Zy)    N)db_connectionc                   V    e Zd ZdZd
dZd
dZdeddfdZdeddfdZde	eef   fd	Z
y)CatalogStatea1  Thread-safe in-memory cursor over stock_history/price_history's AUTOINCREMENT
    ids. The scraper thread and HTTP handler threads share one process (see
    backend/main.py), so this lets GET /api/last_updated answer "did anything
    change" with zero SQLite queries instead of re-scanning either table.returnNc                 R    t        j                         | _        d| _        d| _        y )Nr   )	threadingLock_lock_latest_stock_id_latest_price_idselfs    0/home/container/backend/scraper/catalog_state.py__init__zCatalogState.__init__   s     ^^%
 ! !    c                 `   t               5 }|j                         }|j                  d       |j                         d   xs d}|j                  d       |j                         d   xs d}d d d        | j                  5  | _        | _        d d d        y # 1 sw Y   -xY w# 1 sw Y   y xY w)Nz!SELECT MAX(id) FROM stock_historyr   z!SELECT MAX(id) FROM price_history)r   cursorexecutefetchoner
   r   r   )r   connr   	stock_max	price_maxs        r   init_from_dbzCatalogState.init_from_db   s    _[[]FNN>?)!,1INN>?)!,1I  ZZ$-D!$-D! Z _ Zs   A!B B$B!$B-new_idc                 r    | j                   5  || j                  kD  r|| _        d d d        y # 1 sw Y   y xY wN)r
   r   r   r   s     r   
bump_stockzCatalogState.bump_stock   )    ZZ---(.% ZZ   -6c                 r    | j                   5  || j                  kD  r|| _        d d d        y # 1 sw Y   y xY wr   )r
   r   r   s     r   
bump_pricezCatalogState.bump_price    r   r    c                 v    | j                   5  | j                  | j                  fcd d d        S # 1 sw Y   y xY wr   )r
   r   r   r   s    r   snapshotzCatalogState.snapshot%   s'    ZZ(($*?*?? ZZs   /8)r   N)__name__
__module____qualname____doc__r   r   intr   r"   tupler$    r   r   r   r      sN    M
"
	./ / /
/ / /
@%S/ @r   r   )r   backend.db.connectionr   r   catalog_stater+   r   r   <module>r.      s!     /"@ "@J r   