Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add table map endpoint and l2 cache check #139

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions caveclient/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
l2cache_endpoints_v1 = {
"l2cache_data": l2cache_v1 + "/table/{table_id}/attributes",
"l2cache_meta": l2cache_v1 + "/attribute_metadata",
"l2cache_table_mapping": l2cache_v1 + "/table_mapping",
}

l2cache_api_versions = {1: l2cache_endpoints_v1}
Expand Down
45 changes: 45 additions & 0 deletions caveclient/l2cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
l2cache_endpoints_common,
)
from .auth import AuthClient
from requests.exceptions import HTTPError
from warnings import warn
import json
from urllib.parse import urlparse

server_key = "l2cache_server_address"

Expand Down Expand Up @@ -135,6 +138,48 @@ def attributes(self):
self._available_attributes = list(self.cache_metadata().keys())
return self._available_attributes

def table_mapping(self):
"""Retrieves table mappings for l2 cache.
Parameters
----------

Returns
-------
dict
keys are pcg table names, values are dicts with fields `l2cache_id` and `cv_path`.
"""
endpoint_mapping = self.default_url_mapping
url = self._endpoints["l2cache_table_mapping"].format_map(endpoint_mapping)
response = self.session.get(url)
return handle_response(response)

def has_cache(self, datastack_name=None):
"""Checks if the l2 cache is available for the dataset

Parameters
----------
datastack_name : str, optional
The name of the datastack to check, by default None (if None, uses the client's datastack)

Returns
-------
bool
True if the l2 cache is available, False otherwise
"""
seg_source = self.fc.info.segmentation_source(datastack_name=datastack_name)
if urlparse(seg_source).scheme != "graphene":
return False
table_name = self.fc.chunkedgraph.table_name
try:
table_mapping = self.table_mapping()
except HTTPError as e:
if e.response.status_code == 404:
warn(f"Table '{table_name}' does not have a l2 cache table mapping. Assuming no cache.")
return {}
ceesem marked this conversation as resolved.
Show resolved Hide resolved
else:
raise e
return table_name in table_mapping


client_mapping = {
1: L2CacheClientLegacy,
Expand Down
Loading