Skip to content
Snippets Groups Projects
Verified Commit f8cd21fc authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: `cached_query()` now also caches uniqueness related exceptions.

parent 603af10b
No related branches found
No related tags found
2 merge requests!130Release v0.14.0,!128exception caching
Pipeline #47041 passed
...@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### ### Changed ###
* `cached_query()` now also caches uniqueness related exceptions.
### Deprecated ### ### Deprecated ###
### Removed ### ### Removed ###
......
...@@ -36,6 +36,7 @@ from enum import Enum ...@@ -36,6 +36,7 @@ from enum import Enum
from functools import lru_cache from functools import lru_cache
from typing import Union from typing import Union
from .exceptions import EmptyUniqueQueryError, QueryNotUniqueError
from .utils import get_entity from .utils import get_entity
from .common.models import execute_query, Entity, Container from .common.models import execute_query, Entity, Container
...@@ -80,16 +81,22 @@ If a query phrase is given, the result must be unique. If this is not what you ...@@ -80,16 +81,22 @@ If a query phrase is given, the result must be unique. If this is not what you
if count != 1: if count != 1:
raise ValueError("You must supply exactly one argument.") raise ValueError("You must supply exactly one argument.")
result = (None, )
if eid is not None: if eid is not None:
return _cached_access(AccessType.EID, eid, unique=True) result = _cached_access(AccessType.EID, eid, unique=True)
if name is not None: if name is not None:
return _cached_access(AccessType.NAME, name, unique=True) result = _cached_access(AccessType.NAME, name, unique=True)
if path is not None: if path is not None:
return _cached_access(AccessType.PATH, path, unique=True) result = _cached_access(AccessType.PATH, path, unique=True)
if query is not None: if query is not None:
return _cached_access(AccessType.QUERY, query, unique=True) result = _cached_access(AccessType.QUERY, query, unique=True)
raise ValueError("Not all arguments may be None.") if result != (None, ):
if isinstance(result, (QueryNotUniqueError, EmptyUniqueQueryError)):
raise result
return result
raise RuntimeError("This line should never be reached.")
def cached_query(query_string) -> Container: def cached_query(query_string) -> Container:
...@@ -98,7 +105,10 @@ def cached_query(query_string) -> Container: ...@@ -98,7 +105,10 @@ def cached_query(query_string) -> Container:
All additional arguments are at their default values. All additional arguments are at their default values.
""" """
return _cached_access(AccessType.QUERY, query_string, unique=False) result = _cached_access(AccessType.QUERY, query_string, unique=False)
if isinstance(result, (QueryNotUniqueError, EmptyUniqueQueryError)):
raise result
return result
@lru_cache(maxsize=DEFAULT_SIZE) @lru_cache(maxsize=DEFAULT_SIZE)
...@@ -111,14 +121,17 @@ def _cached_access(kind: AccessType, value: Union[str, int], unique=True): ...@@ -111,14 +121,17 @@ def _cached_access(kind: AccessType, value: Union[str, int], unique=True):
if value in _DUMMY_CACHE: if value in _DUMMY_CACHE:
return _DUMMY_CACHE[value] return _DUMMY_CACHE[value]
if kind == AccessType.QUERY: try:
return execute_query(value, unique=unique) if kind == AccessType.QUERY:
if kind == AccessType.NAME: return execute_query(value, unique=unique)
return get_entity.get_entity_by_name(value) if kind == AccessType.NAME:
if kind == AccessType.EID: return get_entity.get_entity_by_name(value)
return get_entity.get_entity_by_id(value) if kind == AccessType.EID:
if kind == AccessType.PATH: return get_entity.get_entity_by_id(value)
return get_entity.get_entity_by_path(value) if kind == AccessType.PATH:
return get_entity.get_entity_by_path(value)
except (QueryNotUniqueError, EmptyUniqueQueryError) as exc:
return exc
raise ValueError(f"Unknown AccessType: {kind}") raise ValueError(f"Unknown AccessType: {kind}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment