simplify code

This commit is contained in:
bigcat88 2025-09-14 15:31:42 +03:00
parent 0b795dc7a7
commit a2ec1f7637
No known key found for this signature in database
GPG Key ID: 1F0BF0EC3CF22721
2 changed files with 10 additions and 23 deletions

View File

@ -1,3 +1,4 @@
import contextlib
import logging import logging
import os import os
from datetime import datetime from datetime import datetime
@ -291,10 +292,8 @@ async def compute_hash_and_dedup_for_cache_state(
state.asset_id = new_asset.id state.asset_id = new_asset.id
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=state.asset_id) await remove_missing_tag_for_asset_id(session, asset_id=state.asset_id)
except Exception:
pass
await session.flush() await session.flush()
return state.asset_id return state.asset_id
@ -311,15 +310,12 @@ async def compute_hash_and_dedup_for_cache_state(
duplicate_asset_id=this_asset.id, duplicate_asset_id=this_asset.id,
canonical_asset_id=canonical.id, canonical_asset_id=canonical.id,
) )
# Refresh state after the merge
state = await session.get(AssetCacheState, state_id) state = await session.get(AssetCacheState, state_id)
if state: if state:
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=canonical.id) await remove_missing_tag_for_asset_id(session, asset_id=canonical.id)
except Exception:
pass
await session.flush() await session.flush()
return canonical.id return canonical.id
@ -345,10 +341,8 @@ async def compute_hash_and_dedup_for_cache_state(
if state: if state:
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=canonical.id) await remove_missing_tag_for_asset_id(session, asset_id=canonical.id)
except Exception:
pass
await session.flush() await session.flush()
return canonical.id return canonical.id
# If we got here, the integrity error was not about hash uniqueness # If we got here, the integrity error was not about hash uniqueness
@ -357,10 +351,8 @@ async def compute_hash_and_dedup_for_cache_state(
# Claimed successfully # Claimed successfully
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=this_asset.id) await remove_missing_tag_for_asset_id(session, asset_id=this_asset.id)
except Exception:
pass
await session.flush() await session.flush()
return this_asset.id return this_asset.id
@ -370,10 +362,8 @@ async def compute_hash_and_dedup_for_cache_state(
this_asset.size_bytes = new_size this_asset.size_bytes = new_size
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=this_asset.id) await remove_missing_tag_for_asset_id(session, asset_id=this_asset.id)
except Exception:
pass
await session.flush() await session.flush()
return this_asset.id return this_asset.id
@ -393,10 +383,8 @@ async def compute_hash_and_dedup_for_cache_state(
state.asset_id = target_id state.asset_id = target_id
state.mtime_ns = mtime_ns state.mtime_ns = mtime_ns
state.needs_verify = False state.needs_verify = False
try: with contextlib.suppress(Exception):
await remove_missing_tag_for_asset_id(session, asset_id=target_id) await remove_missing_tag_for_asset_id(session, asset_id=target_id)
except Exception:
pass
await session.flush() await session.flush()
return target_id return target_id

View File

@ -366,16 +366,15 @@ async def touch_asset_info_by_id(
asset_info_id: str, asset_info_id: str,
ts: Optional[datetime] = None, ts: Optional[datetime] = None,
only_if_newer: bool = True, only_if_newer: bool = True,
) -> int: ) -> bool:
ts = ts or utcnow() ts = ts or utcnow()
stmt = sa.update(AssetInfo).where(AssetInfo.id == asset_info_id) stmt = sa.update(AssetInfo).where(AssetInfo.id == asset_info_id)
if only_if_newer: if only_if_newer:
stmt = stmt.where( stmt = stmt.where(
sa.or_(AssetInfo.last_access_time.is_(None), AssetInfo.last_access_time < ts) sa.or_(AssetInfo.last_access_time.is_(None), AssetInfo.last_access_time < ts)
) )
stmt = stmt.values(last_access_time=ts) stmt = stmt.values(last_access_time=ts).returning(AssetInfo.id)
res = await session.execute(stmt) return (await session.execute(stmt)).scalar_one_or_none() is not None
return int(res.rowcount or 0)
async def delete_asset_info_by_id(session: AsyncSession, *, asset_info_id: str, owner_id: str) -> bool: async def delete_asset_info_by_id(session: AsyncSession, *, asset_info_id: str, owner_id: str) -> bool: