Skip to content

Commit f0d7249

Browse files
author
Sean Sullivan
committed
Update fast api and hash index to shorten filepath names
1 parent cdb9d34 commit f0d7249

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

elastic_datashader/cache.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import OrderedDict
33
from datetime import datetime, timedelta, timezone
44
from os import scandir
5+
from hashlib import sha256
56
import os
67
from contextlib import suppress
78
from pathlib import Path
@@ -23,14 +24,37 @@ def path_age(now: datetime, path: Path) -> timedelta:
2324

2425
return now - path_dt
2526

27+
index_hash_map = {}
28+
29+
def get_index_hash(idx: str) -> str:
30+
'''
31+
Calculates a hash value for the specific index set
32+
On some OS's the pathname becomes too long and causes errors when
33+
creating files if multiple CCS indexes have been explicitly defined
34+
*:my-data-* listed as
35+
mysite-1:my-data-*,mysite-2:my-data-*,mysite-3:my-data-*,mysite-4:my-data-*,mysite-5:my-data-*
36+
'''
37+
idx_hash = index_hash_map.get(idx,None)
38+
if idx_hash is not None:
39+
return idx_hash
40+
idx_hash = sha256()
41+
idx_hash.update(str(idx).encode("utf-8"))
42+
idx_hash = idx_hash.hexdigest()[0:20]
43+
index_hash_map[idx] = idx_hash
44+
return idx_hash
45+
2646
def tile_name(idx, x, y, z, parameter_hash) -> str:
27-
return f"{idx}/{parameter_hash}/{z}/{x}/{y}.png"
47+
idx_hash = get_index_hash(idx)
48+
return f"{idx_hash}/{parameter_hash}/{z}/{x}/{y}.png"
2849

2950
def rendering_tile_name(idx, x, y, z, parameter_hash) -> str:
30-
return f"{idx}/{parameter_hash}/{z}/{x}/{y}.rendering"
51+
idx_hash = get_index_hash(idx)
52+
53+
return f"{idx_hash}/{parameter_hash}/{z}/{x}/{y}.rendering"
3154

3255
def tile_id(idx, x, y, z, parameter_hash) -> str:
33-
return f"{idx}_{parameter_hash}_{z}_{x}_{y}"
56+
idx_hash = get_index_hash(idx)
57+
return f"{idx_hash}_{parameter_hash}_{z}_{x}_{y}"
3458

3559
def directory_size(path: Path) -> int:
3660
'''
@@ -134,14 +158,14 @@ def release_cache_placeholder(cache_path: Path, tile: str) -> None:
134158
if tile_path.exists():
135159
tile_path.unlink(missing_ok=True)
136160

137-
def check_cache_dir(cache_path: Path, layer_name: str) -> None:
161+
def check_cache_dir(cache_path: Path, idx: str) -> None:
138162
"""
139163
Ensure the folder ``cache_path``/``layer_name`` exists
140164
141165
:param cache_path: Top level directory
142166
:param layer_name: Specific layer in cache
143167
"""
144-
tile_cache_path = cache_path / layer_name
168+
tile_cache_path = cache_path / get_index_hash(idx)
145169
tile_cache_path.mkdir(parents=True, exist_ok=True)
146170

147171
def clear_hash_cache(cache_path: Path, idx_name: str, param_hash: Optional[str]) -> None:

elastic_datashader/parameters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def get_parameter_hash(params: Dict[str, Any]) -> str:
267267
p = p.isoformat()
268268
parameter_hash.update(str(p).encode("utf-8"))
269269

270-
return parameter_hash.hexdigest()
270+
return parameter_hash.hexdigest()[0:30]
271271

272272
def extract_parameters(headers: Dict[Any, Any], query_params: Dict[Any, Any]) -> Tuple[str, Dict[str, Any]]:
273273
"""

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ numpy = "^1.23"
3737
PyYAML = "*"
3838
humanize = "*"
3939
uvicorn = {extras = ["standard"], version = "0.24.0", optional = true}
40-
fastapi = "^0.96"
40+
fastapi = ">=0.109.1"
4141
georgio = "2023.156.924"
4242
jinja2 = "3.1.2"
4343

0 commit comments

Comments
 (0)