|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2025 Atlan Pte. Ltd. |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from typing import TYPE_CHECKING, Any, Dict |
| 8 | + |
| 9 | +from pyatlan.errors import ErrorCode |
| 10 | +from pyatlan.model.assets import Asset |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from pyatlan.client.aio import AsyncAtlanClient |
| 14 | + |
| 15 | + |
| 16 | +class AsyncAbstractAssetCache(ABC): |
| 17 | + """ |
| 18 | + Async base class for reusable components that are common |
| 19 | + to all caches, where a cache is populated entry-by-entry. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, client: AsyncAtlanClient): |
| 23 | + self.client = client |
| 24 | + self.lock = asyncio.Lock() |
| 25 | + self.name_to_guid: Dict[str, str] = dict() |
| 26 | + self.guid_to_asset: Dict[str, Asset] = dict() |
| 27 | + self.qualified_name_to_guid: Dict[str, str] = dict() |
| 28 | + |
| 29 | + @abstractmethod |
| 30 | + async def lookup_by_guid(self, guid: str): |
| 31 | + """Abstract method to lookup asset by guid.""" |
| 32 | + |
| 33 | + @abstractmethod |
| 34 | + async def lookup_by_qualified_name(self, qualified_name: str): |
| 35 | + """Abstract method to lookup asset by qualified name.""" |
| 36 | + |
| 37 | + @abstractmethod |
| 38 | + async def lookup_by_name(self, name: Any): |
| 39 | + """Abstract method to lookup asset by name.""" |
| 40 | + |
| 41 | + @abstractmethod |
| 42 | + def get_name(self, asset: Asset): |
| 43 | + """Abstract method to get name from asset.""" |
| 44 | + |
| 45 | + def is_guid_known(self, guid: str) -> bool: |
| 46 | + """ |
| 47 | + Checks whether the provided Atlan-internal UUID is known. |
| 48 | + NOTE: will not refresh the cache itself to determine this. |
| 49 | +
|
| 50 | + :param guid: Atlan-internal UUID of the object |
| 51 | + :returns: `True` if the object is known, `False` otherwise |
| 52 | + """ |
| 53 | + return guid in self.guid_to_asset |
| 54 | + |
| 55 | + def is_qualified_name_known(self, qualified_name: str) -> bool: |
| 56 | + """ |
| 57 | + Checks whether the provided Atlan-internal ID string is known. |
| 58 | + NOTE: will not refresh the cache itself to determine this. |
| 59 | +
|
| 60 | + :param qualified_name: Atlan-internal ID string of the object |
| 61 | + :returns: `True` if the object is known, `False` otherwise |
| 62 | + """ |
| 63 | + return qualified_name in self.qualified_name_to_guid |
| 64 | + |
| 65 | + def is_name_known(self, name: str) -> bool: |
| 66 | + """ |
| 67 | + Checks whether the provided Atlan-internal ID string is known. |
| 68 | + NOTE: will not refresh the cache itself to determine this. |
| 69 | +
|
| 70 | + :param name: human-constructable name of the object |
| 71 | + :returns: `True` if the object is known, `False` otherwise |
| 72 | + """ |
| 73 | + return name in self.name_to_guid |
| 74 | + |
| 75 | + def cache(self, asset: Asset): |
| 76 | + """ |
| 77 | + Add an entry to the cache. |
| 78 | +
|
| 79 | + :param asset: to be cached |
| 80 | + """ |
| 81 | + name = asset and self.get_name(asset) |
| 82 | + if not all([name, asset.guid, asset.qualified_name]): |
| 83 | + return |
| 84 | + self.name_to_guid[name] = asset.guid # type: ignore[index] |
| 85 | + self.guid_to_asset[asset.guid] = asset # type: ignore[index] |
| 86 | + self.qualified_name_to_guid[asset.qualified_name] = asset.guid # type: ignore[index] |
| 87 | + |
| 88 | + async def _get_by_guid(self, guid: str, allow_refresh: bool = True): |
| 89 | + """ |
| 90 | + Retrieve an asset from the cache by its UUID. |
| 91 | +
|
| 92 | + :param guid: UUID of the asset in Atlan |
| 93 | + :param allow_refresh: whether to allow a refresh of the cache (`True`) or not (`False`) |
| 94 | + :returns: the asset (if found) |
| 95 | + :raises AtlanError: on any API communication problem if the cache needs to be refreshed |
| 96 | + :raises NotFoundError: if the object cannot be found (does not exist) in Atlan |
| 97 | + :raises InvalidRequestError: if no UUID was provided for the object to retrieve |
| 98 | + """ |
| 99 | + if not guid: |
| 100 | + raise ErrorCode.MISSING_GUID.exception_with_parameters() |
| 101 | + asset = self.guid_to_asset.get(guid) |
| 102 | + if not asset and allow_refresh: |
| 103 | + await self.lookup_by_guid(guid) |
| 104 | + asset = self.guid_to_asset.get(guid) |
| 105 | + if not asset: |
| 106 | + raise ErrorCode.ASSET_NOT_FOUND_BY_GUID.exception_with_parameters(guid) |
| 107 | + return asset |
| 108 | + |
| 109 | + async def _get_by_qualified_name( |
| 110 | + self, qualified_name: str, allow_refresh: bool = True |
| 111 | + ): |
| 112 | + """ |
| 113 | + Retrieve an asset from the cache by its qualifiedName. |
| 114 | +
|
| 115 | + :param qualified_name: qualifiedName of the asset in Atlan |
| 116 | + :param allow_refresh: whether to allow a refresh of the cache (`True`) or not (`False`) |
| 117 | + :returns: the asset (if found) |
| 118 | + :raises AtlanError: on any API communication problem if the cache needs to be refreshed |
| 119 | + :raises NotFoundError: if the object cannot be found (does not exist) in Atlan |
| 120 | + :raises InvalidRequestError: if no qualified name was provided for the object to retrieve |
| 121 | + """ |
| 122 | + if not qualified_name: |
| 123 | + raise ErrorCode.MISSING_QUALIFIED_NAME.exception_with_parameters() |
| 124 | + guid = self.qualified_name_to_guid.get(qualified_name) |
| 125 | + if not guid and allow_refresh: |
| 126 | + await self.lookup_by_qualified_name(qualified_name) |
| 127 | + guid = self.qualified_name_to_guid.get(qualified_name) |
| 128 | + if not guid: |
| 129 | + raise ErrorCode.ASSET_NOT_FOUND_BY_QN.exception_with_parameters( |
| 130 | + qualified_name |
| 131 | + ) |
| 132 | + |
| 133 | + return await self._get_by_guid(guid=guid, allow_refresh=False) |
| 134 | + |
| 135 | + async def _get_by_name(self, name, allow_refresh: bool = True): |
| 136 | + """ |
| 137 | + Retrieve an asset from the cache by its uniquely identifiable name. |
| 138 | +
|
| 139 | + :param name: uniquely identifiable name of the asset in Atlan |
| 140 | + :param allow_refresh: whether to allow a refresh of the cache (`True`) or not (`False`) |
| 141 | + :returns: the asset (if found) |
| 142 | + :raises AtlanError: on any API communication problem if the cache needs to be refreshed |
| 143 | + :raises NotFoundError: if the object cannot be found (does not exist) in Atlan |
| 144 | + :raises InvalidRequestError: if no name was provided for the object to retrieve |
| 145 | + """ |
| 146 | + from pyatlan.cache.abstract_asset_cache import AbstractAssetName |
| 147 | + |
| 148 | + if not isinstance(name, AbstractAssetName): |
| 149 | + raise ErrorCode.MISSING_NAME.exception_with_parameters() |
| 150 | + guid = self.name_to_guid.get(str(name)) |
| 151 | + if not guid and allow_refresh: |
| 152 | + await self.lookup_by_name(name) |
| 153 | + guid = self.name_to_guid.get(str(name)) |
| 154 | + if not guid: |
| 155 | + raise ErrorCode.ASSET_NOT_FOUND_BY_NAME.exception_with_parameters( |
| 156 | + name._TYPE_NAME, name |
| 157 | + ) |
| 158 | + |
| 159 | + return await self._get_by_guid(guid=guid, allow_refresh=False) |
0 commit comments