Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
f8cd21fc
Verified
Commit
f8cd21fc
authored
1 year ago
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
ENH: `cached_query()` now also caches uniqueness related exceptions.
parent
603af10b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!130
Release v0.14.0
,
!128
exception caching
Pipeline
#47041
passed
1 year ago
Stage: code_style
Stage: linting
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CHANGELOG.md
+2
-0
2 additions, 0 deletions
CHANGELOG.md
src/linkahead/cached.py
+27
-14
27 additions, 14 deletions
src/linkahead/cached.py
with
29 additions
and
14 deletions
CHANGELOG.md
+
2
−
0
View file @
f8cd21fc
...
...
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ###
*
`cached_query()`
now also caches uniqueness related exceptions.
### Deprecated ###
### Removed ###
...
...
This diff is collapsed.
Click to expand it.
src/linkahead/cached.py
+
27
−
14
View file @
f8cd21fc
...
...
@@ -36,6 +36,7 @@ from enum import Enum
from
functools
import
lru_cache
from
typing
import
Union
from
.exceptions
import
EmptyUniqueQueryError
,
QueryNotUniqueError
from
.utils
import
get_entity
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
if
count
!=
1
:
raise
ValueError
(
"
You must supply exactly one argument.
"
)
result
=
(
None
,
)
if
eid
is
not
None
:
re
turn
_cached_access
(
AccessType
.
EID
,
eid
,
unique
=
True
)
re
sult
=
_cached_access
(
AccessType
.
EID
,
eid
,
unique
=
True
)
if
name
is
not
None
:
re
turn
_cached_access
(
AccessType
.
NAME
,
name
,
unique
=
True
)
re
sult
=
_cached_access
(
AccessType
.
NAME
,
name
,
unique
=
True
)
if
path
is
not
None
:
re
turn
_cached_access
(
AccessType
.
PATH
,
path
,
unique
=
True
)
re
sult
=
_cached_access
(
AccessType
.
PATH
,
path
,
unique
=
True
)
if
query
is
not
None
:
re
turn
_cached_access
(
AccessType
.
QUERY
,
query
,
unique
=
True
)
re
sult
=
_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
:
...
...
@@ -98,7 +105,10 @@ def cached_query(query_string) -> Container:
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
)
...
...
@@ -111,14 +121,17 @@ def _cached_access(kind: AccessType, value: Union[str, int], unique=True):
if
value
in
_DUMMY_CACHE
:
return
_DUMMY_CACHE
[
value
]
if
kind
==
AccessType
.
QUERY
:
return
execute_query
(
value
,
unique
=
unique
)
if
kind
==
AccessType
.
NAME
:
return
get_entity
.
get_entity_by_name
(
value
)
if
kind
==
AccessType
.
EID
:
return
get_entity
.
get_entity_by_id
(
value
)
if
kind
==
AccessType
.
PATH
:
return
get_entity
.
get_entity_by_path
(
value
)
try
:
if
kind
==
AccessType
.
QUERY
:
return
execute_query
(
value
,
unique
=
unique
)
if
kind
==
AccessType
.
NAME
:
return
get_entity
.
get_entity_by_name
(
value
)
if
kind
==
AccessType
.
EID
:
return
get_entity
.
get_entity_by_id
(
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
}
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment